full_matrix_decomp.py 12.3 KB
Newer Older
Mitchell Wortsman's avatar
Mitchell Wortsman committed
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
345
346
347
348
349
350
351
352
353
import json

import time
import torch
import torch.nn as nn
import bitsandbytes.nn as bnn
from bitsandbytes.nn.triton_based_modules import SwitchBackLinear, SwitchBackGlobalLinear, MyLinear

from bitsandbytes.nn.triton_utils.v0.quantize_rowwise_nogroup import quantize_rowwise_nogroup
from bitsandbytes.nn.triton_utils.v0.quantize_columnwise_nogroup_transpose import quantize_columnwise_nogroup_transpose
from bitsandbytes.nn.triton_utils.v0.int8_matmul_rowwise_dequantize_bias import int8_matmul_rowwise_dequantize_bias
from bitsandbytes.nn.triton_utils.v0.int8_matmul_rowwise_dequantize import int8_matmul_rowwise_dequantize
from bitsandbytes.nn.triton_utils.v0.quantize_global import quantize_global, quantize_global_transpose
from bitsandbytes.nn.triton_utils.v0.int8_matmul_mixed_dequanitze import int8_matmul_mixed_dequanitze, int8_matmul_mixed_dequanitze_bias

# KNOW ISSUE: need to optimize "w_quantize_colwise_transpose" when embeddim is too large.
# not that big of an issue.

