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 fill_constant_batch_size_like op kernel into phi #40784

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
19 changes: 5 additions & 14 deletions paddle/fluid/operators/fill_constant_batch_size_like_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ 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/fill_constant_batch_size_like_op.h"
#include "paddle/fluid/operators/batch_size_like.h"

namespace paddle {
Expand All @@ -23,9 +22,13 @@ class FillConstantBatchSizeLikeOp : public BatchSizeLikeOp {
using BatchSizeLikeOp::BatchSizeLikeOp;
framework::OpKernelType GetExpectedKernelType(
const framework::ExecutionContext &ctx) const override {
return framework::OpKernelType(
framework::OpKernelType kernel_type = framework::OpKernelType(
static_cast<framework::proto::VarType::Type>(ctx.Attr<int>("dtype")),
ctx.device_context());
if (ctx.Attr<bool>("force_cpu")) {
kernel_type.place_ = platform::CPUPlace();
}
return kernel_type;
}
};

Expand Down Expand Up @@ -64,15 +67,3 @@ REGISTER_OPERATOR(
paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>,
ops::FillConstantBatchSizeLikeOpMaker,
ops::BatchSizeLikeNoNeedBufferVarsInferer);
REGISTER_OP_CPU_KERNEL(
fill_constant_batch_size_like,
ops::FillConstantBatchSizeLikeOpKernel<paddle::platform::CPUDeviceContext,
float>,
ops::FillConstantBatchSizeLikeOpKernel<paddle::platform::CPUDeviceContext,
double>,
ops::FillConstantBatchSizeLikeOpKernel<paddle::platform::CPUDeviceContext,
int>,
ops::FillConstantBatchSizeLikeOpKernel<paddle::platform::CPUDeviceContext,
int64_t>,
ops::FillConstantBatchSizeLikeOpKernel<paddle::platform::CPUDeviceContext,
bool>);
32 changes: 0 additions & 32 deletions paddle/fluid/operators/fill_constant_batch_size_like_op.cu.cc

This file was deleted.

84 changes: 0 additions & 84 deletions paddle/fluid/operators/fill_constant_batch_size_like_op.h

This file was deleted.

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

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

namespace phi {

template <typename T, typename Context>
void FullBatchSizeLikeKernel(const Context& dev_ctx,
const DenseTensor& x,
const std::vector<int>& shape,
const Scalar& val,
DataType dtype,
int x_batch_size_dim,
int out_batch_size_dim,
DenseTensor* out) {
if (x.lod().size() && x_batch_size_dim == 0) {
// set the correct batch size for the LoDTensor.
auto odims = out->dims();
odims[out_batch_size_dim] = static_cast<int>(x.lod().back().size()) - 1;
FullKernel<T, Context>(dev_ctx, phi::vectorize(odims), val, dtype, out);
}
FullLikeKernel<T, Context>(dev_ctx, x, val, dtype, out);
}

} // namespace phi

PD_REGISTER_KERNEL(full_batch_size_like,
CPU,
ALL_LAYOUT,
phi::FullBatchSizeLikeKernel,
float,
double,
int,
int64_t,
bool) {
kernel->InputAt(0).SetBackend(phi::Backend::ALL_BACKEND);
}
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
PD_REGISTER_KERNEL(full_batch_size_like,
GPU,
ALL_LAYOUT,
phi::FullBatchSizeLikeKernel,
float,
double,
int,
int64_t,
bool,
phi::dtype::float16) {
kernel->InputAt(0).SetBackend(phi::Backend::ALL_BACKEND);
}
#endif
14 changes: 14 additions & 0 deletions paddle/phi/kernels/full_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#pragma once

#include <vector>

#include "paddle/phi/common/scalar.h"
#include "paddle/phi/common/scalar_array.h"
#include "paddle/phi/core/dense_tensor.h"
Expand All @@ -37,6 +39,18 @@ void FullLikeKernel(const Context& dev_ctx,
DataType dtype,
DenseTensor* out);

// In order to be compatible with fill_constant_batch_size_like op
// that are still used in the 2.x APIs
template <typename T, typename Context>
void FullBatchSizeLikeKernel(const Context& dev_ctx,
const DenseTensor& x,
const std::vector<int>& shape,
const Scalar& val,
DataType dtype,
int x_batch_size_dim,
int out_batch_size_dim,
DenseTensor* out);

template <typename T, typename Context>
void Full(const Context& dev_ctx,
const ScalarArray& shape,
Expand Down
43 changes: 43 additions & 0 deletions paddle/phi/ops/compat/fill_constant_batch_size_like_sig.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* 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/core/compat/op_utils.h"

namespace phi {

KernelSignature FillConstantBatchSizeLikeOpArgumentMapping(
const ArgumentMappingContext& ctx) {
const auto& str_value = paddle::any_cast<std::string>(ctx.Attr("str_value"));
if (str_value.empty()) {
return KernelSignature(
"full_batch_size_like",
{"Input"},
{"shape", "value", "dtype", "input_dim_idx", "output_dim_idx"},
{"Out"});
} else {
return KernelSignature(
"full_batch_size_like",
{"Input"},
{"shape", "str_value", "dtype", "input_dim_idx", "output_dim_idx"},
{"Out"});
}
}

} // namespace phi

PD_REGISTER_BASE_KERNEL_NAME(fill_constant_batch_size_like,
full_batch_size_like);

PD_REGISTER_ARG_MAPPING_FN(fill_constant_batch_size_like,
phi::FillConstantBatchSizeLikeOpArgumentMapping);