-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerate_golden_int8.cpp
More file actions
207 lines (148 loc) · 4.82 KB
/
generate_golden_int8.cpp
File metadata and controls
207 lines (148 loc) · 4.82 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
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
199
200
201
202
203
204
205
206
207
/*
* How to run:
* g++ -o generate_golden_int8 generate_golden_int8.cpp
*
*/
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>
#include "../src/kernels/include.h"
int main(){
auto matA = new int8_t [single_M * single_K][mult_X * mult_Y];
auto matB = new int8_t [single_K * single_N][mult_Y * mult_Z];
auto matC = new int [single_M * single_N][mult_X * mult_Z];
// each chuck_C computes the single AI MatMul kernel
// Then reduced on mult_Y dimension
auto chunk_C = new int [single_M * single_N][mult_Y];
std::fstream a_file_array[mult_X * mult_Y];
std::fstream b_file_array[mult_Y * mult_Z];
std::fstream c_file_array[mult_X * mult_Z];
for (int i = 0; i < mult_X * mult_Y; i++){
a_file_array[i].open("./matA" + std::to_string(i) + ".txt", std::ios::out);
}
for (int i = 0; i < mult_Y * mult_Z; i++){
b_file_array[i].open("./matB" + std::to_string(i) + ".txt", std::ios::out);
}
for (int i = 0; i < mult_X * mult_Z; i++){
c_file_array[i].open("./matC" + std::to_string(i) + ".txt", std::ios::out);
}
// seed
srand(time(NULL));
for (int batch = 0; batch < 10; batch++){
// A matrix
for (int xy = 0; xy < mult_X * mult_Y; xy++){
// generate matA in blocked format
for (int i = 0; i < single_M/M_API; i++){
for (int k = 0; k < single_K/K_API; k++){
for (int m_a = 0; m_a < M_API; m_a++){
for (int k_a = 0; k_a < K_API; k_a++){
matA[i*(single_K/K_API)*M_API*K_API + k*M_API*K_API + m_a*K_API + k_a][xy] = rand()%128;
}
}
}
}
// write matA to matA.txt
for (int i = 0; i < single_M*single_K; i++){
a_file_array[xy] << int(matA[i][xy]);
if (i % 16 == 15){
a_file_array[xy] << "\n";
}
else{
a_file_array[xy] << " ";
}
}
}
// B matrix
for (int yz = 0; yz < mult_Y * mult_Z; yz++){
// generate matB in blocked format
for (int k = 0; k < single_K/K_API; k++){
for (int j = 0; j < single_N/N_API; j++){
for (int k_a = 0; k_a < K_API; k_a++){
for (int n_a = 0; n_a < N_API; n_a++){
matB[k*(single_N/N_API)*K_API*N_API + j*K_API*N_API + k_a*N_API + n_a][yz] = rand()%128;
}
}
}
}
// write matB to matB.txt
for (int i = 0; i < single_K*single_N; i++){
b_file_array[yz] << int(matB[i][yz]);
if (i % 16 == 15){
b_file_array[yz] << "\n";
}
else{
b_file_array[yz] << " ";
}
}
}
// Perform MatMul to generate the golden data
for (int ii = 0; ii < mult_X; ii++){
for (int jj = 0; jj < mult_Z; jj++){
for (int kk = 0; kk < mult_Y; kk++){
for (int i = 0; i < single_M/M_API; i++){
for (int j = 0; j < single_N/N_API; j++){
// initialize Chunk to 0
for (int m_a = 0; m_a < M_API; m_a++){
for (int n_a = 0; n_a < N_API; n_a++){
chunk_C[i*(single_N/N_API)*M_API*N_API + j*M_API*N_API + m_a*N_API + n_a][kk] = 0;
}
}
for (int k = 0; k < single_K/K_API; k++){
for (int m_a = 0; m_a < M_API; m_a++){
for (int n_a = 0; n_a < N_API; n_a++){
for (int k_a = 0; k_a < K_API; k_a++){
chunk_C[i*(single_N/N_API)*M_API*N_API + j*M_API*N_API + m_a*N_API + n_a][kk] +=
matA[i*(single_K/K_API)*M_API*K_API + k*M_API*K_API + m_a*K_API + k_a][ii*mult_Y+ kk] *
matB[k*(single_N/N_API)*K_API*N_API + j*K_API*N_API + k_a*N_API + n_a][jj*mult_Y + kk];
}
}
}
}
}
}
// add the partial results (reduction in mult_Y dimension)
if (kk == 0){
for (int i = 0; i < single_M*single_N; i++){
matC[i][ii*mult_Z + jj] = chunk_C[i][0];
}
}
else {
for (int i = 0; i < single_M*single_N; i++){
matC[i][ii*mult_Z + jj] += chunk_C[i][kk];
}
}
}
}
}
for (int xz = 0; xz < mult_X * mult_Z; xz++){
// write to output after elementwise addition
for (int i = 0; i < single_M*single_N; i++){
c_file_array[xz] << int(matC[i][xz]);
if (i % 4 == 3){
c_file_array[xz] << "\n";
}
else{
c_file_array[xz] << " ";
}
}
}
}
// close files
for (int i = 0; i < mult_X * mult_Y; i++){
a_file_array[i].close();
}
for (int i = 0; i < mult_Y * mult_Z; i++){
b_file_array[i].close();
}
for (int i = 0; i < mult_X * mult_Z; i++){
c_file_array[i].close();
}
delete[] matA;
delete[] matB;
delete[] matC;
delete[] chunk_C;
return 0;
}