mlu_baseop.h 99.1 KB
Newer Older
F
fwenguang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* 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>
C
cifar10 已提交
19
#include <mlu_op.h>
F
fwenguang 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33

#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;
34
using ExecutionContext = framework::ExecutionContext;
F
fwenguang 已提交
35
using DeviceContextPool = platform::DeviceContextPool;
36 37
using MLUDeviceContext = platform::MLUDeviceContext;

38
const std::map<std::string, cnnlReduceOp_t> MLUReduceOpMap = {
39 40 41 42 43 44
    {"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},
45 46 47
    {"reduce_prod", CNNL_REDUCE_MUL},
};

48 49 50 51 52 53 54 55 56 57 58 59 60 61
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}};

62 63 64 65 66 67 68 69 70
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));
}

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
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));
}

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

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

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

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

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

C
cifar10 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
inline mluOpDataType_t ToMluOpDataType(
    const paddle::experimental::DataType& dtype) {
  mluOpDataType_t type = MLUOP_DTYPE_FLOAT;
  switch (dtype) {
    case DataType::FLOAT16:
      type = MLUOP_DTYPE_HALF;
      break;
    case DataType::FLOAT32:
      type = MLUOP_DTYPE_FLOAT;
      break;
    case DataType::FLOAT64:
      type = MLUOP_DTYPE_DOUBLE;
      break;
    case DataType::INT8:
      type = MLUOP_DTYPE_INT8;
      break;
    case DataType::INT16:
      type = MLUOP_DTYPE_INT16;
      break;
    case DataType::INT32:
      type = MLUOP_DTYPE_INT32;
      break;
    case DataType::INT64:
      type = MLUOP_DTYPE_INT64;
      break;
    case DataType::BOOL:
      type = MLUOP_DTYPE_BOOL;
      break;
    case DataType::UINT8:
      type = MLUOP_DTYPE_UINT8;
      break;
    default:
      break;
  }
  return type;
}

inline mluOpDataType_t ToMluOpDataType(
    const paddle::framework::proto::VarType::Type& type) {
  return ToMluOpDataType(framework::TransToPhiDataType(type));
}

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

F
fwenguang 已提交
190 191 192 193 194 195 196 197 198 199
// 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;
}

200
inline static cnnlHandle_t GetHandleFromCTX(const ExecutionContext& ctx) {
201 202 203
  return ctx.template device_context<MLUDeviceContext>().cnnl_handle();
}

C
cifar10 已提交
204 205 206 207
inline static mluOpHandle_t GetMLUOpHandleFromCTX(const ExecutionContext& ctx) {
  return ctx.template device_context<MLUDeviceContext>().mluOp_handle();
}

208 209
inline static const MLUDeviceContext& GetDevCtxFromCTX(
    const ExecutionContext& ctx) {
210 211 212
  return ctx.template device_context<MLUDeviceContext>();
}

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
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},
231
        {{VT::INT32, /*cast to*/ VT::INT16}, CNNL_CAST_INT32_TO_INT16},
232 233 234 235 236 237 238 239 240 241 242
        {{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},
243 244
        {{VT::UINT8, /*cast to*/ VT::INT32}, CNNL_CAST_UINT8_TO_INT32},
        {{VT::INT32, /*cast to*/ VT::INT64}, CNNL_CAST_INT32_TO_INT64},
245
        {{VT::INT64, /*cast to*/ VT::INT32}, CNNL_CAST_INT64_TO_INT32},
246 247 248 249 250 251 252 253 254
        {{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},
255 256 257 258
};

cnnlCastDataType_t GetCastDataType(const VT::Type& src_type,
                                   const VT::Type& dst_type);
259 260 261 262

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

263 264
bool MLUSupportsCast(const VT::Type& src_type, const VT::Type& dst_type);

F
fwenguang 已提交
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
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);

284 285
  MLUCnnlTensorDesc(const int tensor_dim,
                    const int dim_sizes[],
F
fwenguang 已提交
286 287
                    const cnnlDataType_t tensor_dtype);

288 289
  MLUCnnlTensorDesc(const int tensor_dim,
                    const int dim_sizes[],
F
fwenguang 已提交
290 291 292
                    const cnnlDataType_t tensor_dtype,
                    const cnnlTensorLayout_t layout);

293 294 295 296
  MLUCnnlTensorDesc(const int tensor_dim,
                    const int dim_sizes[],
                    const cnnlDataType_t tensor_dtype,
                    int position);
F
fwenguang 已提交
297

298 299
  MLUCnnlTensorDesc(const int tensor_dim,
                    const int64_t dim_sizes[],
F
fwenguang 已提交
300 301
                    const cnnlDataType_t tensor_dtype);

302 303
  MLUCnnlTensorDesc(const int tensor_dim,
                    const int64_t dim_sizes[],
F
fwenguang 已提交
304 305 306
                    const cnnlDataType_t tensor_dtype,
                    const cnnlTensorLayout_t layout);

307 308 309 310
  MLUCnnlTensorDesc(const int tensor_dim,
                    const int64_t dim_sizes[],
                    const cnnlDataType_t tensor_dtype,
                    int position);
F
fwenguang 已提交
311

312 313
  MLUCnnlTensorDesc(const Tensor& tensor,
                    const cnnlTensorLayout_t layout,
F
fwenguang 已提交
314 315
                    const cnnlDataType_t tensor_dtype);

316 317
  explicit MLUCnnlTensorDesc(const Tensor& tensor);

318 319 320 321
  MLUCnnlTensorDesc(const Tensor& tensor,
                    cnnlTensorLayout_t layout,
                    const cnnlDataType_t tensor_dtype,
                    int position);
F
fwenguang 已提交
322

323 324 325 326
  MLUCnnlTensorDesc(const Tensor& tensor,
                    cnnlTensorLayout_t layout,
                    const cnnlDataType_t tensor_dtype,
                    int position,
F
fwenguang 已提交
327 328 329 330 331 332 333 334 335 336
                    float scale);

  ~MLUCnnlTensorDesc();

  const cnnlTensorDescriptor_t get() const { return raw_tensor_desc; }

 private:
  cnnlTensorDescriptor_t raw_tensor_desc = nullptr;
};