def get_time_standard_fwd(k, v):

    x = torch.randn(batch_size, dim_in, dtype=torch.float16).cuda()
    g = torch.randn(batch_size, dim_out, dtype=torch.float16).cuda()

    ##### time matmul 1
    for _ in range(repeat // 2):
        g.t().matmul(x)

    torch.cuda.synchronize()
    start = time.time()
    for _ in range(repeat):
        g.t().matmul(x)

    torch.cuda.synchronize()
    end = time.time()
    print(f"time {k}: {(end - start) / repeat * 1000:.3f} ms")
    return (end - start) / repeat * 1000

if __name__ == '__main__':
    torch.manual_seed(0)
    #for (dim, wm) in [(1024, 4), (1280, 4), (1408, 4.3637), (1664, 4.9231), (2048, 4), (4096, 4), (8096, 4)]
    for (dim, wm) in [(1408, 4), (1664, 4),]:

        for batch_size in [256*32, 256*64, 256*128, 256*256, 256*512]:
            #for batch_size in [256*256, 256*512]:

            for switch in [False, True]:


                # hparams
                repeat = 64
                batch_size = batch_size
                dim_out = dim * wm
                dim_in = dim
                if switch:
                    dim_out = dim
                    dim_in = wm * dim

                dim_in = round(dim_in)
                dim_out = round(dim_out)


                # simulate forward pass
                x = torch.randn(batch_size, dim_in, dtype=torch.float16).cuda()
                g = torch.randn(batch_size, dim_out, dtype=torch.float16).cuda()
                w = torch.randn(dim_out, dim_in, dtype=torch.float16).cuda()
                
                x_int8 = x.clone().to(torch.int8)
                g_int8 = g.clone().to(torch.int8)
                w_int8 = w.clone().to(torch.int8)
                wt_int8 = w.t().contiguous().clone().to(torch.int8)
                state_x_rowwise = x.max(dim=1)[0]
                state_g_rowwise = g.max(dim=1)[0]
                state_w_columnwise = w.max(dim=0)[0]
                state_w_rowwise = w.max(dim=1)[0]
                state_w_global = w.max()

                info = {'repeat' : repeat, 'batch_size' : batch_size, 'dim_out' : dim_out, 'dim_in' : dim_in, 'wm' : wm, 'switch' : switch}

                k = 'standard_fwd'
                for _ in range(repeat // 2):
                    x.matmul(w.t())

                torch.cuda.synchronize()
                start = time.time()
                for _ in range(repeat):
                    x.matmul(w.t())

                torch.cuda.synchronize()
                end = time.time()
                ms = (end - start) / repeat * 1000
                print(f"time {k}: {ms:.3f} ms")
                info[k] = ms

                k = 'standard_gw'
                for _ in range(repeat // 2):
                    g.t().matmul(x)

                torch.cuda.synchronize()
                start = time.time()
                for _ in range(repeat):
                    g.t().matmul(x)

                torch.cuda.synchronize()
                end = time.time()
                ms = (end - start) / repeat * 1000
                print(f"time {k}: {ms:.3f} ms")
                info[k] = ms


                k = 'standard_gx'
                for _ in range(repeat // 2):
                    g.matmul(w)

                torch.cuda.synchronize()
                start = time.time()
                for _ in range(repeat):
                    g.matmul(w)

                torch.cuda.synchronize()
                end = time.time()
                ms = (end - start) / repeat * 1000
                print(f"time {k}: {ms:.3f} ms")
                info[k] = ms



                k = 'rowwise_fwd'
                for _ in range(repeat // 2):
                    int8_matmul_rowwise_dequantize(x_int8, w_int8.t(), state_x_rowwise, state_w_columnwise)

                torch.cuda.synchronize()
                start = time.time()
                for _ in range(repeat):
                    int8_matmul_rowwise_dequantize(x_int8, w_int8.t(), state_x_rowwise, state_w_columnwise)

                torch.cuda.synchronize()
                end = time.time()
                ms = (end - start) / repeat * 1000
                print(f"time {k}: {ms:.3f} ms")
                info[k] = ms

                k = 'rowwise_bwd'
                for _ in range(repeat // 2):
                    int8_matmul_rowwise_dequantize(g_int8, wt_int8.t(), state_x_rowwise, state_w_rowwise)

                torch.cuda.synchronize()
                start = time.time()
                for _ in range(repeat):
                    int8_matmul_rowwise_dequantize(g_int8, wt_int8.t(), state_x_rowwise, state_w_rowwise)

                torch.cuda.synchronize()
                end = time.time()
                ms = (end - start) / repeat * 1000
                print(f"time {k}: {ms:.3f} ms")
                info[k] = ms


                k = 'global_fwd'
                for _ in range(repeat // 2):
                    int8_matmul_mixed_dequanitze(x_int8, w_int8.t(), state_x_rowwise, state_w_global)

                torch.cuda.synchronize()
                start = time.time()
                for _ in range(repeat):
                    int8_matmul_mixed_dequanitze(x_int8, w_int8.t(), state_x_rowwise, state_w_global)

                torch.cuda.synchronize()
                end = time.time()
                ms = (end - start) / repeat * 1000
                print(f"time {k}: {ms:.3f} ms")
                info[k] = ms


                k = 'global_bwd'
                for _ in range(repeat // 2):
                    int8_matmul_mixed_dequanitze(g_int8, wt_int8.t(), state_x_rowwise, state_w_global)

                torch.cuda.synchronize()
                start = time.time()
                for _ in range(repeat):
                    int8_matmul_mixed_dequanitze(g_int8, wt_int8.t(), state_x_rowwise, state_w_global)

                torch.cuda.synchronize()
                end = time.time()
                ms = (end - start) / repeat * 1000
                print(f"time {k}: {ms:.3f} ms")
                info[k] = ms


                k = 'x_quantize_rowwise'
                for _ in range(repeat // 2):
                    quantize_rowwise_nogroup(x)

                torch.cuda.synchronize()
                start = time.time()
                for _ in range(repeat):
                    quantize_rowwise_nogroup(x)

                torch.cuda.synchronize()
                end = time.time()
                ms = (end - start) / repeat * 1000
                print(f"time {k}: {ms:.3f} ms")
                info[k] = ms

                k = 'g_quantize_rowwise'
                for _ in range(repeat // 2):
                    quantize_rowwise_nogroup(g)

                torch.cuda.synchronize()
                start = time.time()
                for _ in range(repeat):
                    quantize_rowwise_nogroup(g)

                torch.cuda.synchronize()
                end = time.time()
                ms = (end - start) / repeat * 1000
                print(f"time {k}: {ms:.3f} ms")
                info[k] = ms

                k = 'w_quantize_rowwise'
                for _ in range(repeat // 2):
                    quantize_rowwise_nogroup(w)

                torch.cuda.synchronize()
                start = time.time()
                for _ in range(repeat):
                    quantize_rowwise_nogroup(w)

                torch.cuda.synchronize()
                end = time.time()
                ms = (end - start) / repeat * 1000
                print(f"time {k}: {ms:.3f} ms")
                info[k] = ms


                k = 'w_quantize_colwise_transpose'
                for _ in range(repeat // 2):
                    quantize_columnwise_nogroup_transpose(w)

                torch.cuda.synchronize()
                start = time.time()
                for _ in range(repeat):
                    quantize_columnwise_nogroup_transpose(w)

                torch.cuda.synchronize()
                end = time.time()
                ms = (end - start) / repeat * 1000
                print(f"time {k}: {ms:.3f} ms")
                info[k] = ms


                k = 'w_quantize_global'
                for _ in range(repeat // 2):
                    quantize_global(w)

                torch.cuda.synchronize()
                start = time.time()
                for _ in range(repeat):
                    quantize_global(w)

                torch.cuda.synchronize()
                end = time.time()
                ms = (end - start) / repeat * 1000
                print(f"time {k}: {ms:.3f} ms")
                info[k] = ms

                k = 'w_quantize_global_transpose'
                for _ in range(repeat // 2):
                    quantize_global_transpose(w)

                torch.cuda.synchronize()
                start = time.time()
                for _ in range(repeat):
                    quantize_global_transpose(w)

                torch.cuda.synchronize()
                end = time.time()
                ms = (end - start) / repeat * 1000
                print(f"time {k}: {ms:.3f} ms")
                info[k] = ms


                k = 'cast_x'
                for _ in range(repeat // 2):
                    newx = x.to(torch.int8)

                torch.cuda.synchronize()
                start = time.time()
                for _ in range(repeat):
                    newx = x.to(torch.int8)

                torch.cuda.synchronize()
                end = time.time()
                ms = (end - start) / repeat * 1000
                print(f"time {k}: {ms:.3f} ms")
                info[k] = ms



                k = 'cast_g'
                for _ in range(repeat // 2):
                    newx = g.to(torch.int8)

                torch.cuda.synchronize()
                start = time.time()
                for _ in range(repeat):
                    newx = g.to(torch.int8)

                torch.cuda.synchronize()
                end = time.time()
                ms = (end - start) / repeat * 1000
                print(f"time {k}: {ms:.3f} ms")
                info[k] = ms



                k = 'cast_w'
                for _ in range(repeat // 2):
                    newx = w.to(torch.int8)

                torch.cuda.synchronize()
                start = time.time()
                for _ in range(repeat):
                    newx = w.to(torch.int8)

                torch.cuda.synchronize()
                end = time.time()
                ms = (end - start) / repeat * 1000
                print(f"time {k}: {ms:.3f} ms")
                info[k] = ms


                time_standard = info['standard_fwd'] + info['standard_gx'] + info['standard_gw']
                time_rowwise = info['x_quantize_rowwise'] + info['g_quantize_rowwise']  + info['w_quantize_colwise_transpose'] + info['w_quantize_rowwise'] + info['standard_gw'] + info['rowwise_fwd'] + info['rowwise_bwd']
                time_global = info['x_quantize_rowwise'] + info['g_quantize_rowwise'] + info['w_quantize_global'] + info['w_quantize_global_transpose'] + info['standard_gw'] + info['global_fwd'] + info['global_bwd']

                print('TOTAL STANDARD', time_standard)
                print('TOTAL ROWWISE', time_rowwise)
                print('TOTAL GLOBAL', time_global)

                print('speedup', -100*(time_global - time_standard)/time_standard)

                info['time_standard'] = time_standard
                info['time_rowwise'] = time_rowwise
                info['time_global'] = time_global



                info_json = json.dumps(info)


                with open("tests/triton_tests/info.jsonl", "a") as file:
                    file.write(info_json + "\n")