|
| 1 | +// Copyright (c) 2023 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 | +/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. */ |
| 16 | + |
| 17 | +/*This code is copied from NVIDIA apex: |
| 18 | + * https://github.com/NVIDIA/apex |
| 19 | + * with minor changes. */ |
| 20 | + |
| 21 | +#include "ln.h" // NOLINT |
| 22 | +#include "paddle/phi/core/dense_tensor.h" |
| 23 | +#include "paddle/phi/core/kernel_registry.h" |
| 24 | + |
| 25 | +namespace phi { |
| 26 | + |
| 27 | +template <typename T, typename Context> |
| 28 | +void LnBwdKernel(const Context &dev_ctx, |
| 29 | + const DenseTensor &x, |
| 30 | + const DenseTensor &scale, |
| 31 | + const DenseTensor &mean, |
| 32 | + const DenseTensor &invvar, |
| 33 | + const DenseTensor &y_grad, |
| 34 | + float epsilon, |
| 35 | + DenseTensor *x_grad, |
| 36 | + DenseTensor *scale_grad, |
| 37 | + DenseTensor *bias_grad) { |
| 38 | + auto input_type = x.type(); |
| 39 | + auto weight_type = scale.type(); |
| 40 | + auto output_type = weight_type; |
| 41 | + auto compute_type = paddle::DataType::FLOAT32; |
| 42 | + |
| 43 | + PD_CHECK(y_grad.dtype() == output_type); |
| 44 | + |
| 45 | + auto sizes = x.dims(); |
| 46 | + PD_CHECK(sizes.size() >= 2); |
| 47 | + PD_CHECK(y_grad.dims() == sizes); |
| 48 | + |
| 49 | + int64_t rows = 1; |
| 50 | + for (size_t i = 0; i + 1 < sizes.size(); ++i) { |
| 51 | + rows *= sizes[i]; |
| 52 | + } |
| 53 | + auto cols = sizes[sizes.size() - 1]; |
| 54 | + |
| 55 | + auto hidden_size = scale.numel(); |
| 56 | + |
| 57 | + PD_CHECK(mean.numel() == rows); |
| 58 | + |
| 59 | + PD_CHECK(mean.dims() == invvar.dims()); |
| 60 | + |
| 61 | + PD_CHECK(scale.numel() == cols); |
| 62 | + |
| 63 | + dev_ctx.template Alloc<T>(x_grad); |
| 64 | + dev_ctx.template Alloc<T>(scale_grad); |
| 65 | + dev_ctx.template Alloc<T>(bias_grad); |
| 66 | + |
| 67 | + auto place = x.place(); |
| 68 | + |
| 69 | + LaunchNormBwd<T, Context>( |
| 70 | + dev_ctx, |
| 71 | + dev_ctx.stream(), |
| 72 | + place, |
| 73 | + /* x_ptr */ x.data(), |
| 74 | + /* scale_ptr */ scale.data(), |
| 75 | + /* mean_ptr */ mean.data(), |
| 76 | + /* invvar_ptr */ invvar.data(), |
| 77 | + /* y_grad_ptr */ y_grad.data(), |
| 78 | + /* x_grad_ptr */ x_grad ? x_grad->data() : nullptr, |
| 79 | + /* scale_grad_ptr */ scale_grad ? scale_grad->data() : nullptr, |
| 80 | + /* bias_grad_ptr */ bias_grad ? bias_grad->data() : nullptr, |
| 81 | + weight_type, |
| 82 | + input_type, |
| 83 | + output_type, |
| 84 | + compute_type, |
| 85 | + hidden_size, |
| 86 | + rows, |
| 87 | + cols, |
| 88 | + epsilon); |
| 89 | +} |
| 90 | +} // namespace phi |
| 91 | + |
| 92 | +PD_REGISTER_KERNEL(fast_ln_grad, |
| 93 | + GPU, |
| 94 | + ALL_LAYOUT, |
| 95 | + phi::LnBwdKernel, |
| 96 | + float, |
| 97 | + double, |
| 98 | + phi::float16, |
| 99 | + phi::bfloat16) {} |
0 commit comments