mlu_baseop.h 81.2 KB
Newer Older
F
fwenguang 已提交
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
/* 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. */

#pragma once
#include <cn_api.h>
#include <cnnl.h>
#include <concurrentqueue.h>

#include <string>
#include <vector>

#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/framework/tensor_util.h"
#include "paddle/fluid/framework/type_defs.h"
#include "paddle/fluid/platform/device/mlu/enforce.h"

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;
using DataLayout = framework::DataLayout;
33
using ExecutionContext = framework::ExecutionContext;
F
fwenguang 已提交
34
using DeviceContextPool = platform::DeviceContextPool;
35 36
using MLUDeviceContext = platform::MLUDeviceContext;

37
const std::map<std::string, cnnlReduceOp_t> MLUReduceOpMap = {
38 39 40 41 42 43
    {"reduce_all", CNNL_REDUCE_AND},
    {"reduce_any", CNNL_REDUCE_OR},
    {"reduce_max", CNNL_REDUCE_MAX},
    {"reduce_mean", CNNL_REDUCE_AVG},
    {"reduce_min", CNNL_REDUCE_MIN},
    {"reduce_sum", CNNL_REDUCE_ADD},
44 45 46
    {"reduce_prod", CNNL_REDUCE_MUL},
};

47 48 49 50 51 52 53 54 55 56 57 58 59 60
const std::map<std::string, cnnlInterpMode_t> MLUInterpModeMap = {
    {"bilinear", CNNL_INTERP_BILINEAR},
    {"nearest", CNNL_INTERP_NEAREST},
    {"linear", CNNL_INTERP_LINEAR},
    {"trilinear", CNNL_INTERP_TRILINEAR},
    {"bicubic", CNNL_INTERP_BICUBIC}};

const std::map<std::string, cnnlInterpBackwardMode_t> MLUInterpBackwardModeMap =
    {{"bilinear", CNNL_INTERP_BACKWARD_BILINEAR},
     {"nearest", CNNL_INTERP_BACKWARD_NEAREST},
     {"linear", CNNL_INTERP_BACKWARD_LINEAR},
     {"trilinear", CNNL_INTERP_BACKWARD_TRILINEAR},
     {"bicubic", CNNL_INTERP_BACKWARD_BICUBIC}};

61 62 63 64 65 66 67 68 69
inline cnnlReduceOp_t GetMLUCnnlReduceOp(const std::string reduce_name) {
  auto iter = MLUReduceOpMap.find(reduce_name);
  if (iter != MLUReduceOpMap.end()) {
    return iter->second;
  }
  PADDLE_THROW(platform::errors::InvalidArgument(
      "Not support reduce op type of MLU Device: %s", reduce_name));
}

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
inline cnnlInterpMode_t GetMLUCnnlInterpMode(const std::string interp_mode) {
  auto iter = MLUInterpModeMap.find(interp_mode);
  if (iter != MLUInterpModeMap.end()) {
    return iter->second;
  }
  PADDLE_THROW(platform::errors::InvalidArgument(
      "Not support interp mode of MLU Device: %s", interp_mode));
}

inline cnnlInterpBackwardMode_t GetMLUCnnlInterpBackwardMode(
    const std::string interp_mode) {
  auto iter = MLUInterpBackwardModeMap.find(interp_mode);
  if (iter != MLUInterpBackwardModeMap.end()) {
    return iter->second;
  }
  PADDLE_THROW(platform::errors::InvalidArgument(
      "Not support interp mode of MLU Device: %s", interp_mode));
}

89 90 91 92
inline const void* GetBasePtr(const Tensor* t) { return t->data(); }

inline void* GetBasePtr(Tensor* t) { return t->data(); }

93 94
inline cnnlDataType_t ToCnnlDataType(
    const paddle::experimental::DataType& dtype) {
F
fwenguang 已提交
95
  cnnlDataType_t type = CNNL_DTYPE_FLOAT;
96 97
  switch (dtype) {
    case DataType::FLOAT16:
F
fwenguang 已提交
98 99
      type = CNNL_DTYPE_HALF;
      break;
100
    case DataType::FLOAT32:
F
fwenguang 已提交
101 102
      type = CNNL_DTYPE_FLOAT;
      break;
Q
qipengh 已提交
103 104 105
    case DataType::FLOAT64:
      type = CNNL_DTYPE_DOUBLE;
      break;
106
    case DataType::INT8:
F
fwenguang 已提交
107 108
      type = CNNL_DTYPE_INT8;
      break;
109
    case DataType::INT16:
110 111
      type = CNNL_DTYPE_INT16;
      break;
112
    case DataType::INT32:
F
fwenguang 已提交
113 114
      type = CNNL_DTYPE_INT32;
      break;
115
    case DataType::INT64:
F
fwenguang 已提交
116 117
      type = CNNL_DTYPE_INT64;
      break;
118
    case DataType::BOOL:
F
fwenguang 已提交
119 120
      type = CNNL_DTYPE_BOOL;
      break;
121
    case DataType::UINT8:
122 123
      type = CNNL_DTYPE_UINT8;
      break;
F
fwenguang 已提交
124 125 126 127 128 129
    default:
      break;
  }
  return type;
}

130 131
inline cnnlDataType_t ToCnnlDataType(
    const paddle::framework::proto::VarType::Type& type) {
132
  return ToCnnlDataType(framework::TransToPhiDataType(type));
133 134 135 136 137 138 139 140
}

template <typename T>
inline cnnlDataType_t ToCnnlDataType() {
  auto type = framework::ToDataType(std::type_index(typeid(T)));
  return ToCnnlDataType(type);
}

F
fwenguang 已提交
141 142 143 144 145 146 147 148 149 150
// Converts (via narrowing) a type T value to a type U, and checks that the
// value has no value change due to the conversion.
template <typename WideT, typename NarrowT>
NarrowT CheckedNarrowing(const WideT& wide) {
  NarrowT narrow = wide;
  CHECK_EQ(narrow, wide)
      << "checked narrowing failed; values not equal post-conversion";
  return narrow;
}

151
inline static cnnlHandle_t GetHandleFromCTX(const ExecutionContext& ctx) {
152 153 154
  return ctx.template device_context<MLUDeviceContext>().cnnl_handle();
}

155 156
inline static const MLUDeviceContext& GetDevCtxFromCTX(
    const ExecutionContext& ctx) {
157 158 159
  return ctx.template device_context<MLUDeviceContext>();
}

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
using VT = framework::proto::VarType;
const std::map<std::pair<VT::Type, VT::Type>, cnnlCastDataType_t>
    MLU_SUPPORTED_CAST_TYPE = {
        {{VT::FP32, /*cast to*/ VT::FP16}, CNNL_CAST_FLOAT_TO_HALF},
        {{VT::FP32, /*cast to*/ VT::INT32}, CNNL_CAST_FLOAT_TO_INT32},
        {{VT::FP32, /*cast to*/ VT::INT16}, CNNL_CAST_FLOAT_TO_INT16},
        {{VT::FP32, /*cast to*/ VT::INT8}, CNNL_CAST_FLOAT_TO_INT8},
        {{VT::FP32, /*cast to*/ VT::UINT8}, CNNL_CAST_FLOAT_TO_UINT8},
        {{VT::FP32, /*cast to*/ VT::BOOL}, CNNL_CAST_FLOAT_TO_BOOL},
        {{VT::FP16, /*cast to*/ VT::FP32}, CNNL_CAST_HALF_TO_FLOAT},
        {{VT::FP16, /*cast to*/ VT::INT32}, CNNL_CAST_HALF_TO_INT32},
        {{VT::FP16, /*cast to*/ VT::INT16}, CNNL_CAST_HALF_TO_INT16},
        {{VT::FP16, /*cast to*/ VT::INT8}, CNNL_CAST_HALF_TO_INT8},
        {{VT::FP16, /*cast to*/ VT::UINT8}, CNNL_CAST_HALF_TO_UINT8},
        {{VT::FP16, /*cast to*/ VT::BOOL}, CNNL_CAST_HALF_TO_BOOL},
        {{VT::INT32, /*cast to*/ VT::FP32}, CNNL_CAST_INT32_TO_FLOAT},
        {{VT::INT32, /*cast to*/ VT::FP16}, CNNL_CAST_INT32_TO_HALF},
        {{VT::INT32, /*cast to*/ VT::INT8}, CNNL_CAST_INT32_TO_INT8},
178
        {{VT::INT32, /*cast to*/ VT::INT16}, CNNL_CAST_INT32_TO_INT16},
179 180 181 182 183 184 185 186 187 188 189
        {{VT::INT16, /*cast to*/ VT::FP32}, CNNL_CAST_INT16_TO_FLOAT},
        {{VT::INT16, /*cast to*/ VT::FP16}, CNNL_CAST_INT16_TO_HALF},
        {{VT::INT16, /*cast to*/ VT::INT32}, CNNL_CAST_INT16_TO_INT32},
        {{VT::INT8, /*cast to*/ VT::FP32}, CNNL_CAST_INT8_TO_FLOAT},
        {{VT::INT8, /*cast to*/ VT::FP16}, CNNL_CAST_INT8_TO_HALF},
        {{VT::INT8, /*cast to*/ VT::INT32}, CNNL_CAST_INT8_TO_INT32},
        {{VT::UINT8, /*cast to*/ VT::FP32}, CNNL_CAST_UINT8_TO_FLOAT},
        {{VT::UINT8, /*cast to*/ VT::FP16}, CNNL_CAST_UINT8_TO_HALF},
        {{VT::BOOL, /*cast to*/ VT::FP32}, CNNL_CAST_BOOL_TO_FLOAT},
        {{VT::BOOL, /*cast to*/ VT::FP16}, CNNL_CAST_BOOL_TO_HALF},
        {{VT::BOOL, /*cast to*/ VT::INT32}, CNNL_CAST_BOOL_TO_INT32},
190 191
        {{VT::UINT8, /*cast to*/ VT::INT32}, CNNL_CAST_UINT8_TO_INT32},
        {{VT::INT32, /*cast to*/ VT::INT64}, CNNL_CAST_INT32_TO_INT64},
192
        {{VT::INT64, /*cast to*/ VT::INT32}, CNNL_CAST_INT64_TO_INT32},
193 194 195 196 197 198 199 200 201
        {{VT::INT32, /*cast to*/ VT::BOOL}, CNNL_CAST_INT32_TO_BOOL},
        {{VT::UINT8, /*cast to*/ VT::INT64}, CNNL_CAST_UINT8_TO_INT64},
        {{VT::INT8, /*cast to*/ VT::INT16}, CNNL_CAST_INT8_TO_INT16},
        {{VT::FP32, /*cast to*/ VT::FP64}, CNNL_CAST_FLOAT_TO_DOUBLE},
        {{VT::FP64, /*cast to*/ VT::FP32}, CNNL_CAST_DOUBLE_TO_FLOAT},
        {{VT::INT64, /*cast to*/ VT::FP32}, CNNL_CAST_INT64_TO_FLOAT},
        {{VT::INT64, /*cast to*/ VT::FP16}, CNNL_CAST_INT64_TO_HALF},
        {{VT::FP32, /*cast to*/ VT::INT64}, CNNL_CAST_FLOAT_TO_INT64},
        {{VT::FP16, /*cast to*/ VT::INT64}, CNNL_CAST_HALF_TO_INT64},
202 203 204 205
};

