hl_matrix.h 10.5 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Z
zhangjinchao01 已提交
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

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

#ifndef HL_MATRIX_H_
#define HL_MATRIX_H_

#include "hl_base.h"

/**
 * @brief   Matrix addition: C_d[i] = alpha * A_d[i] + beta * B_d[i].
 *
 * @param[in]   A_d     input matrix (M x N).
 * @param[in]   B_d     input matrix (M x N).
 * @param[out]  C_d     output matrix (M x N).
 * @param[in]   dimM    matrix height.
 * @param[in]   dimN    matrix width.
 * @param[in]   alpha   scalar used for addition.
 * @param[in]   beta    scalar used for addition.
 *
 */
32 33
extern void hl_matrix_add(
    real* A_d, real* B_d, real* C_d, int dimM, int dimN, real alpha, real beta);
Z
zhangjinchao01 已提交
34 35 36 37 38 39 40 41 42
/**
 * @brief   Matrix Softmax.
 *
 * @param[in]   A_d     input maxtrix (M x N).
 * @param[out]  C_d     output matrix (M x N).
 * @param[in]   dimM    matrix height.
 * @param[in]   dimN    matrix width.
 *
 */
43
extern void hl_matrix_softmax(real* A_d, real* C_d, int dimM, int dimN);
Z
zhangjinchao01 已提交
44 45 46 47 48 49 50 51 52 53 54

/**
 * @brief   Matrix softmax derivative.
 *
 * @param[out]  grad_d       intput matrix (M x N).
 * @param[in]   output_d     output matrix (M x N).
 * @param[in]   sftmaxSum_d  softmax sum (M * 1).
 * @param[in]   dimM         matrix height.
 * @param[in]   dimN         matrix width.
 *
 */
55 56
extern void hl_matrix_softmax_derivative(
    real* grad_d, real* output_d, real* sftmaxSum_d, int dimM, int dimN);
Z
zhangjinchao01 已提交
57 58 59 60 61 62 63 64 65 66

/**
 * @brief   Sequence softmax.
 *
 * @param[in]   A_d         input vector.
 * @param[out]  C_d         output vector.
 * @param[in]   index       start positions of sequence.
 * @param[in]   numSequence sequence number.
 *
 */
67 68
extern void hl_sequence_softmax_forward(real* A_d,
                                        real* C_d,
Z
zhangjinchao01 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81
                                        const int* index,
                                        int numSequence);

/**
 * @brief   Matrix cross entropy.
 *
 * @param[in]   A_d     input matrix (M x N).
 * @param[out]  C_d     output matrix (M X 1).
 * @param[in]   label_d input matrix (M x 1).
 * @param[in]   dimM    matrix height.
 * @param[in]   dimN    matrix width.
 *
 */
82 83
extern void hl_matrix_cross_entropy(
    real* A_d, real* C_d, int* label_d, int dimM, int dimN);
Z
zhangjinchao01 已提交
84 85 86 87 88 89 90 91 92 93 94

/**
 * @brief   Matrix cross entropy back propagation.
 *
 * @param[out]  grad_d      output matrix (M x N).
 * @param[in]   output_d    input matrix (M x N).
 * @param[in]   label_d     input vector (M x 1).
 * @param[in]   dimM        matrix height.
 * @param[in]   dimN        matrix width.
 *
 */
95 96
extern void hl_matrix_cross_entropy_bp(
    real* grad_d, real* output_d, int* label_d, int dimM, int dimN);
Z
zhangjinchao01 已提交
97

98 99 100 101 102 103 104 105 106
/**
 * @brief  Matrix multi-binary label cross entropy
 *
 * @param[in]   output    input matrix (M x N).
 * @param[out]  entropy   output matrix (M x 1).
 * @param[in]   mat       input sparse matrix.
 * @param[in]   dimM      matrix height.
 * @param[in]   dimN      matrix width.
 */
107 108
extern void hl_matrix_multi_binary_cross_entropy(
    real* output, real* entropy, hl_sparse_matrix_s mat, int dimM, int dimN);
109 110 111 112 113 114 115 116 117 118

/**
 * @brief  Matrix multi-binary label cross entropy backprop
 *
 * @param[in]   output    input matrix (M x N).
 * @param[out]  grad      output matrix (M x N).
 * @param[in]   mat       input sparse matrix.
 * @param[in]   dimM      matrix height.
 * @param[in]   dimN      matrix width.
 */
119 120
extern void hl_matrix_multi_binary_cross_entropy_bp(
    real* output, real* grad, hl_sparse_matrix_s mat, int dimM, int dimN);
121

Z
zhangjinchao01 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
/**
 * @brief  Matrix zero memory.
 *
 * @param[in,out]  data   input data.
 * @param[in]      num    length of data.
 *
 */
extern void hl_matrix_zero_mem(real* data, int num);

/**
 * @brief parameter relu forward
 *
 * @param[out] output     output data
 * @param[in]  input      input data
 * @param[in]  w          parameter data
 * @param[in]  width      matrix width
 * @param[in]  height     matrix height
 * @param[in]  partial_sum
 */

142 143
extern void hl_param_relu_forward(
    real* output, real* input, real* w, int width, int height, int partial_sum);
Z
zhangjinchao01 已提交
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
/**
 * @brief parameter relu backward w
 *
 * @param[out] grad_w      w grad
 * @param[in]  grad_o      output grad
 * @param[in]  input       input data
 * @param[in]  width       matrix width
 * @param[in]  height      matrix height
 * @param[in]  partial_sum
 */
extern void hl_param_relu_backward_w(real* grad_w,
                                     real* grad_o,
                                     real* input,
                                     int width,
                                     int height,
                                     int partial_sum);
