Repository navigation
Expand file tree
/
Copy pathgemm.cpp
More file actions
44 lines (43 loc) · 1.62 KB
/
Copy pathgemm.cpp
File metadata and controls
44 lines (43 loc) · 1.62 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <cmath>
//cpu gemm
template <typename computeType, typename scaleType, typename inputType, typename resultType>
void cpuGEMM(const inputType* inputA,
const inputType* inputB,
resultType* resultC,
int M,
int N,
int K,
int strideA,
int strideB,
int strideC,
int batchCount,
scaleType alpha,
scaleType beta,
bool transA,
bool transB,
scaleType* bias = nullptr,
bool isColMajorC = true)
{
for (int batch = 0; batch < batchCount; batch++)
{
const inputType* A = inputA + batch * strideA;
const inputType* B = inputB + batch * strideB;
resultType* C = resultC + batch * strideC;
for (int m = 0; m < M; m++)
{
for (int n = 0; n < N; n++)
{
computeType sum = 0;
for (int k = 0; k < K; k++)
{
inputType a = transA ? A[k * M + m] : A[m * K + k];
inputType b = transB ? B[n * K + k] : B[k * N + n];
sum += static_cast<computeType>(a) * static_cast<computeType>(b);
}
unsigned ci = isColMajorC ? n * M + m : m * N + n;
C[ci] = __float2half(alpha * static_cast<computeType>(sum) + beta * static_cast<computeType>(C[ci]));
}
}
}
}