cnnlCastDataType_t GetCastDataType(const VT::Type& src_type,
                                   const VT::Type& dst_type);
206 207 208 209

cnnlCastDataType_t GetCastDataType(const DataType& src_type,
                                   const DataType& dst_type);

210 211
bool MLUSupportsCast(const VT::Type& src_type, const VT::Type& dst_type);

F
fwenguang 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
cnnlDeviceType_t GetCnnlDev(int dev_ordinal);

using CnnlTensorDesc = cnnlTensorDescriptor_t;

class MLUCnnlTensorDesc {
 public:
  MLUCnnlTensorDesc() {}

  // SE_DISALLOW_COPY_AND_ASSIGN
  MLUCnnlTensorDesc(const MLUCnnlTensorDesc& desc) = delete;
  MLUCnnlTensorDesc& operator=(const MLUCnnlTensorDesc&) = delete;

  MLUCnnlTensorDesc(MLUCnnlTensorDesc&& rhs)
      : raw_tensor_desc(rhs.raw_tensor_desc) {
    rhs.raw_tensor_desc = nullptr;
  }

  MLUCnnlTensorDesc& operator=(MLUCnnlTensorDesc&& rhs);

231 232
  MLUCnnlTensorDesc(const int tensor_dim,
                    const int dim_sizes[],
F
fwenguang 已提交
233 234
                    const cnnlDataType_t tensor_dtype);

235 236
  MLUCnnlTensorDesc(const int tensor_dim,
                    const int dim_sizes[],
F
fwenguang 已提交
237 238 239
                    const cnnlDataType_t tensor_dtype,
                    const cnnlTensorLayout_t layout);

240 241 242 243
  MLUCnnlTensorDesc(const int tensor_dim,
                    const int dim_sizes[],
                    const cnnlDataType_t tensor_dtype,
                    int position);
F
fwenguang 已提交
244

245 246
  MLUCnnlTensorDesc(const int tensor_dim,
                    const int64_t dim_sizes[],
F
fwenguang 已提交
247 248
                    const cnnlDataType_t tensor_dtype);

249 250
  MLUCnnlTensorDesc(const int tensor_dim,
                    const int64_t dim_sizes[],
F
fwenguang 已提交
251 252 253
                    const cnnlDataType_t tensor_dtype,
                    const cnnlTensorLayout_t layout);

254 255 256 257
  MLUCnnlTensorDesc(const int tensor_dim,
                    const int64_t dim_sizes[],
                    const cnnlDataType_t tensor_dtype,
                    int position);
F
fwenguang 已提交
258

259 260
  MLUCnnlTensorDesc(const Tensor& tensor,
                    const cnnlTensorLayout_t layout,
F
fwenguang 已提交
261 262
                    const cnnlDataType_t tensor_dtype);

263 264
  explicit MLUCnnlTensorDesc(const Tensor& tensor);

265 266 267 268
  MLUCnnlTensorDesc(const Tensor& tensor,
                    cnnlTensorLayout_t layout,
                    const cnnlDataType_t tensor_dtype,
                    int position);
F
fwenguang 已提交
269

270 271 272 273
  MLUCnnlTensorDesc(const Tensor& tensor,
                    cnnlTensorLayout_t layout,
                    const cnnlDataType_t tensor_dtype,
                    int position,
F
fwenguang 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
                    float scale);

  ~MLUCnnlTensorDesc();

  const cnnlTensorDescriptor_t get() const { return raw_tensor_desc; }

 private:
  cnnlTensorDescriptor_t raw_tensor_desc = nullptr;
};

class MLUCnnlActivationDesc {
 public:
  MLUCnnlActivationDesc(const MLUCnnlActivationDesc& desc) = delete;
  MLUCnnlActivationDesc& operator=(const MLUCnnlActivationDesc& desc) = delete;
  MLUCnnlActivationDesc(const cnnlActivationMode_t act_mode, const float ceof);
289 290 291 292
  MLUCnnlActivationDesc(const cnnlActivationMode_t act_mode,
                        const float ceof,
                        const float sliced_dim,
                        const float selu_alpha,
293
                        const float selu_lambda);
F
fwenguang 已提交
294 295 296 297 298 299 300 301

  const cnnlActivationDescriptor_t get() const;
  ~MLUCnnlActivationDesc();

 private:
  cnnlActivationDescriptor_t active_desc_ = nullptr;
};

302 303 304 305 306 307 308
class MLUCnnlPoolingDesc {
 public:
  MLUCnnlPoolingDesc(const MLUCnnlPoolingDesc& desc) = delete;
  MLUCnnlPoolingDesc& operator=(const MLUCnnlPoolingDesc& desc) = delete;

  MLUCnnlPoolingDesc(const cnnlPoolingMode_t mode,
                     const cnnlNanPropagation_t maxpooling_nan_opt,
309 310 311 312 313 314 315 316 317 318 319
                     int window_rows,
                     int window_cols,
                     int64_t pad_up,
                     int64_t pad_down,
                     int64_t pad_left,
                     int64_t pad_right,
                     int row_stride,
                     int col_stride,
                     int row_dilation,
                     int col_dilation,
                     bool ceil_mode);
320 321 322

  MLUCnnlPoolingDesc(const cnnlPoolingMode_t mode,
                     const cnnlNanPropagation_t maxpooling_nan_opt,
323 324
                     const int tensor_rank,
                     const std::vector<int>& window,
325 326 327 328 329 330 331 332 333 334 335 336 337
                     const std::vector<int>& padding,
                     const std::vector<int>& stride);

  const cnnlPoolingDescriptor_t get() const;

  ~MLUCnnlPoolingDesc();

 private:
  cnnlPoolingDescriptor_t pooling_desc_ = nullptr;
};

class MLUCnnlRandomGeneratorDesc {
 public:
Q
qipengh 已提交
338
  MLUCnnlRandomGeneratorDesc(const ExecutionContext& ctx, const int seed);
339
  const cnnlRandGenerator_t get() const;
Q
qipengh 已提交
340
  Tensor& get_state();
341 342 343
  ~MLUCnnlRandomGeneratorDesc();

 private:
Q
qipengh 已提交
344
  Tensor mlu_state;
345 346 347
  cnnlRandGenerator_t mlu_generator = nullptr;
};

Q
qipengh 已提交
348 349 350
const std::shared_ptr<MLUCnnlRandomGeneratorDesc>& GetMLURandomGenerator(
    const ExecutionContext& ctx, const int64_t device_id, const int seed);

351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
class MLUCnnlReduceDesc {
 public:
  MLUCnnlReduceDesc(const MLUCnnlReduceDesc& desc) = delete;
  MLUCnnlReduceDesc& operator=(const MLUCnnlReduceDesc& desc) = delete;

  MLUCnnlReduceDesc(const std::vector<int>& axis_vec,
                    const cnnlReduceOp_t reduce_op,
                    const cnnlDataType_t data_type,
                    const cnnlNanPropagation_t nan_propagation,
                    const cnnlReduceIndices_t reduce_indices,
                    const cnnlIndicesType_t indices_type);

  const cnnlReduceDescriptor_t get() const;

  ~MLUCnnlReduceDesc();

 private:
  cnnlReduceDescriptor_t reduction_desc_ = nullptr;
};

class MLUCnnlOpTensorDesc {
 public:
  MLUCnnlOpTensorDesc(const MLUCnnlOpTensorDesc& desc) = delete;
  void operator=(const MLUCnnlOpTensorDesc&) = delete;

  MLUCnnlOpTensorDesc(cnnlOpTensorDesc_t op_tensor_op,
                      cnnlDataType_t op_tensor_comp_type,
                      cnnlNanPropagation_t op_tensor_nan_opt);

  const cnnlOpTensorDescriptor_t get() const;

  ~MLUCnnlOpTensorDesc();

 private:
  cnnlOpTensorDescriptor_t op_tensor_desc_ = nullptr;
};

class MLUCnnlNMSDesc {
 public:
  MLUCnnlNMSDesc(const MLUCnnlNMSDesc& desc) = delete;
  MLUCnnlNMSDesc& operator=(const MLUCnnlNMSDesc& desc) = delete;

393 394 395 396
  MLUCnnlNMSDesc(const cnnlNmsOutputMode_t mode,
                 const float iou_threshold,
                 const int max_output_size,
                 const float confidence_threshold,
397 398 399 400 401 402 403 404 405 406 407 408
                 const int input_layout);

  const cnnlNmsDescriptor_t get() const;

  ~MLUCnnlNMSDesc();

 private:
  cnnlNmsDescriptor_t nms_desc_ = nullptr;
};

