/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #include #include #include #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/tensor_util.h" #include "paddle/fluid/operators/npu_op_runner.h" namespace paddle { namespace operators { template class LookupTableV2NPUKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext &ctx) const override { auto *ids_t = ctx.Input("Ids"); // int tensor auto *output_t = ctx.Output("Out"); // float tensor auto *table_t = ctx.Input("W"); // It seems cann 20.1 accepts int64, but cann 20.2+ not. PADDLE_ENFORCE_EQ(ids_t->type(), framework::proto::VarType::INT32, platform::errors::Unimplemented( "The index of LookupTableV2 should be int32.")); auto *table_var = ctx.InputVar("W"); PADDLE_ENFORCE_EQ( table_var->IsType(), true, platform::errors::InvalidArgument("npu only accept LoDTensor")); output_t->mutable_data(ctx.GetPlace()); framework::NPUAttributeMap attr_input = {{"validate_indices", false}}; auto runner = NpuOpRunner("Gather", {*table_t, *ids_t}, {*output_t}, attr_input); auto stream = ctx.template device_context() .stream(); runner.Run(stream); } }; template class LookupTableV2GradNPUKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext &ctx) const override { auto *ids_t = ctx.Input("Ids"); auto *output_grad_t = ctx.Input(framework::GradVarName("Out")); auto *table_grad_t = ctx.Output(framework::GradVarName("W")); auto *p = table_grad_t->mutable_data(ctx.GetPlace()); auto stream = ctx.template device_context() .stream(); platform::NPUMemsetAsync(static_cast(p), 0, table_grad_t->numel() * sizeof(T), stream); // NOTE(zhiqiu): It seems in cann 20.1, the first input and output // can be different tensor, but in cann 20.2+, it does inplace operation. // Thus, the first input and output should be same tensor. auto runner_scatter = NpuOpRunner("ScatterAdd", {*table_grad_t, *ids_t, *output_grad_t}, {*table_grad_t}, {{"use_locking", true}}); runner_scatter.Run(stream); } }; } // namespace operators } // namespace paddle namespace ops = paddle::operators; REGISTER_OP_NPU_KERNEL( lookup_table_v2, ops::LookupTableV2NPUKernel, ops::LookupTableV2NPUKernel, ops::LookupTableV2NPUKernel); REGISTER_OP_NPU_KERNEL( lookup_table_v2_grad, ops::LookupTableV2GradNPUKernel, ops::LookupTableV2GradNPUKernel, ops::LookupTableV2GradNPUKernel);