C
cifar10 已提交
337 338 339 340 341 342 343 344 345 346 347 348 349 350 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 393 394 395 396 397 398 399 400 401 402 403 404
class MLUOpTensorDesc {
 public:
  MLUOpTensorDesc() {}

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

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

  MLUOpTensorDesc& operator=(MLUOpTensorDesc&& rhs);

  MLUOpTensorDesc(const int tensor_dim,
                  const int dim_sizes[],
                  const mluOpDataType_t tensor_dtype);

  MLUOpTensorDesc(const int tensor_dim,
                  const int dim_sizes[],
                  const mluOpDataType_t tensor_dtype,
                  const mluOpTensorLayout_t layout);

  MLUOpTensorDesc(const int tensor_dim,
                  const int dim_sizes[],
                  const mluOpDataType_t tensor_dtype,
                  int position);

  MLUOpTensorDesc(const int tensor_dim,
                  const int64_t dim_sizes[],
                  const mluOpDataType_t tensor_dtype);

  MLUOpTensorDesc(const int tensor_dim,
                  const int64_t dim_sizes[],
                  const mluOpDataType_t tensor_dtype,
                  const mluOpTensorLayout_t layout);

  MLUOpTensorDesc(const int tensor_dim,
                  const int64_t dim_sizes[],
                  const mluOpDataType_t tensor_dtype,
                  int position);

  MLUOpTensorDesc(const Tensor& tensor,
                  const mluOpTensorLayout_t layout,
                  const mluOpDataType_t tensor_dtype);

  explicit MLUOpTensorDesc(const Tensor& tensor);

  MLUOpTensorDesc(const Tensor& tensor,
                  mluOpTensorLayout_t layout,
                  const mluOpDataType_t tensor_dtype,
                  int position);

  MLUOpTensorDesc(const Tensor& tensor,
                  mluOpTensorLayout_t layout,
                  const mluOpDataType_t tensor_dtype,
                  int position,
                  float scale);

  ~MLUOpTensorDesc();

  const mluOpTensorDescriptor_t get() const { return raw_tensor_desc; }

 private:
  mluOpTensorDescriptor_t raw_tensor_desc = nullptr;
};

F
fwenguang 已提交
405 406 407 408 409
class MLUCnnlActivationDesc {
 public:
  MLUCnnlActivationDesc(const MLUCnnlActivationDesc& desc) = delete;
  MLUCnnlActivationDesc& operator=(const MLUCnnlActivationDesc& desc) = delete;
  MLUCnnlActivationDesc(const cnnlActivationMode_t act_mode, const float ceof);
410 411 412 413
  MLUCnnlActivationDesc(const cnnlActivationMode_t act_mode,
                        const float ceof,
                        const float sliced_dim,
                        const float selu_alpha,
414
                        const float selu_lambda);
F
fwenguang 已提交
415 416 417 418 419 420 421 422

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

 private:
  cnnlActivationDescriptor_t active_desc_ = nullptr;
};

423 424 425 426 427 428 429
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,
430 431 432 433 434 435 436 437 438 439 440
                     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);
441 442 443

  MLUCnnlPoolingDesc(const cnnlPoolingMode_t mode,
                     const cnnlNanPropagation_t maxpooling_nan_opt,
444 445
                     const int tensor_rank,
                     const std::vector<int>& window,
446 447 448 449 450 451 452 453 454 455 456 457 458
                     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 已提交
459
  MLUCnnlRandomGeneratorDesc(const ExecutionContext& ctx, const int seed);
460
  const cnnlRandGenerator_t get() const;
Q
qipengh 已提交
461
  Tensor& get_state();
462 463 464
  ~MLUCnnlRandomGeneratorDesc();

 private:
Q
qipengh 已提交
465
  Tensor mlu_state;
466 467 468
  cnnlRandGenerator_t mlu_generator = nullptr;
};

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

472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
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;

514 515 516 517
  MLUCnnlNMSDesc(const cnnlNmsOutputMode_t mode,
                 const float iou_threshold,
                 const int max_output_size,
                 const float confidence_threshold,
518 519 520 521 522 523 524 525 526 527 528 529
                 const int input_layout);

  const cnnlNmsDescriptor_t get() const;

  ~MLUCnnlNMSDesc();

 private:
  cnnlNmsDescriptor_t nms_desc_ = nullptr;
};

