Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[phi] migrate atan2_op into phi #39806

Merged
merged 1 commit into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 9 additions & 32 deletions paddle/fluid/operators/atan2_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,19 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "paddle/fluid/operators/atan2_op.h"

#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/op_version_registry.h"
#include "paddle/phi/core/infermeta_utils.h"
#include "paddle/phi/infermeta/backward.h"
#include "paddle/phi/infermeta/binary.h"

namespace paddle {
namespace operators {

class Atan2Op : public framework::OperatorWithKernel {
public:
using framework::OperatorWithKernel::OperatorWithKernel;

void InferShape(framework::InferShapeContext* ctx) const override {
OP_INOUT_CHECK(ctx->HasInput("X1"), "Input", "X1", "atan2");
OP_INOUT_CHECK(ctx->HasInput("X2"), "Input", "X2", "atan2");
OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "atan2");

auto in_dims = ctx->GetInputDim("X1");

ctx->SetOutputDim("Out", in_dims);
}
};

class Atan2OpMaker : public framework::OpProtoAndCheckerMaker {
Expand Down Expand Up @@ -115,24 +105,11 @@ class Atan2OpVarTypeInference : public framework::VarTypeInference {
} // namespace paddle

namespace ops = paddle::operators;

DELCARE_INFER_SHAPE_FUNCTOR(atan2, Atan2InferShapeFunctor,
PT_INFER_META(phi::Atan2InferMeta));
REGISTER_OPERATOR(atan2, ops::Atan2Op, ops::Atan2OpMaker,
ops::Atan2GradMaker<paddle::framework::OpDesc>,
ops::Atan2GradMaker<paddle::imperative::OpBase>,
ops::Atan2OpVarTypeInference);
ops::Atan2OpVarTypeInference, Atan2InferShapeFunctor);

REGISTER_OPERATOR(atan2_grad, ops::Atan2GradOp);

REGISTER_OP_CPU_KERNEL(
atan2, ops::Atan2Kernel<paddle::platform::CPUDeviceContext, int32_t>,
ops::Atan2Kernel<paddle::platform::CPUDeviceContext, int64_t>,
ops::Atan2Kernel<paddle::platform::CPUDeviceContext, float>,
ops::Atan2Kernel<paddle::platform::CPUDeviceContext, double>,
ops::Atan2Kernel<paddle::platform::CPUDeviceContext,
paddle::platform::float16>);

REGISTER_OP_CPU_KERNEL(
atan2_grad, ops::Atan2GradKernel<paddle::platform::CPUDeviceContext, float>,
ops::Atan2GradKernel<paddle::platform::CPUDeviceContext, double>,
ops::Atan2GradKernel<paddle::platform::CPUDeviceContext,
paddle::platform::float16>);
31 changes: 0 additions & 31 deletions paddle/fluid/operators/atan2_op.cu

This file was deleted.

168 changes: 0 additions & 168 deletions paddle/fluid/operators/atan2_op.h

This file was deleted.

5 changes: 5 additions & 0 deletions paddle/phi/infermeta/binary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,9 @@ void HuberLossInferMeta(const MetaTensor& input,
out->share_lod(input);
}

void Atan2InferMeta(const MetaTensor& x, const MetaTensor& y, MetaTensor* out) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以参考UnchangedInferMeta写法

auto in_dims = x.dims();
out->set_dims(in_dims);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里还需要设置dtype, layout,lod

}

} // namespace phi
2 changes: 2 additions & 0 deletions paddle/phi/infermeta/binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,6 @@ void HuberLossInferMeta(const MetaTensor& input_meta,
MetaTensor* out,
MetaTensor* residual,
MetaConfig config = MetaConfig());

void Atan2InferMeta(const MetaTensor& x, const MetaTensor& y, MetaTensor* out);
} // namespace phi
29 changes: 29 additions & 0 deletions paddle/phi/kernels/atan2_grad_kernel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include "paddle/phi/core/dense_tensor.h"

namespace phi {

template <typename T, typename Context>
void Atan2GradKernel(const Context& ctx,
const DenseTensor& x,
const DenseTensor& y,
const DenseTensor& out_grad,
DenseTensor* x_grad,
DenseTensor* y_grad);

} // namespace phi
27 changes: 27 additions & 0 deletions paddle/phi/kernels/atan2_kernel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include "paddle/phi/core/dense_tensor.h"

namespace phi {

template <typename T, typename Context>
void Atan2Kernel(const Context& ctx,
const DenseTensor& x,
const DenseTensor& y,
DenseTensor* out);

} // namespace phi
27 changes: 27 additions & 0 deletions paddle/phi/kernels/cpu/atan2_grad_kernel.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "paddle/phi/kernels/atan2_grad_kernel.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里加一个空行,隔开本身实现的atan2_kernel.h 头文件和其他依赖的头文件

#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/device_context.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个头文件应该可以去掉,只include cpu_context.h就可以

#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/atan2_grad_kernel_impl.h"

PD_REGISTER_KERNEL(atan2_grad,
CPU,
ALL_LAYOUT,
phi::Atan2GradKernel,
float,
double,
phi::dtype::float16) {}
29 changes: 29 additions & 0 deletions paddle/phi/kernels/cpu/atan2_kernel.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "paddle/phi/kernels/atan2_kernel.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里加一个空行,隔开本身实现的atan2_kernel.h 头文件和其他依赖的头文件

#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/device_context.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/atan2_kernel_impl.h"

PD_REGISTER_KERNEL(atan2,
CPU,
ALL_LAYOUT,
phi::Atan2Kernel,
float,
double,
phi::dtype::float16,
int,
int64_t) {}
Loading