mlu_baseop.h 54.0 KB
Newer Older
F
fwenguang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

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

#include <string>
#include <vector>

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

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;
using DataLayout = framework::DataLayout;
33
using ExecutionContext = framework::ExecutionContext;
F
fwenguang 已提交
34
using DeviceContextPool = platform::DeviceContextPool;
35 36 37 38 39 40 41 42 43 44 45 46
using MLUDeviceContext = platform::MLUDeviceContext;

enum MLULogicMethod {
  CNNL_LOGIC_OP_EQ = 0,
  CNNL_LOGIC_OP_NE = 1,
  CNNL_LOGIC_OP_GT = 2,
  CNNL_LOGIC_OP_GE = 3,
  CNNL_LOGIC_OP_LT = 4,
  CNNL_LOGIC_OP_LE = 5,
  CNNL_LOGIC_OP_AND = 6,
  CNNL_LOGIC_OP_OR = 7,
};
F
fwenguang 已提交
47

48 49 50 51
inline const void* GetBasePtr(const Tensor* t) { return t->data(); }

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

52 53
inline cnnlDataType_t ToCnnlDataType(
    const paddle::experimental::DataType& dtype) {
F
fwenguang 已提交
54
  cnnlDataType_t type = CNNL_DTYPE_FLOAT;
55 56
  switch (dtype) {
    case DataType::FLOAT16:
F
fwenguang 已提交
57 58
      type = CNNL_DTYPE_HALF;
      break;
59
    case DataType::FLOAT32:
F
fwenguang 已提交
60 61
      type = CNNL_DTYPE_FLOAT;
      break;
62
    case DataType::INT8:
F
fwenguang 已提交
63 64
      type = CNNL_DTYPE_INT8;
      break;
65
    case DataType::INT16:
66 67
      type = CNNL_DTYPE_INT16;
      break;
68
    case DataType::INT32:
F
fwenguang 已提交
69 70
      type = CNNL_DTYPE_INT32;
      break;
71
    case DataType::INT64:
F
fwenguang 已提交
72 73
      type = CNNL_DTYPE_INT64;
      break;
74
    case DataType::BOOL:
F
fwenguang 已提交
75 76
      type = CNNL_DTYPE_BOOL;
      break;
77
    case DataType::UINT8:
78 79
      type = CNNL_DTYPE_UINT8;
      break;
F
fwenguang 已提交
80 81 82 83 84 85
    default:
      break;
  }
  return type;
}

86 87 88 89 90 91 92 93 94 95 96
inline cnnlDataType_t ToCnnlDataType(
    const paddle::framework::proto::VarType::Type& type) {
  return ToCnnlDataType(framework::TransToPtenDataType(type));
}

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

F
fwenguang 已提交
97 98 99 100 101 102 103 104 105 106
// 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;
}

107
inline static cnnlHandle_t GetHandleFromCTX(const ExecutionContext& ctx) {
108 109 110
  return ctx.template device_context<MLUDeviceContext>().cnnl_handle();
}

111 112
inline static const MLUDeviceContext& GetDevCtxFromCTX(
    const ExecutionContext& ctx) {
113 114 115
  return ctx.template device_context<MLUDeviceContext>();
}

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
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::INT64}, CNNL_CAST_INT32_TO_INT64},
        {{VT::INT32, /*cast to*/ VT::INT16}, CNNL_CAST_INT32_TO_INT16},
        {{VT::INT32, /*cast to*/ VT::INT8}, CNNL_CAST_INT32_TO_INT8},
        {{VT::INT32, /*cast to*/ VT::BOOL}, CNNL_CAST_INT32_TO_BOOL},
        {{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::UINT8, /*cast to*/ VT::INT64}, CNNL_CAST_UINT8_TO_INT64},
        {{VT::UINT8, /*cast to*/ VT::INT32}, CNNL_CAST_UINT8_TO_INT32},
        {{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},
        {{VT::INT64, /*cast to*/ VT::INT32}, CNNL_CAST_INT64_TO_INT32},
};

cnnlCastDataType_t GetCastDataType(const VT::Type& src_type,
                                   const VT::Type& dst_type);
