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 Kernel ] Transfer as_real to phi. #44263

Merged
merged 3 commits into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
21 changes: 6 additions & 15 deletions paddle/fluid/operators/complex_view_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
#include <vector>

#include "paddle/fluid/framework/data_type.h"
#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/fluid/platform/enforce.h"
#include "paddle/phi/infermeta/unary.h"

namespace paddle {
namespace operators {
Expand Down Expand Up @@ -94,17 +96,6 @@ class AsRealOp : public framework::OperatorWithKernel {
public:
using framework::OperatorWithKernel::OperatorWithKernel;

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

auto out_dims_v = phi::vectorize(ctx->GetInputDim("X"));
out_dims_v.push_back(2);
const framework::DDim out_dims = phi::make_ddim(out_dims_v);
ctx->SetOutputDim("Out", out_dims);
ctx->ShareLoD("X", /*->*/ "Out");
}

protected:
framework::OpKernelType GetExpectedKernelType(
const framework::ExecutionContext &ctx) const override {
Expand Down Expand Up @@ -148,6 +139,9 @@ class AsRealGradMaker : public framework::SingleGradOpMaker<T> {
} // namespace paddle

namespace ops = paddle::operators;
DECLARE_INFER_SHAPE_FUNCTOR(as_real,
AsRealInferShapeFunctor,
PD_INFER_META(phi::AsRealInferMeta));

REGISTER_OPERATOR(as_complex,
ops::AsComplexOp,
Expand All @@ -158,13 +152,10 @@ REGISTER_OPERATOR(as_complex,
REGISTER_OPERATOR(as_real,
ops::AsRealOp,
ops::AsRealOpMaker,
AsRealInferShapeFunctor,
ops::AsRealGradMaker<paddle::framework::OpDesc>,
ops::AsRealGradMaker<paddle::imperative::OpBase>);

REGISTER_OP_CPU_KERNEL(as_complex,
ops::AsComplexKernel<phi::CPUContext, float>,
ops::AsComplexKernel<phi::CPUContext, double>);

REGISTER_OP_CPU_KERNEL(as_real,
ops::AsRealKernel<phi::CPUContext, float>,
ops::AsRealKernel<phi::CPUContext, double>);
5 changes: 0 additions & 5 deletions paddle/fluid/operators/complex_view_op.cu
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,3 @@ REGISTER_OP_CUDA_KERNEL(
as_complex,
ops::AsComplexKernel<paddle::platform::CUDADeviceContext, float>,
ops::AsComplexKernel<paddle::platform::CUDADeviceContext, double>);

REGISTER_OP_CUDA_KERNEL(
as_real,
ops::AsRealKernel<paddle::platform::CUDADeviceContext, float>,
ops::AsRealKernel<paddle::platform::CUDADeviceContext, double>);
15 changes: 0 additions & 15 deletions paddle/fluid/operators/complex_view_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,5 @@ class AsComplexKernel : public framework::OpKernel<T> {
}
};

template <typename DeviceContext, typename T>
class AsRealKernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext& context) const override {
const auto* x = context.Input<framework::LoDTensor>("X");
auto* out = context.Output<framework::LoDTensor>("Out");

out->mutable_data<T>(context.GetPlace());
const framework::DDim out_dims_original = out->dims();
framework::TensorCopy(*x, context.GetPlace(), out);
out->Resize(out_dims_original); // restored the shape
out->mutable_data<T>(context.GetPlace()); // restore the dtype
}
};

} // namespace operators
} // namespace paddle
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 @@ -167,6 +167,15 @@
func : argsort
backward : argsort_grad

- api : as_real
args : (Tensor x)
output : Tensor
infer_meta :
func : AsRealInferMeta
kernel :
func : as_real
# backward : as_complex

# asin
- api : asin
args : (Tensor x)
Expand Down
8 changes: 8 additions & 0 deletions paddle/phi/infermeta/unary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ void ArgsortInferMeta(const MetaTensor& input,
indices->share_lod(input);
}

void AsRealInferMeta(const MetaTensor& input, MetaTensor* output) {
auto out_dims_v = phi::vectorize(input.dims());
out_dims_v.push_back(2);
auto out_dims = phi::make_ddim(out_dims_v);
output->set_dims(out_dims);
output->share_lod(input);
}

void BatchSizeLikeInferMeta(const MetaTensor& x,
const std::vector<int>& shape,
int x_batch_size_dim,
Expand Down
2 changes: 2 additions & 0 deletions paddle/phi/infermeta/unary.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ void ArgsortInferMeta(const MetaTensor& input,
MetaTensor* output,
MetaTensor* indices);

void AsRealInferMeta(const MetaTensor& input, MetaTensor* output);

void BatchSizeLikeInferMeta(const MetaTensor& x,
const std::vector<int>& shape,
int x_batch_size_dim,
Expand Down
26 changes: 26 additions & 0 deletions paddle/phi/kernels/as_real_kernel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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 AsRealKernel(const Context& dev_ctx,
const DenseTensor& x,
DenseTensor* out);

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

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

PD_REGISTER_KERNEL(as_real, CPU, ALL_LAYOUT, phi::AsRealKernel, float, double) {
}
22 changes: 22 additions & 0 deletions paddle/phi/kernels/gpu/as_real_kernel.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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/as_real_kernel.h"

#include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/as_real_impl.h"

PD_REGISTER_KERNEL(as_real, GPU, ALL_LAYOUT, phi::AsRealKernel, float, double) {
}
35 changes: 35 additions & 0 deletions paddle/phi/kernels/impl/as_real_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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/kernels/as_real_kernel.h"

#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/tensor_utils.h"
#include "paddle/phi/kernels/funcs/for_range.h"

namespace phi {
template <typename T, typename Context>
void AsRealKernel(const Context& ctx, const DenseTensor& x, DenseTensor* out) {
ctx.template Alloc<T>(out);
auto out_dims_original = out->dims();
Copy(ctx, x, ctx.GetPlace(), true, out);
Copy link
Contributor

Choose a reason for hiding this comment

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

这里blocking应该设置为false吧?辛苦再确认一下

out->Resize(out_dims_original); // restored the shape.
out->set_type(
paddle::experimental::CppTypeToDataType<T>::Type()); // restored the
// dtype.
}

} // namespace phi
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def setUp(self):
out_ref = ref_view_as_real(x)
self.inputs = {'X': x}
self.outputs = {'Out': out_ref}
self.python_api = paddle.as_real
self.out_grad = np.ones([10, 10, 2], dtype="float64")

def test_check_output(self):
Expand Down