class MLUCnnlConvolutionDesc {
 public:
409 410 411 412 413
  MLUCnnlConvolutionDesc(const int dims,
                         const int pad[],
                         const int stride[],
                         const int dilation[],
                         const int group_count,
414 415
                         const cnnlDataType_t tensor_dtype);

416 417 418 419
  MLUCnnlConvolutionDesc(const int dims,
                         const int64_t pad[],
                         const int64_t stride[],
                         const int64_t dilation[],
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
                         const int group_count,
                         const cnnlDataType_t tensor_dtype);

  MLUCnnlConvolutionDesc(const MLUCnnlConvolutionDesc& desc) = delete;

  MLUCnnlConvolutionDesc& operator=(const MLUCnnlConvolutionDesc& desc) =
      delete;

  const cnnlConvolutionDescriptor_t get() const;

  ~MLUCnnlConvolutionDesc();

 private:
  cnnlConvolutionDescriptor_t conv_desc_ = nullptr;
};

class MLUCnnlBatchSpaceDesc {
 public:
438 439
  MLUCnnlBatchSpaceDesc(uint32_t block_shape[],
                        uint32_t paddings[],
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
                        const uint32_t block_shape_size,
                        const uint32_t paddings_size);

  void getBatch2spaceNdextraInputSize(const ExecutionContext& ctx,
                                      const cnnlTensorDescriptor_t input_desc);

  void getSpace2batchNdextraInputSize(const ExecutionContext& ctx,
                                      const cnnlTensorDescriptor_t input_desc);

  void initSpace2batchNdExtraInput(const ExecutionContext& ctx,
                                   const cnnlTensorDescriptor_t input_desc,
                                   void* extra_host_input);

  void initBatch2spaceNdExtraInput(const ExecutionContext& ctx,
                                   const cnnlTensorDescriptor_t input_desc,
                                   void* extra_host_input);

  const cnnlSpaceBatchNdDescriptor_t get() const;

  size_t getExtraInputSize() const;

  ~MLUCnnlBatchSpaceDesc();

 private:
  cnnlSpaceBatchNdDescriptor_t op_desc_ = nullptr;
  size_t extra_input_size_;
};

class MLUCnnlTrigonDesc {
 public:
  explicit MLUCnnlTrigonDesc(
      const cnnlTrigonFunctionMode_t trigon_function_mode);

  const cnnlTrigonDescriptor_t get() const;

  ~MLUCnnlTrigonDesc();

 private:
  cnnlTrigonDescriptor_t trigon_desc_ = nullptr;
};

481 482
class MLUCnnlDCNDesc {
 public:
483 484 485 486 487 488
  MLUCnnlDCNDesc(int dimNb,
                 const int* pad,
                 const int* stride,
                 const int* dilation,
                 int deformable_group,
                 int conv_group,
489 490 491 492 493 494 495 496 497
                 int im2col_step);
  const cnnlDCNDescriptor_t get() const;

  ~MLUCnnlDCNDesc();

 private:
  cnnlDCNDescriptor_t dcn_desc_ = nullptr;
};

F
fwenguang 已提交
498 499
class MLUCnnl {
 public:
500
  static void Active(const ExecutionContext& ctx,
F
fwenguang 已提交
501
                     cnnlActivationDescriptor_t active_desc,
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
                     const cnnlTensorDescriptor_t input_desc,
                     const void* input,
                     const cnnlTensorDescriptor_t output_desc,
                     void* output);

  static void ActiveGrad(const ExecutionContext& ctx,
                         cnnlActivationDescriptor_t active_desc,
                         const void* alpha,
                         const void* beta,
                         const cnnlTensorDescriptor_t y_desc,
                         const void* y,
                         const cnnlTensorDescriptor_t diff_y_desc,
                         const void* diff_y,
                         const cnnlTensorDescriptor_t x_desc,
                         const void* x,
                         const cnnlTensorDescriptor_t diff_x_desc,
                         void* diff_x);

  static void Concat(const ExecutionContext& ctx,
                     const int pack_num,
                     const int axis,
                     const cnnlTensorDescriptor_t inputs_desc[],
524
                     const void* const inputs[],
525 526
                     const cnnlTensorDescriptor_t output_desc,
                     void* output);
527

528 529 530 531
  static void Concat(const MLUDeviceContext& dev_ctx,
                     const int pack_num,
                     const int axis,
                     const cnnlTensorDescriptor_t inputs_desc[],
Z
zn 已提交
532
                     const void* const inputs[],
533 534
                     const cnnlTensorDescriptor_t output_desc,
                     void* output);
Z
zn 已提交
535

536 537 538 539 540 541
  static void Cast(const ExecutionContext& ctx,
                   cnnlCastDataType_t cast_type,
                   const cnnlTensorDescriptor_t input_desc,
                   const void* input,
                   const cnnlTensorDescriptor_t output_desc,
                   void* output);
542

543
  static void Clip(const ExecutionContext& ctx,
544 545 546 547 548
                   const cnnlTensorDescriptor_t input_desc,
                   const void* input,
                   const void* min,
                   const void* max,
                   void* y);
549

550 551 552 553 554 555 556 557 558
  static void HardtanhBackward(const ExecutionContext& ctx,
                               const cnnlTensorDescriptor_t x_desc,
                               const void* x,
                               const cnnlTensorDescriptor_t diff_y_desc,
                               const void* diff_y,
                               const float max_val,
                               const float min_val,
                               const cnnlTensorDescriptor_t diff_x_desc,
                               void* diff_x);
559

560 561
  static void Div(const ExecutionContext& ctx,
                  cnnlComputationPreference_t prefer,
562 563 564 565 566 567
                  const cnnlTensorDescriptor_t in0_desc,
                  const void* in0,
                  const cnnlTensorDescriptor_t in1_desc,
                  const void* in1,
                  const cnnlTensorDescriptor_t output_desc,
                  void* output);
568

569
  static void Fill(const ExecutionContext& ctx,
570 571 572 573 574 575 576 577 578 579
                   const cnnlPointerMode_t pointer_mode,
                   const void* value_ptr,
                   const cnnlTensorDescriptor_t output_desc,
                   void* output);

  static void LRN(const ExecutionContext& ctx,
                  const int local_size,
                  const double alpha,
                  const double beta,
                  const double k,
580 581
                  const cnnlTensorDescriptor_t input_quant_desc,
                  const void* input_quant,
582 583
                  const cnnlTensorDescriptor_t output_desc,
                  void* output);
584 585 586 587 588 589 590 591 592 593 594

  static void QuantifyOffline(const ExecutionContext& context,
                              cnnlQuantizeMode_t mode,
                              const cnnlTensorDescriptor_t input_desc,
                              const void* input,
                              const cnnlTensorDescriptor_t ouput_desc,
                              void* output);

  static void QuantifyOnline(const ExecutionContext& context,
                             const int bitwidth,
                             const cnnlTensorDescriptor_t input_desc,
595 596 597 598
                             const void* input,
                             const bool compute_scale,
                             void* position,
                             void* scale,
599 600 601 602
                             const cnnlTensorDescriptor_t ouput_desc,
                             void* output);

  static void SGD(const ExecutionContext& context,
603 604 605 606
                  const cnnlTensorDescriptor_t grad_desc,
                  const void* grad,
                  const void* lr,
                  const cnnlTensorDescriptor_t var_desc,
607 608 609 610 611
                  void* var);

  static void ApplyAdaGrad(const ExecutionContext& ctx,
                           const cnnlTensorDescriptor_t grad_desc,
                           const void* grad,
612 613 614 615 616 617
                           const cnnlTensorDescriptor_t accum_desc,
                           void* accum,
                           const cnnlTensorDescriptor_t var_desc,
                           void* var,
                           const void* lr,
                           const bool update_slots);
618 619 620

  static void ApplyRMSProp(const ExecutionContext& context,
                           const cnnlTensorDescriptor_t grad_desc,
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
                           const void* grad,
                           const void* lr,
                           const void* rho,
                           const void* momentum,
                           const void* epsilon,
                           const cnnlTensorDescriptor_t var_desc,
                           void* var,
                           const cnnlTensorDescriptor_t ms_desc,
                           void* ms,
                           const cnnlTensorDescriptor_t mom_desc,
                           void* mom);

  static void ApplyCenterRMSProp(const ExecutionContext& ctx,
                                 const cnnlTensorDescriptor_t grad_desc,
                                 const void* grad,
                                 const void* lr,
                                 const void* rho,
                                 const void* momentum,
                                 const void* epsilon,
                                 const cnnlTensorDescriptor_t var_desc,
                                 void* var,
                                 const cnnlTensorDescriptor_t mg_desc,
                                 void* mg,
                                 const cnnlTensorDescriptor_t ms_desc,
                                 void* ms,
                                 const cnnlTensorDescriptor_t mom_desc,
                                 void* mom);
648 649

  static void ApplyAdam(const ExecutionContext& ctx,
650 651 652 653 654 655
                        const cnnlTensorDescriptor_t var_desc,
                        void* var,
                        const cnnlTensorDescriptor_t m_desc,
                        void* m,
                        const cnnlTensorDescriptor_t v_desc,
                        void* v,
656
                        const cnnlTensorDescriptor_t grad_desc,
657 658 659 660 661 662 663
                        const void* grad,
                        const void* lr,
                        const void* beta1,
                        const void* beta2,
                        const void* beta1_power,
                        const void* beta2_power,
                        const void* epsilon,
664
                        const bool use_nesterov);
665 666 667

  static void ApplyAdaMax(const ExecutionContext& ctx,
                          const cnnlTensorDescriptor_t grad_desc,
668 669 670 671 672 673 674 675 676 677 678
                          const cnnlTensorDescriptor_t var_desc,
                          void* var,
                          const cnnlTensorDescriptor_t m_desc,
                          void* m,
                          const cnnlTensorDescriptor_t v_desc,
                          void* v,
                          const void* diff,
                          const void* lr,
                          const void* beta1,
                          const void* beta2,
                          const void* beta1_power,
679 680 681 682
                          const void* epsilon);