bool MLUSupportsCast(const VT::Type& src_type, const VT::Type& dst_type);

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

  MLUCnnlTensorDesc(const int tensor_dim, const int dim_sizes[],
                    const cnnlDataType_t tensor_dtype);

  MLUCnnlTensorDesc(const int tensor_dim, const int dim_sizes[],
                    const cnnlDataType_t tensor_dtype,
                    const cnnlTensorLayout_t layout);

  MLUCnnlTensorDesc(const int tensor_dim, const int dim_sizes[],
                    const cnnlDataType_t tensor_dtype, int position);

  MLUCnnlTensorDesc(const int tensor_dim, const int64_t dim_sizes[],
                    const cnnlDataType_t tensor_dtype);

  MLUCnnlTensorDesc(const int tensor_dim, const int64_t dim_sizes[],
                    const cnnlDataType_t tensor_dtype,
                    const cnnlTensorLayout_t layout);

  MLUCnnlTensorDesc(const int tensor_dim, const int64_t dim_sizes[],
                    const cnnlDataType_t tensor_dtype, int position);

  MLUCnnlTensorDesc(const Tensor& tensor, const cnnlTensorLayout_t layout,
                    const cnnlDataType_t tensor_dtype);

199 200
  explicit MLUCnnlTensorDesc(const Tensor& tensor);

F
fwenguang 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
  MLUCnnlTensorDesc(const Tensor& tensor, cnnlTensorLayout_t layout,
                    const cnnlDataType_t tensor_dtype, int position);

  MLUCnnlTensorDesc(const Tensor& tensor, cnnlTensorLayout_t layout,
                    const cnnlDataType_t tensor_dtype, int position,
                    float scale);

  ~MLUCnnlTensorDesc();

  const cnnlTensorDescriptor_t get() const { return raw_tensor_desc; }

 private:
  cnnlTensorDescriptor_t raw_tensor_desc = nullptr;
};

class MLUCnnlActivationDesc {
 public:
  MLUCnnlActivationDesc(const MLUCnnlActivationDesc& desc) = delete;
  MLUCnnlActivationDesc& operator=(const MLUCnnlActivationDesc& desc) = delete;
  MLUCnnlActivationDesc(const cnnlActivationMode_t act_mode, const float ceof);

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

 private:
  cnnlActivationDescriptor_t active_desc_ = nullptr;
};

229 230 231 232 233 234 235 236 237
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,
                     int window_rows, int window_cols, int64_t pad_up,
                     int64_t pad_down, int64_t pad_left, int64_t pad_right,
238 239
                     int row_stride, int col_stride, int row_dilation,
                     int col_dilation, bool ceil_mode);
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 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

  MLUCnnlPoolingDesc(const cnnlPoolingMode_t mode,
                     const cnnlNanPropagation_t maxpooling_nan_opt,
                     const int tensor_rank, const std::vector<int>& window,
                     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:
  MLUCnnlRandomGeneratorDesc(const bool is_mlu200, const int seed);
  const cnnlRandGenerator_t get() const;
  ~MLUCnnlRandomGeneratorDesc();

 private:
  cnnlRandGenerator_t mlu_generator = nullptr;
};

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;

  MLUCnnlNMSDesc(const cnnlNmsOutputMode_t mode, const float iou_threshold,
                 const int max_output_size, const float confidence_threshold,
                 const int input_layout);

  const cnnlNmsDescriptor_t get() const;

  ~MLUCnnlNMSDesc();

 private:
  cnnlNmsDescriptor_t nms_desc_ = nullptr;
};

class MLUCnnlConvolutionDesc {
 public:
  MLUCnnlConvolutionDesc(const int dims, const int pad[], const int stride[],
                         const int dilation[], const int group_count,
                         const cnnlDataType_t tensor_dtype);

  MLUCnnlConvolutionDesc(const int dims, const int64_t pad[],
                         const int64_t stride[], const int64_t dilation[],
                         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:
  MLUCnnlBatchSpaceDesc(uint32_t block_shape[], uint32_t paddings[],
                        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;
};

F
fwenguang 已提交
387 388
class MLUCnnl {
 public:
389
  static void Active(const ExecutionContext& ctx,
F
fwenguang 已提交
390 391 392 393
                     cnnlActivationDescriptor_t active_desc,
                     const cnnlTensorDescriptor_t input_desc, const void* input,
                     const cnnlTensorDescriptor_t output_desc, void* output);

394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 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 514
  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[],
                     const void* const inputs[],
                     const cnnlTensorDescriptor_t output_desc, void* output);

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

  static void Div(const ExecutionContext& ctx,
                  cnnlComputationPreference_t prefer,
                  const cnnlTensorDescriptor_t in0_desc, const void* in0,
                  const cnnlTensorDescriptor_t in1_desc, const void* in1,
                  const cnnlTensorDescriptor_t output_desc, void* output);

  static void Fill(const ExecutionContext& ctx, float value,
                   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,
                  const cnnlTensorDescriptor_t input_quant_desc,
                  const void* input_quant,
                  const cnnlTensorDescriptor_t output_desc, void* output);

  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,
                             const void* input, const bool compute_scale,
                             void* position, void* scale,
                             const cnnlTensorDescriptor_t ouput_desc,
                             void* output);

