|
| 1 | +/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +you may not use this file except in compliance with the License. |
| 4 | +You may obtain a copy of the License at |
| 5 | +
|
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +
|
| 8 | +Unless required by applicable law or agreed to in writing, software |
| 9 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +See the License for the specific language governing permissions and |
| 12 | +limitations under the License. */ |
| 13 | + |
| 14 | +#include "paddle/fluid/framework/op_registry.h" |
| 15 | +#include "paddle/fluid/operators/mlu/mlu_baseop.h" |
| 16 | + |
| 17 | +namespace paddle { |
| 18 | +namespace operators { |
| 19 | + |
| 20 | +using Tensor = framework::Tensor; |
| 21 | +constexpr int64_t kNoPadding = -1; |
| 22 | + |
| 23 | +template <typename T> |
| 24 | +class LookupTableV2MLUKernel : public framework::OpKernel<T> { |
| 25 | + public: |
| 26 | + void Compute(const framework::ExecutionContext &ctx) const override { |
| 27 | + auto *ids_t = ctx.Input<framework::LoDTensor>("Ids"); // int tensor |
| 28 | + auto *output_t = ctx.Output<framework::LoDTensor>("Out"); // float tensor |
| 29 | + auto *table_t = ctx.Input<framework::LoDTensor>("W"); |
| 30 | + |
| 31 | + auto *table_var = ctx.InputVar("W"); |
| 32 | + PADDLE_ENFORCE_EQ( |
| 33 | + table_var->IsType<framework::LoDTensor>(), true, |
| 34 | + platform::errors::InvalidArgument("mlu only accept LoDTensor")); |
| 35 | + output_t->mutable_data<T>(ctx.GetPlace()); |
| 36 | + |
| 37 | + MLUCnnlTensorDesc ids_desc(*ids_t); |
| 38 | + MLUCnnlTensorDesc table_desc(*table_t); |
| 39 | + MLUCnnlTensorDesc output_desc(*output_t); |
| 40 | + |
| 41 | + int64_t padding_idx = ctx.Attr<int64_t>("padding_idx"); |
| 42 | + if (padding_idx == kNoPadding) { |
| 43 | + MLUCnnl::GatherFunctor(ctx, /*axis=*/0, /*batch_dims=*/0, |
| 44 | + table_desc.get(), GetBasePtr(table_t), |
| 45 | + ids_desc.get(), GetBasePtr(ids_t), |
| 46 | + output_desc.get(), GetBasePtr(output_t)); |
| 47 | + } else { |
| 48 | + Tensor tmp_table_t(table_t->type()); |
| 49 | + tmp_table_t.mutable_data<T>(table_t->dims(), ctx.GetPlace()); |
| 50 | + |
| 51 | + Tensor index; |
| 52 | + index.mutable_data<int32_t>({1, 1}, ctx.GetPlace()); |
| 53 | + auto idx_value = static_cast<int32_t>(padding_idx); |
| 54 | + MLUCnnlTensorDesc index_desc(index); |
| 55 | + MLUCnnl::Fill(ctx, CNNL_POINTER_MODE_HOST, &idx_value, index_desc.get(), |
| 56 | + GetBasePtr(&index)); |
| 57 | + |
| 58 | + auto update_dim = phi::make_ddim({1, table_t->dims()[1]}); |
| 59 | + Tensor update; |
| 60 | + update.mutable_data<T>(update_dim, ctx.GetPlace()); |
| 61 | + |
| 62 | + auto update_value = static_cast<T>(0); |
| 63 | + MLUCnnlTensorDesc update_desc(update); |
| 64 | + MLUCnnl::Fill(ctx, CNNL_POINTER_MODE_HOST, &update_value, |
| 65 | + update_desc.get(), GetBasePtr(&update)); |
| 66 | + |
| 67 | + MLUCnnlTensorDesc tmp_table_desc(tmp_table_t); |
| 68 | + MLUCnnl::ScatterNd( |
| 69 | + ctx, CNNL_SCATTERND_UPDATE, index_desc.get(), GetBasePtr(&index), |
| 70 | + update_desc.get(), GetBasePtr(&update), table_desc.get(), |
| 71 | + GetBasePtr(table_t), tmp_table_desc.get(), GetBasePtr(&tmp_table_t)); |
| 72 | + |
| 73 | + MLUCnnl::GatherFunctor(ctx, /*axis=*/0, /*batch_dims=*/0, |
| 74 | + tmp_table_desc.get(), GetBasePtr(&tmp_table_t), |
| 75 | + ids_desc.get(), GetBasePtr(ids_t), |
| 76 | + output_desc.get(), GetBasePtr(output_t)); |
| 77 | + } |
| 78 | + } |
| 79 | +}; |
| 80 | + |
| 81 | +template <typename T> |
| 82 | +class LookupTableV2GradMLUKernel : public framework::OpKernel<T> { |
| 83 | + public: |
| 84 | + void Compute(const framework::ExecutionContext &ctx) const override { |
| 85 | + auto *ids_t = ctx.Input<framework::LoDTensor>("Ids"); |
| 86 | + auto *output_grad_t = |
| 87 | + ctx.Input<framework::LoDTensor>(framework::GradVarName("Out")); |
| 88 | + auto *table_grad_t = |
| 89 | + ctx.Output<framework::LoDTensor>(framework::GradVarName("W")); |
| 90 | + table_grad_t->mutable_data<T>(ctx.GetPlace()); |
| 91 | + |
| 92 | + int padding_idx = static_cast<int>(ctx.Attr<int64_t>("padding_idx")); |
| 93 | + |
| 94 | + Tensor ids_int32(ids_t->dtype()); |
| 95 | + if (ids_t->dtype() != DataType::INT32) { |
| 96 | + ids_int32.mutable_data<int>(ids_t->dims(), ctx.GetPlace()); |
| 97 | + MLUCnnlTensorDesc ids_desc(*ids_t); |
| 98 | + MLUCnnlTensorDesc ids_int32_desc(ids_int32); |
| 99 | + auto cast_type = GetCastDataType(ids_t->dtype(), DataType::INT32); |
| 100 | + MLUCnnl::Cast(ctx, cast_type, ids_desc.get(), GetBasePtr(ids_t), |
| 101 | + ids_int32_desc.get(), GetBasePtr(&ids_int32)); |
| 102 | + } else { |
| 103 | + ids_int32 = *ids_t; |
| 104 | + } |
| 105 | + |
| 106 | + MLUCnnlTensorDesc ids_int32_desc(ids_int32); |
| 107 | + MLUCnnlTensorDesc output_grad_desc(*output_grad_t); |
| 108 | + MLUCnnlTensorDesc table_grad_desc(*table_grad_t); |
| 109 | + |
| 110 | + MLUCnnl::EmbeddingBackward(ctx, padding_idx, false, ids_int32_desc.get(), |
| 111 | + GetBasePtr(&ids_int32), output_grad_desc.get(), |
| 112 | + GetBasePtr(output_grad_t), table_grad_desc.get(), |
| 113 | + GetBasePtr(table_grad_t)); |
| 114 | + } |
| 115 | +}; |
| 116 | +} // namespace operators |
| 117 | +} // namespace paddle |
| 118 | + |
| 119 | +namespace ops = paddle::operators; |
| 120 | +namespace plat = paddle::platform; |
| 121 | + |
| 122 | +REGISTER_OP_MLU_KERNEL(lookup_table_v2, ops::LookupTableV2MLUKernel<float>, |
| 123 | + ops::LookupTableV2MLUKernel<int>, |
| 124 | + ops::LookupTableV2MLUKernel<plat::float16>); |
| 125 | + |
| 126 | +REGISTER_OP_MLU_KERNEL(lookup_table_v2_grad, |
| 127 | + ops::LookupTableV2GradMLUKernel<float>, |
| 128 | + ops::LookupTableV2GradMLUKernel<int>, |
| 129 | + ops::LookupTableV2GradMLUKernel<plat::float16>); |
0 commit comments