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]Move angle op to phi #44393

Merged
merged 7 commits into from
Jul 19, 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
61 changes: 18 additions & 43 deletions paddle/fluid/operators/angle_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

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

#include <memory>
#include <string>
#include <unordered_map>
Expand All @@ -22,22 +20,18 @@
#include "paddle/fluid/platform/mkldnn_helper.h"
#endif

#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/phi/core/infermeta_utils.h"
#include "paddle/phi/infermeta/backward.h"
#include "paddle/phi/infermeta/unary.h"

namespace paddle {
namespace operators {

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

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

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

ctx->SetOutputDim("Out", in_dims);
ctx->ShareLoD("X", /*->*/ "Out");
}
};

class AngleOpMaker : public framework::OpProtoAndCheckerMaker {
Expand Down Expand Up @@ -67,20 +61,6 @@ This operator is used to perform elementwise angle for input $X$.
class AngleGradOp : public framework::OperatorWithKernel {
public:
using framework::OperatorWithKernel::OperatorWithKernel;
void InferShape(framework::InferShapeContext* ctx) const override {
OP_INOUT_CHECK(ctx->HasInput(framework::GradVarName("Out")),
"Input",
"Out@Grad",
"angle_grad");
OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "Out@Grad", "angle_grad");
OP_INOUT_CHECK(ctx->HasOutput(framework::GradVarName("X")),
"Output",
"X@Grad",
"angle_grad");

auto dout_dims = ctx->GetInputDim(framework::GradVarName("Out"));
ctx->SetOutputDim(framework::GradVarName("X"), dout_dims);
}

protected:
framework::OpKernelType GetExpectedKernelType(
Expand Down Expand Up @@ -108,24 +88,19 @@ class AngleGradMaker : public framework::SingleGradOpMaker<T> {

namespace ops = paddle::operators;

DECLARE_INFER_SHAPE_FUNCTOR(angle,
Copy link
Contributor

Choose a reason for hiding this comment

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

反向的Infermeta也需要删掉

Copy link
Contributor Author

@0x45f 0x45f Jul 19, 2022

Choose a reason for hiding this comment

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

已迁移反向infeshape,感谢

AngleInferShapeFunctor,
PD_INFER_META(phi::RealAndImagInferMeta));

DECLARE_INFER_SHAPE_FUNCTOR(angle_grad,
AngleGradInferShapeFunctor,
PD_INFER_META(phi::AngleGradInferMeta));

REGISTER_OPERATOR(angle,
ops::AngleOp,
ops::AngleOpMaker,
ops::AngleGradMaker<paddle::framework::OpDesc>,
ops::AngleGradMaker<paddle::imperative::OpBase>);

REGISTER_OP_CPU_KERNEL(
angle,
ops::AngleKernel<phi::CPUContext, float>,
ops::AngleKernel<phi::CPUContext, double>,
ops::AngleKernel<phi::CPUContext, paddle::platform::complex<float>>,
ops::AngleKernel<phi::CPUContext, paddle::platform::complex<double>>);

REGISTER_OPERATOR(angle_grad, ops::AngleGradOp);

REGISTER_OP_CPU_KERNEL(
angle_grad,
ops::AngleGradKernel<phi::CPUContext, float>,
ops::AngleGradKernel<phi::CPUContext, double>,
ops::AngleGradKernel<phi::CPUContext, paddle::platform::complex<float>>,
ops::AngleGradKernel<phi::CPUContext, paddle::platform::complex<double>>);
ops::AngleGradMaker<paddle::imperative::OpBase>,
AngleInferShapeFunctor);

REGISTER_OPERATOR(angle_grad, ops::AngleGradOp, AngleGradInferShapeFunctor);
33 changes: 0 additions & 33 deletions paddle/fluid/operators/angle_op.cu

This file was deleted.

72 changes: 0 additions & 72 deletions paddle/fluid/operators/angle_op.h

This file was deleted.

9 changes: 9 additions & 0 deletions paddle/phi/api/yaml/legacy_api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@
kernel :
func : allclose

- api : angle
args : (Tensor x)
output : Tensor
infer_meta :
func : RealAndImagInferMeta
kernel :
func : angle
backward : angle_grad

- api : any
args : (Tensor x, int64_t[] dims={}, bool keep_dim=false)
output : Tensor(out)
Expand Down
12 changes: 12 additions & 0 deletions paddle/phi/api/yaml/legacy_backward.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@
kernel :
func : addmm_grad

- backward_api : angle_grad
forward : angle (Tensor x) -> Tensor(out)
args : (Tensor x, Tensor out_grad)
output : Tensor(x_grad)
infer_meta :
func : UnchangedInferMeta
param : [x]
kernel :
func : angle_grad
data_transform:
skip_transform : out_grad

- backward_api : argsort_grad
forward : argsort (Tensor x, int axis, bool descending) -> Tensor(out), Tensor(indices)
args : (Tensor indices, Tensor x, Tensor out_grad, int axis, bool descending)
Expand Down
6 changes: 6 additions & 0 deletions paddle/phi/infermeta/backward.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ limitations under the License. */

namespace phi {

void AngleGradInferMeta(const MetaTensor& x,
const MetaTensor& out_grad,
MetaTensor* x_grad) {
UnchangedInferMeta(x, x_grad);
}

void BilinearTensorProductGradInferMeta(const MetaTensor& x,
const MetaTensor& y,
const MetaTensor& weight,
Expand Down
4 changes: 4 additions & 0 deletions paddle/phi/infermeta/backward.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ namespace phi {
//
// NOTE: The InferMeta Functions in this file are arranged in alphabetic order.

void AngleGradInferMeta(const MetaTensor& x,
const MetaTensor& out_grad,
MetaTensor* x_grad);

void BilinearTensorProductGradInferMeta(const MetaTensor& x,
const MetaTensor& y,
const MetaTensor& weight,
Expand Down
27 changes: 27 additions & 0 deletions paddle/phi/kernels/angle_grad_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 AngleGradKernel(const Context& dev_ctx,
const DenseTensor& x,
const DenseTensor& out_grad,
DenseTensor* x_grad);

} // namespace phi
30 changes: 30 additions & 0 deletions paddle/phi/kernels/angle_kernel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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
#ifndef _USE_MATH_DEFINES
#define _USE_MATH_DEFINES
#endif
#include <math.h>

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

namespace phi {

template <typename T, typename Context>
void AngleKernel(const Context& dev_ctx,
const DenseTensor& x,
DenseTensor* out);

} // namespace phi
28 changes: 28 additions & 0 deletions paddle/phi/kernels/cpu/angle_grad_kernel.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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/angle_grad_kernel.h"
#include "paddle/phi/kernels/impl/angle_grad_kernel_impl.h"

#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/kernel_registry.h"

PD_REGISTER_KERNEL(angle_grad,
CPU,
ALL_LAYOUT,
phi::AngleGradKernel,
float,
double,
phi::dtype::complex<float>,
phi::dtype::complex<double>) {}
29 changes: 29 additions & 0 deletions paddle/phi/kernels/cpu/angle_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/angle_kernel.h"
#include "paddle/phi/kernels/impl/angle_kernel_impl.h"

#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/kernel_registry.h"

PD_REGISTER_KERNEL(angle,
CPU,
ALL_LAYOUT,
phi::AngleKernel,
float,
double,
phi::dtype::complex<float>,
phi::dtype::complex<double>) {}
Loading