mlu_baseop.h 86.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;
};

498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
class MLUSeqDataDesc {
 public:
  MLUSeqDataDesc(const MLUSeqDataDesc& desc) = delete;
  MLUSeqDataDesc& operator=(const MLUSeqDataDesc& desc) = delete;

  MLUSeqDataDesc(cnnlSeqDataLayout_t layout,
                 cnnlDataType_t dtype,
                 int dimNb,
                 const int dimSize[],
                 int seqLengthArraySize,
                 const int seqLengthArray[],
                 void* paddingFill);

  const cnnlSeqDataDescriptor_t get() const;

  ~MLUSeqDataDesc();

 private:
  cnnlSeqDataDescriptor_t seq_data_desc_ = nullptr;
};

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

  MLURNNDesc(const int hidden_size,
             const int num_layers,
             const cnnlRNNInputMode_t input_mode,
             const cnnlDirectionMode_t direction,
             const cnnlRNNMode_t rnn_mode);

  MLURNNDesc(cnnlRNNMode_t cell_mode,
             cnnlRNNBiasMode_t bias_mode,
             cnnlDirectionMode_t direction,
             cnnlRNNInputMode_t input_mode,
             cnnlDataType_t data_type,
             cnnlDataType_t math_prec,
             int input_size,
             int hidden_size,
             int proj_size,
             int layer_num,
             void* dropout_desc,
             cnnlRNNPaddingMode_t padding_mode);

  void SetRNNProjectionLayers(const int rec_proj_size,
                              const int out_proj_size) {
    PADDLE_ENFORCE_MLU_SUCCESS(
        cnnlSetRNNProjectionLayers(rnn_desc_, rec_proj_size, out_proj_size));
  }

  void SetPeepholeMode(const cnnlRNNPeepholeMode_t peephole_mode) {
    PADDLE_ENFORCE_MLU_SUCCESS(
        cnnlSetRNNPeepholeMode(rnn_desc_, peephole_mode));
  }

  void SetRNNBiasMode(const cnnlRNNBiasMode_t bias_mode) {
    PADDLE_ENFORCE_MLU_SUCCESS(cnnlSetRNNBiasMode(rnn_desc_, bias_mode));
  }

  void SetRNNMaskMode(const cnnlRNNMaskMode_t mask_mode) {
    PADDLE_ENFORCE_MLU_SUCCESS(cnnlSetRNNMaskMode(rnn_desc_, mask_mode));
  }

  void SetRNNClip(const cnnlRNNClipMode_t clip_mode,
                  const cnnlNanPropagation_t clip_nan_opt,
                  const double left_clip,
                  const double right_clip) {
    PADDLE_ENFORCE_MLU_SUCCESS(cnnlSetRNNClip(
        rnn_desc_, clip_mode, clip_nan_opt, left_clip, right_clip));
  }

  void SetRNNPaddingMode(const cnnlRNNPaddingMode_t padding_mode) {
    PADDLE_ENFORCE_MLU_SUCCESS(cnnlSetRNNPaddingMode(rnn_desc_, padding_mode));
  }

  const cnnlRNNDescriptor_t get() const;

  ~MLURNNDesc();

 private:
  cnnlRNNDescriptor_t rnn_desc_ = nullptr;
};

F
fwenguang 已提交
582 583
class MLUCnnl {
 public:
584
  static void Active(const ExecutionContext& ctx,
F
fwenguang 已提交
585
                     cnnlActivationDescriptor_t active_desc,
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
                     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[],
608
                     const void* const inputs[],
609 610
                     const cnnlTensorDescriptor_t output_desc,
                     void* output);
611

612 613 614 615
  static void Concat(const MLUDeviceContext& dev_ctx,
                     const int pack_num,
                     const int axis,
                     const cnnlTensorDescriptor_t inputs_desc[],
Z
zn 已提交
616
                     const void* const inputs[],
617 618
                     const cnnlTensorDescriptor_t output_desc,
                     void* output);
Z
zn 已提交
619

620 621 622 623 624 625
  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);
626

627
  static void Clip(const ExecutionContext& ctx,
628 629 630 631 632
                   const cnnlTensorDescriptor_t input_desc,
                   const void* input,
                   const void* min,
                   const void* max,
                   void* y);
