gemm.h 7.8 KB
Newer Older
W
wangliu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/* Copyright (c) 2018 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. */
Z
zhaojiaying01 已提交
14 15

#pragma once
16 17
#include <string>
#include "common/log.h"
Z
zhaojiaying01 已提交
18

Z
zhaojiaying01 已提交
19 20 21 22
// 矩阵取值运算宏,假设矩阵按行存储
#define A(i, j) A[(i)*lda + (j)]
#define B(i, j) B[(i)*ldb + (j)]
#define C(i, j) C[(i)*ldc + (j)]
Z
zhaojiaying01 已提交
23

Z
zhaojiaying01 已提交
24 25 26 27
#if __aarch64__
#define MR 6
#define NR 16
#else
Z
zhaojiaying01 已提交
28
#define MR 6
29
#define NR 8
Z
zhaojiaying01 已提交
30
#endif
Z
zhaojiaying01 已提交
31

W
wangliu 已提交
32
#define s_min(i, j) ((i) < (j) ? (i) : (j))
Z
zhaojiaying01 已提交
33 34 35 36 37

namespace paddle_mobile {
namespace operators {
namespace math {

38
/*
Z
zhaojiaying01 已提交
39 40 41 42 43 44 45
// 将 A 矩阵分块复制到连续内存(ColMajor)
void PackMatrixA(int m, int k, int m_tail, const float *A, int lda,
                 float *buffer);

// 将 B 矩阵分块复制到连续内存(ColMajor)
void PackMatrixB(int k, int n, int n_tail, const float *B, int ldb,
                 float *buffer);
46
*/
Z
zhaojiaying01 已提交
47 48

// 将 A 矩阵分块复制到连续内存(RowMajor)
Z
zhaojiaying01 已提交
49 50 51 52
void PackMatrixA_4r(int m, int k, int m_tail, const float *A, int lda,
                    float *buffer);
void PackMatrixA_6r(int m, int k, int m_tail, const float *A, int lda,
                    float *buffer);
Z
zhaojiaying01 已提交
53 54
void PackMatrixA_8r(int m, int k, int m_tail, const float *A, int lda,
                    float *buffer);
55 56 57 58
void PackMatrixA_omp_6r(int m, int k, int m_tail, const float *A, int lda,
                        float *buffer);
void PackMatrixA_omp_8r(int m, int k, int m_tail, const float *A, int lda,
                        float *buffer);
Z
zhaojiaying01 已提交
59 60

// 将 B 矩阵分块复制到连续内存(RowMajor)
Z
zhaojiaying01 已提交
61 62
void PackMatrixB_8c(int k, int n, int n_tail, const float *B, int ldb,
                    float *buffer);
Z
zhaojiaying01 已提交
63 64 65 66
void PackMatrixB_12c(int k, int n, int n_tail, const float *B, int ldb,
                     float *buffer);
void PackMatrixB_16c(int k, int n, int n_tail, const float *B, int ldb,
                     float *buffer);
67 68 69 70 71 72
void PackMatrixB_omp_8c(int k, int n, int n_tail, const float *B, int ldb,
                        float *buffer);
void PackMatrixB_omp_12c(int k, int n, int n_tail, const float *B, int ldb,
                         float *buffer);
void PackMatrixB_omp_16c(int k, int n, int n_tail, const float *B, int ldb,
                         float *buffer);
Z
zhaojiaying01 已提交
73 74 75 76

// 分块矩阵乘法
void InnerKernel(int mc, int nc, float alpha, const float *a, const float *b,
                 float beta, float *c, float *C, int ldc, bool relu);
77 78 79
void InnerKernelWithBias(int mc, int nc, float alpha, const float *a,
                         const float *b, float beta, float *c, float *C,
                         int ldc, bool relu, float *bias);
Z
zhaojiaying01 已提交
80 81 82 83

void InnerKernelWithBn(int mc, int nc, float alpha, const float *a,
                       const float *b, float beta, float *c, float *C, int ldc,
                       bool relu, float *new_scale, float *new_bias);
84 85 86 87
void InnerKernelWithBnAdd(int mc, int nc, float alpha, const float *a,
                          const float *b, float beta, float *c, float *C,
                          int ldc, bool relu, float *new_scale, float *new_bias,
                          float *bias);
88 89 90
void InnerKernelWithPRelu(int mc, int nc, const float *a, const float *b,
                          float *c, float *C, int ldc, float *p,
                          std::string mode, float *bias, float *bias1);
91
/*
Z
zhaojiaying01 已提交
92 93 94 95 96 97 98 99
// 向量矩阵乘法 (M = 1)
void VectorKernel(int m, int n, int k, float alpha, const float *A, int lda,
                  const float *B, int ldb, float beta, float *C, int ldc,
                  bool relu);

void VectorKernelWithBn(int m, int n, int k, float alpha, const float *A,
                        int lda, const float *B, int ldb, float beta, float *C,
                        int ldc, bool relu, float *new_scale, float *new_bias);
100
*/
Z
zhaojiaying01 已提交
101 102 103 104

// 计算一个更小的 C 矩阵分块
void AddDot4x4(int k, const float *a, const float *b, float *c, int ldc);
void AddDot4x8(int k, const float *a, const float *b, float *c, int ldc);
Z
zhaojiaying01 已提交
105
void AddDot6x8(int k, const float *a, const float *b, float *c, int ldc);
Z
zhaojiaying01 已提交
106 107
void AddDot8x12(int k, const float *a, const float *b, float *c, int ldc);
void AddDot6x16(int k, const float *a, const float *b, float *c, int ldc);
Z
zhaojiaying01 已提交
108 109 110 111 112 113 114 115

// 分块矩阵乘法结果回写
// C = A * B
void WriteBasic(int mc, int nc, float *c, float *C, int ldc);
// C = alpha * A * B + beta * C
void WriteWithAlphaBeta(int mc, int nc, float *c, float *C, int ldc);
// C = A * B + C
void WriteWithAdd(int mc, int nc, float *c, float *C, int ldc);
116 117
// C = A * B + bias
void WriteWithAddV1(int mc, int nc, float *c, float *C, int ldc, float *bias);
Z
zhaojiaying01 已提交
118 119
// C = A * B + C, relu(C)
void WriteWithAddRelu(int mc, int nc, float *c, float *C, int ldc);
120 121 122
// C = A * B + C,prelu(C)
void WriteWithAddPRelu(int mc, int nc, float *c, float *C, int ldc, float *p,
                       std::string mode, float *bias, float *bias1);
123 124 125
// C = A * B + bias ,relu(C)
void WriteWithAddReluV1(int mc, int nc, float *c, float *C, int ldc,
                        float *bias);
Z
zhaojiaying01 已提交
126 127 128 129 130 131
// C = A * B, batchnorm(C)
void WriteWithBn(int mc, int nc, float *c, float *C, int ldc, float *new_scale,
                 float *new_bias);
// C = A * B, batchnorm(C), relu(C)
void WriteWithBnRelu(int mc, int nc, float *c, float *C, int ldc,
                     float *new_scale, float *new_bias);
132 133
void WriteWithBnAddRelu(int mc, int nc, float *c, float *C, int ldc,
                        float *new_scale, float *new_bias, float *bias1);
134
/*
Z
zhaojiaying01 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
// 向量矩阵乘法结果回写
// C = A * B
void VecWriteBasic(int n, float *c, float *C, int ldc);
// C = alpha * A * B + beta * C
void VecWriteWithAlphaBeta(int n, float *c, float *C, int ldc);
// C = A * B + C
void VecWriteWithAdd(int n, float *c, float *C, int ldc);
// C = A * B + C, relu(C)
void VecWriteWithAddRelu(int n, float *c, float *C, int ldc);
// C = A * B, batchnorm(C)
void VecWriteWithBn(int n, float *c, float *C, int ldc, float *new_scale,
                    float *new_bias);
// C = A * B, batchnorm(C), relu(C)
void VecWriteWithBnRelu(int n, float *c, float *C, int ldc, float *new_scale,
                        float *new_bias);
150
*/
Z
zhaojiaying01 已提交
151 152 153

// 32位 float 矩阵乘法
void Sgemm(int m, int n, int k, float alpha, const float *A, int lda,
154 155
           const float *B, int ldb, float beta, float *C, int ldc, bool relu,
           float *bias);
Z
zhaojiaying01 已提交
156 157 158 159

// 32位 float 矩阵乘法, 并对结果进行 batchnrom
void SgemmWithBn(int m, int n, int k, float alpha, const float *A, int lda,
                 const float *B, int ldb, float beta, float *C, int ldc,
Y
yangfei 已提交
160
                 bool relu, float *new_scale, float *new_bias, float *bias);
161 162 163 164
void SgemmWithPRelu(int m, int n, int k, const float *A, int lda,
                    const float *B, int ldb, float *C, int ldc, float *p,
                    std::string mode, float *bias, float *bias1);

165 166 167 168 169 170 171 172
// 32位 float 矩阵乘法(openmp 多线程版本)
void Sgemm_omp(int m, int n, int k, float alpha, const float *A, int lda,
               const float *B, int ldb, float beta, float *C, int ldc,
               bool relu, float *bias);

// 32位 float 矩阵乘法, 并对结果进行 batchnrom(openmp 多线程版本)
void SgemmWithBn_omp(int m, int n, int k, float alpha, const float *A, int lda,
                     const float *B, int ldb, float beta, float *C, int ldc,
Y
yangfei 已提交
173
                     bool relu, float *new_scale, float *new_bias, float *bias);
174

175 176 177 178
void SgemmWithPRelu_omp(int m, int n, int k, const float *A, int lda,
                        const float *B, int ldb, float *C, int ldc, float *p,
                        std::string mode, float *bias, float *bias1);

Z
zhaojiaying01 已提交
179 180 181
}  // namespace math
}  // namespace operators
}  // namespace paddle_mobile