class MLUCnnlConvolutionDesc {
 public:
530 531 532 533 534
  MLUCnnlConvolutionDesc(const int dims,
                         const int pad[],
                         const int stride[],
                         const int dilation[],
                         const int group_count,
535 536
                         const cnnlDataType_t tensor_dtype);

537 538 539 540
  MLUCnnlConvolutionDesc(const int dims,
                         const int64_t pad[],
                         const int64_t stride[],
                         const int64_t dilation[],
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
                         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:
559 560
  MLUCnnlBatchSpaceDesc(uint32_t block_shape[],
                        uint32_t paddings[],
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
                        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;
};

602 603
class MLUCnnlDCNDesc {
 public:
604 605 606 607 608 609
  MLUCnnlDCNDesc(int dimNb,
                 const int* pad,
                 const int* stride,
                 const int* dilation,
                 int deformable_group,
                 int conv_group,
610 611 612 613 614 615 616 617 618
                 int im2col_step);
  const cnnlDCNDescriptor_t get() const;

  ~MLUCnnlDCNDesc();

 private:
  cnnlDCNDescriptor_t dcn_desc_ = nullptr;
};

619 620 621 622 623 624 625 626 627 628 629 630 631 632
class MLUCnnlGridSampleDesc {
 public:
  MLUCnnlGridSampleDesc(const std::string& interp_mode_str,
                        const std::string& padding_mode_str,
                        bool align_corners);

  const cnnlGridSampleDescriptor_t get() const;

  ~MLUCnnlGridSampleDesc();

 private:
  cnnlGridSampleDescriptor_t grid_sample_desc_ = nullptr;
};

633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
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 已提交
717 718
class MLUCnnl {
 public:
719
  static void Active(const ExecutionContext& ctx,
F
fwenguang 已提交
720
                     cnnlActivationDescriptor_t active_desc,
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
                     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[],
743
                     const void* const inputs[],
744 745
                     const cnnlTensorDescriptor_t output_desc,
                     void* output);
746

747 748 749 750
  static void Concat(const MLUDeviceContext& dev_ctx,
                     const int pack_num,
                     const int axis,
                     const cnnlTensorDescriptor_t inputs_desc[],
Z
zn 已提交
751
                     const void* const inputs[],
752 753
                     const cnnlTensorDescriptor_t output_desc,
                     void* output);
Z
zn 已提交
754

755 756 757 758 759 760
  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);
761

762
  static void Clip(const ExecutionContext& ctx,
763 764 765 766 767
                   const cnnlTensorDescriptor_t input_desc,
                   const void* input,
                   const void* min,
                   const void* max,
                   void* y);
768

769 770 771 772 773 774 775 776 777
  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);
778

779 780
  static void Div(const ExecutionContext& ctx,
                  cnnlComputationPreference_t prefer,
781 782 783 784 785 786
                  const cnnlTensorDescriptor_t in0_desc,
                  const void* in0,
                  const cnnlTensorDescriptor_t in1_desc,
                  const void* in1,
                  const cnnlTensorDescriptor_t output_desc,
                  void* output);
787

788
  static void Fill(const ExecutionContext& ctx,
789 790 791 792 793 794 795 796 797 798
                   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,
799 800
                  const cnnlTensorDescriptor_t input_quant_desc,
                  const void* input_quant,
801 802
                  const cnnlTensorDescriptor_t output_desc,
                  void* output);
803 804 805 806 807 808 809 810 811 812 813

  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,
814 815 816 817
                             const void* input,
                             const bool compute_scale,
                             void* position,
                             void* scale,
818 819 820 821
                             const cnnlTensorDescriptor_t ouput_desc,
                             void* output);

  static void SGD(const ExecutionContext& context,
822 823 824 825
                  const cnnlTensorDescriptor_t grad_desc,
                  const void* grad,
                  const void* lr,
                  const cnnlTensorDescriptor_t var_desc,
826 827 828 829 830
                  void* var);

  static void ApplyAdaGrad(const ExecutionContext& ctx,
                           const cnnlTensorDescriptor_t grad_desc,
                           const void* grad,
831 832 833 834 835 836
                           const cnnlTensorDescriptor_t accum_desc,
                           void* accum,
                           const cnnlTensorDescriptor_t var_desc,
                           void* var,
                           const void* lr,
                           const bool update_slots);
837 838 839

  static void ApplyRMSProp(const ExecutionContext& context,
                           const cnnlTensorDescriptor_t grad_desc,
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
                           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);
867 868

  static void ApplyAdam(const ExecutionContext& ctx,
869 870 871 872 873 874
                        const cnnlTensorDescriptor_t var_desc,
                        void* var,
                        const cnnlTensorDescriptor_t m_desc,
                        void* m,
                        const cnnlTensorDescriptor_t v_desc,
                        void* v,
875
                        const cnnlTensorDescriptor_t grad_desc,
876 877 878 879 880 881 882
                        const void* grad,
                        const void* lr,
                        const void* beta1,
                        const void* beta2,
                        const void* beta1_power,
                        const void* beta2_power,
                        const void* epsilon,
883
                        const bool use_nesterov);
884 885 886

  static void ApplyAdaMax(const ExecutionContext& ctx,
                          const cnnlTensorDescriptor_t grad_desc,
887 888 889 890 891 892 893 894 895 896 897
                          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,
898 899 900 901
                          const void* epsilon);

  static void ApplyMomentum(const ExecutionContext& ctx,
                            const cnnlTensorDescriptor_t grad_desc,
902 903 904 905 906
                            const void* grad,
                            const bool use_nesterov,
                            const void* lr,
                            const void* momentum,
                            void* var,
907 908 909 910
                            void* accum);

  static void ApplyKerasMomentum(const ExecutionContext& ctx,
                                 const cnnlTensorDescriptor_t grad_desc,
911 912 913 914 915 916
                                 const void* grad,
                                 const bool use_nesterov,
                                 const void* lr,
                                 const void* momentum,
                                 void* var,
                                 void* accum);
917 918 919

  static void ApplyAdadelta(const ExecutionContext& ctx,
                            const cnnlTensorDescriptor_t grad_desc,
920 921 922 923 924 925
                            const void* diff,
                            const void* lr,
                            const void* rho,
                            const void* epsilon,
                            void* var,
                            void* accum,
926 927 928
                            void* accum_update);

  static void SparseSoftmaxXentWithLogits(
929 930 931 932 933 934 935 936 937 938 939 940 941
      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,
942 943
                            const cnnlDataType_t data_type,
                            const cnnlRandGenerator_t mlu_generator,
944 945
                            void* mlu_state,
                            void* output);
Q
qipengh 已提交
946

947 948 949 950 951 952 953 954 955 956
  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);
957

958 959 960 961 962 963 964 965
  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);
966 967 968 969 970 971 972

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

973 974 975 976 977 978 979 980 981
  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);
982

983 984 985 986 987 988 989 990
  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);
991

992 993
  static void ScatterFunctor(const ExecutionContext& ctx,
                             const cnnlTensorDescriptor_t params_desc,
994
                             void* params,
995 996 997
                             const cnnlTensorDescriptor_t updates_desc,
                             const void* updates,
                             const cnnlTensorDescriptor_t indices_desc,
998 999
                             const void* indices,
                             const int dim,
1000 1001
                             const cnnlScatterMode_t mode = CNNL_SCATTER);

1002 1003 1004 1005 1006 1007
  static void Range(const ExecutionContext& ctx,
                    const void* start,
                    const void* end,
                    const void* step,
                    const cnnlDataType_t output_dtype,
                    void* output);
1008 1009

  static void Round(const ExecutionContext& ctx,
1010 1011 1012 1013
                    const cnnlTensorDescriptor_t input_desc,
                    const void* input,
                    const cnnlTensorDescriptor_t output_desc,
                    void* output);
1014

1015 1016 1017 1018 1019 1020 1021
  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,
1022 1023 1024 1025 1026
                   const cnnlTensorDescriptor_t values_output_desc,
                   void* values_out,
                   const cnnlTensorDescriptor_t indices_output_desc,
                   void* indices_out);

1027 1028 1029 1030
  static void StridedSlice(const ExecutionContext& ctx,
                           const int begin[],
                           const int end[],
                           const int strides[],
1031 1032 1033 1034 1035
                           const cnnlTensorDescriptor_t input_desc,
                           const void* input,
                           const cnnlTensorDescriptor_t output_desc,
                           void* output);

