Skip to content

Commit 345cc8f

Browse files
authored
Move real and imag op to phi (#39777)
* Move Real OP to phi * Move Imag OP to phi * Move Real and Imag InferShape to phi * Move Real and Imag to complex_kernel * Change PT_REGISTER_XXX to PD_REGISTER_XXX
1 parent 74c0bc1 commit 345cc8f

File tree

15 files changed

+263
-231
lines changed

15 files changed

+263
-231
lines changed

paddle/fluid/operators/imag_op.cc

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,17 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
See the License for the specific language governing permissions and
1313
limitations under the License. */
1414

15-
#include "paddle/fluid/operators/imag_op.h"
15+
#include "paddle/fluid/framework/infershape_utils.h"
16+
#include "paddle/fluid/framework/op_registry.h"
17+
#include "paddle/phi/core/infermeta_utils.h"
18+
#include "paddle/phi/infermeta/unary.h"
1619

1720
namespace paddle {
1821
namespace operators {
1922

2023
class ImagOp : public framework::OperatorWithKernel {
2124
public:
2225
using framework::OperatorWithKernel::OperatorWithKernel;
23-
24-
void InferShape(framework::InferShapeContext* ctx) const override {
25-
OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "Imag");
26-
OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "Imag");
27-
28-
auto x_dims = ctx->GetInputDim("X");
29-
ctx->SetOutputDim("Out", x_dims);
30-
ctx->ShareLoD("X", "Out");
31-
}
3226
};
3327

3428
class ImagOpMaker : public framework::OpProtoAndCheckerMaker {
@@ -88,19 +82,13 @@ DECLARE_INPLACE_OP_INFERER(ImagGradOpInplaceInferer,
8882
} // namespace operators
8983
} // namespace paddle
9084

85+
DELCARE_INFER_SHAPE_FUNCTOR(imag, ImagInferShapeFunctor,
86+
PT_INFER_META(phi::UnchangedInferMeta));
87+
9188
namespace ops = paddle::operators;
9289

9390
REGISTER_OPERATOR(imag, ops::ImagOp, ops::ImagOpMaker,
9491
ops::ImagGradOpMaker<paddle::framework::OpDesc>,
95-
ops::ImagGradOpMaker<paddle::imperative::OpBase>);
92+
ops::ImagGradOpMaker<paddle::imperative::OpBase>,
93+
ImagInferShapeFunctor);
9694
REGISTER_OPERATOR(imag_grad, ops::ImagGradOp);
97-
98-
REGISTER_OP_CPU_KERNEL(imag, ops::ImagKernel<paddle::platform::CPUDeviceContext,
99-
paddle::platform::complex<float>>,
100-
ops::ImagKernel<paddle::platform::CPUDeviceContext,
101-
paddle::platform::complex<double>>);
102-
REGISTER_OP_CPU_KERNEL(imag_grad,
103-
ops::ImagGradKernel<paddle::platform::CPUDeviceContext,
104-
paddle::platform::complex<float>>,
105-
ops::ImagGradKernel<paddle::platform::CPUDeviceContext,
106-
paddle::platform::complex<double>>);

paddle/fluid/operators/imag_op.cu

Lines changed: 0 additions & 28 deletions
This file was deleted.

paddle/fluid/operators/imag_op.h

Lines changed: 0 additions & 67 deletions
This file was deleted.

paddle/fluid/operators/real_op.cc

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,17 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
See the License for the specific language governing permissions and
1313
limitations under the License. */
1414

15-
#include "paddle/fluid/operators/real_op.h"
15+
#include "paddle/fluid/framework/infershape_utils.h"
16+
#include "paddle/fluid/framework/op_registry.h"
17+
#include "paddle/phi/core/infermeta_utils.h"
18+
#include "paddle/phi/infermeta/unary.h"
1619

1720
namespace paddle {
1821
namespace operators {
1922

2023
class RealOp : public framework::OperatorWithKernel {
2124
public:
2225
using framework::OperatorWithKernel::OperatorWithKernel;
23-
void InferShape(framework::InferShapeContext* ctx) const override {
24-
OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "Real");
25-
OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "Real");
26-
27-
auto x_dims = ctx->GetInputDim("X");
28-
ctx->SetOutputDim("Out", x_dims);
29-
ctx->ShareLoD("X", "Out");
30-
}
3126
};
3227

