tile_op_xpu.cc 5.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/* 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. */

#ifdef PADDLE_WITH_XPU

14 15
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/tile_op_functor.h"
16 17 18 19

namespace paddle {
namespace operators {

20 21
using Tensor = framework::Tensor;

22 23 24 25 26 27
template <typename T>
class TileXPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    auto rank = context.Input<Tensor>("X")->dims().size();
    PADDLE_ENFORCE_GE(
28 29
        rank,
        1,
30 31 32 33
        platform::errors::InvalidArgument(
            "The rank of the input 'x' for tile op must be a positive "
            "integer, but the value received is %d.",
            rank));
34
    PADDLE_ENFORCE_LE(
35 36
        rank,
        MAX_RANK_SUPPORTED,
37 38 39
        platform::errors::InvalidArgument(
            "The rank of the input 'x' for tile op "
            "must be less than or equal to %d, but the value received is %d.",
40 41
            MAX_RANK_SUPPORTED,
            rank));
42 43 44
    auto repeat_times = get_repeat_times(context);
    int repeat_times_size = repeat_times.size();
    PADDLE_ENFORCE_GE(
45 46
        repeat_times_size,
        1,
47 48 49 50 51
        platform::errors::InvalidArgument(
            "The number of elements of the input 'repeat_times' for tile "
            "op must be positive, but the value received is %d.",
            repeat_times_size));
    PADDLE_ENFORCE_LE(
52 53
        repeat_times_size,
        MAX_RANK_SUPPORTED,
54 55 56
        platform::errors::InvalidArgument(
            "The number of elements of the input 'repeat_times' for tile op "
            "must be less than or equal to %d, but the value received is %d.",
57 58
            MAX_RANK_SUPPORTED,
            repeat_times_size));
59 60 61 62 63

    auto* in0 = context.Input<framework::Tensor>("X");
    auto in_dims = in0->dims();
    for (size_t i = 0; i < repeat_times.size(); ++i) {
      PADDLE_ENFORCE_GT(
64 65
          repeat_times[i],
          0,
66 67 68 69 70
          platform::errors::InvalidArgument(
              "All elements of the input 'repeat_times' for tile op must "
              "be positive integers, but the value received is %d.",
              repeat_times[i]));
    }
71
    auto vec_in_dims = phi::vectorize<int>(in_dims);
72 73 74 75 76 77 78 79
    if (repeat_times.size() < vec_in_dims.size()) {
      int diff = vec_in_dims.size() - repeat_times.size();
      repeat_times.insert(repeat_times.begin(), diff, 1);
    } else {
      int diff = repeat_times.size() - vec_in_dims.size();
      vec_in_dims.insert(vec_in_dims.begin(), diff, 1);
    }
    PADDLE_ENFORCE_EQ(
80 81
        repeat_times.size(),
        vec_in_dims.size(),
82 83 84
        platform::errors::InvalidArgument(
            "The rank (%d) of the input 'x' and the rank (%d) of the input "
            "'repeat_times' for tile op must match after promotion.",
85 86
            vec_in_dims.size(),
            repeat_times.size()));
87 88

    auto* out0 = context.Output<framework::Tensor>("Out");
89
    framework::DDim new_in_dims = phi::make_ddim(vec_in_dims);
90 91 92 93 94
    framework::DDim out_dims(new_in_dims);

    for (size_t i = 0; i < repeat_times.size(); ++i) {
      out_dims[i] *= repeat_times[i];
    }
95
    auto vec_out_dims = phi::vectorize<int>(out_dims);
96 97 98 99 100 101 102 103 104 105 106 107 108 109
    out0->Resize(out_dims);
    out0->mutable_data<T>(context.GetPlace());

    auto& dev_ctx =
        context.template device_context<paddle::platform::XPUDeviceContext>();
    std::vector<int> temp(repeat_times.size(), 1);
    if (repeat_times == temp) {
      framework::TensorCopy(*in0, context.GetPlace(), dev_ctx, out0);
      return;
    }

    int ret = XPU_SUCCESS;
    if (std::is_same<T, bool>::value) {
      ret = xpu::broadcast<int8_t>(
110 111 112 113
          dev_ctx.x_context(),
          reinterpret_cast<const int8_t*>(in0->data<T>()),
          reinterpret_cast<int8_t*>(out0->data<T>()),
          vec_in_dims,
114 115 116
          vec_out_dims);

    } else {
117 118 119 120 121
      ret = xpu::broadcast<T>(dev_ctx.x_context(),
                              in0->data<T>(),
                              out0->data<T>(),
                              vec_in_dims,
                              vec_out_dims);
122 123
    }
    PADDLE_ENFORCE_EQ(
124 125
        ret,
        XPU_SUCCESS,
126
        platform::errors::External("XPU tile kernel return wrong value[%d %s]",
127 128
                                   ret,
                                   XPUAPIErrorMsg[ret]));
129 130 131 132 133 134 135
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
136 137 138 139 140
REGISTER_OP_XPU_KERNEL(tile,
                       ops::TileXPUKernel<bool>,
                       ops::TileXPUKernel<int>,
                       ops::TileXPUKernel<int64_t>,
                       ops::TileXPUKernel<float>);
141 142

#endif