633

634 635 636 637 638 639 640 641 642
  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);
643

644 645
  static void Div(const ExecutionContext& ctx,
                  cnnlComputationPreference_t prefer,
646 647 648 649 650 651
                  const cnnlTensorDescriptor_t in0_desc,
                  const void* in0,
                  const cnnlTensorDescriptor_t in1_desc,
                  const void* in1,
                  const cnnlTensorDescriptor_t output_desc,
                  void* output);
652

653
  static void Fill(const ExecutionContext& ctx,
654 655 656 657 658 659 660 661 662 663
                   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,
664 665
                  const cnnlTensorDescriptor_t input_quant_desc,
                  const void* input_quant,
666 667
                  const cnnlTensorDescriptor_t output_desc,
                  void* output);
668 669 670 671 672 673 674 675 676 677 678

  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,
679 680 681 682
                             const void* input,
                             const bool compute_scale,
                             void* position,
                             void* scale,
683 684 685 686
                             const cnnlTensorDescriptor_t ouput_desc,
                             void* output);

  static void SGD(const ExecutionContext& context,
687 688 689 690
                  const cnnlTensorDescriptor_t grad_desc,
                  const void* grad,
                  const void* lr,
                  const cnnlTensorDescriptor_t var_desc,
691 692 693 694 695
                  void* var);

  static void ApplyAdaGrad(const ExecutionContext& ctx,
                           const cnnlTensorDescriptor_t grad_desc,
                           const void* grad,
696 697 698 699 700 701
                           const cnnlTensorDescriptor_t accum_desc,
                           void* accum,
                           const cnnlTensorDescriptor_t var_desc,
                           void* var,
                           const void* lr,
                           const bool update_slots);
702 703 704

  static void ApplyRMSProp(const ExecutionContext& context,
                           const cnnlTensorDescriptor_t grad_desc,
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
                           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);
732 733

  static void ApplyAdam(const ExecutionContext& ctx,
734 735 736 737 738 739
                        const cnnlTensorDescriptor_t var_desc,
                        void* var,
                        const cnnlTensorDescriptor_t m_desc,
                        void* m,
                        const cnnlTensorDescriptor_t v_desc,
                        void* v,
740
                        const cnnlTensorDescriptor_t grad_desc,
741 742 743 744 745 746 747
                        const void* grad,
                        const void* lr,
                        const void* beta1,
                        const void* beta2,
                        const void* beta1_power,
                        const void* beta2_power,
                        const void* epsilon,
748
                        const bool use_nesterov);
749 750 751

  static void ApplyAdaMax(const ExecutionContext& ctx,
                          const cnnlTensorDescriptor_t grad_desc,
752 753 754 755 756 757 758 759 760 761 762
                          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,
763 764 765 766
                          const void* epsilon);

  static void ApplyMomentum(const ExecutionContext& ctx,
                            const cnnlTensorDescriptor_t grad_desc,
767 768 769 770 771
                            const void* grad,
                            const bool use_nesterov,
                            const void* lr,
                            const void* momentum,
                            void* var,
772 773 774 775
                            void* accum);

  static void ApplyKerasMomentum(const ExecutionContext& ctx,
                                 const cnnlTensorDescriptor_t grad_desc,
776 777 778 779 780 781
                                 const void* grad,
                                 const bool use_nesterov,
                                 const void* lr,
                                 const void* momentum,
                                 void* var,
                                 void* accum);
782 783 784

  static void ApplyAdadelta(const ExecutionContext& ctx,
                            const cnnlTensorDescriptor_t grad_desc,
785 786 787 788 789 790
                            const void* diff,
                            const void* lr,
                            const void* rho,
                            const void* epsilon,
                            void* var,
                            void* accum,
791 792 793
                            void* accum_update);

  static void SparseSoftmaxXentWithLogits(
794 795 796 797 798 799 800 801 802 803 804 805 806
      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,
807 808
                            const cnnlDataType_t data_type,
                            const cnnlRandGenerator_t mlu_generator,
809 810
                            void* mlu_state,
                            void* output);
