hl_base.h 6.3 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Z
zhangjinchao01 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

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. */

F
fengjiayi 已提交
15
#pragma once
Z
zhangjinchao01 已提交
16 17

#include <cstddef>
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

#ifdef PADDLE_TYPE_DOUBLE
#define HL_FLOAT_MAX 3.40282347e+38F
#define HL_FLOAT_MIN 1.17549435e-38F
using real = double;
#else
#define HL_FLOAT_MAX 1.7976931348623157e+308
#define HL_FLOAT_MIN 2.2250738585072014e-308
using real = float;
#endif

/**
 * The maximum input value for exp, used to avoid overflow problem.
 * currently only used for tanh function.
 */
#define EXP_MAX_INPUT 40.0

/**
 * @brief DIVUP(x, y) is similar to ceil(x / y).
 * @note  For CUDA, DIVUP will be used to specify
 *        the size of blockDim.
 */
#ifndef DIVUP
#define DIVUP(x, y) (((x) + (y)-1) / (y))
#endif
Z
zhangjinchao01 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56

/**
 * HPPL is an internal high performance parallel computing library
 * for high-level neural network routines, which can support many
 * heterogeneous compute architectures, such as GPU, FPGA, etc.
 */

/**
 * @brief   HPPL CUDA Stream.
 *
 * @note    Each thread can use HPPL_STREAM_* after calling hl_init.
 *          HPPL_STREAM_DEFAULT is HPPL default stream.
 */
typedef enum {
57 58 59 60 61 62 63 64 65 66
  HPPL_STREAM_DEFAULT = 0, /* Thread Default Stream*/
  HPPL_STREAM_1 = 1,
  HPPL_STREAM_2 = 2,
  HPPL_STREAM_3 = 3,
  HPPL_STREAM_4 = 4,
  HPPL_THREAD_STREAM_1 = 5,
  HPPL_THREAD_STREAM_2 = 6,
  HPPL_THREAD_STREAM_3 = 7,
  HPPL_THREAD_STREAM_4 = 8,
  HPPL_STREAM_END
Z
zhangjinchao01 已提交
67 68 69 70 71 72
} hl_stream_t;

/**
 * @brief HPPL activation mode.
 */
typedef enum {
73 74 75 76 77
  HL_ACTIVATION_SIGMOID = 0,
  HL_ACTIVATION_RELU = 1,
  HL_ACTIVATION_TANH = 2,
  HL_ACTIVATION_LINEAR = 3,
  HL_ACTIVATION_END
Z
zhangjinchao01 已提交
78 79 80 81 82 83
} hl_activation_mode_t;

/**
 * @brief Transpose type.
 */
typedef enum {
84 85 86
  HPPL_OP_N = 0, /* transpose */
  HPPL_OP_T = 1, /* non transpose */
  HPPL_OP_END
Z
zhangjinchao01 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
} hl_trans_op_t;

/**
 * @brief Lstm value.
 *
 * @param  gateValue         input value.
 * @param  prevStateValue    previous state value.
 * @param  stateValue        state value.
 * @param  stateActiveValue  state active value.
 * @param  outputValue       output value.
 */
typedef struct {
  real *gateValue;
  real *prevStateValue;
  real *stateValue;
  real *stateActiveValue;
  real *outputValue;
  real *checkIg;
  real *checkFg;
  real *checkOg;
} hl_lstm_value;

/**
 * @brief Lstm gradient.
 *
 * @param  gateGrad          input gradient.
 * @param  prevStateGrad     previous state gradient.
 * @param  stateGrad         state gradient.
 * @param  stateActiveGrad   state active gradient.
 * @param  outputGrad        output gradient.
 */
typedef struct {
  real *gateGrad;
  real *prevStateGrad;
  real *stateGrad;
  real *stateActiveGrad;
  real *outputGrad;
  real *checkIgGrad;
  real *checkFgGrad;
  real *checkOgGrad;
} hl_lstm_grad;

/**
 * @brief Gru value.
 *
 * @param  gateWeight           gate weight (updateGate + resetGate).
 * @param  stateWeight          frame state weight.
 * @param  gateValue            gate value results.
 * @param  resetOutputValue     resetOutput value.
 * @param  outputValue          output value.
 * @param  prevOutValue         previous output value.
 *
 */