1036 1037 1038
  static void Split(const ExecutionContext& ctx,
                    int split_num,
                    int axis,
1039 1040 1041 1042 1043
                    const cnnlTensorDescriptor_t input_desc,
                    const void* input_ptr,
                    const cnnlTensorDescriptor_t output_descs[],
                    void* output_ptrs[]);

1044 1045 1046
  static void Split(const MLUDeviceContext& dev_ctx,
                    int split_num,
                    int axis,
Z
zn 已提交
1047 1048 1049 1050 1051
                    const cnnlTensorDescriptor_t input_desc,
                    const void* input_ptr,
                    const cnnlTensorDescriptor_t output_descs[],
                    void* output_ptrs[]);

1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
  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);
1062

1063 1064
  static void AddN(const ExecutionContext& ctx,
                   uint32_t input_num,
1065 1066
                   const cnnlTensorDescriptor_t inputs_desc[],
                   const void* inputs[],
1067 1068
                   const cnnlTensorDescriptor_t output_desc,
                   void* output);
1069 1070

  static void Log(const ExecutionContext& ctx,
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
                  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[],
1082 1083 1084 1085 1086
                               const cnnlTensorDescriptor_t input_desc,
                               const void* input,
                               const cnnlTensorDescriptor_t output_desc,
                               void* output);

1087 1088
  static void Logic(const ExecutionContext& ctx,
                    const cnnlLogicOp_t log_method,
1089 1090 1091
                    const cnnlTensorDescriptor_t input1_desc,
                    const void* input1,
                    const cnnlTensorDescriptor_t input2_desc,
1092 1093
                    const void* input2,
                    const cnnlTensorDescriptor_t ouput_desc,
1094 1095
                    void* output);

1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
  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,
1108 1109 1110
                        const void* beta,
                        const cnnlTensorDescriptor_t update_desc,
                        const void* update,
1111 1112
                        const cnnlTensorDescriptor_t param_desc,
                        void* param);
1113

1114 1115
  static void AssignSub(const ExecutionContext& ctx,
                        const void* alpha,
1116 1117 1118
                        const void* beta,
                        const cnnlTensorDescriptor_t update_desc,
                        const void* update,
1119 1120
                        const cnnlTensorDescriptor_t param_desc,
                        void* param);
1121 1122 1123 1124

  static void Assign(const ExecutionContext& ctx,
                     const cnnlTensorDescriptor_t update_desc,
                     const void* update,
1125 1126
                     const cnnlTensorDescriptor_t param_desc,
                     void* param);
1127 1128 1129 1130 1131 1132

  static void GatherNd(const ExecutionContext& ctx,
                       const cnnlTensorDescriptor_t params_desc,
                       const void* params,
                       const cnnlTensorDescriptor_t indices_desc,
                       const void* indices,
1133 1134
                       const cnnlTensorDescriptor_t output_desc,
                       void* output);
1135 1136 1137 1138 1139

  static void BatchToSpace(const ExecutionContext& ctx,
                           const cnnlTensorDescriptor_t input_desc,
                           const void* input,
                           const cnnlTensorDescriptor_t output_desc,
1140 1141
                           void* output,
                           const cnnlSpaceBatchParam_t param);
1142 1143 1144 1145 1146

  static void BatchToSpaceNd(const ExecutionContext& ctx,
                             const cnnlTensorDescriptor_t input_desc,
                             const void* input,
                             cnnlSpaceBatchNdDescriptor_t param,
1147 1148
                             void* extra_device_input,
                             size_t extra_input_size,
1149 1150 1151
                             const cnnlTensorDescriptor_t output_desc,
                             void* output);

1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
  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);
1164

1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
  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,
1176
                     const std::vector<int64_t>& output_shape,
1177 1178 1179 1180 1181 1182
                     cnnlPoolingDescriptor_t pooling_desc,
                     const void* alpha,
                     const cnnlTensorDescriptor_t input_desc,
                     const void* input,
                     const void* beta,
                     const cnnlTensorDescriptor_t output_desc,
1183 1184 1185
                     void* output);

  static void Pad(const ExecutionContext& ctx,
1186 1187 1188 1189 1190 1191 1192 1193 1194
                  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,
1195
                     const bool transpose_b,
1196 1197 1198 1199 1200 1201
                     const cnnlTensorDescriptor_t in0_desc,
                     const void* in0,
                     const cnnlTensorDescriptor_t in1_desc,
                     const void* in1,
                     const cnnlTensorDescriptor_t output_desc,
                     void* output);
1202

1203 1204 1205 1206 1207 1208 1209 1210 1211
  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);
1212

Q
qipengh 已提交
1213
  static void MulAx(const ExecutionContext& ctx,
1214 1215 1216 1217
                    const cnnlTensorDescriptor_t alpha_desc,
                    const void* alpha,
                    const cnnlTensorDescriptor_t output_desc,
                    void* output);
Q
qipengh 已提交
1218

1219 1220
  static void OpTensor(const ExecutionContext& ctx,
                       const cnnlOpTensorDescriptor_t op_tensor_desc,
1221 1222 1223 1224 1225 1226
                       const cnnlTensorDescriptor_t a_desc,
                       const void* a,
                       const cnnlTensorDescriptor_t b_desc,
                       const void* b,
                       const cnnlTensorDescriptor_t output_desc,
                       void* output,
1227 1228 1229 1230
                       const cnnlDataType_t dtype,
                       const float alpha1_float = 1.f,
                       const float alpha2_float = 1.f,
                       const float beta_float = 0.f);
1231

1232 1233
  static void BiasAddGrad(const ExecutionContext& ctx,
                          const int axis,
1234 1235 1236 1237 1238 1239 1240
                          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,
1241 1242 1243 1244 1245 1246 1247
                     const void* indices,
                     const int depth,
                     const void* on_value,
                     const void* off_value,
                     const int axis,
                     cnnlDataType_t output_data_type,
                     void* output);
1248 1249 1250 1251 1252 1253 1254 1255

  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,
1256 1257
                                void* output,
                                void* output_size);
