diff --git a/paddle/fluid/operators/clip_by_norm_op.cc b/paddle/fluid/operators/clip_by_norm_op.cc index cfb56a4b2a6b1bb0238dacdfde625a34e5af6d38..3805e11d752e3ee4f04bcd85f586835869fc583b 100644 --- a/paddle/fluid/operators/clip_by_norm_op.cc +++ b/paddle/fluid/operators/clip_by_norm_op.cc @@ -13,11 +13,17 @@ See the License for the specific language governing permissions and limitations under the License. */ #include "paddle/fluid/operators/clip_by_norm_op.h" +#include "paddle/fluid/framework/infershape_utils.h" +#include "paddle/phi/core/infermeta_utils.h" +#include "paddle/phi/infermeta/unary.h" namespace ops = paddle::operators; + +DECLARE_INFER_SHAPE_FUNCTOR(clip_by_norm, + ClipByNormInferShapeFunctor, + PD_INFER_META(phi::ClipByNormInferMeta)); + REGISTER_OP_WITHOUT_GRADIENT(clip_by_norm, ops::ClipByNormOp, - ops::ClipByNormOpMaker); - -REGISTER_OP_CPU_KERNEL(clip_by_norm, - ops::ClipByNormKernel); + ops::ClipByNormOpMaker, + ClipByNormInferShapeFunctor); diff --git a/paddle/fluid/operators/clip_by_norm_op.cu b/paddle/fluid/operators/clip_by_norm_op.cu deleted file mode 100644 index b747682716b3fcf3027e846a1260caace6e56caa..0000000000000000000000000000000000000000 --- a/paddle/fluid/operators/clip_by_norm_op.cu +++ /dev/null @@ -1,122 +0,0 @@ -/* Copyright (c) 2016 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/clip_by_norm_op.h" -#include "paddle/fluid/operators/reduce_ops/reduce_op.cu.h" - -namespace paddle { -namespace operators { -using Tensor = framework::Tensor; - -template <> -class ClipByNormKernel - : public framework::OpKernel { - public: - void Compute(const framework::ExecutionContext& context) const override { - auto max_norm = context.Attr("max_norm"); - auto in_var = context.InputVar("X"); - auto& dev_ctx = - context.template device_context(); - - Tensor* output = nullptr; - const Tensor* input = nullptr; - if (in_var->IsType()) { - input = context.Input("X"); - - output = context.Output("Out"); - output->mutable_data(context.GetPlace()); - } else if (in_var->IsType()) { - auto* x = context.Input("X"); - - // merge ids in selected rows first - math::scatter::MergeAdd - merge_func; - phi::SelectedRows* merged_input = - const_cast(context.scope()) - .Var() - ->GetMutable(); - merge_func(context.template device_context(), - *x, - merged_input); - input = &(merged_input->value()); - - phi::SelectedRows* output_selected_rows = - context.Output("Out"); - output_selected_rows->set_rows(merged_input->rows()); - output_selected_rows->set_height(merged_input->height()); - output = output_selected_rows->mutable_value(); - output->Resize(merged_input->value().dims()); - output->mutable_data(context.GetPlace()); - } else { - PADDLE_THROW(platform::errors::InvalidArgument( - "Invalid input variable type, only support LodTensor and " - "SelectedRows types, but got type is %s.", - framework::ToTypeName(in_var->Type()))); - } - - PADDLE_ENFORCE_NOT_NULL(input, - platform::errors::InvalidArgument( - "Input(X) of ClipByNormOp should not be null. " - "Please check if it is created correctly.")); - std::vector reduce_dims; - reduce_dims.resize(input->dims().size()); - for (int i = 0; i < reduce_dims.size(); ++i) { - reduce_dims[i] = i; - } - Tensor tmp = context.AllocateTmpTensor( - {1}, dev_ctx); - TensorReduceImpl>( - dev_ctx, - *input, - &tmp, - kps::SquareFunctor(), - reduce_dims, - dev_ctx.stream()); - auto tmp_eigen = EigenVector::Flatten(tmp); - auto x_norm = tmp_eigen.sqrt(); - - auto x = EigenVector::Flatten(*input); - auto out = EigenVector::Flatten(*output); - - auto& place = - *context.template device_context() - .eigen_device(); - - auto temp = (x_norm <= max_norm).template cast(); - auto epsilon = - ((x_norm <= static_cast(1e-30)).all().template cast()) * - static_cast(1e-6); - - auto scaling = - (temp + (static_cast(1) - temp) * max_norm / (x_norm + epsilon)) - .template cast(); - Eigen::array one_dim{{1}}; - Eigen::DSizes m_dsize(input->numel()); - - out.device(place) = x * scaling.reshape(one_dim).broadcast(m_dsize); - } -}; - -} // namespace operators -} // namespace paddle - -namespace ops = paddle::operators; -namespace plat = paddle::platform; -REGISTER_OP_CUDA_KERNEL( - clip_by_norm, - ops::ClipByNormKernel, - ops::ClipByNormKernel); diff --git a/paddle/fluid/operators/clip_by_norm_op.h b/paddle/fluid/operators/clip_by_norm_op.h index 7387821338cd9c0b54b70ad5442f3604ec7cfd9e..6fde5106f10a4ed280ab0c74fdff7a37123b44a0 100644 --- a/paddle/fluid/operators/clip_by_norm_op.h +++ b/paddle/fluid/operators/clip_by_norm_op.h @@ -30,76 +30,6 @@ template using EigenVector = framework::EigenVector; -template -class ClipByNormKernel : public framework::OpKernel { - public: - void Compute(const framework::ExecutionContext& context) const override { - auto max_norm = context.Attr("max_norm"); - auto in_var = context.InputVar("X"); - - Tensor* output = nullptr; - const Tensor* input = nullptr; - if (in_var->IsType()) { - input = context.Input("X"); - - output = context.Output("Out"); - output->mutable_data(context.GetPlace()); - } else if (in_var->IsType()) { - auto* x = context.Input("X"); - - // merge ids in selected rows first - math::scatter::MergeAdd merge_func; - phi::SelectedRows* merged_input = - const_cast(context.scope()) - .Var() - ->GetMutable(); - merge_func( - context.template device_context(), *x, merged_input); - input = &(merged_input->value()); - - phi::SelectedRows* output_selected_rows = - context.Output("Out"); - output_selected_rows->set_rows(merged_input->rows()); - output_selected_rows->set_height(merged_input->height()); - output = output_selected_rows->mutable_value(); - output->Resize(merged_input->value().dims()); - output->mutable_data(context.GetPlace()); - } else { - PADDLE_THROW(platform::errors::InvalidArgument( - "Invalid input variable type, only support LodTensor and " - "SelectedRows types, but got type is %s.", - framework::ToTypeName(in_var->Type()))); - } - - PADDLE_ENFORCE_NOT_NULL(input, - platform::errors::InvalidArgument( - "Input(X) of ClipByNormOp should not be null. " - "Please check if it is created correctly.")); - - auto x = EigenVector::Flatten(*input); - auto out = EigenVector::Flatten(*output); - auto x_norm = x.square().sum().sqrt(); - auto& place = - *context.template device_context().eigen_device(); - - auto temp = (x_norm <= max_norm).template cast(); - auto epsilon = - ((x_norm <= static_cast(1e-30)).all().template cast()) * - static_cast(1e-6); - - auto scaling = - temp + (static_cast(1) - temp) * max_norm / (x_norm + epsilon); - Eigen::array one_dim{{1}}; - Eigen::DSizes m_dsize(input->numel()); - if (context.GetPlace() == platform::CPUPlace()) { - out.device(place) = - x * scaling.reshape(one_dim).eval().broadcast(m_dsize); - } else { - out.device(place) = x * scaling.reshape(one_dim).broadcast(m_dsize); - } - } -}; - class ClipByNormOp : public framework::OperatorWithKernel { public: using framework::OperatorWithKernel::OperatorWithKernel; diff --git a/paddle/fluid/operators/dgc_clip_by_norm_op.h b/paddle/fluid/operators/dgc_clip_by_norm_op.h index 197bf59b2a470e1f6e4e31c6706d1e3f8e73fbbc..27c30a8997b2ced3032ca1d75f491ec46ee09782 100644 --- a/paddle/fluid/operators/dgc_clip_by_norm_op.h +++ b/paddle/fluid/operators/dgc_clip_by_norm_op.h @@ -15,20 +15,24 @@ limitations under the License. */ #pragma once #include "paddle/fluid/operators/clip_by_norm_op.h" +#include "paddle/phi/kernels/clip_by_norm_kernel.h" +#include "paddle/phi/kernels/selected_rows/clip_by_norm_kernel.h" namespace paddle { namespace operators { +using Tensor = framework::Tensor; + template -class DGCClipByNormKernel : public ClipByNormKernel { +class DGCClipByNormKernel : public framework::OpKernel { public: - void Compute(const framework::ExecutionContext& context) const override { - auto rampup_begin_step = context.Attr("rampup_begin_step"); + void Compute(const framework::ExecutionContext& ctx) const override { + auto rampup_begin_step = ctx.Attr("rampup_begin_step"); if (static_cast(rampup_begin_step) < 0) { return; } - auto current_step_tensor = context.Input("current_step"); + auto current_step_tensor = ctx.Input("current_step"); auto* current_step = current_step_tensor->data(); VLOG(10) << "current_step:" << *current_step @@ -41,7 +45,30 @@ class DGCClipByNormKernel : public ClipByNormKernel { return; } - return ClipByNormKernel::Compute(context); + auto in_var = ctx.InputVar("X"); + auto max_norm = ctx.Attr("max_norm"); + auto& dev_ctx = ctx.device_context(); + + if (in_var->IsType()) { + auto* x = ctx.Input("X"); + auto* y = ctx.Output("Out"); + return phi::ClipByNormKernel( + static_cast::TYPE&>(dev_ctx), + *x, + max_norm, + y); + } else if (in_var->IsType()) { + auto* x = ctx.Input("X"); + phi::SelectedRows* output_selected_rows = + ctx.Output("Out"); + return phi::sr::ClipByNormKernel( + static_cast::TYPE&>(dev_ctx), + *x, + max_norm, + output_selected_rows); + } }; }; diff --git a/paddle/phi/api/yaml/legacy_api.yaml b/paddle/phi/api/yaml/legacy_api.yaml index 40fbdc9a9170d0924c8621af13453fd1aa58e47a..a562db94745c91f5c7230656fd0b2d8fea6ba37f 100644 --- a/paddle/phi/api/yaml/legacy_api.yaml +++ b/paddle/phi/api/yaml/legacy_api.yaml @@ -368,6 +368,14 @@ func : clip backward : clip_grad +- api : clip_by_norm + args : (Tensor x, float max_norm) + output : Tensor(out) + infer_meta : + func : ClipByNormInferMeta + kernel : + func : clip_by_norm + - api : complex args : (Tensor x, Tensor y) output : Tensor diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index 7b1c6dfe65a047af27c70476a7c146018830638c..35cada2c325e52117b100045aefdb77a425955af 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -264,6 +264,18 @@ void CholeskyInferMeta(const MetaTensor& x, bool upper, MetaTensor* out) { out->set_dtype(x.dtype()); } +void ClipByNormInferMeta(const MetaTensor& x, float max_norm, MetaTensor* out) { + PADDLE_ENFORCE_GT( + max_norm, + 0, + phi::errors::InvalidArgument("max_norm should be greater than 0. " + "Received max_norm is %f.", + max_norm)); + out->set_dims(x.dims()); + out->set_dtype(x.dtype()); + out->share_lod(x); +} + void CreateLikeInferMeta(const MetaTensor& x, DataType dtype, MetaTensor* out) { out->set_dims(x.dims()); out->set_dtype(dtype == DataType::UNDEFINED ? x.dtype() : dtype); diff --git a/paddle/phi/infermeta/unary.h b/paddle/phi/infermeta/unary.h index e825ba98f44e32680fcd9d5dbedc9432d399730b..1a0da23600339b1210887046e3e8efe9837921ef 100644 --- a/paddle/phi/infermeta/unary.h +++ b/paddle/phi/infermeta/unary.h @@ -62,6 +62,8 @@ void CastInferMeta(const MetaTensor& x, DataType out_dtype, MetaTensor* out); void CholeskyInferMeta(const MetaTensor& x, bool upper, MetaTensor* out); +void ClipByNormInferMeta(const MetaTensor& x, float max_norm, MetaTensor* out); + void CreateLikeInferMeta(const MetaTensor& x, DataType dtype, MetaTensor* out); void CumInferMeta(const MetaTensor& x, diff --git a/paddle/phi/kernels/clip_by_norm_kernel.h b/paddle/phi/kernels/clip_by_norm_kernel.h new file mode 100644 index 0000000000000000000000000000000000000000..debff5d08b6466568ca6fed02ee2247c58e1dff3 --- /dev/null +++ b/paddle/phi/kernels/clip_by_norm_kernel.h @@ -0,0 +1,27 @@ +// 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 +void ClipByNormKernel(const Context& dev_ctx, + const DenseTensor& x, + float max_norm, + DenseTensor* out); + +} // namespace phi diff --git a/paddle/phi/kernels/cpu/clip_by_norm_kernel.cc b/paddle/phi/kernels/cpu/clip_by_norm_kernel.cc new file mode 100644 index 0000000000000000000000000000000000000000..8d8e27dda32b4da97e349584471043301aadac89 --- /dev/null +++ b/paddle/phi/kernels/cpu/clip_by_norm_kernel.cc @@ -0,0 +1,34 @@ +// 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/clip_by_norm_kernel.h" + +#include "paddle/phi/backends/cpu/cpu_context.h" +#include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/kernels/impl/clip_by_norm_kernel_impl.h" + +namespace phi { + +template +void ClipByNormKernel(const Context& dev_ctx, + const DenseTensor& in, + float max_norm, + DenseTensor* output) { + return ClipByNormFunctor(dev_ctx, in, max_norm, output); +} + +} // namespace phi + +PD_REGISTER_KERNEL( + clip_by_norm, CPU, ALL_LAYOUT, phi::ClipByNormKernel, float) {} diff --git a/paddle/phi/kernels/gpu/clip_by_norm_kernel.cu b/paddle/phi/kernels/gpu/clip_by_norm_kernel.cu new file mode 100644 index 0000000000000000000000000000000000000000..6c3abf843f99873fcecb1627e2fe3ad8b2d8439c --- /dev/null +++ b/paddle/phi/kernels/gpu/clip_by_norm_kernel.cu @@ -0,0 +1,89 @@ +// 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/clip_by_norm_kernel.h" + +#include + +#include "paddle/phi/backends/gpu/gpu_context.h" +#include "paddle/phi/common/float16.h" +#include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/kernels/funcs/eigen/common.h" +#include "paddle/phi/kernels/funcs/reduce_function.h" +#include "paddle/phi/kernels/impl/clip_by_norm_kernel_impl.h" + +namespace phi { + +template +void ClipByNormKernel(const Context& dev_ctx, + const DenseTensor& in, + float max_norm, + DenseTensor* output) { + if (typeid(T) == typeid(float)) { + return ClipByNormFunctor(dev_ctx, in, max_norm, output); + } + auto input = ∈ + dev_ctx.template Alloc(output); + + PADDLE_ENFORCE_NOT_NULL(input, + phi::errors::InvalidArgument( + "Input(X) of ClipByNormOp should not be null. " + "Please check if it is created correctly.")); + std::vector reduce_dims; + reduce_dims.resize(input->dims().size()); + for (int i = 0; i < reduce_dims.size(); ++i) { + reduce_dims[i] = i; + } + DenseTensor tmp_tensor; + auto* tmp = &tmp_tensor; + tmp->Resize({1}); + dev_ctx.template Alloc(tmp); + phi::funcs::ReduceKernel>( + dev_ctx, + *input, + tmp, + kps::SquareFunctor(), + reduce_dims); + auto tmp_eigen = phi::EigenVector::Flatten(*tmp); + auto x_norm = tmp_eigen.sqrt(); + + auto x = phi::EigenVector::Flatten(*input); + auto out = phi::EigenVector::Flatten(*output); + auto* place = dev_ctx.eigen_device(); + + auto temp = (x_norm <= max_norm).template cast(); + auto epsilon = + ((x_norm <= static_cast(1e-30)).all().template cast()) * + static_cast(1e-6); + + auto scaling = + (temp + (static_cast(1) - temp) * max_norm / (x_norm + epsilon)) + .template cast(); + Eigen::array one_dim{{1}}; + Eigen::DSizes m_dsize(input->numel()); + + out.device(*place) = x * scaling.reshape(one_dim).broadcast(m_dsize); +} + +} // namespace phi + +PD_REGISTER_KERNEL(clip_by_norm, + GPU, + ALL_LAYOUT, + phi::ClipByNormKernel, + float, + phi::dtype::float16) {} diff --git a/paddle/phi/kernels/impl/clip_by_norm_kernel_impl.h b/paddle/phi/kernels/impl/clip_by_norm_kernel_impl.h new file mode 100644 index 0000000000000000000000000000000000000000..079254bb8284c041ce10241491b3fe9d0fb00340 --- /dev/null +++ b/paddle/phi/kernels/impl/clip_by_norm_kernel_impl.h @@ -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. + +#pragma once + +#include "paddle/phi/core/dense_tensor.h" +#include "paddle/phi/kernels/funcs/eigen/common.h" + +namespace phi { + +template +void ClipByNormFunctor(const Context& dev_ctx, + const DenseTensor& in, + float max_norm, + DenseTensor* output) { + auto input = ∈ + dev_ctx.template Alloc(output); + + PADDLE_ENFORCE_NOT_NULL(input, + phi::errors::InvalidArgument( + "Input(X) of ClipByNormOp should not be null. " + "Please check if it is created correctly.")); + + auto x = phi::EigenVector::Flatten(*input); + auto out = phi::EigenVector::Flatten(*output); + auto x_norm = x.square().sum().sqrt(); + auto* place = dev_ctx.eigen_device(); + + auto temp = (x_norm <= max_norm).template cast(); + auto epsilon = ((x_norm <= static_cast(1e-30)).all().template cast()) * + static_cast(1e-6); + + auto scaling = + temp + (static_cast(1) - temp) * max_norm / (x_norm + epsilon); + Eigen::array one_dim{{1}}; + Eigen::DSizes m_dsize(input->numel()); + if (dev_ctx.GetPlace() == phi::CPUPlace()) { + out.device(*place) = x * scaling.reshape(one_dim).eval().broadcast(m_dsize); + } else { + out.device(*place) = x * scaling.reshape(one_dim).broadcast(m_dsize); + } +} + +} // namespace phi diff --git a/paddle/phi/kernels/selected_rows/clip_by_norm_kernel.h b/paddle/phi/kernels/selected_rows/clip_by_norm_kernel.h new file mode 100644 index 0000000000000000000000000000000000000000..975aac23ff3ac6974e44ba6527d48ef0318a87fd --- /dev/null +++ b/paddle/phi/kernels/selected_rows/clip_by_norm_kernel.h @@ -0,0 +1,29 @@ +// 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" +#include "paddle/phi/core/selected_rows.h" + +namespace phi { +namespace sr { + +template +void ClipByNormKernel(const Context& dev_ctx, + const SelectedRows& x, + float max_norm, + SelectedRows* out); +} // namespace sr +} // namespace phi diff --git a/paddle/phi/kernels/selected_rows/cpu/clip_by_norm_kernel.cc b/paddle/phi/kernels/selected_rows/cpu/clip_by_norm_kernel.cc new file mode 100644 index 0000000000000000000000000000000000000000..ecefe8f74bb72778015048cc07a8be3b66042037 --- /dev/null +++ b/paddle/phi/kernels/selected_rows/cpu/clip_by_norm_kernel.cc @@ -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/selected_rows/clip_by_norm_kernel.h" + +#include "paddle/phi/backends/cpu/cpu_context.h" +#include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/kernels/selected_rows/impl/clip_by_norm_kernel_impl.h" + +PD_REGISTER_KERNEL( + clip_by_norm_sr, CPU, ALL_LAYOUT, phi::sr::ClipByNormKernel, float) {} diff --git a/paddle/phi/kernels/selected_rows/gpu/clip_by_norm_kernel.cu b/paddle/phi/kernels/selected_rows/gpu/clip_by_norm_kernel.cu new file mode 100644 index 0000000000000000000000000000000000000000..4245aa35b3918eb01232e6f17deb53240527c2e8 --- /dev/null +++ b/paddle/phi/kernels/selected_rows/gpu/clip_by_norm_kernel.cu @@ -0,0 +1,27 @@ +// 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/selected_rows/clip_by_norm_kernel.h" + +#include "paddle/phi/backends/gpu/gpu_context.h" +#include "paddle/phi/common/float16.h" +#include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/kernels/selected_rows/impl/clip_by_norm_kernel_impl.h" + +PD_REGISTER_KERNEL(clip_by_norm_sr, + GPU, + ALL_LAYOUT, + phi::sr::ClipByNormKernel, + float, + phi::dtype::float16) {} diff --git a/paddle/phi/kernels/selected_rows/impl/clip_by_norm_kernel_impl.h b/paddle/phi/kernels/selected_rows/impl/clip_by_norm_kernel_impl.h new file mode 100644 index 0000000000000000000000000000000000000000..5d79393a32d66df3d44a7d79187bda4288cec1e6 --- /dev/null +++ b/paddle/phi/kernels/selected_rows/impl/clip_by_norm_kernel_impl.h @@ -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. + +#pragma once + +#include "paddle/fluid/operators/math/selected_rows_functor.h" +#include "paddle/phi/core/dense_tensor.h" +#include "paddle/phi/core/device_context.h" +#include "paddle/phi/core/selected_rows.h" +#include "paddle/phi/kernels/clip_by_norm_kernel.h" +#include "paddle/phi/kernels/selected_rows/clip_by_norm_kernel.h" + +namespace phi { +namespace sr { + +template +void ClipByNormKernel(const Context& dev_ctx, + const SelectedRows& x, + float max_norm, + SelectedRows* out) { + phi::SelectedRows merged_input; + paddle::operators::math::scatter::MergeAdd merge_func; + merge_func(dev_ctx, x, &merged_input); + auto input = &(merged_input.value()); + out->set_rows(merged_input.rows()); + out->set_height(merged_input.height()); + auto out_tensor = out->mutable_value(); + out_tensor->Resize(merged_input.value().dims()); + return phi::ClipByNormKernel( + dev_ctx, *input, max_norm, out_tensor); +} + +} // namespace sr +} // namespace phi diff --git a/paddle/phi/ops/compat/clip_by_norm_sig.cc b/paddle/phi/ops/compat/clip_by_norm_sig.cc new file mode 100644 index 0000000000000000000000000000000000000000..8a2cecc0293d3f658067580401c4586e37aa35a5 --- /dev/null +++ b/paddle/phi/ops/compat/clip_by_norm_sig.cc @@ -0,0 +1,30 @@ +/* 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 ClipByNormOpArgumentMapping(const ArgumentMappingContext& ctx) { + if (ctx.IsDenseTensorInput("X")) { + return KernelSignature("clip_by_norm", {"X"}, {"max_norm"}, {"Out"}); + } else if (ctx.IsSelectedRowsInput("X")) { + return KernelSignature("clip_by_norm_sr", {"X"}, {"max_norm"}, {"Out"}); + } + return KernelSignature("unregistered", {}, {}, {}); +} + +} // namespace phi + +PD_REGISTER_ARG_MAPPING_FN(clip_by_norm, phi::ClipByNormOpArgumentMapping); diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index 050d6bfcb6bbb29be289c00d258e152eb0bbefc8..e68b70107c109858c5b1340445f58e4a077ff277 100755 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -13043,6 +13043,8 @@ def clip_by_norm(x, max_norm, name=None): # [[0.5, 0.5], [0.5, 0.5]] """ + if in_dygraph_mode(): + return _C_ops.final_state_clip_by_norm(x, max_norm) if _non_static_mode(): return _C_ops.clip_by_norm(x, 'max_norm', max_norm) diff --git a/python/paddle/fluid/tests/unittests/test_clip_by_norm_op.py b/python/paddle/fluid/tests/unittests/test_clip_by_norm_op.py index 8eb4c7a8be9655b221cac62d1537ced87af34f0e..04b9c5b8b8bc7c7573e4d36c14baf7718da00129 100644 --- a/python/paddle/fluid/tests/unittests/test_clip_by_norm_op.py +++ b/python/paddle/fluid/tests/unittests/test_clip_by_norm_op.py @@ -27,6 +27,7 @@ class TestClipByNormOp(OpTest): def setUp(self): self.max_relative_error = 0.006 + self.python_api = fluid.layers.clip_by_norm self.init_dtype() self.initTestCase() input = np.random.random(self.shape).astype(self.dtype) @@ -45,7 +46,7 @@ class TestClipByNormOp(OpTest): self.outputs = {'Out': output} def test_check_output(self): - self.check_output() + self.check_output(check_eager=True) def initTestCase(self): self.shape = (100, ) @@ -85,7 +86,9 @@ class TestClipByNormOpFp16(TestClipByNormOp): if core.is_compiled_with_cuda(): place = core.CUDAPlace(0) if core.is_float16_supported(place): - self.check_output_with_place(place, atol=0.001) + self.check_output_with_place(place, + atol=0.001, + check_eager=True) class TestClipByNormOpFp16Case1(TestClipByNormOpFp16):