/**
 * @brief parameter relu backward diff
 *
 * @param[in]       grad_o      output grad
 * @param[in]       input       input data
 * @param[in]       w           parameter
 * @param[out]      diff        diff
 * @param[in]       width       matrix width
 * @param[in]       height      matrix height
 * @param[in]       partial_sum
 */
extern void hl_param_relu_backward_diff(real* grad_o,
                                        real* input,
                                        real* w,
                                        real* diff,
                                        int width,
                                        int height,
                                        int partial_sum);

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
/**
 * @brief   Matrix addition: A_d[i][j] += scale * B_d[j/channel].
 *
 * @param[in]   A_d     input matrix (M x N).
 * @param[in]   B_d     input matrix (1 x channel).
 * @param[in]   channel width of B.
 * @param[in]   dimM    height of A.
 * @param[in]   dimN    width of A.
 * @param[in]   scale   scalar used for addition.
 *
 */
extern void hl_matrix_add_shared_bias(real* A_d,
                                      real* B_d,
                                      const int channel,
                                      const int dimM,
                                      const int dimN,
                                      real scale);

/**
 * @brief   Matrix addition: A_d[i][j] += scale * B_d[j/channel].
 *
 * @param[in]   B_d     input matrix (1 x channel).
 * @param[in]   A_d     input matrix (M x N).
 * @param[in]   channel width of B.
 * @param[in]   dimM    height of A.
 * @param[in]   dimN    width of A.
 * @param[in]   scale   scalar used for addition.
 *
 */
extern void hl_matrix_collect_shared_bias(real* B_d,
                                          real* A_d,
                                          const int channel,
                                          const int dimM,
                                          const int dimN,
                                          real scale);

H
Haonan 已提交
215 216 217 218 219 220 221 222 223 224 225 226
/**
 * @brief  Matrix rotation in 90 degrees
 *
 * @param[in]   mat       input matrix (M x N).
 * @param[out]  matRot    output matrix (N x M).
 * @param[in]   dimM      input matrix height.
 * @param[in]   dimN      input matrix width.
 * @param[in]   clockWise rotation direction
 */
extern void hl_matrix_rotate(
    real* mat, real* matRot, int dimM, int dimN, bool clockWise);

C
chengduoZH 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
/**
 * @brief  Matrix vol2Col: Convert 3D volume into col matrix
 *
 * @param[in]   matSrc     input matrix.
 * @param[in]   channel    channel of matSrc.
 * @param[in]   depth      depth of matSrc.
 * @param[in]   height     height of matSrc.
 * @param[in]   width      width of matSrc.
 * @param[in]   filterD    depth of filter.
 * @param[in]   filterH    height of filter.
 * @param[in]   filterW    width of filter.
 * @param[in]   strideD    stride in the depth.
 * @param[in]   strideH    stride in the height.
 * @param[in]   strideW    stride in the width.
 * @param[in]   paddingD   padding in the depth.
 * @param[in]   paddingH   padding in the height.
C
chengduoZH 已提交
243
 * @param[in]   paddingW   padding in the width.
C
chengduoZH 已提交
244
 * @param[out]   dataDst     output matrix.
C
chengduoZH 已提交
245
 *
C
chengduoZH 已提交
246
 */
C
chengduoZH 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
extern void hl_matrix_vol2Col(const real* dataSrc,
                              int channels,
                              int depth,
                              int height,
                              int width,
                              int filterD,
                              int filterH,
                              int filterW,
                              int strideD,
                              int strideH,
                              int strideW,
                              int paddingD,
                              int paddingH,
                              int paddingW,
                              real* dataDst);
C
chengduoZH 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278

/**
 * @brief  Matrix col2Vol: Convert col matrix into 3D volume
 *
 * @param[out]  matDst     output matrix.
 * @param[in]   channel    channel of matDst.
 * @param[in]   depth      depth of matDst.
 * @param[in]   height     height of matDst.
 * @param[in]   width      width of matDst.
 * @param[in]   filterD    depth of filter.
 * @param[in]   filterH    height of filter.
 * @param[in]   filterW    width of filter.
 * @param[in]   strideD    stride in the depth.
 * @param[in]   strideH    stride in the height.
 * @param[in]   strideW    stride in the width.
 * @param[in]   paddingD   padding in the depth.
 * @param[in]   paddingH   padding in the height.
C
chengduoZH 已提交
279
 * @param[in]   paddingW   padding in the width.
C
chengduoZH 已提交
280
 * @param[in]   matSrc     input matrix.
C
chengduoZH 已提交
281 282 283
 * @param[in]   beta       input
 * @param[in]   alpha      input
 *
C
chengduoZH 已提交
284
 */
C
chengduoZH 已提交
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
extern void hl_matrix_col2Vol(real* dataDst,
                              int channels,
                              int depth,
                              int height,
                              int width,
                              int filterD,
                              int filterH,
                              int filterW,
                              int strideD,
                              int strideH,
                              int strideW,
                              int paddingD,
                              int paddingH,
                              int paddingW,
                              const real* dataSrc,
                              real alpha,
                              real beta);
C
chengduoZH 已提交
302

303 304 305 306 307 308 309 310
/**
 * @brief  Matrix col2Vol: Convert col matrix into 3D volume
 * @param[out]  out     output int vector.
 * @param[in]   vec     input float vector.
 * @param[in]   size    size of the vector.
 */
extern void hl_vector_cast2int(int* out, real* vec, int size);

Z
zhangjinchao01 已提交
311
#endif /* HL_MATRIX_H_ */