3328
class RealOpMaker : public framework::OpProtoAndCheckerMaker {
@@ -87,19 +82,13 @@ DECLARE_INPLACE_OP_INFERER(RealGradOpInplaceInferer,
8782
} // namespace operators
8883
} // namespace paddle
8984

85+
DELCARE_INFER_SHAPE_FUNCTOR(real, RealInferShapeFunctor,
86+
PT_INFER_META(phi::UnchangedInferMeta));
87+
9088
namespace ops = paddle::operators;
9189

9290
REGISTER_OPERATOR(real, ops::RealOp, ops::RealOpMaker,
9391
ops::RealGradOpMaker<::paddle::framework::OpDesc>,
94-
ops::RealGradOpMaker<::paddle::imperative::OpBase>);
92+
ops::RealGradOpMaker<::paddle::imperative::OpBase>,
93+
RealInferShapeFunctor);
9594
REGISTER_OPERATOR(real_grad, ops::RealGradOp);
96-
97-
REGISTER_OP_CPU_KERNEL(real, ops::RealKernel<paddle::platform::CPUDeviceContext,
98-
paddle::platform::complex<float>>,
99-
ops::RealKernel<paddle::platform::CPUDeviceContext,
100-
paddle::platform::complex<double>>);
101-
REGISTER_OP_CPU_KERNEL(real_grad,
102-
ops::RealGradKernel<paddle::platform::CPUDeviceContext,
103-
paddle::platform::complex<float>>,
104-
ops::RealGradKernel<paddle::platform::CPUDeviceContext,
105-
paddle::platform::complex<double>>);

paddle/fluid/operators/real_op.cu

Lines changed: 0 additions & 28 deletions
This file was deleted.

paddle/fluid/operators/real_op.h

Lines changed: 0 additions & 67 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#pragma once
16+
17+
#include "paddle/phi/core/dense_tensor.h"
18+
19+
namespace phi {
20+
21+
template <typename T, typename Context>
22+
void RealGradKernel(const Context& dev_ctx,
23+
const DenseTensor& dout,
24+
DenseTensor* dx);
25+
26+
template <typename T, typename Context>
27+
void ImagGradKernel(const Context& dev_ctx,
28+
const DenseTensor& dout,
29+
DenseTensor* dx);
30+
31+
} // namespace phi

paddle/phi/kernels/complex_kernel.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,14 @@ DenseTensor Conj(const Context& dev_ctx, const DenseTensor& x) {
5050
return x;
5151
}
5252

53+
template <typename T, typename DeviceContext>
54+
void RealKernel(const DeviceContext& dev_ctx,
55+
const DenseTensor& x,
56+
DenseTensor* out);
57+
58+
template <typename T, typename DeviceContext>
59+
void ImagKernel(const DeviceContext& dev_ctx,
60+
const DenseTensor& x,
61+
DenseTensor* out);
62+
5363
} // namespace phi
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "paddle/phi/kernels/complex_grad_kernel.h"
16+
#include "paddle/phi/kernels/impl/complex_grad_kernel_impl.h"
17+
18+
#include "paddle/phi/common/complex.h"
19+
#include "paddle/phi/core/kernel_registry.h"
20+
21+
PD_REGISTER_KERNEL(real_grad,
22+
CPU,
23+
ALL_LAYOUT,
24+
phi::RealGradKernel,
25+
phi::dtype::complex<float>,
26+
phi::dtype::complex<double>) {}
27+
28+
PD_REGISTER_KERNEL(imag_grad,
29+
CPU,
30+
ALL_LAYOUT,
31+
phi::ImagGradKernel,
32+
phi::dtype::complex<float>,
33+
phi::dtype::complex<double>) {}

paddle/phi/kernels/cpu/complex_kernel.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,17 @@ PD_REGISTER_KERNEL(conj,
3131
double,
3232
int,
3333
int64_t) {}
34+
35+
PD_REGISTER_KERNEL(real,
36+
CPU,
37+
ALL_LAYOUT,
38+
phi::RealKernel,
39+
phi::dtype::complex<float>,
40+
phi::dtype::complex<double>) {}
41+
42+
PD_REGISTER_KERNEL(imag,
43+
CPU,
44+
ALL_LAYOUT,
45+
phi::ImagKernel,
46+
phi::dtype::complex<float>,
47+
phi::dtype::complex<double>) {}

0 commit comments

Comments
 (0)