Q
qipengh 已提交
811

812 813 814 815 816 817 818 819 820 821
  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);
822

823 824 825 826 827 828 829 830
  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);
831 832 833 834 835 836 837

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

838 839 840 841 842 843 844 845 846
  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);
847

848 849 850 851 852 853 854 855
  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);
856

857 858
  static void ScatterFunctor(const ExecutionContext& ctx,
                             const cnnlTensorDescriptor_t params_desc,
859
                             void* params,
860 861 862
                             const cnnlTensorDescriptor_t updates_desc,
                             const void* updates,
                             const cnnlTensorDescriptor_t indices_desc,
863 864
                             const void* indices,
                             const int dim,
865 866
                             const cnnlScatterMode_t mode = CNNL_SCATTER);

867 868 869 870 871 872
  static void Range(const ExecutionContext& ctx,
                    const void* start,
                    const void* end,
                    const void* step,
                    const cnnlDataType_t output_dtype,
                    void* output);
873 874

  static void Round(const ExecutionContext& ctx,
875 876 877 878
                    const cnnlTensorDescriptor_t input_desc,
                    const void* input,
                    const cnnlTensorDescriptor_t output_desc,
                    void* output);
879

880 881 882 883 884 885 886
  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,
887 888 889 890 891
                   const cnnlTensorDescriptor_t values_output_desc,
                   void* values_out,
                   const cnnlTensorDescriptor_t indices_output_desc,
                   void* indices_out);

892 893 894 895
  static void StridedSlice(const ExecutionContext& ctx,
                           const int begin[],
                           const int end[],
                           const int strides[],
896 897 898 899 900
                           const cnnlTensorDescriptor_t input_desc,
                           const void* input,
                           const cnnlTensorDescriptor_t output_desc,
                           void* output);

901 902 903
  static void Split(const ExecutionContext& ctx,
                    int split_num,
                    int axis,
904 905 906 907 908
                    const cnnlTensorDescriptor_t input_desc,
                    const void* input_ptr,
                    const cnnlTensorDescriptor_t output_descs[],
                    void* output_ptrs[]);

909 910 911
  static void Split(const MLUDeviceContext& dev_ctx,
                    int split_num,
                    int axis,
Z
zn 已提交
912 913 914 915 916
                    const cnnlTensorDescriptor_t input_desc,
                    const void* input_ptr,
                    const cnnlTensorDescriptor_t output_descs[],
                    void* output_ptrs[]);

917 918 919 920 921 922 923 924 925 926
  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);
927

928 929
  static void AddN(const ExecutionContext& ctx,
                   uint32_t input_num,
930 931
                   const cnnlTensorDescriptor_t inputs_desc[],
                   const void* inputs[],
932 933
                   const cnnlTensorDescriptor_t output_desc,
                   void* output);
934 935

  static void Log(const ExecutionContext& ctx,
936 937 938 939 940 941 942 943 944 945 946
                  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[],
947 948 949 950 951
                               const cnnlTensorDescriptor_t input_desc,
                               const void* input,
                               const cnnlTensorDescriptor_t output_desc,
                               void* output);

952 953
  static void Logic(const ExecutionContext& ctx,
                    const cnnlLogicOp_t log_method,
954 955 956
                    const cnnlTensorDescriptor_t input1_desc,
                    const void* input1,
                    const cnnlTensorDescriptor_t input2_desc,
957 958
                    const void* input2,
                    const cnnlTensorDescriptor_t ouput_desc,
959 960
                    void* output);

961 962 963 964 965 966 967 968 969 970 971 972
  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,
973 974 975
                        const void* beta,
                        const cnnlTensorDescriptor_t update_desc,
                        const void* update,
976 977
                        const cnnlTensorDescriptor_t param_desc,
                        void* param);
978

979 980
  static void AssignSub(const ExecutionContext& ctx,
                        const void* alpha,
981 982 983
                        const void* beta,
                        const cnnlTensorDescriptor_t update_desc,
                        const void* update,
984 985
                        const cnnlTensorDescriptor_t param_desc,
                        void* param);
