lookup_table_v2_op_mlu.cc 5.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* Copyright (c) 2022 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 "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/mlu/mlu_baseop.h"

namespace paddle {
namespace operators {

20
using Tensor = phi::DenseTensor;
21 22 23 24 25

template <typename T>
class LookupTableV2MLUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext &ctx) const override {
26 27 28
    auto *ids_t = ctx.Input<phi::DenseTensor>("Ids");      // int tensor
    auto *output_t = ctx.Output<phi::DenseTensor>("Out");  // float tensor
    auto *table_t = ctx.Input<phi::DenseTensor>("W");
29
    int padding_idx = static_cast<int>(ctx.Attr<int64_t>("padding_idx"));
30 31 32

    auto *table_var = ctx.InputVar("W");
    PADDLE_ENFORCE_EQ(
33
        table_var->IsType<phi::DenseTensor>(),
34
        true,
35
        platform::errors::InvalidArgument("mlu only accept phi::DenseTensor"));
36 37 38 39 40 41
    output_t->mutable_data<T>(ctx.GetPlace());

    MLUCnnlTensorDesc ids_desc(*ids_t);
    MLUCnnlTensorDesc table_desc(*table_t);
    MLUCnnlTensorDesc output_desc(*output_t);

42 43 44 45 46
    MLUCnnl::EmbeddingForward(ctx,
                              padding_idx,
                              table_desc.get(),
                              GetBasePtr(table_t),
                              ids_desc.get(),
47
                              static_cast<const int *>(GetBasePtr(ids_t)),
48 49
                              output_desc.get(),
                              GetBasePtr(output_t));
50 51 52 53 54 55 56
  }
};

template <typename T>
class LookupTableV2GradMLUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext &ctx) const override {
57
    auto *table_var = ctx.InputVar("W");
58 59 60 61 62 63
    PADDLE_ENFORCE_EQ(
        table_var->IsType<phi::DenseTensor>(),
        true,
        platform::errors::PermissionDenied(
            "Unsupported Variable Type , idx in "
            "LookupTableV2GradMLUKernel should be phi::DenseTensor."));
64 65
    bool is_sparse = ctx.Attr<bool>("is_sparse");
    PADDLE_ENFORCE_EQ(
66 67
        is_sparse,
        false,
68 69
        platform::errors::InvalidArgument(
            "LookupTableV2GradMLUKernel dose NOT support is_sparse = True."));
70
    auto *ids_t = ctx.Input<phi::DenseTensor>("Ids");
71
    auto *output_grad_t =
72
        ctx.Input<phi::DenseTensor>(framework::GradVarName("Out"));
73
    auto *table_grad_t =
74
        ctx.Output<phi::DenseTensor>(framework::GradVarName("W"));
75 76 77 78
    table_grad_t->mutable_data<T>(ctx.GetPlace());

    int padding_idx = static_cast<int>(ctx.Attr<int64_t>("padding_idx"));

79 80
    int64_t ids_numel = ids_t->numel();
    PADDLE_ENFORCE_EQ(
81 82
        ids_numel <= std::numeric_limits<int32_t>::max(),
        true,
83 84 85 86
        platform::errors::OutOfRange(
            "Number of ids greater than int32_t::max , please check "
            "number of ids in LookupTableV2GradMLUKernel."));

87 88 89 90 91 92
    Tensor ids_int32(ids_t->dtype());
    if (ids_t->dtype() != DataType::INT32) {
      ids_int32.mutable_data<int>(ids_t->dims(), ctx.GetPlace());
      MLUCnnlTensorDesc ids_desc(*ids_t);
      MLUCnnlTensorDesc ids_int32_desc(ids_int32);
      auto cast_type = GetCastDataType(ids_t->dtype(), DataType::INT32);
93 94 95 96 97 98
      MLUCnnl::Cast(ctx,
                    cast_type,
                    ids_desc.get(),
                    GetBasePtr(ids_t),
                    ids_int32_desc.get(),
                    GetBasePtr(&ids_int32));
99 100 101 102 103 104 105 106
    } else {
      ids_int32 = *ids_t;
    }

    MLUCnnlTensorDesc ids_int32_desc(ids_int32);
    MLUCnnlTensorDesc output_grad_desc(*output_grad_t);
    MLUCnnlTensorDesc table_grad_desc(*table_grad_t);

107 108 109 110 111 112 113 114
    MLUCnnl::EmbeddingBackward(ctx,
                               padding_idx,
                               false,
                               ids_int32_desc.get(),
                               GetBasePtr(&ids_int32),
                               output_grad_desc.get(),
                               GetBasePtr(output_grad_t),
                               table_grad_desc.get(),
115 116 117 118 119 120 121 122 123
                               GetBasePtr(table_grad_t));
  }
};
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
namespace plat = paddle::platform;

124 125
REGISTER_OP_MLU_KERNEL(lookup_table_v2,
                       ops::LookupTableV2MLUKernel<float>,
126 127 128 129 130 131
                       ops::LookupTableV2MLUKernel<int>,
                       ops::LookupTableV2MLUKernel<plat::float16>);

REGISTER_OP_MLU_KERNEL(lookup_table_v2_grad,
                       ops::LookupTableV2GradMLUKernel<float>,
                       ops::LookupTableV2GradMLUKernel<plat::float16>);