|
| 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 | +#include "paddle/phi/kernels/c_embedding_grad_kernel.h" |
| 16 | +#include "glog/logging.h" |
| 17 | +#include "paddle/phi/api/backward/backward_api.h" |
| 18 | +#include "paddle/phi/api/include/api.h" |
| 19 | +#include "paddle/phi/backends/all_context.h" |
| 20 | +#include "paddle/phi/common/float16.h" |
| 21 | +#include "paddle/phi/core/kernel_registry.h" |
| 22 | + |
| 23 | +namespace phi { |
| 24 | + |
| 25 | +template <typename T, typename Context> |
| 26 | +void CEmbeddingGradKernel(const Context& dev_ctx, |
| 27 | + const DenseTensor& w, |
| 28 | + const DenseTensor& ids, |
| 29 | + const DenseTensor& out_grad, |
| 30 | + int64_t start_index, |
| 31 | + DenseTensor* w_grad) { |
| 32 | + w_grad->Resize(w.dims()); |
| 33 | + dev_ctx.template Alloc(w_grad, w.dtype()); |
| 34 | + const auto& index_type = ids.dtype(); |
| 35 | + if (index_type == phi::DataType::INT32 || |
| 36 | + index_type == phi::DataType::INT64) { |
| 37 | + auto K = ids.numel(); |
| 38 | + auto N = w.dims()[0]; |
| 39 | + auto D = w.dims()[1]; |
| 40 | + |
| 41 | + auto x_tmp = std::make_shared<phi::DenseTensor>(); |
| 42 | + x_tmp->ShareDataWith(ids).Resize({K}); |
| 43 | + auto w_tmp = std::make_shared<phi::DenseTensor>(); |
| 44 | + w_tmp->set_meta(w.meta()); |
| 45 | + dev_ctx.Alloc(w_tmp.get(), w_tmp->dtype()); |
| 46 | + auto out_grad_tmp = std::make_shared<phi::DenseTensor>(); |
| 47 | + out_grad_tmp->ShareDataWith(out_grad).Resize({K, D}); |
| 48 | + paddle::Tensor x_tensor(x_tmp), w_tensor(w_tmp), |
| 49 | + out_grad_tensor(out_grad_tmp); |
| 50 | + |
| 51 | + auto start_index_tensor = paddle::experimental::full_like( |
| 52 | + x_tensor, start_index, x_tensor.dtype(), x_tensor.place()); |
| 53 | + auto end_index_tensor = paddle::experimental::full_like( |
| 54 | + x_tensor, start_index + N, x_tensor.dtype(), x_tensor.place()); |
| 55 | + auto ids_mask_tensor = paddle::experimental::logical_and( |
| 56 | + x_tensor.greater_equal(start_index_tensor), |
| 57 | + x_tensor.less_than(end_index_tensor)); |
| 58 | + auto real_ids_tensor = (x_tensor - start_index_tensor) |
| 59 | + .multiply(paddle::experimental::cast( |
| 60 | + ids_mask_tensor, x_tensor.dtype())); |
| 61 | + auto out_grad_tensor_mul_mask = |
| 62 | + paddle::experimental::reshape(out_grad_tensor, {K, D}) |
| 63 | + .multiply(paddle::experimental::reshape( |
| 64 | + paddle::experimental::cast(ids_mask_tensor, w.dtype()), |
| 65 | + {K, 1})); |
| 66 | + paddle::Tensor w_grad_tensor; |
| 67 | + paddle::experimental::embedding_grad(real_ids_tensor, |
| 68 | + w_tensor, |
| 69 | + out_grad_tensor_mul_mask, |
| 70 | + -1, |
| 71 | + false, |
| 72 | + &w_grad_tensor); |
| 73 | + w_grad->ShareDataWith( |
| 74 | + *reinterpret_cast<phi::DenseTensor*>(w_grad_tensor.impl().get())); |
| 75 | + |
| 76 | + } else { |
| 77 | + PADDLE_THROW(phi::errors::Unavailable( |
| 78 | + "Custom Device c_embedding_grad ids only support int32 or int64.")); |
| 79 | + } |
| 80 | +} |
| 81 | +} // namespace phi |
| 82 | + |
| 83 | +#ifdef PADDLE_WITH_CUSTOM_DEVICE |
| 84 | +PD_REGISTER_KERNEL(c_embedding_grad, |
| 85 | + Custom, |
| 86 | + ALL_LAYOUT, |
| 87 | + phi::CEmbeddingGradKernel, |
| 88 | + float, |
| 89 | + phi::dtype::float16, |
| 90 | + phi::dtype::bfloat16) {} |
| 91 | +#endif |
0 commit comments