1258 1259

  static void SoftmaxCrossEntropyWithLogits(
1260 1261
      const ExecutionContext& ctx,
      cnnlSoftmaxMode_t mode,
1262
      cnnlComputationPreference_t prefer,
1263 1264 1265 1266 1267 1268 1269 1270
      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);
1271 1272 1273

  static void SoftmaxForward(const ExecutionContext& ctx,
                             cnnlSoftmaxAlgorithm_t algorithm,
1274 1275
                             cnnlSoftmaxMode_t mode,
                             const void* alpha,
1276
                             const cnnlTensorDescriptor_t input_desc,
1277 1278
                             const void* input,
                             const void* beta,
1279 1280 1281
                             const cnnlTensorDescriptor_t output_desc,
                             void* output);

1282 1283 1284 1285 1286 1287 1288 1289 1290
  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);
1291

1292 1293 1294
  static void Softplus(const ExecutionContext& ctx,
                       const cnnlTensorDescriptor_t features_desc,
                       const void* features,
1295 1296
                       const cnnlTensorDescriptor_t output_desc,
                       void* output);
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306

  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,
1307 1308 1309 1310
                        const cnnlTensorDescriptor_t data_desc,
                        const void* y,
                        const void* diff_y,
                        void* output);
1311 1312

  static void SqrtGrad(const ExecutionContext& ctx,
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
                       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);
1354 1355

  static void Tile(const ExecutionContext& ctx,
1356 1357 1358 1359
                   const cnnlTensorDescriptor_t input_desc,
                   const void* input,
                   const cnnlTensorDescriptor_t output_desc,
                   void* output);
1360 1361 1362 1363 1364 1365 1366 1367 1368

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

1369 1370
  static void Reduce(const ExecutionContext& ctx,
                     const bool need_workspace,
1371
                     const cnnlReduceDescriptor_t reduction_desc,
1372 1373 1374 1375 1376 1377 1378 1379
                     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);
1380 1381 1382 1383 1384 1385 1386

  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,
1387 1388
                       const cnnlTensorDescriptor_t output_desc,
                       void* output);
1389 1390 1391 1392 1393 1394

  static void FloorMod(const ExecutionContext& ctx,
                       const cnnlTensorDescriptor_t input1_desc,
                       const void* input1,
                       const cnnlTensorDescriptor_t input2_desc,
                       const void* input2,
1395 1396
                       const cnnlTensorDescriptor_t output_desc,
                       void* output);
1397 1398 1399 1400 1401 1402

  static void Maximum(const ExecutionContext& ctx,
                      const cnnlTensorDescriptor_t input1_desc,
                      const void* input1,
                      const cnnlTensorDescriptor_t input2_desc,
                      const void* input2,
1403 1404
                      const cnnlTensorDescriptor_t output_desc,
                      void* output);
1405 1406 1407 1408 1409 1410

  static void Minimum(const ExecutionContext& ctx,
                      const cnnlTensorDescriptor_t input1_desc,
                      const void* input1,
                      const cnnlTensorDescriptor_t input2_desc,
                      const void* input2,
1411 1412
                      const cnnlTensorDescriptor_t output_desc,
                      void* output);
1413

Q
qipengh 已提交
1414 1415 1416 1417 1418 1419 1420 1421 1422
  static void Pow(const ExecutionContext& ctx,
                  cnnlComputationPreference_t prefer,
                  const cnnlTensorDescriptor_t input1_desc,
                  const void* input1,
                  const cnnlTensorDescriptor_t input2_desc,
                  const void* input2,
                  const cnnlTensorDescriptor_t output_desc,
                  void* output);

1423 1424
  static void PowR(const ExecutionContext& ctx,
                   cnnlComputationPreference_t prefer,
1425 1426 1427 1428 1429 1430
                   const cnnlTensorDescriptor_t input1_desc,
                   const void* input1,
                   const cnnlTensorDescriptor_t input2_desc,
                   const void* input2,
                   const cnnlTensorDescriptor_t output_desc,
                   void* output);
1431 1432 1433 1434 1435 1436 1437

  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,
1438 1439
                       const cnnlTensorDescriptor_t output_desc,
                       void* output);
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449

  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,
1450 1451
                     const cnnlTensorDescriptor_t input_desc,
                     const void* input,
1452 1453 1454
                     void* output);

  static void Abs(const ExecutionContext& ctx,
1455 1456 1457 1458
                  const cnnlTensorDescriptor_t input_desc,
                  const void* input,
                  const cnnlTensorDescriptor_t output_desc,
                  void* output);
1459 1460

  static void Neg(const ExecutionContext& ctx,
1461 1462 1463 1464
                  const cnnlTensorDescriptor_t input_desc,
                  const void* input,
                  const cnnlTensorDescriptor_t output_desc,
                  void* output);
1465 1466

  static void Floor(const ExecutionContext& ctx,
1467 1468 1469 1470
                    const cnnlTensorDescriptor_t input_desc,
                    const void* input,
                    const cnnlTensorDescriptor_t output_desc,
                    void* output);
1471 1472

  static void Ceil(const ExecutionContext& ctx,
1473 1474 1475 1476
                   const cnnlTensorDescriptor_t input_desc,
                   const void* input,
                   const cnnlTensorDescriptor_t output_desc,
                   void* output);
1477 1478

  static void IsNan(const ExecutionContext& ctx,
1479 1480 1481 1482
                    const cnnlTensorDescriptor_t input_desc,
                    const void* input,
                    const cnnlTensorDescriptor_t output_desc,
                    void* output);
1483 1484

  static void Square(const ExecutionContext& ctx,
1485 1486 1487 1488
                     const cnnlTensorDescriptor_t input_desc,
                     const void* input,
                     const cnnlTensorDescriptor_t output_desc,
                     void* output);
1489 1490 1491

  static void Sqrt(const ExecutionContext& ctx,
                   cnnlComputationPreference_t prefer,
1492 1493 1494 1495
                   const cnnlTensorDescriptor_t input_desc,
                   const void* input,
                   const cnnlTensorDescriptor_t output_desc,
                   void* output);
1496 1497 1498

  static void Rsqrt(const ExecutionContext& ctx,
                    cnnlComputationPreference_t prefer,
1499 1500 1501 1502
                    const cnnlTensorDescriptor_t input_desc,
                    const void* input,
                    const cnnlTensorDescriptor_t output_desc,
                    void* output);
