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;
DELCARE_INFER_SHAPE_FUNCTOR(is_empty, IsEmptyInferShapeFunctor,
PT_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.

3 changes: 0 additions & 3 deletions paddle/fluid/operators/is_empty_op.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.

Copy link
Contributor

Choose a reason for hiding this comment

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

licence里的这些空行是固定格式,不用删,可参考其他文件

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

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.
Expand Down
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/core/enforce.h"
#include "paddle/phi/core/infermeta_utils.h"
Expand Down Expand Up @@ -268,6 +269,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 @@ -56,6 +56,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
52 changes: 52 additions & 0 deletions paddle/phi/kernels/is_empty_kernel.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 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.
out->mutable_data<bool>(phi::CPUPlace())[0] = phi::product(x.dims()) == 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

这里可以使用dev_ctx.template HostAlloc<T>()接口替换mutable_data

Copy link
Contributor

Choose a reason for hiding this comment

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

看注释,这里应该是改为: phi::CPUPlace().HostAlloc() 吧

Copy link
Contributor Author

Choose a reason for hiding this comment

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

具体应该怎么改呢,改成下面这样吗

out->mutable_data(phi::CPUPlace().HostAlloc())[0] = phi::product(x.dims()) == 0;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

}

} // 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
Copy link
Contributor

Choose a reason for hiding this comment

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

这里有个warning,后边可以添加换行

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