986 987 988 989

  static void Assign(const ExecutionContext& ctx,
                     const cnnlTensorDescriptor_t update_desc,
                     const void* update,
990 991
                     const cnnlTensorDescriptor_t param_desc,
                     void* param);
992 993 994 995 996 997

  static void GatherNd(const ExecutionContext& ctx,
                       const cnnlTensorDescriptor_t params_desc,
                       const void* params,
                       const cnnlTensorDescriptor_t indices_desc,
                       const void* indices,
998 999
                       const cnnlTensorDescriptor_t output_desc,
                       void* output);
1000 1001 1002 1003 1004

  static void BatchToSpace(const ExecutionContext& ctx,
                           const cnnlTensorDescriptor_t input_desc,
                           const void* input,
                           const cnnlTensorDescriptor_t output_desc,
1005 1006
                           void* output,
                           const cnnlSpaceBatchParam_t param);
1007 1008 1009 1010 1011

  static void BatchToSpaceNd(const ExecutionContext& ctx,
                             const cnnlTensorDescriptor_t input_desc,
                             const void* input,
                             cnnlSpaceBatchNdDescriptor_t param,
1012 1013
                             void* extra_device_input,
                             size_t extra_input_size,
1014 1015 1016
                             const cnnlTensorDescriptor_t output_desc,
                             void* output);

1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
  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);
1029

1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
  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,
1041
                     const std::vector<int64_t>& output_shape,
1042 1043 1044 1045 1046 1047
                     cnnlPoolingDescriptor_t pooling_desc,
                     const void* alpha,
                     const cnnlTensorDescriptor_t input_desc,
                     const void* input,
                     const void* beta,
                     const cnnlTensorDescriptor_t output_desc,
1048 1049 1050
                     void* output);

  static void Pad(const ExecutionContext& ctx,
1051 1052 1053 1054 1055 1056 1057 1058 1059
                  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,
1060
                     const bool transpose_b,
1061 1062 1063 1064 1065 1066
                     const cnnlTensorDescriptor_t in0_desc,
                     const void* in0,
                     const cnnlTensorDescriptor_t in1_desc,
                     const void* in1,
                     const cnnlTensorDescriptor_t output_desc,
                     void* output);
1067

1068 1069 1070 1071 1072 1073 1074 1075 1076
  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);
1077

Q
qipengh 已提交
1078
  static void MulAx(const ExecutionContext& ctx,
1079 1080 1081 1082
                    const cnnlTensorDescriptor_t alpha_desc,
                    const void* alpha,
                    const cnnlTensorDescriptor_t output_desc,
                    void* output);
Q
qipengh 已提交
1083

1084 1085
  static void OpTensor(const ExecutionContext& ctx,
                       const cnnlOpTensorDescriptor_t op_tensor_desc,
1086 1087 1088 1089 1090 1091
                       const cnnlTensorDescriptor_t a_desc,
                       const void* a,
                       const cnnlTensorDescriptor_t b_desc,
                       const void* b,
                       const cnnlTensorDescriptor_t output_desc,
                       void* output,
1092 1093 1094 1095
                       const cnnlDataType_t dtype,
                       const float alpha1_float = 1.f,
                       const float alpha2_float = 1.f,
                       const float beta_float = 0.f);
1096

1097 1098
  static void BiasAddGrad(const ExecutionContext& ctx,
                          const int axis,
1099 1100 1101 1102 1103 1104 1105
                          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,
1106 1107 1108 1109 1110 1111 1112
                     const void* indices,
                     const int depth,
                     const void* on_value,
                     const void* off_value,
                     const int axis,
                     cnnlDataType_t output_data_type,
                     void* output);
1113 1114 1115 1116 1117 1118 1119 1120

  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,
1121 1122
                                void* output,
                                void* output_size);
1123 1124

  static void SoftmaxCrossEntropyWithLogits(
1125 1126
      const ExecutionContext& ctx,
      cnnlSoftmaxMode_t mode,
1127
      cnnlComputationPreference_t prefer,
1128 1129 1130 1131 1132 1133 1134 1135
      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);