  static void ApplyMomentum(const ExecutionContext& ctx,
                            const cnnlTensorDescriptor_t grad_desc,
683 684 685 686 687
                            const void* grad,
                            const bool use_nesterov,
                            const void* lr,
                            const void* momentum,
                            void* var,
688 689 690 691
                            void* accum);

  static void ApplyKerasMomentum(const ExecutionContext& ctx,
                                 const cnnlTensorDescriptor_t grad_desc,
692 693 694 695 696 697
                                 const void* grad,
                                 const bool use_nesterov,
                                 const void* lr,
                                 const void* momentum,
                                 void* var,
                                 void* accum);
698 699 700

  static void ApplyAdadelta(const ExecutionContext& ctx,
                            const cnnlTensorDescriptor_t grad_desc,
701 702 703 704 705 706
                            const void* diff,
                            const void* lr,
                            const void* rho,
                            const void* epsilon,
                            void* var,
                            void* accum,
707 708 709
                            void* accum_update);

  static void SparseSoftmaxXentWithLogits(
710 711 712 713 714 715 716 717 718 719 720 721 722
      const ExecutionContext& ctx,
      cnnlSoftmaxMode_t mode,
      const cnnlTensorDescriptor_t x_desc,
      const void* input,
      const cnnlTensorDescriptor_t label_desc,
      const void* label,
      const cnnlTensorDescriptor_t y_desc,
      void* output,
      const cnnlTensorDescriptor_t diff_y_desc,
      void* back_out);

  static void RandomUniform(const ExecutionContext& ctx,
                            const int num,
723 724
                            const cnnlDataType_t data_type,
                            const cnnlRandGenerator_t mlu_generator,
725 726
                            void* mlu_state,
                            void* output);
Q
qipengh 已提交
727

728 729 730 731 732 733 734 735 736 737
  static void FusedDropout(const ExecutionContext& ctx,
                           const cnnlRandGenerator_t generator,
                           const cnnlTensorDescriptor_t input_desc,
                           const void* input,
                           const float p,
                           void* state,
                           const cnnlTensorDescriptor_t mask_desc,
                           const void* mask,
                           const cnnlTensorDescriptor_t output_desc,
                           void* output);
738

739 740 741 742 743 744 745 746
  static void Cumsum(const ExecutionContext& ctx,
                     const int axis,
                     const bool exclusive,
                     const bool reverse,
                     const cnnlTensorDescriptor_t input_desc,
                     const void* input,
                     const cnnlTensorDescriptor_t ouput_desc,
                     void* output);
747 748 749 750 751 752 753

  static void BroadcastTo(const ExecutionContext& ctx,
                          const cnnlTensorDescriptor_t input_desc,
                          const void* input,
                          const cnnlTensorDescriptor_t output_desc,
                          void* output);

754 755 756 757 758 759 760 761 762
  static void GatherFunctor(const ExecutionContext& ctx,
                            const int axis,
                            const int batch_dims,
                            const cnnlTensorDescriptor_t params_desc,
                            const void* params,
                            const cnnlTensorDescriptor_t indices_desc,
                            const void* indices,
                            const cnnlTensorDescriptor_t output_desc,
                            void* output);
763

764 765 766 767 768 769 770 771
  static void ScatterRefFunctor(const ExecutionContext& ctx,
                                const cnnlTensorDescriptor_t params_desc,
                                const void* params,
                                const cnnlTensorDescriptor_t updates_desc,
                                const void* updates,
                                const cnnlTensorDescriptor_t indices_desc,
                                const void* indices,
                                const cnnlScatterRefMode_t mode);
772

773 774
  static void ScatterFunctor(const ExecutionContext& ctx,
                             const cnnlTensorDescriptor_t params_desc,
775
                             void* params,
776 777 778
                             const cnnlTensorDescriptor_t updates_desc,
                             const void* updates,
                             const cnnlTensorDescriptor_t indices_desc,
779 780
                             const void* indices,
                             const int dim,
781 782
                             const cnnlScatterMode_t mode = CNNL_SCATTER);

783 784 785 786 787 788
  static void Range(const ExecutionContext& ctx,
                    const void* start,
                    const void* end,
                    const void* step,
                    const cnnlDataType_t output_dtype,
                    void* output);
789 790

  static void Round(const ExecutionContext& ctx,
791 792 793 794
                    const cnnlTensorDescriptor_t input_desc,
                    const void* input,
                    const cnnlTensorDescriptor_t output_desc,
                    void* output);
795

796 797 798 799 800 801 802
  static void TopK(const ExecutionContext& ctx,
                   const int k,
                   const int dim,
                   const bool largest,
                   const bool sorted,
                   const cnnlTensorDescriptor_t input_desc,
                   const void* input,
803 804 805 806 807
                   const cnnlTensorDescriptor_t values_output_desc,
                   void* values_out,
                   const cnnlTensorDescriptor_t indices_output_desc,
                   void* indices_out);

808 809 810 811
  static void StridedSlice(const ExecutionContext& ctx,
                           const int begin[],
                           const int end[],
                           const int strides[],
812 813 814 815 816
                           const cnnlTensorDescriptor_t input_desc,
                           const void* input,
                           const cnnlTensorDescriptor_t output_desc,
                           void* output);

817 818 819
  static void Split(const ExecutionContext& ctx,
                    int split_num,
                    int axis,
820 821 822 823 824
                    const cnnlTensorDescriptor_t input_desc,
                    const void* input_ptr,
                    const cnnlTensorDescriptor_t output_descs[],
                    void* output_ptrs[]);

825 826 827
  static void Split(const MLUDeviceContext& dev_ctx,
                    int split_num,
                    int axis,
Z
zn 已提交
828 829 830 831 832
                    const cnnlTensorDescriptor_t input_desc,
                    const void* input_ptr,
                    const cnnlTensorDescriptor_t output_descs[],
                    void* output_ptrs[]);

833 834 835 836 837 838 839 840 841 842
  static void Scale(const ExecutionContext& ctx,
                    const int axis,
                    const cnnlTensorDescriptor_t input_desc,
                    const void* input,
                    const cnnlTensorDescriptor_t alpha_desc,
                    const void* alpha,
                    const cnnlTensorDescriptor_t beta_desc,
                    const void* beta,
                    const cnnlTensorDescriptor_t output_desc,
                    void* output);
843

844 845
  static void AddN(const ExecutionContext& ctx,
                   uint32_t input_num,
846 847
                   const cnnlTensorDescriptor_t inputs_desc[],
                   const void* inputs[],
848 849
                   const cnnlTensorDescriptor_t output_desc,
                   void* output);
850 851

  static void Log(const ExecutionContext& ctx,
852 853 854 855 856 857 858 859 860 861 862
                  cnnlComputationPreference_t prefer,
                  cnnlLogBase_t log_base,
                  const cnnlTensorDescriptor_t input_desc,
                  const void* input,
                  const cnnlTensorDescriptor_t output_desc,
                  void* output);

  static void StridedSliceGrad(const ExecutionContext& ctx,
                               const int begin[],
                               const int end[],
                               const int strides[],
863 864 865 866 867
                               const cnnlTensorDescriptor_t input_desc,
                               const void* input,
                               const cnnlTensorDescriptor_t output_desc,
                               void* output);

868 869
  static void Logic(const ExecutionContext& ctx,
                    const cnnlLogicOp_t log_method,
870 871 872
                    const cnnlTensorDescriptor_t input1_desc,
                    const void* input1,
                    const cnnlTensorDescriptor_t input2_desc,
873 874
                    const void* input2,
                    const cnnlTensorDescriptor_t ouput_desc,
875 876
                    void* output);

877 878 879 880 881 882 883 884 885 886 887 888
  static void Select(const ExecutionContext& ctx,
                     const cnnlTensorDescriptor_t condition_desc,
                     const void* condition_ptr,
                     const cnnlTensorDescriptor_t then_desc,
                     const void* then_ptr,
                     const cnnlTensorDescriptor_t else_desc,
                     const void* else_ptr,
                     const cnnlTensorDescriptor_t output_desc,
                     void* output_ptr);

  static void AssignAdd(const ExecutionContext& ctx,
                        const void* alpha,
889 890 891
                        const void* beta,
                        const cnnlTensorDescriptor_t update_desc,
                        const void* update,
892 893
                        const cnnlTensorDescriptor_t param_desc,
                        void* param);
894

895 896
  static void AssignSub(const ExecutionContext& ctx,
                        const void* alpha,
897 898 899
                        const void* beta,
                        const cnnlTensorDescriptor_t update_desc,
                        const void* update,
900 901
                        const cnnlTensorDescriptor_t param_desc,
                        void* param);
902 903 904 905

  static void Assign(const ExecutionContext& ctx,
                     const cnnlTensorDescriptor_t update_desc,
                     const void* update,
906 907
                     const cnnlTensorDescriptor_t param_desc,
                     void* param);
908 909 910 911 912 913

  static void GatherNd(const ExecutionContext& ctx,
                       const cnnlTensorDescriptor_t params_desc,
                       const void* params,
                       const cnnlTensorDescriptor_t indices_desc,
                       const void* indices,
914 915
                       const cnnlTensorDescriptor_t output_desc,
                       void* output);
916 917 918 919 920

  static void BatchToSpace(const ExecutionContext& ctx,
                           const cnnlTensorDescriptor_t input_desc,
                           const void* input,
                           const cnnlTensorDescriptor_t output_desc,
921 922
                           void* output,
                           const cnnlSpaceBatchParam_t param);
923 924 925 926 927