1503 1504 1505

  static void Cos(const ExecutionContext& ctx,
                  cnnlComputationPreference_t prefer,
1506 1507 1508 1509
                  const cnnlTensorDescriptor_t input_desc,
                  const void* input,
                  const cnnlTensorDescriptor_t output_desc,
                  void* output);
1510 1511 1512

  static void Sin(const ExecutionContext& ctx,
                  cnnlComputationPreference_t prefer,
1513 1514 1515 1516
                  const cnnlTensorDescriptor_t input_desc,
                  const void* input,
                  const cnnlTensorDescriptor_t output_desc,
                  void* output);
1517 1518 1519 1520 1521 1522 1523 1524 1525 1526

  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,
1527 1528 1529 1530
                  const cnnlTensorDescriptor_t input_desc,
                  const void* input,
                  const cnnlTensorDescriptor_t output_desc,
                  void* output);
1531 1532

  static void Sign(const ExecutionContext& ctx,
1533 1534 1535 1536
                   const cnnlTensorDescriptor_t input_desc,
                   const void* input,
                   const cnnlTensorDescriptor_t output_desc,
                   void* output);
1537

1538 1539 1540 1541 1542 1543 1544 1545 1546
  static void IndexSelect(const ExecutionContext& ctx,
                          const int dim,
                          cnnlTensorDescriptor_t input_desc,
                          const void* input,
                          const cnnlTensorDescriptor_t index_desc,
                          const void* index,
                          const cnnlTensorDescriptor_t output_desc,
                          void* output);

1547 1548 1549
  static void IsFinite(const ExecutionContext& ctx,
                       const cnnlTensorDescriptor_t input_desc,
                       const void* input,
1550 1551
                       const cnnlTensorDescriptor_t output_desc,
                       void* output);
1552 1553 1554

  static void IsNanInf(const ExecutionContext& ctx,
                       const cnnlTensorDescriptor_t input_desc,
1555 1556
                       const void* input,
                       void* output);
1557 1558 1559

  static void Erf(const ExecutionContext& ctx,
                  cnnlComputationPreference_t prefer,
1560 1561 1562 1563
                  const cnnlTensorDescriptor_t input_desc,
                  const void* input,
                  const cnnlTensorDescriptor_t output_desc,
                  void* output);
1564 1565 1566

  static void Log1p(const ExecutionContext& ctx,
                    cnnlComputationPreference_t prefer,
1567 1568 1569 1570
                    const cnnlTensorDescriptor_t input_desc,
                    const void* input,
                    const cnnlTensorDescriptor_t output_desc,
                    void* output);
1571 1572 1573 1574 1575 1576 1577

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

1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598
  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);
1599 1600

  static void CropAndResizeBackwardImage(
1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
      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);
1611 1612

  static void CropAndResizeBackwardBoxes(
1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
      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,
1623 1624
      void* output);

1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645
  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);
1646

1647 1648
  static void PoolingIndex(const ExecutionContext& ctx,
                           const cnnlPoolingDescriptor_t pooling_desc,
1649 1650 1651 1652
                           const cnnlTensorDescriptor_t x_desc,
                           const void* x,
                           const cnnlTensorDescriptor_t y_desc,
                           void* y);
1653 1654 1655 1656 1657

  static void SpaceToBatch(const ExecutionContext& ctx,
                           const cnnlTensorDescriptor_t input_desc,
                           const void* input,
                           const cnnlTensorDescriptor_t output_desc,
1658 1659
                           void* output,
                           const int64_t block_shape[]);
1660 1661 1662 1663 1664

  static void SpaceToBatchNd(const ExecutionContext& ctx,
                             const cnnlTensorDescriptor_t input_desc,
                             const void* input,
                             cnnlSpaceBatchNdDescriptor_t param,
1665 1666
                             void* extra_device_input,
                             size_t extra_input_size,
1667 1668 1669
                             const cnnlTensorDescriptor_t output_desc,
                             void* output);

1670 1671 1672 1673 1674 1675 1676 1677
  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);
1678

1679 1680 1681 1682 1683 1684 1685 1686
  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);
1687 1688

  static void QuantizeParam(const ExecutionContext& ctx,
1689 1690
                            const cnnlQuantizeMode_t mode,
                            const int bitwidth,
1691
                            const cnnlTensorDescriptor_t input_desc,
1692 1693 1694
                            const void* input,
                            void* position,
                            void* scale,
1695 1696
                            void* offset);

1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 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
  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,
1787 1788 1789
                               const cnnlTensorDescriptor_t x_desc,
                               const void* x,
                               const cnnlTensorDescriptor_t weight_bias_desc,
1790 1791 1792 1793 1794
                               const void* weight,
                               const void* bias,
                               float eps,
                               const cnnlTensorDescriptor_t y_desc,
                               void* y,
1795
                               const cnnlTensorDescriptor_t mean_rstd_desc,
1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813
                               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);
1814

1815
  static void Transpose(const ExecutionContext& ctx,
1816 1817
                        const std::vector<int> perm,
                        const int input_dim,
1818 1819
                        const cnnlTensorDescriptor_t input_desc,
                        const void* input,
1820 1821
                        const cnnlTensorDescriptor_t output_desc,
                        void* output);
1822

1823 1824
  static void TrilTriu(const ExecutionContext& ctx,
                       const int diagonal_k,
1825 1826 1827
                       const bool tri_up_mode,
                       const cnnlTensorDescriptor_t input_desc,
                       const void* input,
1828 1829
                       const cnnlTensorDescriptor_t output_desc,
                       void* output);
1830

1831 1832
  static void MatrixBandPart(const ExecutionContext& ctx,
                             const cnnlTensorDescriptor_t data_desc,
1833 1834 1835 1836
                             const void* input,
                             const int num_lower,
                             const int num_upper,
                             void* output);
1837 1838

  static void NumTrue(const ExecutionContext& ctx,
1839 1840
                      const cnnlTensorDescriptor_t x_desc,
                      const void* x,
F
fwenguang 已提交
1841 1842
                      const cnnlTensorDescriptor_t num_true_desc,
                      void* num_true);