1136 1137 1138

  static void SoftmaxForward(const ExecutionContext& ctx,
                             cnnlSoftmaxAlgorithm_t algorithm,
1139 1140
                             cnnlSoftmaxMode_t mode,
                             const void* alpha,
1141
                             const cnnlTensorDescriptor_t input_desc,
1142 1143
                             const void* input,
                             const void* beta,
1144 1145 1146
                             const cnnlTensorDescriptor_t output_desc,
                             void* output);

1147 1148 1149 1150 1151 1152 1153 1154 1155
  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);
1156

1157 1158 1159
  static void Softplus(const ExecutionContext& ctx,
                       const cnnlTensorDescriptor_t features_desc,
                       const void* features,
1160 1161
                       const cnnlTensorDescriptor_t output_desc,
                       void* output);
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171

  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,
1172 1173 1174 1175
                        const cnnlTensorDescriptor_t data_desc,
                        const void* y,
                        const void* diff_y,
                        void* output);
1176 1177

  static void SqrtGrad(const ExecutionContext& ctx,
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
                       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);
1219 1220

  static void Tile(const ExecutionContext& ctx,
1221 1222 1223 1224
                   const cnnlTensorDescriptor_t input_desc,
                   const void* input,
                   const cnnlTensorDescriptor_t output_desc,
                   void* output);
1225 1226 1227 1228 1229 1230 1231 1232 1233

  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);

1234 1235
  static void Reduce(const ExecutionContext& ctx,
                     const bool need_workspace,
1236
                     const cnnlReduceDescriptor_t reduction_desc,
1237 1238 1239 1240 1241 1242 1243 1244
                     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);
1245 1246 1247 1248 1249 1250 1251

  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,
1252 1253
                       const cnnlTensorDescriptor_t output_desc,
                       void* output);
1254 1255 1256 1257 1258 1259

  static void FloorMod(const ExecutionContext& ctx,
                       const cnnlTensorDescriptor_t input1_desc,
                       const void* input1,
                       const cnnlTensorDescriptor_t input2_desc,
                       const void* input2,
1260 1261
                       const cnnlTensorDescriptor_t output_desc,
                       void* output);
1262 1263 1264 1265 1266 1267

  static void Maximum(const ExecutionContext& ctx,
                      const cnnlTensorDescriptor_t input1_desc,
                      const void* input1,
                      const cnnlTensorDescriptor_t input2_desc,
                      const void* input2,
1268 1269
                      const cnnlTensorDescriptor_t output_desc,
                      void* output);
1270 1271 1272 1273 1274 1275

  static void Minimum(const ExecutionContext& ctx,
                      const cnnlTensorDescriptor_t input1_desc,
                      const void* input1,
                      const cnnlTensorDescriptor_t input2_desc,
                      const void* input2,
1276 1277
                      const cnnlTensorDescriptor_t output_desc,
                      void* output);
1278 1279 1280

  static void PowR(const ExecutionContext& ctx,
                   cnnlComputationPreference_t prefer,
1281 1282 1283 1284 1285 1286
                   const cnnlTensorDescriptor_t input1_desc,
                   const void* input1,
                   const cnnlTensorDescriptor_t input2_desc,
                   const void* input2,
                   const cnnlTensorDescriptor_t output_desc,
                   void* output);
1287 1288 1289 1290 1291 1292 1293

  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,
1294 1295
                       const cnnlTensorDescriptor_t output_desc,
                       void* output);
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305

  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,
1306 1307
                     const cnnlTensorDescriptor_t input_desc,
                     const void* input,
1308 1309 1310
                     void* output);

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

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

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

  static void Ceil(const ExecutionContext& ctx,
1329 1330 1331 1332
                   const cnnlTensorDescriptor_t input_desc,
                   const void* input,
                   const cnnlTensorDescriptor_t output_desc,
                   void* output);
1333 1334

  static void IsNan(const ExecutionContext& ctx,
1335 1336 1337 1338
                    const cnnlTensorDescriptor_t input_desc,
                    const void* input,
                    const cnnlTensorDescriptor_t output_desc,
                    void* output);
1339 1340

  static void Square(const ExecutionContext& ctx,
1341 1342 1343 1344
                     const cnnlTensorDescriptor_t input_desc,
                     const void* input,
                     const cnnlTensorDescriptor_t output_desc,
                     void* output);
