|
| 1 | +// Copyright (c) 2025 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 "paddle/phi/api/backward/backward_api.h" |
| 16 | +#include "paddle/phi/api/include/api.h" |
| 17 | +#include "paddle/phi/backends/all_context.h" |
| 18 | +#include "paddle/phi/backends/device_manager.h" |
| 19 | +#include "paddle/phi/core/distributed/collective/process_group.h" |
| 20 | +#include "paddle/phi/core/distributed/comm_context_manager.h" |
| 21 | +#include "paddle/phi/core/distributed/xccl_comm_context.h" |
| 22 | +#include "paddle/phi/core/kernel_registry.h" |
| 23 | +#include "paddle/phi/core/tensor_utils.h" |
| 24 | +#include "paddle/phi/kernels/funcs/axis_utils.h" |
| 25 | +#ifdef PADDLE_WITH_CUSTOM_DEVICE |
| 26 | +namespace phi { |
| 27 | + |
| 28 | +template <typename T, typename Context> |
| 29 | +void CSoftmaxWithEntropyGradKernel(const Context& dev_ctx, |
| 30 | + const DenseTensor& softmax_in, |
| 31 | + const DenseTensor& label_in, |
| 32 | + const DenseTensor& loss_grad_in, |
| 33 | + int64_t ignore_index, |
| 34 | + int ring_id, |
| 35 | + int rank, |
| 36 | + int nranks, |
| 37 | + DenseTensor* logits_grad) { |
| 38 | + const phi::DenseTensor* labels = &label_in; |
| 39 | + const phi::DenseTensor* loss_grad = &loss_grad_in; |
| 40 | + const phi::DenseTensor* softmax = &softmax_in; |
| 41 | + phi::DenseTensor* logit_grad = logits_grad; |
| 42 | + |
| 43 | + if (logit_grad != softmax) { |
| 44 | + phi::Copy(dev_ctx, *softmax, dev_ctx.GetPlace(), false, logit_grad); |
| 45 | + } |
| 46 | + const auto softmax_dims = softmax->dims(); |
| 47 | + const int axis = softmax_dims.size() - 1; |
| 48 | + const int N = phi::funcs::SizeToAxis(axis, softmax_dims); |
| 49 | + const int D = phi::funcs::SizeFromAxis(axis, softmax_dims); |
| 50 | + const auto& label_type = labels->dtype(); |
| 51 | + |
| 52 | + if (label_type == phi::DataType::INT32 || |
| 53 | + label_type == phi::DataType::INT64) { |
| 54 | + auto logit_grad_t = std::make_shared<phi::DenseTensor>(); |
| 55 | + logit_grad_t->ShareDataWith(*logit_grad).Resize({N, D}); |
| 56 | + auto loss_grad_t = std::make_shared<phi::DenseTensor>(); |
| 57 | + loss_grad_t->ShareDataWith(*loss_grad).Resize({N}); |
| 58 | + auto labels_1d = std::make_shared<phi::DenseTensor>(); |
| 59 | + labels_1d->ShareDataWith(*labels).Resize({N}); |
| 60 | + paddle::Tensor logits_grad_tensor(logit_grad_t), |
| 61 | + loss_grad_tensor(loss_grad_t), labels_1d_tensor(labels_1d); |
| 62 | + |
| 63 | + auto labels_1d_not_equal_ignore = paddle::experimental::reshape( |
| 64 | + paddle::experimental::not_equal( |
| 65 | + labels_1d_tensor, |
| 66 | + paddle::experimental::full_like(labels_1d_tensor, |
| 67 | + ignore_index, |
| 68 | + labels_1d_tensor.dtype(), |
| 69 | + labels_1d_tensor.place())), |
| 70 | + {N, 1}); |
| 71 | + auto start_index_tensor = |
| 72 | + paddle::experimental::full_like(labels_1d_tensor, |
| 73 | + rank * D, |
| 74 | + labels_1d_tensor.dtype(), |
| 75 | + labels_1d_tensor.place()); |
| 76 | + |
| 77 | + auto logits_grad_out_tensor1 = paddle::experimental::subtract( |
| 78 | + paddle::experimental::multiply( |
| 79 | + logits_grad_tensor, |
| 80 | + paddle::experimental::cast(labels_1d_not_equal_ignore, |
| 81 | + logits_grad_tensor.dtype())), |
| 82 | + paddle::experimental::cast( |
| 83 | + paddle::experimental::one_hot( |
| 84 | + paddle::experimental::subtract(labels_1d_tensor, |
| 85 | + start_index_tensor), |
| 86 | + D), |
| 87 | + logits_grad_tensor.dtype())); |
| 88 | + |
| 89 | + auto logits_grad_out_tensor2 = paddle::experimental::multiply( |
| 90 | + logits_grad_out_tensor1, |
| 91 | + paddle::experimental::reshape(loss_grad_tensor, {N, 1})); |
| 92 | + logit_grad |
| 93 | + ->ShareDataWith(*reinterpret_cast<phi::DenseTensor*>( |
| 94 | + logits_grad_out_tensor2.impl().get())) |
| 95 | + .Resize(softmax_dims); |
| 96 | + } else { |
| 97 | + PADDLE_THROW(common::errors::Unavailable( |
| 98 | + "CustomDevice c_softmax_with_cross_entropy_grad " |
| 99 | + "label_type only support int32/int64")); |
| 100 | + } |
| 101 | +} |
| 102 | +} // namespace phi |
| 103 | + |
| 104 | +PD_REGISTER_KERNEL(c_softmax_with_cross_entropy_grad, |
| 105 | + Custom, |
| 106 | + ALL_LAYOUT, |
| 107 | + phi::CSoftmaxWithEntropyGradKernel, |
| 108 | + float, |
| 109 | + double, |
| 110 | + phi::dtype::float16) {} |
| 111 | +#endif |
0 commit comments