/* Copyright (c) 2021 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/norm_op.h" #include "paddle/fluid/operators/npu_op_runner.h" namespace paddle { namespace operators { using DDim = framework::DDim; using Tensor = framework::Tensor; void CheckAxis(int axis, int rank) { // check the axis is in [-rank, rank-1] if (axis <= rank - 1 && axis >= -rank) return; PADDLE_THROW(platform::errors::InvalidArgument( "axis in norm operator must between (%d) and (%d)" "but got (%d).", -rank, rank - 1, axis)); } template class NormNPUKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext &ctx) const override { VLOG(4) << "Launch Norm Op Kernel on NPU." << std::endl; auto *in_x = ctx.Input("X"); auto *out_y = ctx.Output("Out"); auto *out_norm = ctx.Output("Norm"); out_y->mutable_data(ctx.GetPlace()); out_norm->mutable_data(ctx.GetPlace()); auto xdim = in_x->dims(); float eps = ctx.Attr("epsilon"); int axis = ctx.Attr("axis"); CheckAxis(axis, xdim.size()); if (axis < 0) axis = xdim.size() + axis; framework::NPUAttributeMap attr_input_norm; attr_input_norm["axes"] = std::vector({axis}); attr_input_norm["p"] = 2; attr_input_norm["keepdim"] = true; attr_input_norm["epsilon"] = eps; const auto &runner = NpuOpRunner("LpNorm", {*in_x}, {*out_norm}, attr_input_norm); auto stream = ctx.template device_context() .stream(); runner.Run(stream); NpuOpRunner("Div", {*in_x, *out_norm}, {*out_y}, {}).Run(stream); } }; template class NormGradNPUKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext &ctx) const override { float epsilon = ctx.Attr("epsilon"); int axis = ctx.Attr("axis"); auto *x = ctx.Input("X"); auto *y = ctx.Input("Out"); auto *dy = ctx.Input(framework::GradVarName("Out")); auto *dx = ctx.Output(framework::GradVarName("X")); auto xdim = x->dims(); CheckAxis(axis, xdim.size()); auto place = ctx.GetPlace(); dx->mutable_data(place); framework::NPUAttributeMap attr_input_norm; attr_input_norm["dim"] = std::vector({axis}); attr_input_norm["eps"] = epsilon; const auto &runner = NpuOpRunner("L2NormalizeGrad", {*x, *y, *dy}, {*dx}, attr_input_norm); auto stream = ctx.template device_context() .stream(); runner.Run(stream); } }; } // namespace operators } // namespace paddle namespace ops = paddle::operators; namespace plat = paddle::platform; REGISTER_OP_NPU_KERNEL( norm, ops::NormNPUKernel, ops::NormNPUKernel) REGISTER_OP_NPU_KERNEL( norm_grad, ops::NormGradNPUKernel, ops::NormGradNPUKernel);