1345 1346 1347

  static void Sqrt(const ExecutionContext& ctx,
                   cnnlComputationPreference_t prefer,
1348 1349 1350 1351
                   const cnnlTensorDescriptor_t input_desc,
                   const void* input,
                   const cnnlTensorDescriptor_t output_desc,
                   void* output);
1352 1353 1354

  static void Rsqrt(const ExecutionContext& ctx,
                    cnnlComputationPreference_t prefer,
1355 1356 1357 1358
                    const cnnlTensorDescriptor_t input_desc,
                    const void* input,
                    const cnnlTensorDescriptor_t output_desc,
                    void* output);
1359 1360 1361

  static void Cos(const ExecutionContext& ctx,
                  cnnlComputationPreference_t prefer,
1362 1363 1364 1365
                  const cnnlTensorDescriptor_t input_desc,
                  const void* input,
                  const cnnlTensorDescriptor_t output_desc,
                  void* output);
1366 1367 1368

  static void Sin(const ExecutionContext& ctx,
                  cnnlComputationPreference_t prefer,
1369 1370 1371 1372
                  const cnnlTensorDescriptor_t input_desc,
                  const void* input,
                  const cnnlTensorDescriptor_t output_desc,
                  void* output);
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382

  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,
1383 1384 1385 1386
                  const cnnlTensorDescriptor_t input_desc,
                  const void* input,
                  const cnnlTensorDescriptor_t output_desc,
                  void* output);
1387 1388

  static void Sign(const ExecutionContext& ctx,
1389 1390 1391 1392
                   const cnnlTensorDescriptor_t input_desc,
                   const void* input,
                   const cnnlTensorDescriptor_t output_desc,
                   void* output);
1393 1394 1395 1396

  static void IsFinite(const ExecutionContext& ctx,
                       const cnnlTensorDescriptor_t input_desc,
                       const void* input,
1397 1398
                       const cnnlTensorDescriptor_t output_desc,
                       void* output);
1399 1400 1401

  static void IsNanInf(const ExecutionContext& ctx,
                       const cnnlTensorDescriptor_t input_desc,
1402 1403
                       const void* input,
                       void* output);
1404 1405 1406

  static void Erf(const ExecutionContext& ctx,
                  cnnlComputationPreference_t prefer,
1407 1408 1409 1410
                  const cnnlTensorDescriptor_t input_desc,
                  const void* input,
                  const cnnlTensorDescriptor_t output_desc,
                  void* output);
1411 1412 1413

  static void Log1p(const ExecutionContext& ctx,
                    cnnlComputationPreference_t prefer,
1414 1415 1416 1417
                    const cnnlTensorDescriptor_t input_desc,
                    const void* input,
                    const cnnlTensorDescriptor_t output_desc,
                    void* output);
1418 1419 1420 1421 1422 1423 1424

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

1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
  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);
1446 1447

  static void CropAndResizeBackwardImage(
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
      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);
1458 1459

  static void CropAndResizeBackwardBoxes(
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469
      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,
1470 1471
      void* output);

1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492
  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);
1493

1494 1495
  static void PoolingIndex(const ExecutionContext& ctx,
                           const cnnlPoolingDescriptor_t pooling_desc,
1496 1497 1498 1499
                           const cnnlTensorDescriptor_t x_desc,
                           const void* x,
                           const cnnlTensorDescriptor_t y_desc,
                           void* y);
1500 1501 1502 1503 1504

  static void SpaceToBatch(const ExecutionContext& ctx,
                           const cnnlTensorDescriptor_t input_desc,
                           const void* input,
                           const cnnlTensorDescriptor_t output_desc,
1505 1506
                           void* output,
                           const int64_t block_shape[]);
1507 1508 1509 1510 1511

  static void SpaceToBatchNd(const ExecutionContext& ctx,
                             const cnnlTensorDescriptor_t input_desc,
                             const void* input,
                             cnnlSpaceBatchNdDescriptor_t param,
1512 1513
                             void* extra_device_input,
                             size_t extra_input_size,
1514 1515 1516
                             const cnnlTensorDescriptor_t output_desc,
                             void* output);