  static void BatchToSpaceNd(const ExecutionContext& ctx,
                             const cnnlTensorDescriptor_t input_desc,
                             const void* input,
                             cnnlSpaceBatchNdDescriptor_t param,
928 929
                             void* extra_device_input,
                             size_t extra_input_size,
930 931 932
                             const cnnlTensorDescriptor_t output_desc,
                             void* output);

933 934 935 936 937 938 939 940 941 942 943 944
  static void PoolingForward(const ExecutionContext& ctx,
                             cnnlPoolingMode_t pool_mode,
                             int64_t output_h,
                             int64_t output_w,
                             cnnlPoolingDescriptor_t pooling_desc,
                             const void* alpha,
                             const cnnlTensorDescriptor_t input_desc,
                             const void* input,
                             const void* beta,
                             const void* extra_input_ptr,
                             const cnnlTensorDescriptor_t output_desc,
                             void* output);
945

946 947 948 949 950 951 952 953 954 955 956
  static void AdaptivePoolingForward(const ExecutionContext& ctx,
                                     cnnlPoolingMode_t pool_mode,
                                     const cnnlTensorDescriptor_t input_desc,
                                     const void* input,
                                     const cnnlTensorDescriptor_t output_desc,
                                     void* output,
                                     const cnnlTensorDescriptor_t index_desc,
                                     void* index);

  static void Pool3D(const ExecutionContext& ctx,
                     cnnlPoolingMode_t pool_mode,
957
                     const std::vector<int64_t>& output_shape,
958 959 960 961 962 963
                     cnnlPoolingDescriptor_t pooling_desc,
                     const void* alpha,
                     const cnnlTensorDescriptor_t input_desc,
                     const void* input,
                     const void* beta,
                     const cnnlTensorDescriptor_t output_desc,
964 965 966
                     void* output);

  static void Pad(const ExecutionContext& ctx,
967 968 969 970 971 972 973 974 975
                  const cnnlTensorDescriptor_t input_desc,
                  const void* input,
                  const void* paddings,
                  const void* padding_value,
                  const cnnlTensorDescriptor_t output_desc,
                  void* output);

  static void Matmul(const ExecutionContext& ctx,
                     const bool transpose_a,
976
                     const bool transpose_b,
977 978 979 980 981 982
                     const cnnlTensorDescriptor_t in0_desc,
                     const void* in0,
                     const cnnlTensorDescriptor_t in1_desc,
                     const void* in1,
                     const cnnlTensorDescriptor_t output_desc,
                     void* output);
983

984 985 986 987 988 989 990 991 992
  static void BatchMatmul(const ExecutionContext& ctx,
                          const bool transpose_a,
                          const bool transpose_b,
                          const cnnlTensorDescriptor_t in0_desc,
                          const void* in0,
                          const cnnlTensorDescriptor_t in1_desc,
                          const void* in1,
                          const cnnlTensorDescriptor_t output_desc,
                          void* output);
993

Q
qipengh 已提交
994
  static void MulAx(const ExecutionContext& ctx,
995 996 997 998
                    const cnnlTensorDescriptor_t alpha_desc,
                    const void* alpha,
                    const cnnlTensorDescriptor_t output_desc,
                    void* output);
Q
qipengh 已提交
999

1000 1001
  static void OpTensor(const ExecutionContext& ctx,
                       const cnnlOpTensorDescriptor_t op_tensor_desc,
1002 1003 1004 1005 1006 1007
                       const cnnlTensorDescriptor_t a_desc,
                       const void* a,
                       const cnnlTensorDescriptor_t b_desc,
                       const void* b,
                       const cnnlTensorDescriptor_t output_desc,
                       void* output,
1008 1009 1010 1011
                       const cnnlDataType_t dtype,
                       const float alpha1_float = 1.f,
                       const float alpha2_float = 1.f,
                       const float beta_float = 0.f);
1012

1013 1014
  static void BiasAddGrad(const ExecutionContext& ctx,
                          const int axis,
1015 1016 1017 1018 1019 1020 1021
                          const cnnlTensorDescriptor_t out_backprop_desc,
                          const void* out_backprop,
                          const cnnlTensorDescriptor_t output_desc,
                          void* output);

  static void OneHot(const ExecutionContext& ctx,
                     const cnnlTensorDescriptor_t desc_indices,
1022 1023 1024 1025 1026 1027 1028
                     const void* indices,
                     const int depth,
                     const void* on_value,
                     const void* off_value,
                     const int axis,
                     cnnlDataType_t output_data_type,
                     void* output);
1029 1030 1031 1032 1033 1034 1035 1036

  static void NonMaxSuppression(const ExecutionContext& ctx,
                                const cnnlNmsDescriptor_t nms_desc,
                                const cnnlTensorDescriptor_t boxes_desc,
                                const void* boxes,
                                const cnnlTensorDescriptor_t confidence_desc,
                                const void* confidence,
                                const cnnlTensorDescriptor_t output_desc,
1037 1038
                                void* output,
                                void* output_size);
1039 1040

  static void SoftmaxCrossEntropyWithLogits(
1041 1042
      const ExecutionContext& ctx,
      cnnlSoftmaxMode_t mode,
1043
      cnnlComputationPreference_t prefer,
1044 1045 1046 1047 1048 1049 1050 1051
      const cnnlTensorDescriptor_t input_desc,
      const void* logits_in,
      const cnnlTensorDescriptor_t label_desc,
      const void* labels_in,
      const cnnlTensorDescriptor_t loss_out_desc,
      void* loss_out,
      const cnnlTensorDescriptor_t back_out_desc,
      void* back_out);
1052 1053 1054

  static void SoftmaxForward(const ExecutionContext& ctx,
                             cnnlSoftmaxAlgorithm_t algorithm,
1055 1056
                             cnnlSoftmaxMode_t mode,
                             const void* alpha,
1057
                             const cnnlTensorDescriptor_t input_desc,
1058 1059
                             const void* input,
                             const void* beta,
1060 1061 1062
                             const cnnlTensorDescriptor_t output_desc,
                             void* output);

1063 1064 1065 1066 1067 1068 1069 1070 1071
  static void SoftmaxBackward(const ExecutionContext& ctx,
                              cnnlSoftmaxAlgorithm_t algorithm,
                              cnnlSoftmaxMode_t mode,
                              const cnnlTensorDescriptor_t y_desc,
                              const void* y,
                              const cnnlTensorDescriptor_t diff_y_desc,
                              const void* diff_y,
                              const cnnlTensorDescriptor_t diff_x_desc,
                              void* diff_x);
1072

1073 1074 1075
  static void Softplus(const ExecutionContext& ctx,
                       const cnnlTensorDescriptor_t features_desc,
                       const void* features,
1076 1077
                       const cnnlTensorDescriptor_t output_desc,
                       void* output);
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087

  static void SoftplusGrad(const ExecutionContext& ctx,
                           const cnnlTensorDescriptor_t gradients_desc,
                           const void* gradients,
                           const cnnlTensorDescriptor_t features_desc,
                           const void* features,
                           const cnnlTensorDescriptor_t output_desc,
                           void* output);

  static void RsqrtGrad(const ExecutionContext& ctx,
1088 1089 1090 1091
                        const cnnlTensorDescriptor_t data_desc,
                        const void* y,
                        const void* diff_y,
                        void* output);
1092 1093

  static void SqrtGrad(const ExecutionContext& ctx,
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
                       const cnnlTensorDescriptor_t data_desc,
                       const void* y,
                       const void* diff_y,
                       void* output);

  static void ConvolutionForward(const ExecutionContext& ctx,
                                 cnnlConvolutionDescriptor_t conv_desc_,
                                 const void* alpha,
                                 const void* beta,
                                 const cnnlTensorDescriptor_t bias_desc,
                                 const void* bias_ptr,
                                 const cnnlTensorDescriptor_t input_desc,
                                 const void* input,
                                 const cnnlTensorDescriptor_t filtet_desc,
                                 const void* filter,
                                 const cnnlTensorDescriptor_t output_desc,
                                 void* output);

  static void FusedConvBNQuantify(const ExecutionContext& ctx,
                                  cnnlConvolutionDescriptor_t conv_desc,
                                  const void* epsilon_ptr,
                                  const int fused_ops_number,
                                  const cnnlDataType_t tensor_dtype,
                                  const int input_position,
                                  const float input_scale,
                                  const int filter_position,
                                  const float filter_scale,
                                  const cnnlTensorDescriptor_t scale_desc,
                                  const void* scale_ptr,
                                  const cnnlTensorDescriptor_t offset_desc,
                                  const void* offset_ptr,
                                  const cnnlTensorDescriptor_t mean_desc,
                                  const void* mean_ptr,
                                  const cnnlTensorDescriptor_t variance_desc,
                                  const void* variance_ptr,
                                  const cnnlTensorDescriptor_t input_desc,
                                  const void* input,
                                  const cnnlTensorDescriptor_t filtet_desc,
                                  const void* filter,
                                  const cnnlTensorDescriptor_t output_desc,
                                  void* output);
1135 1136

  static void Tile(const ExecutionContext& ctx,
1137 1138 1139 1140
                   const cnnlTensorDescriptor_t input_desc,
                   const void* input,
                   const cnnlTensorDescriptor_t output_desc,
                   void* output);
1141 1142 1143 1144 1145 1146 1147 1148 1149

  static void UnsortedSegmentSum(const ExecutionContext& ctx,
                                 const cnnlTensorDescriptor_t data_desc,
                                 const void* data,
                                 const cnnlTensorDescriptor_t ids_desc,
                                 const int* segment_ids,
                                 const cnnlTensorDescriptor_t output_desc,
                                 void* output);

1150 1151
  static void Reduce(const ExecutionContext& ctx,
                     const bool need_workspace,
1152
                     const cnnlReduceDescriptor_t reduction_desc,
1153 1154 1155 1156 1157 1158 1159 1160
                     const void* alpha,
                     const cnnlTensorDescriptor_t input_desc,
                     const void* input,
                     const size_t indices_size,
                     void* indices,
                     const void* beta,
                     const cnnlTensorDescriptor_t output_desc,
                     void* output);
1161 1162 1163 1164 1165 1166 1167

  static void FloorDiv(const ExecutionContext& ctx,
                       cnnlComputationPreference_t prefer,
                       const cnnlTensorDescriptor_t input1_desc,
                       const void* input1,
                       const cnnlTensorDescriptor_t input2_desc,
                       const void* input2,
1168 1169
                       const cnnlTensorDescriptor_t output_desc,
                       void* output);
1170 1171 1172 1173 1174 1175

