forked from kzhangucsb/tt_quant
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtt_contract.cpp
More file actions
344 lines (328 loc) · 11.3 KB
/
Copy pathtt_contract.cpp
File metadata and controls
344 lines (328 loc) · 11.3 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include "tt_nn.h"
#ifndef SYNTHESIS
#include <assert.h>
#include <iostream>
#include <math.h>
using namespace std;
#endif
void tensor_cont_mid(
TYPE_DATA* data_in,
TYPE_DATA* data_out,
TYPE_WEIGHT* weight,
int array_in_size_0,
int array_in_size_1,
int array_in_size_2,
int array_weight_size_0,
int array_weight_size_2,
float shift,
TYPE_INTER* max
){
/* tensor contraction on the second dimension
ABCxDBE->ADEC
* array_in: size (array_in_size_0*array_in_size_1*array_in_size_2),
* array_weight: size (array_weight_size_0*array_in_size_1*array_weight_size_2),
* array_out: size(array_in_size_0*array_weight_size_0*array_weight_size_2*array_in_size_2)
* All arrays are in C order
*/
#ifndef SYNTHESIS
assert (array_in_size_2 % PARALLEL_DEGREE == 0);
#endif
TYPE_INTER res[PARALLEL_DEGREE];
for (int i_in_0 = 0; i_in_0 < array_in_size_0; i_in_0++) {
for (int i_w_0 = 0; i_w_0 < array_weight_size_0; i_w_0++) {
for (int i_in_2 = 0; i_in_2 < array_in_size_2; i_in_2+=PARALLEL_DEGREE) {
for (int i_w_2 = 0; i_w_2 < array_weight_size_2; i_w_2++) {
for (int i_in_o = 0; i_in_o < PARALLEL_DEGREE; i_in_o++){
res[i_in_o] = 0;
}
for (int i_in_1 = 0; i_in_1 < array_in_size_1; i_in_1 += 1) {
int ind_in = sub2ind3(i_in_0, i_in_1, i_in_2, array_in_size_1, array_in_size_2);
int ind_w = sub2ind3(i_w_0, i_in_1, i_w_2, array_in_size_1, array_weight_size_2);
for (int i_in_o = 0; i_in_o < PARALLEL_DEGREE; i_in_o++){
res[i_in_o] += float(data_in[ind_in + i_in_o]) * float(weight[ind_w]);
}
}
int ind_out = sub2ind4(i_in_0, i_w_0, i_w_2, i_in_2,
array_weight_size_0, array_weight_size_2, array_in_size_2);
TYPE_RINT rn = pseudo_random();
for (int i_in_o = 0; i_in_o < PARALLEL_DEGREE; i_in_o++){
res[i_in_o] /= pow(2, shift);
data_out[ind_out+i_in_o] = TYPE_GRAD(res[i_in_o] + randadj(rn, i_in_o));
if (*max < abs(res[i_in_o])) {
*max = abs(res[i_in_o]);
}
}
}
}
}
}
}
void tensor_cont_last(
TYPE_DATA* data_in,
TYPE_DATA* data_out,
TYPE_WEIGHT* weight,
int array_in_size_0,
int array_in_size_1,
int array_in_size_2,
int array_weight_size_1,
float shift,
TYPE_INTER* max
){
/* tensor contraction on the first and last dimension
ABCxBDC->AD
*/
#ifndef SYNTHESIS
assert (array_in_size_2 % PARALLEL_DEGREE == 0);
#endif
//TYPE_INTER res;
for (int i_in_0 = 0; i_in_0 < array_in_size_0; i_in_0++) {
for (int i_w_1 = 0; i_w_1 < array_weight_size_1; i_w_1++) {
TYPE_INTER res = 0;
for (int i_in_1 = 0; i_in_1 < array_in_size_1; i_in_1++) {
for (int i_in_2 = 0; i_in_2 < array_in_size_2; i_in_2 += PARALLEL_DEGREE) {
int ind_in = sub2ind3(i_in_0, i_in_1, i_in_2, array_in_size_1, array_in_size_2);
int ind_w = sub2ind3(i_in_1, i_w_1, i_in_2, array_weight_size_1, array_in_size_2);
for (int i_in_o = 0; i_in_o < PARALLEL_DEGREE; i_in_o++) {
res += TYPE_INTER(data_in[ind_in+i_in_o]) * TYPE_INTER(weight[ind_w+i_in_o]);
}
}
}
int ind_out = sub2ind3(0, i_in_0, i_w_1, array_in_size_0, array_weight_size_1);
res /= pow(2, shift);
TYPE_RINT rn = pseudo_random();
data_out[ind_out] = TYPE_GRAD(res + randadj(rn, 0));
if (*max < abs(res)) {
*max = abs(res);
}
}
}
}
void getstride1(
int* shape,
int dim,
int* res
){
int st = 1;
for (int i = dim - 1; i >= 0; i--) {
res[i] = st;
st *= shape[i];
}
}
void getstride2(
int* shape1, // input
int* shape2, // output
int dim,
int* res
){
int st = 1;
for (int i = dim - 1; i >= 0; i--) {
res[i*2+1] = st;
st *= shape1[i];
res[i*2] = st;
st *= shape2[i];
}
}
int ind2offset(
int* index,
int* stride,
int dim
){
int ret = 0;
for (int i = 0; i < dim; i++) {
ret += index[i] * stride[i];
}
return ret;
}
int ind22offset(
int* index1,
int* index2,
int* stride,
int dim
){
int ret = 0;
for (int i = 0; i < dim; i++) {
ret += index1[i] * stride[i * 2 + 1];
ret += index2[i] * stride[i * 2];
}
return ret;
}
void tensor_cont_outer_prod(
TYPE_DATA *data_in, // input
TYPE_DATA *grad_out, // output_grad
TYPE_GRAD *grad_wt,
int* input_shape,
int* output_shape,
int dim,
float shift,
TYPE_INTER* max
){
int stride1[5];
int stride2[5];
int strideo[10];
int index1[5] = {0};
int index2[5] = {0};
assert(dim <= 5); // dim too high
int shape1exp[5] = {1, 1, 1, 1, 1};
int shape2exp[5] = {1, 1, 1, 1, 1};
for (int i = 0; i < dim; i++) {
shape1exp[i + 5 - dim] = input_shape[i];
shape2exp[i + 5 - dim] = output_shape[i];
}
getstride1(shape1exp, 5, stride1);
getstride1(shape2exp, 5, stride2);
getstride2(shape1exp, shape2exp, 5, strideo);
int ind_i = 0, ind_o = 0, ind_r = 0;
for (index2[0] = 0; index2[0] < shape2exp[0]; index2[0]++) {
for (index2[1] = 0; index2[1] < shape2exp[1]; index2[1]++) {
for (index2[2] = 0; index2[2] < shape2exp[2]; index2[2]++) {
for (index2[3] = 0; index2[3] < shape2exp[3]; index2[3]++) {
for (index2[4] = 0; index2[4] < shape2exp[4]; index2[4]++) {
for (index1[0] = 0; index1[0] < shape1exp[0]; index1[0]++) {
for (index1[1] = 0; index1[1] < shape1exp[1]; index1[1]++) {
for (index1[2] = 0; index1[2] < shape1exp[2]; index1[2]++) {
for (index1[3] = 0; index1[3] < shape1exp[3]; index1[3]++) {
for (index1[4] = 0; index1[4] < shape1exp[4]; index1[4]++) {
// assert(ind_i == ind2offset(index1, stride1, 5));
// assert(ind_o == ind2offset(index2, stride2, 5));
// assert(ind_r == ind22offset(index1, index2, strideo, 5));
TYPE_INTER res = float(data_in[ind_i]) * float(grad_out[ind_o]);
res /= pow(2.0, shift);
if (*max < abs(res)) {
*max = abs(res);
}
TYPE_RINT rn = pseudo_random();
res += randadj(rn, 0);
grad_wt[ind_r] += TYPE_GRAD(res);
ind_i += stride1[4];
ind_r += strideo[9];
}
ind_i -= stride1[4] * shape1exp[4];
ind_r -= strideo[9] * shape1exp[4];
ind_i += stride1[3];
ind_r += strideo[7];
}
ind_i -= stride1[3] * shape1exp[3];
ind_r -= strideo[7] * shape1exp[3];
ind_i += stride1[2];
ind_r += strideo[5];
}
ind_i -= stride1[2] * shape1exp[2];
ind_r -= strideo[5] * shape1exp[2];
ind_i += stride1[1];
ind_r += strideo[3];
}
ind_i -= stride1[1] * shape1exp[1];
ind_r -= strideo[3] * shape1exp[1];
ind_i += stride1[0];
ind_r += strideo[1];
}
ind_i = 0;
ind_r -= strideo[1];
ind_o += stride2[4];
ind_r += strideo[8];
}
ind_o -= stride2[4] * shape2exp[4];
ind_r -= strideo[8] * shape2exp[4];
ind_o += stride2[3];
ind_r += strideo[6];
}
ind_o -= stride2[3] * shape2exp[3];
ind_r -= strideo[6] * shape2exp[3];
ind_o += stride2[2];
ind_r += strideo[4];
}
ind_o -= stride2[2] * shape2exp[2];
ind_r -= strideo[4] * shape2exp[2];
ind_o += stride2[1];
ind_r += strideo[2];
}
ind_o -= stride2[1] * shape2exp[1];
ind_r -= strideo[2] * shape2exp[1];
ind_o += stride2[0];
ind_r += strideo[0];
}
}
// ************* below should not be used *******************************
// void tensor_cont_end_backward(
// TYPE_DATA *data_in_1,
// TYPE_DATA *data_in_2,
// TYPE_GRAD *grad_out,
// int array_in_size_0,
// int array_in_size_1,
// int array_in_size_2,
// int array_in_size_3,
// int array_weight_size_1,
// int shift,
// TYPE_INTER* max
// ){
// /* tensor contraction on the first and last dimension
// ABCDxAED->BEC
// */
// #ifndef SYNTHESIS
// assert (array_in_size_3 % PARALLEL_DEGREE == 0);
// #endif
// TYPE_INTER res;
// for (int i_in_1 = 0; i_in_1 < array_in_size_1; i_in_1++) {
// for (int i_in_2 = 0; i_in_2 < array_in_size_2; i_in_2++) {
// for (int i_w_1 = 0; i_w_1 < array_weight_size_1; i_w_1++) {
// res = 0;
// for (int i_in_0 = 0; i_in_0 < array_in_size_0; i_in_0++) {
// for (int i_in_3 = 0; i_in_3 < array_in_size_3; i_in_3 += PARALLEL_DEGREE) {
// int ind_in = sub2ind4(i_in_0, i_in_1, i_in_2, i_in_3,
// array_in_size_1, array_in_size_2, array_in_size_3);
// int ind_w = sub2ind3(i_in_0, i_w_1, i_in_3, array_weight_size_1, array_in_size_3);
// for (int i_in_o = 0; i_in_o < PARALLEL_DEGREE; i_in_o++) {
// res += data_in_1[ind_in+i_in_o] * data_in_2[ind_w+i_in_o];
// }
// }
// }
// int ind_out = sub2ind3(i_in_1, i_w_1, i_in_2, array_weight_size_1, array_in_size_2);
// res /= pow(2, shift);
// grad_out[ind_out] += res;
// if (*max < abs(res)) {
// *max = abs(res);
// }
// }
// }
// }
// }
// void tensor_cont_head_backward(
// TYPE_DATA *data_in_1,
// TYPE_DATA *data_in_2,
// TYPE_GRAD *grad_out,
// int array_in_size_0,
// int array_in_size_1,
// int array_weight_size_1,
// int shift,
// TYPE_INTER* max
// ){
// /* tensor contraction on the first and last dimension
// ABxAE->BE
// */
// #ifndef SYNTHESIS
// assert (array_in_size_1 % PARALLEL_DEGREE == 0);
// #endif
// TYPE_INTER res[PARALLEL_DEGREE];
// for (int i_in_1 = 0; i_in_1 < array_in_size_1; i_in_1++) {
// for (int i_w_1 = 0; i_w_1 < array_weight_size_1; i_w_1+= PARALLEL_DEGREE) {
// for (int i_in_o = 0; i_in_o < PARALLEL_DEGREE; i_in_o++){
// res[i_in_o] = 0;
// }
// for (int i_in_0 = 0; i_in_0 < array_in_size_0; i_in_0++) {
// int ind_in = sub2ind3(0, i_in_0, i_in_1, array_in_size_0, array_in_size_1);
// int ind_w = sub2ind3(0, i_in_0, i_w_1, array_in_size_0, array_weight_size_1);
// for (int i_in_o = 0; i_in_o < PARALLEL_DEGREE; i_in_o++){
// res[i_in_o] += data_in_1[ind_in] * data_in_2[ind_w + i_in_o];
// }
// }
// int ind_out = sub2ind3(0, i_in_1, i_w_1, array_in_size_1, array_weight_size_1);
// for (int i_in_o = 0; i_in_o < PARALLEL_DEGREE; i_in_o++){
// res[i_in_o] /= pow(2, shift);
// grad_out[ind_out + i_in_o] += res[i_in_o];
// if (*max < abs(res[i_in_o])) {
// *max = abs(res[i_in_o]);
// }
// }
// }
// }
// }