  static void SGD(const ExecutionContext& context,
                  const cnnlTensorDescriptor_t grad_desc, const void* grad,
                  const void* lr, const cnnlTensorDescriptor_t var_desc,
                  void* var);

  static void ApplyAdaGrad(const ExecutionContext& ctx,
                           const cnnlTensorDescriptor_t grad_desc,
                           const void* grad,
                           const cnnlTensorDescriptor_t accum_desc, void* accum,
                           const cnnlTensorDescriptor_t var_desc, void* var,
                           const void* lr, const bool update_slots);

  static void ApplyRMSProp(const ExecutionContext& context,
                           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 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);

  static void ApplyAdam(const ExecutionContext& ctx,
                        const cnnlTensorDescriptor_t grad_desc,
                        const void* grad, const void* lr, const void* beta1,
                        const void* beta2, const void* beta1_power,
                        const void* beta2_power, const void* epsilon,
                        const bool use_nesterov,
                        const cnnlTensorDescriptor_t var_desc, void* var,
                        const cnnlTensorDescriptor_t m_desc, void* m,
                        const cnnlTensorDescriptor_t v_desc, void* v);

  static void ApplyAdaMax(const ExecutionContext& ctx,
                          const cnnlTensorDescriptor_t grad_desc,
                          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,
                          const void* epsilon);

  static void ApplyMomentum(const ExecutionContext& ctx,
                            const cnnlTensorDescriptor_t grad_desc,
                            const void* grad, const bool use_nesterov,
                            const void* lr, const void* momentum, void* var,
                            void* accum);

  static void ApplyKerasMomentum(const ExecutionContext& ctx,
                                 const cnnlTensorDescriptor_t grad_desc,
                                 const void* grad, const bool use_nesterov,
                                 const void* lr, const void* momentum,
                                 void* var, void* accum);

  static void ApplyAdadelta(const ExecutionContext& ctx,
                            const cnnlTensorDescriptor_t grad_desc,
                            const void* diff, const void* lr, const void* rho,
                            const void* epsilon, void* var, void* accum,
                            void* accum_update);

  static void SparseSoftmaxXentWithLogits(
      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,
                            const cnnlDataType_t data_type,
                            const cnnlRandGenerator_t mlu_generator,
J
joeqiao12 已提交
515
                            const float min, const float max, void* output);
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645

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

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

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

  static void ScatterFunctor(
      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);