  static void FloorMod(const ExecutionContext& ctx,
                       const cnnlTensorDescriptor_t input1_desc,
                       const void* input1,
                       const cnnlTensorDescriptor_t input2_desc,
                       const void* input2,
1176 1177
                       const cnnlTensorDescriptor_t output_desc,
                       void* output);
1178 1179 1180 1181 1182 1183

  static void Maximum(const ExecutionContext& ctx,
                      const cnnlTensorDescriptor_t input1_desc,
                      const void* input1,
                      const cnnlTensorDescriptor_t input2_desc,
                      const void* input2,
1184 1185
                      const cnnlTensorDescriptor_t output_desc,
                      void* output);
1186 1187 1188 1189 1190 1191

  static void Minimum(const ExecutionContext& ctx,
                      const cnnlTensorDescriptor_t input1_desc,
                      const void* input1,
                      const cnnlTensorDescriptor_t input2_desc,
                      const void* input2,
1192 1193
                      const cnnlTensorDescriptor_t output_desc,
                      void* output);
1194 1195 1196

  static void PowR(const ExecutionContext& ctx,
                   cnnlComputationPreference_t prefer,
1197 1198 1199 1200 1201 1202
                   const cnnlTensorDescriptor_t input1_desc,
                   const void* input1,
                   const cnnlTensorDescriptor_t input2_desc,
                   const void* input2,
                   const cnnlTensorDescriptor_t output_desc,
                   void* output);
1203 1204 1205 1206 1207 1208 1209

  static void DivNoNan(const ExecutionContext& ctx,
                       cnnlComputationPreference_t prefer,
                       const cnnlTensorDescriptor_t input1_desc,
                       const void* input1,
                       const cnnlTensorDescriptor_t input2_desc,
                       const void* input2,
1210 1211
                       const cnnlTensorDescriptor_t output_desc,
                       void* output);
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221

  static void SquaredDifference(const ExecutionContext& ctx,
                                const cnnlTensorDescriptor_t input1_desc,
                                const void* input1,
                                const cnnlTensorDescriptor_t input2_desc,
                                const void* input2,
                                const cnnlTensorDescriptor_t output_desc,
                                void* output);

  static void L2Loss(const ExecutionContext& ctx,
1222 1223
                     const cnnlTensorDescriptor_t input_desc,
                     const void* input,
1224 1225 1226
                     void* output);

  static void Abs(const ExecutionContext& ctx,
1227 1228 1229 1230
                  const cnnlTensorDescriptor_t input_desc,
                  const void* input,
                  const cnnlTensorDescriptor_t output_desc,
                  void* output);
1231 1232

  static void Neg(const ExecutionContext& ctx,
1233 1234 1235 1236
                  const cnnlTensorDescriptor_t input_desc,
                  const void* input,
                  const cnnlTensorDescriptor_t output_desc,
                  void* output);
1237 1238

  static void Floor(const ExecutionContext& ctx,
1239 1240 1241 1242
                    const cnnlTensorDescriptor_t input_desc,
                    const void* input,
                    const cnnlTensorDescriptor_t output_desc,
                    void* output);
1243 1244

  static void Ceil(const ExecutionContext& ctx,
1245 1246 1247 1248
                   const cnnlTensorDescriptor_t input_desc,
                   const void* input,
                   const cnnlTensorDescriptor_t output_desc,
                   void* output);
1249 1250

  static void IsNan(const ExecutionContext& ctx,
1251 1252 1253 1254
                    const cnnlTensorDescriptor_t input_desc,
                    const void* input,
                    const cnnlTensorDescriptor_t output_desc,
                    void* output);
1255 1256

  static void Square(const ExecutionContext& ctx,
1257 1258 1259 1260
                     const cnnlTensorDescriptor_t input_desc,
                     const void* input,
                     const cnnlTensorDescriptor_t output_desc,
                     void* output);
1261 1262 1263

  static void Sqrt(const ExecutionContext& ctx,
                   cnnlComputationPreference_t prefer,
1264 1265 1266 1267
                   const cnnlTensorDescriptor_t input_desc,
                   const void* input,
                   const cnnlTensorDescriptor_t output_desc,
                   void* output);
1268 1269 1270

  static void Rsqrt(const ExecutionContext& ctx,
                    cnnlComputationPreference_t prefer,
1271 1272 1273 1274
                    const cnnlTensorDescriptor_t input_desc,
                    const void* input,
                    const cnnlTensorDescriptor_t output_desc,
                    void* output);
1275 1276 1277

  static void Cos(const ExecutionContext& ctx,
                  cnnlComputationPreference_t prefer,
1278 1279 1280 1281
                  const cnnlTensorDescriptor_t input_desc,
                  const void* input,
                  const cnnlTensorDescriptor_t output_desc,
                  void* output);
1282 1283 1284

  static void Sin(const ExecutionContext& ctx,
                  cnnlComputationPreference_t prefer,
1285 1286 1287 1288
                  const cnnlTensorDescriptor_t input_desc,
                  const void* input,
                  const cnnlTensorDescriptor_t output_desc,
                  void* output);
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298

  static void TrigonForward(const ExecutionContext& ctx,
                            const cnnlTrigonDescriptor_t trigon_desc,
                            const cnnlTensorDescriptor_t input_desc,
                            const void* input,
                            const cnnlTensorDescriptor_t output_desc,
                            void* output);

  static void Exp(const ExecutionContext& ctx,
                  cnnlComputationPreference_t prefer,
1299 1300 1301 1302
                  const cnnlTensorDescriptor_t input_desc,
                  const void* input,
                  const cnnlTensorDescriptor_t output_desc,
                  void* output);
1303 1304

  static void Sign(const ExecutionContext& ctx,
1305 1306 1307 1308
                   const cnnlTensorDescriptor_t input_desc,
                   const void* input,
                   const cnnlTensorDescriptor_t output_desc,
                   void* output);
1309 1310 1311 1312

  static void IsFinite(const ExecutionContext& ctx,
                       const cnnlTensorDescriptor_t input_desc,
                       const void* input,
1313 1314
                       const cnnlTensorDescriptor_t output_desc,
                       void* output);
1315 1316 1317

  static void IsNanInf(const ExecutionContext& ctx,
                       const cnnlTensorDescriptor_t input_desc,
1318 1319
                       const void* input,
                       void* output);
1320 1321 1322

  static void Erf(const ExecutionContext& ctx,
                  cnnlComputationPreference_t prefer,
1323 1324 1325 1326
                  const cnnlTensorDescriptor_t input_desc,
                  const void* input,
                  const cnnlTensorDescriptor_t output_desc,
                  void* output);
1327 1328 1329

  static void Log1p(const ExecutionContext& ctx,
                    cnnlComputationPreference_t prefer,
1330 1331 1332 1333
                    const cnnlTensorDescriptor_t input_desc,
                    const void* input,
                    const cnnlTensorDescriptor_t output_desc,
                    void* output);
1334 1335 1336 1337 1338 1339 1340

  static void LogicalNot(const ExecutionContext& ctx,
                         const cnnlTensorDescriptor_t input_desc,
                         const void* input,
                         const cnnlTensorDescriptor_t output_desc,
                         void* output);

1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
  static void DynamicStitch(const ExecutionContext& ctx,
                            const cnnlTensorDescriptor_t* indices_desc,
                            const int** indices,
                            const cnnlTensorDescriptor_t* data_desc,
                            const void** data,
                            const int size,
                            int* indices_dims,
                            const cnnlTensorDescriptor_t output_desc,
                            void* output);

  static void CropAndResize(const ExecutionContext& ctx,
                            const std::string method_name,
                            const float extrapolation_value,
                            const cnnlTensorDescriptor_t image_desc,
                            const void* image,
                            const cnnlTensorDescriptor_t boxes_desc,
                            const void* boxes,
                            const cnnlTensorDescriptor_t box_index_desc,
                            const void* box_index,
                            const cnnlTensorDescriptor_t output_desc,
                            void* output);
1362 1363

  static void CropAndResizeBackwardImage(
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
      const ExecutionContext& ctx,
      const std::string method_name,
      const cnnlTensorDescriptor_t image_desc,
      const void* image,
      const cnnlTensorDescriptor_t boxes_desc,
      const void* boxes,
      const cnnlTensorDescriptor_t box_idx_desc,
      const void* box_idx,
      const cnnlTensorDescriptor_t grads_image_desc,
      void* grads_image);
1374 1375

  static void CropAndResizeBackwardBoxes(
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
      const ExecutionContext& ctx,
      const cnnlTensorDescriptor_t input_desc,
      const void* input,
      const cnnlTensorDescriptor_t image_desc,
      const void* image,
      const cnnlTensorDescriptor_t boxes_desc,
      const void* boxes,
      const cnnlTensorDescriptor_t box_idx_desc,
      const void* box_idx,
      const cnnlTensorDescriptor_t output_desc,
1386 1387
      void* output);

1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
  static void PoolingBackward(const ExecutionContext& ctx,
                              const cnnlPoolingDescriptor_t pooling_desc,
                              const void* alpha,
                              const cnnlTensorDescriptor_t y_desc,
                              const void* y,
                              const cnnlTensorDescriptor_t diff_y_desc,
                              const void* diff_y,
                              const cnnlTensorDescriptor_t x_desc,
                              const void* x,
                              const void* beta,
                              const cnnlTensorDescriptor_t diff_x_desc,
                              void* diff_x);

  static void AdaptivePoolingBackward(const ExecutionContext& ctx,
                                      const cnnlPoolingMode_t pool_mode,
                                      const cnnlTensorDescriptor_t y_desc,
                                      const void* y,
                                      const cnnlTensorDescriptor_t index_desc,
                                      const void* index,
                                      const cnnlTensorDescriptor_t diff_x_desc,
                                      void* diff_x);
1409

1410 1411
  static void PoolingIndex(const ExecutionContext& ctx,
                           const cnnlPoolingDescriptor_t pooling_desc,
1412 1413 1414 1415
                           const cnnlTensorDescriptor_t x_desc,
                           const void* x,
                           const cnnlTensorDescriptor_t y_desc,
                           void* y);
1416 1417 1418 1419 1420

