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 is_empty to phi #39919

Merged
merged 13 commits into from
Mar 7, 2022
20 changes: 7 additions & 13 deletions paddle/fluid/operators/is_empty_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ 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/fluid/operators/is_empty_op.h"
#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"
#include "paddle/phi/core/infermeta_utils.h"
#include "paddle/phi/infermeta/unary.h"

namespace paddle {
namespace operators {
Expand All @@ -24,12 +26,6 @@ class IsEmptyOp : public framework::OperatorWithKernel {
using framework::OperatorWithKernel::OperatorWithKernel;

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

framework::OpKernelType GetExpectedKernelType(
const framework::ExecutionContext &ctx) const override {
auto *x = ctx.Input<framework::LoDTensor>("X");
Expand All @@ -56,12 +52,10 @@ It will just return product(tensor.ddims()) > 0;
} // namespace paddle

namespace ops = paddle::operators;
DECLARE_INFER_SHAPE_FUNCTOR(is_empty, IsEmptyInferShapeFunctor,
PD_INFER_META(phi::IsEmptyInferMeta));
REGISTER_OPERATOR(
is_empty, ops::IsEmptyOp, ops::IsEmptyOpMaker,
paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>);
REGISTER_OP_CPU_KERNEL(
is_empty, ops::IsEmptyOpKernel<paddle::platform::CPUDeviceContext, float>,
ops::IsEmptyOpKernel<paddle::platform::CPUDeviceContext, double>,
ops::IsEmptyOpKernel<paddle::platform::CPUDeviceContext, int>,
ops::IsEmptyOpKernel<paddle::platform::CPUDeviceContext, int64_t>);
paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>,
IsEmptyInferShapeFunctor);
23 changes: 0 additions & 23 deletions paddle/fluid/operators/is_empty_op.cu.cc

This file was deleted.

6 changes: 6 additions & 0 deletions paddle/phi/infermeta/unary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License. */

#include <algorithm>
#include <set>

#include "paddle/phi/common/data_type.h"
#include "paddle/phi/common/type_traits.h"
#include "paddle/phi/core/enforce.h"
Expand Down Expand Up @@ -307,6 +308,11 @@ void InferMetaFromVecValue(const MetaTensor& x,
}
}

void IsEmptyInferMeta(const MetaTensor& x, MetaTensor* out) {
out->set_dims(phi::make_ddim({1}));
out->set_dtype(DataType::BOOL);
}

void MultinomialInferMeta(const MetaTensor& x,
int num_samples,
bool replacement,
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 @@ -69,6 +69,8 @@ void InferMetaFromVecValue(const MetaTensor& x,
const std::vector<int64_t>& shape,
MetaTensor* out);

void IsEmptyInferMeta(const MetaTensor& x, MetaTensor* out);

void MultinomialInferMeta(const MetaTensor& x,
int num_samples,
bool replacement,
Expand Down
53 changes: 53 additions & 0 deletions paddle/phi/kernels/is_empty_kernel.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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/is_empty_kernel.h"

#include "paddle/phi/backends/all_context.h"
#include "paddle/phi/core/kernel_registry.h"

namespace phi {

template <typename T, typename Context>
void IsEmptyKernel(const Context& dev_ctx,
const DenseTensor& x,
DenseTensor* out) {
// Note: is_empty is always executed on CPU and the output data should
// always be allocated for CPUPlace. We reigister CUDA kernel for this op to
// avoid the unnecessary data transform.
bool* out_data = dev_ctx.template HostAlloc<bool>(out);
out_data[0] = phi::product(x.dims()) == 0;
}

} // namespace phi

PD_REGISTER_KERNEL(is_empty,
CPU,
ALL_LAYOUT,
phi::IsEmptyKernel,
float,
double,
int,
int64_t) {}

#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
PD_REGISTER_KERNEL(is_empty,
GPU,
ALL_LAYOUT,
phi::IsEmptyKernel,
float,
double,
int,
int64_t) {}
#endif
24 changes: 24 additions & 0 deletions paddle/phi/kernels/is_empty_kernel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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 IsEmptyKernel(const Context& ctx, const DenseTensor& x, DenseTensor* out);

} // namespace phi