  static void Range(const ExecutionContext& ctx, const void* start,
                    const void* end, const void* step,
                    const cnnlDataType_t output_dtype, void* output);

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

  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,
                   const cnnlTensorDescriptor_t values_output_desc,
                   void* values_out,
                   const cnnlTensorDescriptor_t indices_output_desc,
                   void* indices_out);

  static void StridedSlice(const ExecutionContext& ctx, const int begin[],
                           const int end[], const int strides[],
                           const cnnlTensorDescriptor_t input_desc,
                           const void* input,
                           const cnnlTensorDescriptor_t output_desc,
                           void* output);

  static void Split(const ExecutionContext& ctx, int split_num, int axis,
                    const cnnlTensorDescriptor_t input_desc,
                    const void* input_ptr,
                    const cnnlTensorDescriptor_t output_descs[],
                    void* output_ptrs[]);

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

  static void AddN(const ExecutionContext& ctx, uint32_t input_num,
                   const cnnlTensorDescriptor_t inputs_desc[],
                   const void* inputs[],
                   const cnnlTensorDescriptor_t output_desc, void* output);

  static void Log(const ExecutionContext& ctx,
                  cnnlComputationPreference_t prefer,
                  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[],
                               const cnnlTensorDescriptor_t input_desc,
                               const void* input,
                               const cnnlTensorDescriptor_t output_desc,
                               void* output);

  static void Logic(const ExecutionContext& ctx,
                    const MLULogicMethod log_method,
                    const cnnlTensorDescriptor_t input1_desc,
                    const void* input1,
                    const cnnlTensorDescriptor_t input2_desc,
                    const void* input2, const cnnlTensorDescriptor_t ouput_desc,
                    void* output);

  static void Select(const ExecutionContext& ctx,
                     const cnnlTensorDescriptor_t then_desc, const void* p_then,
                     const cnnlTensorDescriptor_t else_desc, const void* p_else,
                     const cnnlTensorDescriptor_t output_desc, void* output,
                     const bool* condition, const int condition_size);

  static void AssignAdd(const ExecutionContext& ctx, const void* alpha,
                        const void* beta,
                        const cnnlTensorDescriptor_t update_desc,
                        const void* update,
                        const cnnlTensorDescriptor_t param_desc, void* param);

  static void AssignSub(const ExecutionContext& ctx, const void* alpha,
                        const void* beta,
                        const cnnlTensorDescriptor_t update_desc,
                        const void* update,
                        const cnnlTensorDescriptor_t param_desc, void* param);

  static void Assign(const ExecutionContext& ctx,
                     const cnnlTensorDescriptor_t update_desc,
                     const void* update,
                     const cnnlTensorDescriptor_t param_desc, void* param);

  static void GatherNd(const ExecutionContext& ctx,
                       const cnnlTensorDescriptor_t params_desc,
                       const void* params,
                       const cnnlTensorDescriptor_t indices_desc,
                       const void* indices,
                       const cnnlTensorDescriptor_t output_desc, void* output);

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

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

  static void PoolingForward(
      const ExecutionContext& ctx, cnnlPoolingMode_t pool_mode,
646 647 648
      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,
649 650
      const cnnlTensorDescriptor_t output_desc, void* output);

651 652 653 654 655 656
  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);

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
  static void Pool3D(const ExecutionContext& ctx, cnnlPoolingMode_t pool_mode,
                     const std::vector<int64_t>& output_shape,
                     cnnlPoolingDescriptor_t pooling_desc, const void* alpha,
                     const cnnlTensorDescriptor_t input_desc, const void* input,
                     const void* beta, const cnnlTensorDescriptor_t output_desc,
                     void* output);

  static void Pad(const ExecutionContext& ctx,
                  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,
                     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);

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

  static void OpTensor(const ExecutionContext& ctx,
                       const cnnlOpTensorDescriptor_t op_tensor_desc,
                       const cnnlTensorDescriptor_t a_desc, const void* a,
                       const cnnlTensorDescriptor_t b_desc, const void* b,
                       const cnnlTensorDescriptor_t output_desc, void* output,
686 687 688 689
                       const cnnlDataType_t dtype,
                       const float alpha1_float = 1.f,
                       const float alpha2_float = 1.f,
                       const float beta_float = 0.f);
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 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 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 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965

  static void BiasAddGrad(const ExecutionContext& ctx, const int axis,
                          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,
                     const void* indices, const int depth, const void* on_value,
                     const void* off_value, const int axis,
                     cnnlDataType_t output_data_type, void* output);

  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,
                                void* output, void* output_size);

  static void SoftmaxCrossEntropyWithLogits(
      const ExecutionContext& ctx, cnnlSoftmaxMode_t mode,
      cnnlComputationPreference_t prefer,
      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);

  static void SoftmaxForward(const ExecutionContext& ctx,
                             cnnlSoftmaxAlgorithm_t algorithm,
                             cnnlSoftmaxMode_t mode, const void* alpha,
                             const cnnlTensorDescriptor_t input_desc,
                             const void* input, const void* beta,
                             const cnnlTensorDescriptor_t output_desc,
                             void* output);

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

  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,
                        const cnnlTensorDescriptor_t data_desc, const void* y,
                        const void* diff_y, void* output);

  static void SqrtGrad(const ExecutionContext& ctx,
                       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);

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

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

  static void Reduce(const ExecutionContext& ctx, const bool need_workspace,
                     const cnnlReduceDescriptor_t reduction_desc,
                     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);

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

  static void FloorMod(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 Maximum(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 Minimum(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 PowR(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);

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

  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,
                     const cnnlTensorDescriptor_t input_desc, const void* input,
                     void* output);

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

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

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

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

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

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

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

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

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

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

  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,
                  const cnnlTensorDescriptor_t input_desc, const void* input,
                  const cnnlTensorDescriptor_t output_desc, void* output);

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

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

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

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

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

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

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

  static void CropAndResizeBackwardImage(
      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);

  static void CropAndResizeBackwardBoxes(
      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,
      void* output);

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

966 967 968 969 970 971
  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);