  static void SpaceToBatch(const ExecutionContext& ctx,
                           const cnnlTensorDescriptor_t input_desc,
                           const void* input,
                           const cnnlTensorDescriptor_t output_desc,
1421 1422
                           void* output,
                           const int64_t block_shape[]);
1423 1424 1425 1426 1427

  static void SpaceToBatchNd(const ExecutionContext& ctx,
                             const cnnlTensorDescriptor_t input_desc,
                             const void* input,
                             cnnlSpaceBatchNdDescriptor_t param,
1428 1429
                             void* extra_device_input,
                             size_t extra_input_size,
1430 1431 1432
                             const cnnlTensorDescriptor_t output_desc,
                             void* output);

1433 1434 1435 1436 1437 1438 1439 1440
  static void Interp(const ExecutionContext& ctx,
                     const cnnlInterpMode_t mode,
                     const bool align_corners,
                     const bool half_pixel_centers,
                     const cnnlTensorDescriptor_t input_desc,
                     const void* input,
                     const cnnlTensorDescriptor_t output_desc,
                     void* output);
1441

1442 1443 1444 1445 1446 1447 1448 1449
  static void InterpBackward(const ExecutionContext& ctx,
                             const cnnlInterpBackwardMode_t mode,
                             const bool align_corners,
                             const bool half_pixel_centers,
                             const cnnlTensorDescriptor_t input_desc,
                             const void* input,
                             const cnnlTensorDescriptor_t output_desc,
                             void* output);
1450 1451

  static void QuantizeParam(const ExecutionContext& ctx,
1452 1453
                            const cnnlQuantizeMode_t mode,
                            const int bitwidth,
1454
                            const cnnlTensorDescriptor_t input_desc,
1455 1456 1457
                            const void* input,
                            void* position,
                            void* scale,
1458 1459
                            void* offset);

1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549
  static void QuantizeMatMul(const ExecutionContext& ctx,
                             const bool transpose_a,
                             const bool transpose_b,
                             const cnnlTensorDescriptor_t a_desc,
                             const void* a,
                             const void* a_position,
                             const void* a_scale,
                             const void* a_offset,
                             const cnnlTensorDescriptor_t b_desc,
                             const void* b,
                             const void* b_position,
                             const void* b_scale,
                             const void* b_offset,
                             const cnnlDataType_t quant_type,
                             const cnnlDataType_t data_type,
                             const cnnlTensorDescriptor_t output_desc,
                             void* output);

  static void QuantizeBatchMatMul(const ExecutionContext& ctx,
                                  const bool adj_x,
                                  const bool adj_y,
                                  const cnnlTensorDescriptor_t a_desc,
                                  const void* a,
                                  const void* a_position,
                                  const void* a_scale,
                                  const void* a_offset,
                                  const cnnlTensorDescriptor_t b_desc,
                                  const void* b,
                                  const void* b_position,
                                  const void* b_scale,
                                  const void* b_offset,
                                  const cnnlDataType_t quant_type,
                                  const cnnlDataType_t data_type,
                                  const cnnlTensorDescriptor_t output_desc,
                                  void* output);

  static void QuantizeBatchMatMulBCast(const ExecutionContext& ctx,
                                       const bool adj_x,
                                       const bool adj_y,
                                       const cnnlTensorDescriptor_t a_desc,
                                       const void* a,
                                       const void* a_position,
                                       const void* a_scale,
                                       const void* a_offset,
                                       const cnnlTensorDescriptor_t b_desc,
                                       const void* b,
                                       const void* b_position,
                                       const void* b_scale,
                                       const void* b_offset,
                                       const cnnlDataType_t quant_type,
                                       const cnnlDataType_t data_type,
                                       const cnnlTensorDescriptor_t output_desc,
                                       void* output);

  static void FusedBatchNorm(const ExecutionContext& ctx,
                             const bool is_training,
                             const cnnlTensorDescriptor_t x_desc,
                             const void* x,
                             const cnnlTensorDescriptor_t scale_desc,
                             const void* scale,
                             const void* offset,
                             const void* estimated_mean,
                             const void* estimated_variance,
                             float epsilon,
                             float momentum,
                             const cnnlTensorDescriptor_t output_desc,
                             void* output,
                             void* batch_mean,
                             void* batch_var,
                             void* saved_mean,
                             void* saved_var);

  static void FusedBatchNormGrad(const ExecutionContext& ctx,
                                 const bool is_training,
                                 const cnnlTensorDescriptor_t y_backprop_desc,
                                 const void* y_backprop,
                                 const cnnlTensorDescriptor_t x_desc,
                                 const void* x,
                                 const cnnlTensorDescriptor_t scale_desc,
                                 const void* scale,
                                 const void* saved_mean,
                                 const void* saved_var,
                                 float epsilon,
                                 const cnnlTensorDescriptor_t x_backprop_desc,
                                 void* x_backprop,
                                 void* scale_backprop,
                                 void* offset_backprop);

  static void LayerNormForward(const ExecutionContext& ctx,
                               int axis,
1550 1551 1552
                               const cnnlTensorDescriptor_t x_desc,
                               const void* x,
                               const cnnlTensorDescriptor_t weight_bias_desc,
1553 1554 1555 1556 1557
                               const void* weight,
                               const void* bias,
                               float eps,
                               const cnnlTensorDescriptor_t y_desc,
                               void* y,
1558
                               const cnnlTensorDescriptor_t mean_rstd_desc,
1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
                               void* saved_mean,
                               void* saved_rstd);

  static void LayerNormBackward(const ExecutionContext& ctx,
                                int axis,
                                const cnnlTensorDescriptor_t x_desc,
                                const void* x,
                                const cnnlTensorDescriptor_t diff_z_desc,
                                const void* diff_z,
                                const cnnlTensorDescriptor_t weight_bias_desc,
                                const void* weight,
                                const cnnlTensorDescriptor_t mean_rstd_desc,
                                const void* saved_mean,
                                const void* saved_rstd,
                                const cnnlTensorDescriptor_t diff_x_desc,
                                void* diff_x,
                                void* diff_weight,
                                void* diff_bias);
1577

1578
  static void Transpose(const ExecutionContext& ctx,
1579 1580
                        const std::vector<int> perm,
                        const int input_dim,
1581 1582
                        const cnnlTensorDescriptor_t input_desc,
                        const void* input,
1583 1584
                        const cnnlTensorDescriptor_t output_desc,
                        void* output);
1585

1586 1587
  static void TrilTriu(const ExecutionContext& ctx,
                       const int diagonal_k,
1588 1589 1590
                       const bool tri_up_mode,
                       const cnnlTensorDescriptor_t input_desc,
                       const void* input,
1591 1592
                       const cnnlTensorDescriptor_t output_desc,
                       void* output);
1593

1594 1595
  static void MatrixBandPart(const ExecutionContext& ctx,
                             const cnnlTensorDescriptor_t data_desc,
1596 1597 1598 1599
                             const void* input,
                             const int num_lower,
                             const int num_upper,
                             void* output);
1600 1601

  static void NumTrue(const ExecutionContext& ctx,
1602 1603 1604 1605
                      const cnnlTensorDescriptor_t x_desc,
                      const void* x,
                      Tensor index,
                      uint32_t* num_true);
1606 1607

  static void Where(const ExecutionContext& ctx,
1608 1609
                    const cnnlTensorDescriptor_t x_desc,
                    const void* x,
1610 1611 1612
                    const cnnlTensorDescriptor_t num_true_desc,
                    const void* num_true,
                    const bool as_tuple,
1613
                    const cnnlTensorDescriptor_t y_desc,
1614
                    void* y);
1615 1616 1617
  static void Conv2D(const ExecutionContext& ctx,
                     const cnnlConvolutionDescriptor_t conv_desc,
                     const cnnlDataType_t tensor_dtype,
1618 1619 1620 1621 1622 1623
                     const cnnlDataType_t dt_onchip,
                     const void* input_position,
                     const void* input_scale,
                     const void* input_offset,
                     const void* filter_position,
                     const void* filter_scale,
1624
                     const void* filter_offset,
1625 1626
                     const cnnlTensorDescriptor_t input_desc,
                     const void* input,
1627
                     const cnnlTensorDescriptor_t filter_desc,
1628 1629 1630 1631
                     const void* filter,
                     const cnnlTensorDescriptor_t bias_desc,
                     const void* bias,
                     const cnnlTensorDescriptor_t output_desc,
1632 1633
                     void* output);

1634 1635 1636 1637 1638 1639 1640 1641
  static void ConvBackpropInput(const ExecutionContext& ctx,
                                const cnnlConvolutionDescriptor_t conv_desc,
                                const cnnlTensorDescriptor_t filter_desc,
                                const void* filter,
                                const cnnlTensorDescriptor_t out_backprop_desc,
                                const void* out_backprop,
                                const cnnlTensorDescriptor_t in_backprop_desc,
                                void* in_backprop);
1642 1643

  static void QuantizeConvBackpropInput(
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659
      const ExecutionContext& ctx,
      const cnnlConvolutionDescriptor_t conv_desc,
      const cnnlDataType_t tensor_dtype,
      const cnnlDataType_t dt_onchip,
      const void* filter_position,
      const void* filter_scale,
      const void* filter_offset,
      const void* out_backprop_position,
      const void* out_backprop_scale,
      const void* out_backprop_offset,
      const cnnlTensorDescriptor_t input_desc,
      const void* filter,
      const cnnlTensorDescriptor_t out_backprop_desc,
      const void* out_backprop,
      const cnnlTensorDescriptor_t in_backprop_desc,
      void* in_backprop);
1660 1661

  static void ConvBackpropFilter(
1662 1663 1664 1665 1666 1667 1668 1669
      const ExecutionContext& ctx,
      const cnnlConvolutionDescriptor_t conv_desc,
      const cnnlTensorDescriptor_t input_desc,
      const void* input,
      const cnnlTensorDescriptor_t out_backprop_desc,
      const void* out_backprop,
      const cnnlTensorDescriptor_t filter_backprop_desc,
      void* filter_backprop);
1670 1671