1843 1844

  static void Where(const ExecutionContext& ctx,
1845 1846
                    const cnnlTensorDescriptor_t x_desc,
                    const void* x,
1847 1848 1849
                    const cnnlTensorDescriptor_t num_true_desc,
                    const void* num_true,
                    const bool as_tuple,
1850
                    const cnnlTensorDescriptor_t y_desc,
1851
                    void* y);
1852 1853 1854
  static void Conv2D(const ExecutionContext& ctx,
                     const cnnlConvolutionDescriptor_t conv_desc,
                     const cnnlDataType_t tensor_dtype,
1855 1856 1857 1858 1859 1860
                     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,
1861
                     const void* filter_offset,
1862 1863
                     const cnnlTensorDescriptor_t input_desc,
                     const void* input,
1864
                     const cnnlTensorDescriptor_t filter_desc,
1865 1866 1867 1868
                     const void* filter,
                     const cnnlTensorDescriptor_t bias_desc,
                     const void* bias,
                     const cnnlTensorDescriptor_t output_desc,
1869 1870
                     void* output);

1871 1872 1873 1874 1875 1876 1877 1878
  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);
1879 1880

  static void QuantizeConvBackpropInput(
1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896
      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);
1897 1898

  static void ConvBackpropFilter(
1899 1900 1901 1902 1903 1904 1905 1906
      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);
1907 1908

  static void QuantizeConvBackpropFilter(
1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 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 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973
      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);
1974

1975 1976 1977 1978
  static void InTopK(const ExecutionContext& ctx,
                     const cnnlTensorDescriptor_t predictions_desc,
                     const void* predictions,
                     const cnnlTensorDescriptor_t targets_desc,
1979 1980 1981 1982 1983 1984
                     const void* targets,
                     const cnnlTensorDescriptor_t k_desc,
                     const void* k,
                     const int k_int,
                     const cnnlTensorDescriptor_t output_desc,
                     void* output);
1985

1986 1987
  static void ScatterNd(const ExecutionContext& ctx,
                        cnnlScatterNdMode_t mode,
1988 1989 1990 1991
                        const cnnlTensorDescriptor_t indices_desc,
                        const void* indices,
                        const cnnlTensorDescriptor_t updates_desc,
                        const void* updates,
1992 1993
                        const cnnlTensorDescriptor_t input_desc,
                        const void* input,
1994 1995
                        const cnnlTensorDescriptor_t output_desc,
                        void* output);
1996 1997 1998 1999 2000 2001 2002

  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,
2003 2004
                      const cnnlTensorDescriptor_t output_desc,
                      void* output);
2005 2006

  static void QR(const ExecutionContext& ctx,
2007 2008 2009 2010 2011 2012 2013
                 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);
2014 2015 2016 2017 2018 2019

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

2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044
  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);

2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066
  static void SmoothL1LossForward(const ExecutionContext& ctx,
                                  const cnnlTensorDescriptor_t x_desc,
                                  const void* x,
                                  const cnnlTensorDescriptor_t t_desc,
                                  const void* target,
                                  const float beta,
                                  const cnnlSmoothL1LossAlgorithm_t algorithm,
                                  const cnnlTensorDescriptor_t y_desc,
                                  void* y);

  static void SmoothL1LossBackward(const ExecutionContext& ctx,
                                   const cnnlTensorDescriptor_t x_desc,
                                   const void* x,
                                   const cnnlTensorDescriptor_t target_desc,
                                   const void* target,
                                   const cnnlTensorDescriptor_t dy_desc,
                                   const void* dy,
                                   const float beta,
                                   const cnnlSmoothL1LossAlgorithm_t algorithm,
                                   const cnnlTensorDescriptor_t dx_desc,
                                   void* dx);

2067 2068 2069 2070 2071 2072 2073 2074 2075
  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);

2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092
  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);

2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116
  static void RNNBackward(const ExecutionContext& ctx,
                          const cnnlRNNDescriptor_t rnn_desc,
                          cnnlWgradMode_t add_grad,
                          const int dev_seq_lengths[],
                          const void* weight_param_ptr,
                          void* dweight_param_ptr,
                          size_t weightspace_size,
                          const cnnlSeqDataDescriptor_t x_desc,
                          const void* x,
                          void* dx,
                          const cnnlSeqDataDescriptor_t y_desc,
                          const void* y,
                          const void* dy,
                          const cnnlTensorDescriptor_t hx_desc,
                          const void* hx,
                          const void* dhy,
                          void* dhx,
                          const cnnlTensorDescriptor_t cx_desc,
                          const void* cx,
                          const void* dcy,
                          void* dcx,
                          void* reservespace_ptr,
                          size_t reservespace_size);

2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128
  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);

2129 2130
  static void Transform(const ExecutionContext& ctx,
                        const void* alpha,
2131 2132 2133
                        const void* beta,
                        const cnnlTensorDescriptor_t input_desc,
                        const void* input,
2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158
                        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 已提交
2159 2160

  static void BceWithLogitsBackward(
2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174
      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);
2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198

  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);
Q
qipengh 已提交
2199

2200 2201 2202 2203 2204 2205 2206 2207 2208
  static void GridSample(const ExecutionContext& ctx,
                         const cnnlGridSampleDescriptor_t grid_sample_desc,
                         const cnnlTensorDescriptor_t input_desc,
                         const void* input,
                         const cnnlTensorDescriptor_t grid_desc,
                         const void* grid,
                         const cnnlTensorDescriptor_t output_desc,
                         void* output);

