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] Migrate lable_smooth_op into Phi #39796

Merged
merged 3 commits into from
Feb 23, 2022
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
2 changes: 1 addition & 1 deletion paddle/fluid/framework/operator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2037,7 +2037,7 @@ void OperatorWithKernel::BuildPtenKernelContext(
(i == 0 ? 0 : pt_kernel_context->InputRangeAt(i - 1).second);

// deal with optional here
if ((it == ctx.inputs.end()) &&
if ((it == ctx.inputs.end() || it->second.size() == 0) &&
(input_defs[i].type_index ==
std::type_index(typeid(paddle::optional<const phi::DenseTensor&>)))) {
pt_kernel_context->EmplaceBackInputWithoutSetRange(nullptr);
Expand Down
11 changes: 1 addition & 10 deletions paddle/fluid/operators/label_smooth_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ 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/label_smooth_op.h"

#include <string>
#include "paddle/fluid/framework/op_registry.h"

namespace paddle {
namespace framework {
Expand Down Expand Up @@ -152,11 +151,3 @@ REGISTER_OPERATOR(label_smooth, ops::LabelSmoothOp, ops::LabelSmoothOpMaker,
ops::LabelSmoothGradMaker<paddle::framework::OpDesc>,
ops::LabelSmoothGradMaker<paddle::imperative::OpBase>);
REGISTER_OPERATOR(label_smooth_grad, ops::LabelSmoothGradOp);
REGISTER_OP_CPU_KERNEL(
label_smooth,
ops::LabelSmoothKernel<paddle::platform::CPUDeviceContext, float>,
ops::LabelSmoothKernel<paddle::platform::CPUDeviceContext, double>);
REGISTER_OP_CPU_KERNEL(
label_smooth_grad,
ops::LabelSmoothGradKernel<paddle::platform::CPUDeviceContext, float>,
ops::LabelSmoothGradKernel<paddle::platform::CPUDeviceContext, double>);
125 changes: 0 additions & 125 deletions paddle/fluid/operators/label_smooth_op.cu

This file was deleted.

70 changes: 0 additions & 70 deletions paddle/fluid/operators/label_smooth_op.h

This file was deleted.

2 changes: 1 addition & 1 deletion paddle/fluid/operators/label_smooth_op_npu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "paddle/fluid/operators/label_smooth_op.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/platform/device/npu/npu_op_runner.h"

namespace paddle {
Expand Down
1 change: 0 additions & 1 deletion paddle/fluid/operators/label_smooth_op_xpu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ See the License for the specific language governing permissions and
limitations under the License. */

#ifdef PADDLE_WITH_XPU
#include "paddle/fluid/operators/label_smooth_op.h"
#include "paddle/fluid/framework/op_registry.h"

namespace paddle {
Expand Down
45 changes: 45 additions & 0 deletions paddle/phi/kernels/cpu/label_smooth_grad_kernel.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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/label_smooth_grad_kernel.h"
#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/funcs/eigen/common.h"

namespace phi {

template <typename T, typename Context>
void LabelSmoothGradKernel(const Context& ctx,
const DenseTensor& out_grad,
float epsilon,
DenseTensor* label_grad) {
ctx.template Alloc<T>(label_grad);
auto d_out_dim = out_grad.dims()[out_grad.dims().size() - 1];
if (d_out_dim != 0) {
auto d_out = EigenVector<T>::Flatten(out_grad);
auto d_in = EigenVector<T>::Flatten(*label_grad);

auto& dev = *ctx.eigen_device();
d_in.device(dev) = static_cast<T>(1 - epsilon) * d_out;
}
}

} // namespace phi

PD_REGISTER_KERNEL(label_smooth_grad,
CPU,
ALL_LAYOUT,
phi::LabelSmoothGradKernel,
float,
double) {}
50 changes: 50 additions & 0 deletions paddle/phi/kernels/cpu/label_smooth_kernel.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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/label_smooth_kernel.h"
#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/funcs/eigen/common.h"

namespace phi {

template <typename T, typename Context>
void LabelSmoothKernel(const Context& ctx,
const DenseTensor& label,
paddle::optional<const DenseTensor&> prior_dist,
float epsilon,
DenseTensor* out) {
auto label_dim = label.dims()[label.dims().size() - 1];
ctx.template Alloc<T>(out);
auto& dev = *ctx.eigen_device();
if (label_dim != 0) {
auto eigen_out = EigenVector<T>::Flatten(*out);
auto eigen_in = EigenVector<T>::Flatten(label);
if (prior_dist.is_initialized()) {
auto dist = EigenVector<T>::Flatten(*prior_dist.get_ptr());
eigen_out.device(dev) =
static_cast<T>(1 - epsilon) * eigen_in +
static_cast<T>(epsilon) *
dist.broadcast(Eigen::DSizes<int, 1>(label.numel() / label_dim));
} else {
eigen_out.device(dev) = static_cast<T>(1 - epsilon) * eigen_in +
static_cast<T>(epsilon / label_dim);
}
}
}

} // namespace phi

PD_REGISTER_KERNEL(
label_smooth, CPU, ALL_LAYOUT, phi::LabelSmoothKernel, float, double) {}
55 changes: 55 additions & 0 deletions paddle/phi/kernels/gpu/label_smooth_grad_kernel.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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/fluid/operators/elementwise/elementwise_op_impl.cu.h"
Copy link
Contributor Author

@Aurelius84 Aurelius84 Feb 23, 2022

Choose a reason for hiding this comment

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

我会提一个单独的迁移此头文件的PR,放在一起PR会比较大

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

namespace phi {
template <typename T>
struct LabelSmoothGradFunctor {
T epsilon;

__forceinline__ LabelSmoothGradFunctor(float epsilon_data) {
epsilon = static_cast<T>(epsilon_data);
}

__device__ __forceinline__ T operator()(const T x) const {
return static_cast<T>(1 - epsilon) * x;
}
};

template <typename T, typename Context>
void LabelSmoothGradKernel(const Context& ctx,
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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok,我下个pr统一fix

const DenseTensor& out_grad,
float epsilon,
DenseTensor* label_grad) {
ctx.template Alloc<T>(label_grad);

std::vector<const DenseTensor*> ins = {&out_grad};
std::vector<DenseTensor*> outs = {label_grad};
auto functor = LabelSmoothGradFunctor<T>(epsilon);
paddle::operators::LaunchSameDimsElementwiseCudaKernel<T>(
ctx, ins, &outs, functor);
}

} // namespace phi

PD_REGISTER_KERNEL(label_smooth_grad,
GPU,
ALL_LAYOUT,
phi::LabelSmoothGradKernel,
float,
double) {}
Loading