  static void QuantizeConvBackpropFilter(
1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736
      const ExecutionContext& ctx,
      const cnnlConvolutionDescriptor_t conv_desc,
      const cnnlDataType_t tensor_dtype,
      const cnnlDataType_t dt_onchip,
      const void* input_position,
      const void* input_scale,
      const void* input_offset,
      const void* out_backprop_position,
      const void* out_backprop_scale,
      const void* out_backprop_offset,
      const cnnlTensorDescriptor_t input_desc,
      const void* input,
      const cnnlTensorDescriptor_t out_backprop_desc,
      const void* out_backprop,
      const cnnlTensorDescriptor_t filter_backprop_desc,
      void* filter_backprop);

  static void DCNForward(const ExecutionContext& ctx,
                         const cnnlDCNDescriptor_t dcn_desc,
                         const cnnlTensorDescriptor_t input_desc,
                         const void* input,
                         const cnnlTensorDescriptor_t offset_desc,
                         const void* offset,
                         const cnnlTensorDescriptor_t mask_desc,
                         const void* mask,
                         const cnnlTensorDescriptor_t weight_desc,
                         const void* weight,
                         const cnnlTensorDescriptor_t bias_desc,
                         const void* bias,
                         const cnnlTensorDescriptor_t output_desc,
                         void* output);

  static void DCNBackwardData(const ExecutionContext& ctx,
                              const cnnlDCNDescriptor_t dcn_desc,
                              const cnnlTensorDescriptor_t input_desc,
                              const void* input,
                              const cnnlTensorDescriptor_t offset_desc,
                              const void* offset,
                              const cnnlTensorDescriptor_t mask_desc,
                              const void* mask,
                              const cnnlTensorDescriptor_t weight_desc,
                              const void* weight,
                              const cnnlTensorDescriptor_t grad_output_desc,
                              const void* grad_output,
                              const cnnlTensorDescriptor_t grad_input_desc,
                              void* grad_input,
                              const cnnlTensorDescriptor_t grad_offset_desc,
                              void* grad_offset,
                              const cnnlTensorDescriptor_t grad_mask_desc,
                              void* grad_mask);

  static void DCNBackwardWeight(const ExecutionContext& ctx,
                                const cnnlDCNDescriptor_t dcn_desc,
                                const cnnlTensorDescriptor_t input_desc,
                                const void* input,
                                const cnnlTensorDescriptor_t offset_desc,
                                const void* offset,
                                const cnnlTensorDescriptor_t mask_desc,
                                const void* mask,
                                const cnnlTensorDescriptor_t grad_output_desc,
                                const void* grad_output,
                                const cnnlTensorDescriptor_t grad_weight_desc,
                                void* grad_weight,
                                const cnnlTensorDescriptor_t grad_bias_desc,
                                void* grad_bias);
1737

1738 1739 1740 1741
  static void InTopK(const ExecutionContext& ctx,
                     const cnnlTensorDescriptor_t predictions_desc,
                     const void* predictions,
                     const cnnlTensorDescriptor_t targets_desc,
1742 1743 1744 1745 1746 1747
                     const void* targets,
                     const cnnlTensorDescriptor_t k_desc,
                     const void* k,
                     const int k_int,
                     const cnnlTensorDescriptor_t output_desc,
                     void* output);
1748

1749 1750
  static void ScatterNd(const ExecutionContext& ctx,
                        cnnlScatterNdMode_t mode,
1751 1752 1753 1754
                        const cnnlTensorDescriptor_t indices_desc,
                        const void* indices,
                        const cnnlTensorDescriptor_t updates_desc,
                        const void* updates,
1755 1756
                        const cnnlTensorDescriptor_t input_desc,
                        const void* input,
1757 1758
                        const cnnlTensorDescriptor_t output_desc,
                        void* output);
1759 1760 1761 1762 1763 1764 1765

  static void BitWise(const ExecutionContext& ctx,
                      const cnnlBitComputeOp_t optype,
                      const cnnlTensorDescriptor_t input1_desc,
                      const void* input1,
                      const cnnlTensorDescriptor_t input2_desc,
                      const void* input2,
1766 1767
                      const cnnlTensorDescriptor_t output_desc,
                      void* output);
1768 1769

  static void QR(const ExecutionContext& ctx,
1770 1771 1772 1773 1774 1775 1776
                 const cnnlTensorDescriptor_t a_desc,
                 const void* a,
                 const cnnlTensorDescriptor_t q_desc,
                 void* q,
                 const cnnlTensorDescriptor_t r_desc,
                 void* r,
                 const bool some);
1777 1778 1779 1780 1781 1782

  static void Reciprocal(const ExecutionContext& ctx,
                         const cnnlTensorDescriptor_t input_desc,
                         const void* input,
                         const cnnlTensorDescriptor_t output_desc,
                         void* output);
1783

1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818
  static void BceLoss(const ExecutionContext& ctx,
                      const cnnlBceLossReduction_t reduction,
                      const cnnlTensorDescriptor_t input_desc,
                      const void* input,
                      const cnnlTensorDescriptor_t target_desc,
                      const void* target,
                      const cnnlTensorDescriptor_t weight_desc,
                      const void* weight,
                      const cnnlTensorDescriptor_t output_desc,
                      void* output);

  static void BceLossBackward(const ExecutionContext& ctx,
                              const cnnlBceLossReduction_t reduction,
                              const cnnlTensorDescriptor_t grad_desc,
                              const void* grad,
                              const cnnlTensorDescriptor_t input_desc,
                              const void* input,
                              const cnnlTensorDescriptor_t target_desc,
                              const void* target,
                              const cnnlTensorDescriptor_t weight_desc,
                              const void* weight,
                              const cnnlTensorDescriptor_t output_desc,
                              void* output);

  static void EmbeddingForward(const ExecutionContext& ctx,
                               const int padding_idx,
                               const cnnlTensorDescriptor_t weight_desc,
                               const void* weight,
                               const cnnlTensorDescriptor_t indices_desc,
                               const int* indices,
                               const cnnlTensorDescriptor_t output_desc,
                               void* output);

  static void Transform(const ExecutionContext& ctx,
                        const void* alpha,
1819 1820 1821
                        const void* beta,
                        const cnnlTensorDescriptor_t input_desc,
                        const void* input,
1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846
                        const cnnlTensorDescriptor_t output_desc,
                        void* output);

  static void EmbeddingBackward(const ExecutionContext& ctx,
                                int padding_idx,
                                bool scale_grad_by_freq,
                                const cnnlTensorDescriptor_t indices_desc,
                                const void* indices,
                                const cnnlTensorDescriptor_t diff_desc,
                                const void* diff,
                                const cnnlTensorDescriptor_t output_desc,
                                void* output);

  static void BceWithLogits(const ExecutionContext& ctx,
                            cnnlBceWithLogitsReduction_t reduction,
                            const cnnlTensorDescriptor_t input_desc,
                            const void* input,
                            const cnnlTensorDescriptor_t target_desc,
                            const void* target,
                            const cnnlTensorDescriptor_t weight_desc,
                            const void* weight,
                            const cnnlTensorDescriptor_t pos_weight_desc,
                            const void* pos_weight,
                            const cnnlTensorDescriptor_t output_desc,
                            void* output);
F
fwenguang 已提交
1847 1848

  static void BceWithLogitsBackward(
1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862
      const ExecutionContext& ctx,
      cnnlBceWithLogitsReduction_t reduction,
      const cnnlTensorDescriptor_t grad_desc,
      const void* grad,
      const cnnlTensorDescriptor_t input_desc,
      const void* input,
      const cnnlTensorDescriptor_t target_desc,
      const void* target,
      const cnnlTensorDescriptor_t weight_desc,
      const void* weight,
      const cnnlTensorDescriptor_t pos_weight_desc,
      const void* pos_weight,
      const cnnlTensorDescriptor_t diff_input_desc,
      void* diff_input);
F
fwenguang 已提交
1863 1864
};

Q
qipengh 已提交
1865 1866 1867 1868 1869 1870
template <typename T>
inline void TransposeFromMLUTensor(const ExecutionContext& ctx,
                                   const std::vector<int> perm,
                                   const Tensor* transformed_input,
                                   Tensor* transformed_output,
                                   bool need_reshape_or_alloc) {
1871
  const int dim_size = perm.size();
Q
qipengh 已提交
1872
  if (need_reshape_or_alloc) {
1873 1874 1875 1876 1877
    std::vector<int> output_shape;
    auto input_dims = transformed_input->dims();
    for (int i = 0; i < dim_size; ++i) {
      output_shape.push_back(input_dims[perm[i]]);
    }
Q
qipengh 已提交
1878
    transformed_output->mutable_data<T>(
1879
        framework::DDim(output_shape.data(), dim_size), ctx.GetPlace());
Q
qipengh 已提交
1880
  }
1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891
  MLUCnnlTensorDesc trans_in_desc(
      *transformed_input, CNNL_LAYOUT_ARRAY, ToCnnlDataType<T>());
  MLUCnnlTensorDesc trans_out_desc(
      *transformed_output, CNNL_LAYOUT_ARRAY, ToCnnlDataType<T>());

  MLUCnnl::Transpose(ctx,
                     perm,
                     dim_size,
                     trans_in_desc.get(),
                     GetBasePtr(transformed_input),
                     trans_out_desc.get(),
Q
qipengh 已提交
1892 1893 1894
                     GetBasePtr(transformed_output));
}

1895
template <typename T>
1896 1897
inline void FillMLUTensorWithHostValue(const ExecutionContext& ctx,
                                       T value,
1898 1899
                                       Tensor* out) {
  MLUCnnlTensorDesc out_desc(*out);
1900 1901
  MLUCnnl::Fill(
      ctx, CNNL_POINTER_MODE_HOST, &value, out_desc.get(), GetBasePtr(out));
1902 1903
}

F
fwenguang 已提交
1904 1905
}  // namespace operators
}  // namespace paddle