1517 1518 1519 1520 1521 1522 1523 1524
  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);
1525

1526 1527 1528 1529 1530 1531 1532 1533
  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);
1534 1535

  static void QuantizeParam(const ExecutionContext& ctx,
1536 1537
                            const cnnlQuantizeMode_t mode,
                            const int bitwidth,
1538
                            const cnnlTensorDescriptor_t input_desc,
1539 1540 1541
                            const void* input,
                            void* position,
                            void* scale,
1542 1543
                            void* offset);

1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633
  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,
1634 1635 1636
                               const cnnlTensorDescriptor_t x_desc,
                               const void* x,
                               const cnnlTensorDescriptor_t weight_bias_desc,
1637 1638 1639 1640 1641
                               const void* weight,
                               const void* bias,
                               float eps,
                               const cnnlTensorDescriptor_t y_desc,
                               void* y,
1642
                               const cnnlTensorDescriptor_t mean_rstd_desc,
1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660
                               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);
1661

1662
  static void Transpose(const ExecutionContext& ctx,
1663 1664
                        const std::vector<int> perm,
                        const int input_dim,
1665 1666
                        const cnnlTensorDescriptor_t input_desc,
                        const void* input,
1667 1668
                        const cnnlTensorDescriptor_t output_desc,
                        void* output);
1669

1670 1671
  static void TrilTriu(const ExecutionContext& ctx,
                       const int diagonal_k,
1672 1673 1674
                       const bool tri_up_mode,
                       const cnnlTensorDescriptor_t input_desc,
                       const void* input,
1675 1676
                       const cnnlTensorDescriptor_t output_desc,
                       void* output);
1677

1678 1679
  static void MatrixBandPart(const ExecutionContext& ctx,
                             const cnnlTensorDescriptor_t data_desc,
1680 1681 1682 1683
                             const void* input,
                             const int num_lower,
                             const int num_upper,
                             void* output);
1684 1685

  static void NumTrue(const ExecutionContext& ctx,
1686 1687 1688 1689
                      const cnnlTensorDescriptor_t x_desc,
                      const void* x,
                      Tensor index,
                      uint32_t* num_true);
1690 1691

  static void Where(const ExecutionContext& ctx,
1692 1693
                    const cnnlTensorDescriptor_t x_desc,
                    const void* x,
1694 1695 1696
                    const cnnlTensorDescriptor_t num_true_desc,
                    const void* num_true,
                    const bool as_tuple,
1697
                    const cnnlTensorDescriptor_t y_desc,
1698
                    void* y);
1699 1700 1701
  static void Conv2D(const ExecutionContext& ctx,
                     const cnnlConvolutionDescriptor_t conv_desc,
                     const cnnlDataType_t tensor_dtype,
1702 1703 1704 1705 1706 1707
                     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,
1708
                     const void* filter_offset,
1709 1710
                     const cnnlTensorDescriptor_t input_desc,
                     const void* input,
1711
                     const cnnlTensorDescriptor_t filter_desc,
1712 1713 1714 1715
                     const void* filter,
                     const cnnlTensorDescriptor_t bias_desc,
                     const void* bias,
                     const cnnlTensorDescriptor_t output_desc,
1716 1717
                     void* output);

1718 1719 1720 1721 1722 1723 1724 1725
  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);
1726 1727

  static void QuantizeConvBackpropInput(
1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
      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);
1744 1745

  static void ConvBackpropFilter(
1746 1747 1748 1749 1750 1751 1752 1753
      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);
1754 1755

  static void QuantizeConvBackpropFilter(
1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 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 1819 1820
      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);
1821

1822 1823 1824 1825
  static void InTopK(const ExecutionContext& ctx,
                     const cnnlTensorDescriptor_t predictions_desc,
                     const void* predictions,
                     const cnnlTensorDescriptor_t targets_desc,
1826 1827 1828 1829 1830 1831
                     const void* targets,
                     const cnnlTensorDescriptor_t k_desc,
                     const void* k,
                     const int k_int,
                     const cnnlTensorDescriptor_t output_desc,
                     void* output);
1832