972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
  static void PoolingIndex(const ExecutionContext& ctx,
                           const cnnlPoolingDescriptor_t pooling_desc,
                           const cnnlTensorDescriptor_t x_desc, const void* x,
                           const cnnlTensorDescriptor_t y_desc, void* y);

  static void SpaceToBatch(const ExecutionContext& ctx,
                           const cnnlTensorDescriptor_t input_desc,
                           const void* input,
                           const cnnlTensorDescriptor_t output_desc,
                           void* output, const int64_t block_shape[]);

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

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

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

  static void QuantizeParam(const ExecutionContext& ctx,
                            const cnnlQuantizeMode_t mode, const int bitwidth,
                            const cnnlTensorDescriptor_t input_desc,
                            const void* input, void* position, void* scale,
                            void* offset);

  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 Transpose(const ExecutionContext& ctx,
                        const std::vector<int> perm, const int input_dim,
                        const cnnlTensorDescriptor_t input_desc,
                        const void* input,
                        const cnnlTensorDescriptor_t output_desc, void* output);

  static void MatrixBandPart(const ExecutionContext& ctx,
                             const cnnlTensorDescriptor_t data_desc,
                             const void* input, const int num_lower,
                             const int num_upper, void* output);

  static void NumTrue(const ExecutionContext& ctx,
                      const cnnlTensorDescriptor_t x_desc, const void* x,
                      Tensor index, uint32_t* num_true);

  static void Where(const ExecutionContext& ctx,
                    const cnnlTensorDescriptor_t x_desc, const void* x,
                    const uint32_t* strides, const uint32_t* index,
                    const cnnlTensorDescriptor_t y_desc, int* y,
                    const bool as_tuple);

  static void Conv2D(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* filter_position, const void* filter_scale,
                     const void* filter_offset,
                     const cnnlTensorDescriptor_t input_desc, const void* input,
                     const cnnlTensorDescriptor_t filter_desc,
                     const void* filter, const cnnlTensorDescriptor_t bias_desc,
                     const void* bias, const cnnlTensorDescriptor_t output_desc,
                     void* output);

  static void ConvBackpropInput(
      const ExecutionContext& ctx, const cnnlConvolutionDescriptor_t conv_desc,
      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);

  static void QuantizeConvBackpropInput(
      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);

  static void ConvBackpropFilter(
      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);

  static void QuantizeConvBackpropFilter(
      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 InTopK(const ExecutionContext& ctx,
                     const cnnlTensorDescriptor_t predictions_desc,
                     const void* predictions,
                     const cnnlTensorDescriptor_t targets_desc,
                     const void* targets, const cnnlTensorDescriptor_t k_desc,
                     const void* k, const int k_int,
                     const cnnlTensorDescriptor_t output_desc, void* output);

  static void ScatterNd(const ExecutionContext& ctx,
                        const cnnlTensorDescriptor_t indices_desc,
                        const void* indices,
                        const cnnlTensorDescriptor_t updates_desc,
                        const void* updates,
                        const cnnlTensorDescriptor_t output_desc, void* output);

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

  static void QR(const ExecutionContext& ctx,
                 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);

  static void Reciprocal(const ExecutionContext& ctx,
                         const cnnlTensorDescriptor_t input_desc,
                         const void* input,
                         const cnnlTensorDescriptor_t output_desc,
                         void* output);
F
fwenguang 已提交
1152 1153
};

Q
qipengh 已提交
1154 1155 1156 1157 1158 1159
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) {
1160
  auto in_dims_vec = pten::vectorize(transformed_input->dims());
Q
qipengh 已提交
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
  if (need_reshape_or_alloc) {
    transformed_output->mutable_data<T>(
        {in_dims_vec[perm[0]], in_dims_vec[perm[1]], in_dims_vec[perm[2]],
         in_dims_vec[perm[3]]},
        ctx.GetPlace());
  }
  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, in_dims_vec.size(), trans_in_desc.get(),
                     GetBasePtr(transformed_input), trans_out_desc.get(),
                     GetBasePtr(transformed_output));
}

F
fwenguang 已提交
1177 1178
}  // namespace operators
}  // namespace paddle