typedef struct {
  real *gateWeight;
  real *stateWeight;
  real *gateValue;
  real *resetOutputValue;
  real *outputValue;
  real *prevOutValue;
} hl_gru_value;

/**
 * @brief Gru gradient.
 *
 * @param  gateWeightGrad       gate weight gradient.
 * @param  stateWeightGrad      frame state weight gradient.
 * @param  gateGrad             gate gradient results.
 * @param  resetOutputGrad      resetOutput gradient.
 * @param  outputGrad           output gradient.
 * @param  prevOutGrad          previous output gradient.
 */
typedef struct {
  real *gateWeightGrad;
  real *stateWeightGrad;
  real *gateGrad;
  real *resetOutputGrad;
  real *outputGrad;
  real *prevOutGrad;
} hl_gru_grad;

/**
 * @brief  Sparse matrix value type.
 */
typedef enum {
172 173 174
  HL_NO_VALUE = 0, /* matrix values only 0 or 1 */
  HL_FLOAT_VALUE = 1,
  HL_VALUE_END
Z
zhangjinchao01 已提交
175 176 177 178 179 180
} hl_matrix_value_t;

/**
 * @brief  HPPL matrix format.
 */
typedef enum {
181 182 183
  HL_SPARSE_CSR = 0,
  HL_SPARSE_CSC = 1,
  HL_SPARSE_END
Z
zhangjinchao01 已提交
184 185
} hl_matrix_format_t;

186
typedef struct _hl_matrix_s *hl_matrix_s;
Z
zhangjinchao01 已提交
187 188 189 190 191 192 193 194 195 196 197 198

/**
 * @brief   HPPL sparse matrix.
 *
 * @param  matrix     sparse matrix.
 * @param  format     matrix format.
 * @param  type       the type of matrix values.
 * @param  rows       matrix rows.
 * @param  cols       matrix columns.
 * @param  nnz        nonzero values of sparse matrix.
 */
typedef struct {
199 200 201 202 203 204
  hl_matrix_s matrix;
  hl_matrix_format_t format;
  hl_matrix_value_t type;
  int rows;
  int cols;
  size_t nnz;
Z
zhangjinchao01 已提交
205 206 207 208
} _hl_sparse_matrix_s, *hl_sparse_matrix_s;

#ifdef __NVCC__

F
fengjiayi 已提交
209
#include <cuda_runtime.h>
X
Xin Pan 已提交
210
#include "paddle/legacy/cuda/include/hl_cuda.h"
Y
Yu Yang 已提交
211
#include "paddle/utils/Logging.h"
Z
zhangjinchao01 已提交
212

213
extern __thread bool g_sync_flag;
Z
zhangjinchao01 已提交
214 215 216 217 218 219 220
extern __thread cudaStream_t default_stream;
#define STREAM_DEFAULT default_stream

/**
 * @brief   Check cuda kernel execution.
 * @param   msg   error string
 */
221 222 223 224 225 226 227
#define CHECK_SYNC(msg)                                               \
  if (true == g_sync_flag) {                                          \
    hl_stream_synchronize(HPPL_STREAM_DEFAULT);                       \
    cudaError_t err = (cudaError_t)hl_get_device_last_error();        \
    CHECK_EQ(cudaSuccess, err)                                        \
        << "[" << msg << "] "                                         \
        << "CUDA error: " << hl_get_device_error_string((size_t)err); \
Z
zhangjinchao01 已提交
228 229
  }

C
chengduoZH 已提交
230 231
// __shfl has been deprecated as of CUDA 9.0.
#if CUDA_VERSION < 9000
C
chengduo 已提交
232 233 234 235 236
template <typename T>
__forceinline__ __device__ T __shfl_down_sync(unsigned, T val, int delta) {
  return __shfl_down(val, delta);
}

C
chengduoZH 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249
template <typename T>
__forceinline__ __device__ T
__shfl_sync(unsigned, T val, int src_line, int width) {
  return __shfl(val, src_line, width);
}

#define CREATE_SHFL_MASK(mask, predicate) mask = 0u;
#else
#define FULL_WARP_MASK 0xFFFFFFFF
#define CREATE_SHFL_MASK(mask, predicate) \
  mask = __ballot_sync(FULL_WARP_MASK, (predicate))
#endif

F
fengjiayi 已提交
250
#endif  // __NVCC__