|
| 1 | +// Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "kernels/funcs/elementwise_utils.h" |
| 16 | +#include "kernels/funcs/mlu_baseop.h" |
| 17 | +#include "kernels/funcs/mlu_funcs.h" |
| 18 | + |
| 19 | +namespace custom_kernel { |
| 20 | +template <typename T, typename Context> |
| 21 | +void ClipByNormKernel(const Context& dev_ctx, |
| 22 | + const phi::DenseTensor& x, |
| 23 | + float max_norm, |
| 24 | + phi::DenseTensor* out) { |
| 25 | + PADDLE_ENFORCE_NOT_NULL(&x, |
| 26 | + phi::errors::InvalidArgument( |
| 27 | + "Input(X) of ClipByNormOp should not be null. " |
| 28 | + "Please check if it is created correctly.")); |
| 29 | + phi::DenseTensor square_sum; |
| 30 | + phi::DenseTensorMeta square_sum_meta = {x.dtype(), phi::DDim({1})}; |
| 31 | + square_sum.set_meta(square_sum_meta); |
| 32 | + dev_ctx.template Alloc<T>(&square_sum); |
| 33 | + |
| 34 | + MLUCnnlTensorDesc input_desc(x); |
| 35 | + MLUCnnlTensorDesc square_sum_desc(square_sum); |
| 36 | + |
| 37 | + // L2Loss |
| 38 | + MLUCnnl::L2Loss( |
| 39 | + dev_ctx, input_desc.get(), GetBasePtr(&x), GetBasePtr(&square_sum)); |
| 40 | + |
| 41 | + // do mul |
| 42 | + phi::DenseTensor scale_tensor; |
| 43 | + scale_tensor.Resize({1}); |
| 44 | + dev_ctx.template Alloc<T>(&scale_tensor); |
| 45 | + |
| 46 | + phi::DenseTensor bias_tensor; |
| 47 | + bias_tensor.Resize({1}); |
| 48 | + dev_ctx.template Alloc<T>(&bias_tensor); |
| 49 | + |
| 50 | + MLUCnnlTensorDesc scale_desc(scale_tensor); |
| 51 | + MLUCnnlTensorDesc bias_desc(bias_tensor); |
| 52 | + FillMLUTensorWithHostValue(dev_ctx, static_cast<T>(2.0f), &scale_tensor); |
| 53 | + FillMLUTensorWithHostValue(dev_ctx, static_cast<T>(0.0f), &bias_tensor); |
| 54 | + |
| 55 | + MLUCnnl::Scale(dev_ctx, |
| 56 | + 0, |
| 57 | + square_sum_desc.get(), |
| 58 | + GetBasePtr(&square_sum), |
| 59 | + scale_desc.get(), |
| 60 | + GetBasePtr(&scale_tensor), |
| 61 | + bias_desc.get(), |
| 62 | + GetBasePtr(&bias_tensor), |
| 63 | + square_sum_desc.get(), |
| 64 | + GetBasePtr(&square_sum)); |
| 65 | + |
| 66 | + // sqrt |
| 67 | + phi::DenseTensor x_norm; |
| 68 | + phi::DenseTensorMeta x_norm_meta = {x.dtype(), phi::DDim({1})}; |
| 69 | + x_norm.set_meta(x_norm_meta); |
| 70 | + dev_ctx.template Alloc<T>(&x_norm); |
| 71 | + |
| 72 | + MLUCnnlTensorDesc x_norm_desc(x_norm); |
| 73 | + cnnlComputationPreference_t prefer = CNNL_COMPUTATION_HIGH_PRECISION; |
| 74 | + MLUCnnl::Sqrt(dev_ctx, |
| 75 | + prefer, |
| 76 | + square_sum_desc.get(), |
| 77 | + GetBasePtr(&square_sum), |
| 78 | + x_norm_desc.get(), |
| 79 | + GetBasePtr(&x_norm)); |
| 80 | + |
| 81 | + phi::DenseTensor x_norm_t; |
| 82 | + phi::DenseTensorMeta x_norm_t_meta = { |
| 83 | + x_norm.dtype(), x_norm.dims(), x_norm.layout()}; |
| 84 | + x_norm_t.set_meta(x_norm_t_meta); |
| 85 | + |
| 86 | + // sync copy |
| 87 | + dev_ctx.Wait(); |
| 88 | + TensorCopy(dev_ctx, x_norm, true, &x_norm_t, phi::CPUPlace()); |
| 89 | + auto x_norm_v = static_cast<float>(*(x_norm_t.data<T>())); |
| 90 | + |
| 91 | + dev_ctx.template Alloc<T>(out); |
| 92 | + if (x_norm_v <= max_norm) { |
| 93 | + TensorCopy(dev_ctx, x, false, out); |
| 94 | + } else { |
| 95 | + auto epsilon = x_norm_v <= static_cast<float>(1e-30) |
| 96 | + ? static_cast<float>(1e-6) |
| 97 | + : static_cast<float>(0); |
| 98 | + |
| 99 | + float scaling = max_norm / (x_norm_v + epsilon); |
| 100 | + auto scale_t = static_cast<T>(scaling); |
| 101 | + phi::DenseTensor scaling_tensor; |
| 102 | + scaling_tensor.Resize({1}); |
| 103 | + dev_ctx.template Alloc<T>(&scaling_tensor); |
| 104 | + MLUCnnlTensorDesc scaling_tensor_desc(scaling_tensor); |
| 105 | + MLUCnnl::Fill(dev_ctx, |
| 106 | + CNNL_POINTER_MODE_HOST, |
| 107 | + &scale_t, |
| 108 | + scaling_tensor_desc.get(), |
| 109 | + GetBasePtr(&scaling_tensor)); |
| 110 | + |
| 111 | + auto data_type = ToCnnlDataType<T>(); |
| 112 | + MLUCnnlTensorDesc out_desc(*out); |
| 113 | + |
| 114 | + // compute out = scaling_tensor * x |
| 115 | + MLUOpTensorKernel<T>( |
| 116 | + dev_ctx, scaling_tensor, x, -1, CNNL_OP_TENSOR_MUL, out); |
| 117 | + } |
| 118 | +} |
| 119 | +} // namespace custom_kernel |
| 120 | + |
| 121 | +PD_REGISTER_PLUGIN_KERNEL(clip_by_norm, |
| 122 | + mlu, |
| 123 | + ALL_LAYOUT, |
| 124 | + custom_kernel::ClipByNormKernel, |
| 125 | + float, |
| 126 | + phi::dtype::float16) {} |
0 commit comments