1833 1834
  static void ScatterNd(const ExecutionContext& ctx,
                        cnnlScatterNdMode_t mode,
1835 1836 1837 1838
                        const cnnlTensorDescriptor_t indices_desc,
                        const void* indices,
                        const cnnlTensorDescriptor_t updates_desc,
                        const void* updates,
1839 1840
                        const cnnlTensorDescriptor_t input_desc,
                        const void* input,
1841 1842
                        const cnnlTensorDescriptor_t output_desc,
                        void* output);
1843 1844 1845 1846 1847 1848 1849

  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,
1850 1851
                      const cnnlTensorDescriptor_t output_desc,
                      void* output);
1852 1853

  static void QR(const ExecutionContext& ctx,
1854 1855 1856 1857 1858 1859 1860
                 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);
1861 1862 1863 1864 1865 1866

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

1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900
  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);

1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929
  static void RNNForward(const ExecutionContext& ctx,
                         const cnnlRNNDescriptor_t rnn_desc,
                         const int dev_seq_lengths[],
                         const void* weight_param_ptr,
                         size_t weightspace_size,
                         const cnnlSeqDataDescriptor_t x_desc,
                         const void* x,
                         const cnnlSeqDataDescriptor_t y_desc,
                         void* y,
                         const cnnlTensorDescriptor_t h_desc,
                         const void* hx,
                         void* hy,
                         const cnnlTensorDescriptor_t c_desc,
                         const void* cx,
                         void* cy,
                         void* reservespace_ptr);

  static void Mask(const ExecutionContext& ctx,
                   cnnlMaskedOp_t masked_mode,
                   const cnnlTensorDescriptor_t input_desc,
                   const void* input,
                   const cnnlTensorDescriptor_t masked_desc,
                   const void* masked,
                   const cnnlTensorDescriptor_t value_desc,
                   const void* value,
                   const cnnlTensorDescriptor_t output_desc,
                   void* output,
                   uint32_t* number);

1930 1931
  static void Transform(const ExecutionContext& ctx,
                        const void* alpha,
1932 1933 1934
                        const void* beta,
                        const cnnlTensorDescriptor_t input_desc,
                        const void* input,
1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959
                        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 已提交
1960 1961

  static void BceWithLogitsBackward(
1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975
      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);
1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999

  static void RoiAlign(const ExecutionContext& ctx,
                       const int pooled_height,
                       const int pooled_width,
                       const int sampling_ratio,
                       const float spatial_scale,
                       const bool aligned,
                       const cnnlTensorDescriptor_t input_desc,
                       const void* input,
                       const cnnlTensorDescriptor_t boxes_desc,
                       const void* boxes,
                       const cnnlTensorDescriptor_t output_desc,
                       void* output);

  static void RoiAlignBackward(const ExecutionContext& ctx,
                               const int sampling_ratio,
                               const float spatial_scale,
                               const bool aligned,
                               const cnnlTensorDescriptor_t grads_desc,
                               const void* grads,
                               const cnnlTensorDescriptor_t boxes_desc,
                               const void* boxes,
                               const cnnlTensorDescriptor_t grads_image_desc,
                               void* grads_image);
F
fwenguang 已提交
2000 2001
};

Q
qipengh 已提交
2002 2003 2004 2005 2006 2007
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) {
2008
  const int dim_size = perm.size();
Q
qipengh 已提交
2009
  if (need_reshape_or_alloc) {
2010 2011 2012 2013 2014
    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 已提交
2015
    transformed_output->mutable_data<T>(
2016
        framework::DDim(output_shape.data(), dim_size), ctx.GetPlace());
Q
qipengh 已提交
2017
  }
2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028
  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 已提交
2029 2030 2031
                     GetBasePtr(transformed_output));
}

2032
template <typename T>
2033 2034
inline void FillMLUTensorWithHostValue(const ExecutionContext& ctx,
                                       T value,
2035 2036
                                       Tensor* out) {
  MLUCnnlTensorDesc out_desc(*out);
2037 2038
  MLUCnnl::Fill(
      ctx, CNNL_POINTER_MODE_HOST, &value, out_desc.get(), GetBasePtr(out));
2039 2040
}

F
fwenguang 已提交
2041 2042
}  // namespace operators
}  // namespace paddle