Q
qipengh 已提交
2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292
  static void SyncBatchNormStats(const ExecutionContext& ctx,
                                 const cnnlTensorDescriptor_t x_desc,
                                 const void* x,
                                 const float eps,
                                 const cnnlTensorDescriptor_t mean_desc,
                                 void* mean,
                                 const cnnlTensorDescriptor_t invstd_desc,
                                 void* invstd);

  static void SyncBatchNormGatherStatsWithCounts(
      const ExecutionContext& ctx,
      float momentum,
      float eps,
      const cnnlTensorDescriptor_t mean_all_desc,
      const void* mean_all,
      const cnnlTensorDescriptor_t invstd_all_desc,
      const void* invstd_all,
      const cnnlTensorDescriptor_t moving_mean_desc,
      void* moving_mean,
      const cnnlTensorDescriptor_t moving_var_desc,
      void* moving_var,
      const cnnlTensorDescriptor_t count_all_desc,
      const void* count_all,
      const cnnlTensorDescriptor_t mean_desc,
      void* mean,
      const cnnlTensorDescriptor_t invstd_desc,
      void* invstd);

  static void SyncBatchNormElemt(const ExecutionContext& ctx,
                                 const cnnlTensorDescriptor_t x_desc,
                                 const void* x,
                                 const cnnlTensorDescriptor_t mean_desc,
                                 const void* mean,
                                 const cnnlTensorDescriptor_t invstd_desc,
                                 const void* invstd,
                                 const cnnlTensorDescriptor_t weight_desc,
                                 const void* weight,
                                 const cnnlTensorDescriptor_t bias_desc,
                                 const void* bias,
                                 const cnnlTensorDescriptor_t y_desc,
                                 void* y);

  static void SyncBatchnormBackwardReduce(
      const ExecutionContext& ctx,
      const cnnlTensorDescriptor_t desc_dz,
      const void* dz,
      const cnnlTensorDescriptor_t desc_x,
      const void* x,
      const cnnlTensorDescriptor_t desc_mean,
      const void* mean,
      const cnnlTensorDescriptor_t desc_invstd,
      const void* invstd,
      const cnnlTensorDescriptor_t desc_dweight,
      void* dweight,
      const cnnlTensorDescriptor_t desc_dbias,
      void* dbias,
      const cnnlTensorDescriptor_t desc_sum_dy,
      void* sum_dy,
      const cnnlTensorDescriptor_t desc_sum_dy_xmu,
      void* sum_dy_xmu,
      const bool needs_input_grad0,
      const bool needs_input_grad1,
      const bool needs_input_grad2);

  static void SyncBatchNormBackwardElemt(
      const ExecutionContext& ctx,
      const cnnlTensorDescriptor_t diff_y_desc,
      const void* diff_y,
      const cnnlTensorDescriptor_t x_desc,
      const void* x,
      const cnnlTensorDescriptor_t mean_desc,
      const void* mean,
      const cnnlTensorDescriptor_t invstd_desc,
      const void* invstd,
      const cnnlTensorDescriptor_t weight_desc,
      const void* weight,
      const cnnlTensorDescriptor_t sum_dy_desc,
      const void* sum_dy,
      const cnnlTensorDescriptor_t sum_dy_xmu_desc,
      const void* sum_dy_xmu,
      const cnnlTensorDescriptor_t count_desc,
      const void* count,
      const cnnlTensorDescriptor_t diff_x_desc,
      void* diff_x);
F
fwenguang 已提交
2293 2294
};

Q
qipengh 已提交
2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353
const std::map<const std::string, std::pair<std::vector<int>, std::vector<int>>>
    TransPermMap = {
        // trans_mode, (forward_perm, backward_perm)
        {"3D_NCHW2NHWC", {{0, 2, 1}, {0, 2, 1}}},
        {"4D_NCHW2NHWC", {{0, 2, 3, 1}, {0, 3, 1, 2}}},
        {"5D_NCHWD2NDHWC", {{0, 4, 2, 3, 1}, {0, 4, 2, 3, 1}}},
        {"5D_NHWDC2NDHWC", {{0, 3, 1, 2, 4}, {0, 2, 3, 4, 1}}}};

inline void SetMLUTransposePerm(const framework::DDim& dims,
                                const DataLayout& data_layout,
                                std::vector<int>* forward_perm,
                                std::vector<int>* backward_perm,
                                std::vector<int>* out_shape) {
  const int dim_size = dims.size();
  PADDLE_ENFORCE_EQ((dim_size >= 3) && (dim_size <= 5),
                    true,
                    platform::errors::InvalidArgument(
                        "MLUTransposePerm func only support (dim_size >= 3) && "
                        "(dim_size <= 5), but now dim_size is %d.",
                        dim_size));

  PADDLE_ENFORCE_EQ(
      (data_layout == DataLayout::kNCHW) || (data_layout == DataLayout::kNHWC),
      true,
      platform::errors::InvalidArgument(
          "MLUTransposePerm func only support DataLayout: kNCHW or kNHWC, but "
          "now data_layout is %s.",
          data_layout));

  // case 1: NCHW of Paddle != NHWC of MLU when dims==3,4
  // case 2: NHWDC and NCHWD of Paddle != NDHWC of MLU when dims==5
  std::string map_key = "";
  if (data_layout == DataLayout::kNCHW) {
    switch (dim_size) {
      case 3:
        map_key = "3D_NCHW2NHWC";
        break;
      case 4:
        map_key = "4D_NCHW2NHWC";
        break;
      case 5:
        map_key = "5D_NCHWD2NDHWC";
        break;
    }
  } else if (data_layout == DataLayout::kNHWC && dim_size == 5) {
    map_key = "5D_NHWDC2NDHWC";
  }
  assert(map_key != "");
  forward_perm->assign(TransPermMap.at(map_key).first.begin(),
                       TransPermMap.at(map_key).first.end());
  backward_perm->assign(TransPermMap.at(map_key).second.begin(),
                        TransPermMap.at(map_key).second.end());

  auto in_dims = phi::vectorize(dims);
  for (size_t i = 0; i < in_dims.size(); i++) {
    out_shape->push_back(in_dims[forward_perm->at(i)]);
  }
}

Q
qipengh 已提交
2354 2355 2356 2357 2358 2359
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) {
2360
  const int dim_size = perm.size();
Q
qipengh 已提交
2361
  if (need_reshape_or_alloc) {
2362 2363 2364 2365 2366
    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 已提交
2367
    transformed_output->mutable_data<T>(
2368
        framework::DDim(output_shape.data(), dim_size), ctx.GetPlace());
Q
qipengh 已提交
2369
  }
2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380
  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 已提交
2381 2382 2383
                     GetBasePtr(transformed_output));
}

2384
template <typename T>
2385 2386
inline void FillMLUTensorWithHostValue(const ExecutionContext& ctx,
                                       T value,
2387 2388
                                       Tensor* out) {
  MLUCnnlTensorDesc out_desc(*out);
2389 2390
  MLUCnnl::Fill(
      ctx, CNNL_POINTER_MODE_HOST, &value, out_desc.get(), GetBasePtr(out));
2391 2392
}

F
fwenguang 已提交
2393 2394
}  // namespace operators
}  // namespace paddle