argsort_kernel.cc 8.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// 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/phi/kernels/argsort_kernel.h"

#include "paddle/phi/backends/xpu/xpu_context.h"
#include "paddle/phi/core/kernel_registry.h"

namespace phi {
T
TTerror 已提交
21 22 23 24

const int XPU_SORT_MAX_SIZE = 16384;

template <typename T, typename TID>
25 26 27 28 29 30
static inline void xpu_argsort(xpu::Context* ctx,
                               const T* input_data,
                               T* output_data,
                               TID* indices_data,
                               int m,
                               int n,
T
TTerror 已提交
31 32 33 34
                               bool descending) {
  int ret =
      xpu::sort(ctx, input_data, output_data, indices_data, m, n, descending);
  PADDLE_ENFORCE_EQ(
35 36
      ret,
      XPU_SUCCESS,
37 38 39
      errors::External("XPU sort kernel return wrong value[%d %s].",
                       ret,
                       XPUAPIErrorMsg[ret]));
T
TTerror 已提交
40 41 42
}

template <typename T>
43 44 45
static inline void xpu_transpose(xpu::Context* ctx,
                                 const T* x,
                                 T* y,
T
TTerror 已提交
46 47 48
                                 const std::vector<int>& xshape,
                                 const std::vector<int>& permute) {
  int ret = xpu::transpose(ctx, x, y, xshape, permute);
49 50 51 52 53 54
  PADDLE_ENFORCE_EQ(
      ret,
      XPU_SUCCESS,
      errors::External("XPU transpose kernel return wrong value[%d %s]",
                       ret,
                       XPUAPIErrorMsg[ret]));
T
TTerror 已提交
55 56 57 58 59 60
}

template <typename TX, typename TY>
static inline void xpu_cast(xpu::Context* ctx, const TX* x, TY* y, int len) {
  int ret = xpu::cast_v2(ctx, x, y, len);
  PADDLE_ENFORCE_EQ(
61 62
      ret,
      XPU_SUCCESS,
63 64 65
      errors::External("XPU cast kernel return wrong value[%d %s]",
                       ret,
                       XPUAPIErrorMsg[ret]));
T
TTerror 已提交
66 67
}

68 69
template <typename T,
          bool VALUE_NEED_CAST = false,
T
TTerror 已提交
70 71
          bool INDEX_NEED_CAST = false>
struct XPUArgsort {
72 73 74 75 76 77 78
  void operator()(xpu::Context* ctx,
                  const T* input_data,
                  T* output_data,
                  int64_t* indices_data,
                  const std::vector<int>& data_shape,
                  const std::vector<int>& permute,
                  bool descending) {
T
TTerror 已提交
79 80 81 82
    xpu::ctx_guard RAII_GUARD(ctx);
    int m = data_shape[0] * data_shape[2];
    int n = data_shape[1];
    int len = data_shape[0] * data_shape[1] * data_shape[2];
83 84
    std::vector<int> trans_data_shape{
        data_shape[0], data_shape[2], data_shape[1]};
T
TTerror 已提交
85 86 87 88 89 90

    T* input_data_trans = RAII_GUARD.alloc_l3_or_gm<T>(len);
    T* output_data_trans = RAII_GUARD.alloc_l3_or_gm<T>(len);
    int64_t* indices_data_trans = RAII_GUARD.alloc_l3_or_gm<int64_t>(len);

    xpu_transpose(ctx, input_data, input_data_trans, data_shape, permute);
91 92 93 94 95 96 97 98 99 100 101
    xpu_argsort(ctx,
                input_data_trans,
                output_data_trans,
                indices_data_trans,
                m,
                n,
                descending);
    xpu_transpose(
        ctx, output_data_trans, output_data, trans_data_shape, permute);
    xpu_transpose(
        ctx, indices_data_trans, indices_data, trans_data_shape, permute);
T
TTerror 已提交
102 103 104 105 106
  }
};

template <typename T>
struct XPUArgsort<T, false, true> {
107 108 109 110 111 112 113
  void operator()(xpu::Context* ctx,
                  const T* input_data,
                  T* output_data,
                  int64_t* indices_data,
                  const std::vector<int>& data_shape,
                  const std::vector<int>& permute,
                  bool descending) {
T
TTerror 已提交
114 115 116 117
    xpu::ctx_guard RAII_GUARD(ctx);
    int m = data_shape[0] * data_shape[2];
    int n = data_shape[1];
    int len = data_shape[0] * data_shape[1] * data_shape[2];
118 119
    std::vector<int> trans_data_shape{
        data_shape[0], data_shape[2], data_shape[1]};
T
TTerror 已提交
120 121 122 123 124 125 126

    T* input_data_trans = RAII_GUARD.alloc_l3_or_gm<T>(len);
    T* output_data_trans = RAII_GUARD.alloc_l3_or_gm<T>(len);
    int* indices_data_trans = RAII_GUARD.alloc_l3_or_gm<int>(len);
    int64_t* cast_data_int64 = RAII_GUARD.alloc_l3_or_gm<int64_t>(len);

    xpu_transpose(ctx, input_data, input_data_trans, data_shape, permute);
127 128 129 130 131 132 133 134 135
    xpu_argsort(ctx,
                input_data_trans,
                output_data_trans,
                indices_data_trans,
                m,
                n,
                descending);
    xpu_transpose(
        ctx, output_data_trans, output_data, trans_data_shape, permute);
T
TTerror 已提交
136
    xpu_cast(ctx, indices_data_trans, cast_data_int64, len);
137 138
    xpu_transpose(
        ctx, cast_data_int64, indices_data, trans_data_shape, permute);
T
TTerror 已提交
139 140 141 142 143
  }
};

template <>
struct XPUArgsort<int64_t, true, true> {
144 145 146 147
  void operator()(xpu::Context* ctx,
                  const int64_t* input_data,
                  int64_t* output_data,
                  int64_t* indices_data,
T
TTerror 已提交
148
                  const std::vector<int>& data_shape,
149 150
                  const std::vector<int>& permute,
                  bool descending) {
T
TTerror 已提交
151 152 153 154
    xpu::ctx_guard RAII_GUARD(ctx);
    int m = data_shape[0] * data_shape[2];
    int n = data_shape[1];
    int len = data_shape[0] * data_shape[1] * data_shape[2];
155 156
    std::vector<int> trans_data_shape{
        data_shape[0], data_shape[2], data_shape[1]};
T
TTerror 已提交
157 158 159 160 161 162 163 164 165

    int* input_data_trans = RAII_GUARD.alloc_l3_or_gm<int>(len);
    int* output_data_trans = RAII_GUARD.alloc_l3_or_gm<int>(len);
    int* indices_data_trans = RAII_GUARD.alloc_l3_or_gm<int>(len);
    int* cast_data_int = RAII_GUARD.alloc_l3_or_gm<int>(len);
    int64_t* cast_data_int64 = RAII_GUARD.alloc_l3_or_gm<int64_t>(len);

    xpu_cast(ctx, input_data, cast_data_int, len);
    xpu_transpose(ctx, cast_data_int, input_data_trans, data_shape, permute);
166 167 168 169 170 171 172
    xpu_argsort(ctx,
                input_data_trans,
                output_data_trans,
                indices_data_trans,
                m,
                n,
                descending);
T
TTerror 已提交
173 174 175 176

    xpu_cast(ctx, output_data_trans, cast_data_int64, len);
    xpu_transpose(ctx, cast_data_int64, output_data, trans_data_shape, permute);
    xpu_cast(ctx, indices_data_trans, cast_data_int64, len);
177 178
    xpu_transpose(
        ctx, cast_data_int64, indices_data, trans_data_shape, permute);
T
TTerror 已提交
179 180 181
  }
};

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
template <typename T, typename Context>
void ArgsortKernel(const Context& dev_ctx,
                   const DenseTensor& input,
                   int axis,
                   bool descending,
                   DenseTensor* output,
                   DenseTensor* indices) {
  auto in_dims = input.dims();
  axis = (axis < 0) ? (in_dims.size() + axis) : axis;
  int n = in_dims[axis];

  PADDLE_ENFORCE_LT(
      n,
      XPU_SORT_MAX_SIZE,
      errors::InvalidArgument(
          "The axis dimension of Input should less than %d, but got %d.",
          XPU_SORT_MAX_SIZE,
          in_dims[axis]));

  auto input_data = input.data<T>();
  auto output_data = dev_ctx.template Alloc<T>(output);
  auto indices_data = dev_ctx.template Alloc<int64_t>(indices);

  int len_before = phi::product(phi::slice_ddim(in_dims, 0, axis));
  int len_after =
      phi::product(phi::slice_ddim(in_dims, axis + 1, in_dims.size()));
  bool int64_need_cast =
      (std::is_same<T, int64_t>::value && n > (XPU_SORT_MAX_SIZE / 2)) ? true
                                                                       : false;
  bool index_need_cast = (n > (XPU_SORT_MAX_SIZE / 2)) ? true : false;
  std::vector<int> permute_vec{0, 2, 1};
  std::vector<int> data_shape{len_before, n, len_after};

  if (int64_need_cast) {
    XPUArgsort<T, true, true>()(dev_ctx.x_context(),
                                input_data,
                                output_data,
                                indices_data,
                                data_shape,
                                permute_vec,
                                descending);
  } else if (index_need_cast) {
    XPUArgsort<T, false, true>()(dev_ctx.x_context(),
                                 input_data,
                                 output_data,
                                 indices_data,
                                 data_shape,
                                 permute_vec,
                                 descending);
  } else {
    XPUArgsort<T, false, false>()(dev_ctx.x_context(),
233 234 235 236 237
                                  input_data,
                                  output_data,
                                  indices_data,
                                  data_shape,
                                  permute_vec,
T
TTerror 已提交
238 239
                                  descending);
  }
240
}
T
TTerror 已提交
241

242
}  // namespace phi
T
TTerror 已提交
243

244 245
PD_REGISTER_KERNEL(
    argsort, XPU, ALL_LAYOUT, phi::ArgsortKernel, float, int, int64_t) {}