one_hot_v2_op_xpu.cc 2.7 KB
Newer Older
T
taixiurong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
//   Copyright (c) 2018 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.

#ifdef PADDLE_WITH_XPU
#include <string>
#include <vector>

#include "paddle/fluid/operators/one_hot_op.h"

namespace paddle {
namespace operators {

using LoDTensor = framework::LoDTensor;
using Tensor = framework::Tensor;

template <typename DeviceContext, typename T>
class OneHotV2XPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    auto* in = context.Input<LoDTensor>("X");
    auto* out = context.Output<LoDTensor>("Out");
    int depth = context.Attr<int>("depth");
    if (context.HasInput("depth_tensor")) {
      auto* depth_tensor = context.Input<Tensor>("depth_tensor");
      auto* depth_data = depth_tensor->data<int32_t>();
      if (platform::is_xpu_place(depth_tensor->place())) {
        xpu_memcpy(static_cast<void*>(&depth),
39 40
                   static_cast<const void*>(depth_data),
                   sizeof(int32_t),
T
taixiurong 已提交
41 42 43 44 45 46 47 48 49 50 51
                   XPU_DEVICE_TO_HOST);
      } else {
        depth = depth_data[0];
      }
      auto out_dims = out->dims();
      out_dims[out_dims.size() - 1] = depth;
      out->Resize(out_dims);
    }

    auto& dev_ctx = context.template device_context<DeviceContext>();
    int len = in->numel();
52 53 54 55 56 57 58
    int ret = xpu::one_hot<T>(dev_ctx.x_context(),
                              in->data<T>(),
                              out->mutable_data<float>(context.GetPlace()),
                              len,
                              depth,
                              1.0,
                              0.0);
T
taixiurong 已提交
59

60 61
    PADDLE_ENFORCE_EQ(ret,
                      XPU_SUCCESS,
T
taixiurong 已提交
62
                      platform::errors::External(
63 64
                          "XPU one_hot kernel return wrong value[%d %s]",
                          ret,
T
taixiurong 已提交
65 66 67 68 69 70 71 72 73
                          XPUAPIErrorMsg[ret]));
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OP_XPU_KERNEL(
74 75
    one_hot_v2,
    ops::OneHotV2XPUKernel<paddle::platform::XPUDeviceContext, int>,
T
taixiurong 已提交
76 77
    ops::OneHotV2XPUKernel<paddle::platform::XPUDeviceContext, int64_t>);
#endif