-
Notifications
You must be signed in to change notification settings - Fork 284
Expand file tree
/
Copy pathsemi_structured_sparse.py
More file actions
614 lines (537 loc) ยท 28.1 KB
/
semi_structured_sparse.py
File metadata and controls
614 lines (537 loc) ยท 28.1 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
# -*- coding: utf-8 -*-
"""
(beta) ๋ฐ๊ตฌ์กฐ์ (2:4) ํฌ์์ฑ์ ํตํ BERT ๊ฐ์ํ
=====================================================
**์ ์**: `Jesse Cai <https://github.com/jcaip>`_
**๋ฒ์ญ**: `์ด์ฑ์ด <https://github.com/dlcodns>`_
"""
####################################################################
# ๊ฐ์
# ----
#
# ๋ค๋ฅธ ํํ์ ํฌ์์ฑ(sparsity)์ฒ๋ผ, **๋ฐ๊ตฌ์กฐ์ ํฌ์์ฑ**์ ์ ๊ฒฝ๋ง์ ๋ฉ๋ชจ๋ฆฌ ์ค๋ฒํค๋์ ์ง์ฐ ์๊ฐ์
# ์ค์ด๊ธฐ ์ํ ๋ชจ๋ธ ์ต์ ํ ๊ธฐ๋ฒ์ผ๋ก, ์ผ๋ถ ๋ชจ๋ธ ์ ํ๋๋ ํฌ์ํ๊ฒ ๋ฉ๋๋ค. ์ด ๋ฐฉ๋ฒ์
# **์ธ๋ถํ๋ ๊ตฌ์กฐ์ ํฌ์์ฑ** ๋๋ **2:4 ๊ตฌ์กฐ์ ํฌ์์ฑ**์ผ๋ก๋ ์๋ ค์ ธ ์์ต๋๋ค.
#
# ๋ฐ๊ตฌ์กฐ์ ํฌ์์ฑ์ ๊ณ ์ ํ ํฌ์์ฑ ํจํด์์ ์ ๋ํ๋ฉฐ, ์ฌ๊ธฐ์ 2n๊ฐ์ ์์ ์ค n๊ฐ์ ์์๊ฐ
# ๊ฐ์ง์น๊ธฐ(prune)๋ฉ๋๋ค. ์ผ๋ฐ์ ์ผ๋ก n=2์ธ ๊ฒฝ์ฐ๊ฐ ๋ง์ 2:4 ํฌ์์ฑ์ด๋ผ๊ณ ๋ถ๋ฆ
๋๋ค.
# ๋ฐ๊ตฌ์กฐ์ ํฌ์์ฑ์ GPU์์ ํจ์จ์ ์ผ๋ก ๊ฐ์ํ๋ ์ ์๊ณ , ๋ค๋ฅธ ํฌ์์ฑ ํจํด๋งํผ ๋ชจ๋ธ
# ์ ํ๋๋ฅผ ์ ํ์ํค์ง ์๊ธฐ ๋๋ฌธ์ ํนํ ํฅ๋ฏธ๋กญ์ต๋๋ค.
#
# `๋ฐ๊ตฌ์กฐ์ ํฌ์์ฑ ์ง์ <https://pytorch.org/docs/2.1/sparse.html#sparse-semi-structured-tensors>`_,
# ์ด ๋์
๋๋ฉด์, PyTorch๋ฅผ ๋ฒ์ด๋์ง ์๊ณ ๋ ๋ฐ๊ตฌ์กฐ์ ํฌ์ ๋ชจ๋ธ์ ๊ฐ์ง์น๊ธฐํ๊ณ ๊ฐ์ํํ
# ์ ์์ต๋๋ค. ์ด ํํ ๋ฆฌ์ผ์์๋ ์ด ๊ณผ์ ์ ์ค๋ช
ํ ๊ฒ์
๋๋ค.
#
# .. image:: ../../_static/img/pruning_flow.jpg
#
# ํํ ๋ฆฌ์ผ์ด ๋๋๋ฉด BERT ์ง๋ฌธ-์๋ต ๋ชจ๋ธ์ 2:4 ํฌ์ํํ์ฌ ๊ฑฐ์ ๋ชจ๋ F1 ์์ค์ ํ๋ณตํ
# ์ํ(86.92์ ๋ฐ์ง ๋ชจ๋ธ vs 86.48์ ํฌ์ ๋ชจ๋ธ)๋ก ๋ฏธ์ธ ์กฐ์ ํ ๊ฒ์
๋๋ค. ๋ง์ง๋ง์ผ๋ก
# ์ด 2:4 ํฌ์ ๋ชจ๋ธ์ ์ถ๋ก ์ ์ํด ๊ฐ์ํํ์ฌ 1.3๋ฐฐ ์๋ ํฅ์์ ๋ฌ์ฑํ ๊ฒ์
๋๋ค.
#
#####################################################
# ์๊ตฌ์ฌํญ
# --------
#
# - PyTorch >= 2.1.
# - ๋ฐ๊ตฌ์กฐ์ ํฌ์์ฑ์ ์ง์ํ๋ NVIDIA GPU(Compute Capability 8.0+)
#
# ์ด ํํ ๋ฆฌ์ผ์ ์ด๋ณด์์๊ฒ ๋ฐ๊ตฌ์กฐ์ ํฌ์์ฑ ๋ฐ ์ผ๋ฐ์ ์ธ ํฌ์์ฑ์ ๋ง์ถค ์ค๋ช
ํฉ๋๋ค.
# ์ด๋ฏธ 2:4 ํฌ์ ๋ชจ๋ธ์ ๋ณด์ ํ ์ฌ์ฉ์์๊ฒ๋ ``to_sparse_semi_structured``๋ฅผ ์ฌ์ฉํ์ฌ
# ์ถ๋ก ์ ์ํ ``nn.Linear`` ๋ ์ด์ด๋ฅผ ๊ฐ์ํํ๋ ๊ฒ์ด ๋งค์ฐ ๊ฐ๋จํฉ๋๋ค. ๋ค์์ ๊ทธ ์์์
๋๋ค:
#
import torch
from torch.sparse import to_sparse_semi_structured, SparseSemiStructuredTensor
from torch.utils.benchmark import Timer
SparseSemiStructuredTensor._FORCE_CUTLASS = True
# Linear ๊ฐ์ค์น๋ฅผ 2:4 ํฌ์์ฑ์ผ๋ก ๋ง์คํน
mask = torch.Tensor([0, 0, 1, 1]).tile((3072, 2560)).cuda().bool()
linear = torch.nn.Linear(10240, 3072).half().cuda().eval()
linear.weight = torch.nn.Parameter(mask * linear.weight)
x = torch.rand(3072, 10240).half().cuda()
with torch.inference_mode():
dense_output = linear(x)
dense_t = Timer(stmt="linear(x)",
globals={"linear": linear,
"x": x}).blocked_autorange().median * 1e3
# SparseSemiStructuredTensor๋ฅผ ํตํด ๊ฐ์ํ
linear.weight = torch.nn.Parameter(to_sparse_semi_structured(linear.weight))
sparse_output = linear(x)
sparse_t = Timer(stmt="linear(x)",
globals={"linear": linear,
"x": x}).blocked_autorange().median * 1e3
# ํฌ์ ๋ฐ ๋ฐ์ง ํ๋ ฌ ๊ณฑ์
์ ์์น์ ์ผ๋ก ๋์ผํจ
# A100 80GB์์, ๋ค์๊ณผ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ํ์ธ: `Dense: 0.870ms Sparse: 0.630ms | Speedup: 1.382x`
assert torch.allclose(sparse_output, dense_output, atol=1e-3)
print(f"Dense: {dense_t:.3f}ms Sparse: {sparse_t:.3f}ms | Speedup: {(dense_t / sparse_t):.3f}x")
######################################################################
# ๋ฐ๊ตฌ์กฐ์ ํฌ์์ฑ์ ์ด๋ค ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋๊ฐ?
# ------------------------------------------------------
#
# ํฌ์์ฑ์ ์ผ๋ฐ์ ์ธ ๋ชฉ์ ์ ๊ฐ๋จํฉ๋๋ค: ๋คํธ์ํฌ ๋ด์ 0์ด ์๋ ๊ฒฝ์ฐ,
# ํด๋น ๋งค๊ฐ๋ณ์๋ฅผ ์ ์ฅํ๊ฑฐ๋ ๊ณ์ฐํ์ง ์์์ผ๋ก์จ ํจ์จ์ฑ์ ์ต์ ํํ ์ ์์ต๋๋ค.
# ๊ทธ๋ฌ๋ ํฌ์์ฑ์ ๊ตฌ์ฒด์ ์ธ ๊ตฌํ์ ๊น๋ค๋กญ์ต๋๋ค. ๋งค๊ฐ๋ณ์๋ฅผ 0์ผ๋ก ๋ง๋๋ ๊ฒ๋ง์ผ๋ก๋
# ๊ธฐ๋ณธ์ ์ผ๋ก ๋ชจ๋ธ์ ์ง์ฐ ์๊ฐ / ๋ฉ๋ชจ๋ฆฌ ์ค๋ฒํค๋์ ์ํฅ์ ๋ฏธ์น์ง ์์ต๋๋ค.
#
# ๊ทธ ์ด์ ๋ dense tensor๊ฐ ์ฌ์ ํ ๊ฐ์ง์น๊ธฐ๋(0์ธ) ์์๋ฅผ ํฌํจํ๊ณ ์์ผ๋ฉฐ,
# ๋ฐ์ง ํ๋ ฌ ๊ณฑ์
์ปค๋์ด ์ด๋ฌํ ์์์ ๋ํด ๊ณ์ ์ฐ์ฐ์ ์ํํ๊ธฐ ๋๋ฌธ์
๋๋ค. ์ฑ๋ฅ ํฅ์์
# ์คํํ๋ ค๋ฉด, ๋ฐ์ง ์ปค๋์ ๊ฐ์ง์น๊ธฐ๋ ์์์ ๊ณ์ฐ์ ๊ฑด๋๋ฐ๋ ํฌ์ ์ปค๋๋ก ๊ต์ฒดํด์ผ ํฉ๋๋ค.
#
# ์ด๋ฅผ ์ํด, ํฌ์ ์ปค๋์ ๊ฐ์ง์น๊ธฐ๋ ์์๋ฅผ ์ ์ฅํ์ง ์๊ณ , ์ง์ ๋ ์์๋ฅผ ์์ถ๋ ํ์์ผ๋ก
# ์ ์ฅํ๋ ํฌ์ ํ๋ ฌ์ ์ฌ์ฉํฉ๋๋ค.
#
# ๋ฐ๊ตฌ์กฐ์ ํฌ์์ฑ์ ๊ฒฝ์ฐ, ์๋ ๋งค๊ฐ๋ณ์์ ์ ํํ ์ ๋ฐ๊ณผ ์์๊ฐ ์ด๋ป๊ฒ
# ๋ฐฐ์ด๋์๋์ง์ ๋ํ ์์ถ๋ ๋ฉํ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํฉ๋๋ค.
#
# .. image:: https://developer-blogs.nvidia.com/wp-content/uploads/2023/06/2-4-structured-sparsity-pattern.png
# :align: center
# :width: 80%
#
# Image sourced from `NVIDIA blog post <https://developer.nvidia.com/blog/structured-sparsity-in-the-nvidia-ampere-architecture-and-applications-in-search-engines/>`_ on semi-structured sparsity.
#
# ํฌ์ ๋ ์ด์์์๋ ๊ฐ๊ธฐ ๋ค๋ฅธ ์ฅ์ ๊ณผ ๋จ์ ์ ๊ฐ์ง ์ฌ๋ฌ ๊ฐ์ง๊ฐ ์์ต๋๋ค. 2:4 ๋ฐ๊ตฌ์กฐ์
# ํฌ์ ๋ ์ด์์์ ํฅ๋ฏธ๋ก์ด ๋ ๊ฐ์ง ์ด์ ๊ฐ ์์ต๋๋ค.
#
# * ์ด์ ์ ํฌ์ ํ์๊ณผ ๋ฌ๋ฆฌ ๋ฐ๊ตฌ์กฐ์ ํฌ์์ฑ์ GPU์์ ํจ์จ์ ์ผ๋ก ๊ฐ์๋๋๋ก ์ค๊ณ๋์์ต๋๋ค.
# 2020๋
NVIDIA๋ Ampere ์ํคํ
์ฒ๋ฅผ ํตํด ๋ฐ๊ตฌ์กฐ์ ํฌ์์ฑ์ ์ํ ํ๋์จ์ด ์ง์์ ๋์
ํ์ผ๋ฉฐ,
# CUTLASS `cuSPARSELt <https://docs.nvidia.com/cuda/cusparselt/index.html>`__
# cuSPARSELt๋ฅผ ํตํด ๋น ๋ฅธ ํฌ์ ์ปค๋๋ ์ถ์ํ์ต๋๋ค.
#
# * ๋์์ ๋ฐ๊ตฌ์กฐ์ ํฌ์์ฑ์ ๋ค๋ฅธ ํฌ์ ํ์์ ๋นํด ๋ชจ๋ธ ์ ํ๋์ ๋ฏธ์น๋ ์ํฅ์ด ๋ํ ๊ฒฝํฅ์ด ์์ต๋๋ค.
# ํนํ ๋ ๋ฐ์ ๋ ๊ฐ์ง์น๊ธฐ ๋ฐ ๋ฏธ์ธ ์กฐ์ ๋ฐฉ๋ฒ์ ๊ณ ๋ คํ ๋ ๊ทธ๋ ์ต๋๋ค.
# NVIDIA๊ฐ ๊ณต๊ฐํ `๋ฐฑ์ <https://arxiv.org/abs/2104.08378>`_
# ์์ 2:4 ํฌ์์ฑ์ ๋ชฉํ๋ก ํ ๋จ์ํ ํฌ๊ธฐ ๊ธฐ์ค ๊ฐ์ง์น๊ธฐ(magnitude pruning) ํ ๋ชจ๋ธ์
# ์ฌํ์ตํ๋ฉด ๊ฑฐ์ ๋์ผํ ๋ชจ๋ธ ์ ํ๋๋ฅผ ๋ฌ์ฑํ ์ ์์์ ๋ณด์ฌ์ฃผ์์ต๋๋ค.
#
# ๋ฐ๊ตฌ์กฐ์ ํฌ์์ฑ์ ์ด๋ก ์ ์ผ๋ก 2๋ฐฐ์ ์๋ ํฅ์์ ์ ๊ณตํ๋ฉด์๋ ํฌ์์ฑ ์์ค์ด ๋ฎ๊ณ (50%), ๋ชจ๋ธ
# ์ ํ๋๋ฅผ ์ ์งํ๊ธฐ์ ์ถฉ๋ถํ ์ธ๋ฐํ ์ ์ ํ ๊ท ํ์ ์ ์ ๊ณตํฉ๋๋ค.
#
# +---------------------+-------------+--------+------------+-------------+
# | ๋คํธ์ํฌ | ๋ฐ์ดํฐ ์ธํธ | ๋ฉํธ๋ฆญ | Dense FP16 | Sparse FP16 |
# +=====================+=============+========+============+=============+
# | ResNet-50 | ImageNet | Top-1 | 76.1 | 76.2 |
# +---------------------+-------------+--------+------------+-------------+
# | ResNeXt-101_32x8d | ImageNet | Top-1 | 79.3 | 79.3 |
# +---------------------+-------------+--------+------------+-------------+
# | Xception | ImageNet | Top-1 | 79.2 | 79.2 |
# +---------------------+-------------+--------+------------+-------------+
# | SSD-RN50 | COCO2017 | bbAP | 24.8 | 24.8 |
# +---------------------+-------------+--------+------------+-------------+
# | MaskRCNN-RN50 | COCO2017 | bbAP | 37.9 | 37.9 |
# +---------------------+-------------+--------+------------+-------------+
# | FairSeq Transformer | EN-DE WMT14 | BLEU | 28.2 | 28.5 |
# +---------------------+-------------+--------+------------+-------------+
# | BERT-Large | SQuAD v1.1 | F1 | 91.9 | 91.9 |
# +---------------------+-------------+--------+------------+-------------+
#
# ๋ฐ๊ตฌ์กฐ์ ํฌ์์ฑ์ ์ํฌํ๋ก ๊ด์ ์์ ์ถ๊ฐ์ ์ธ ์ฅ์ ์ด ์์ต๋๋ค. ํฌ์์ฑ ์์ค์ด 50%๋ก ๊ณ ์ ๋์ด
# ์์ด ๋ชจ๋ธ์ ํฌ์ํํ๋ ๋ฌธ์ ๋ฅผ ๋ ๊ฐ์ง ๋ณ๊ฐ์ ํ์ ๋ฌธ์ ๋ก ๋ถํดํ๊ธฐ๊ฐ ๋ ์ฝ์ต๋๋ค.
#
# - ์ ํ๋ - 2:4 ํฌ์ ๊ฐ์ค์น ์ธํธ๋ฅผ ์ฐพ์ ๋ชจ๋ธ์ ์ ํ๋ ์ ํ๋ฅผ ์ต์ํํ ์ ์๋ ๋ฐฉ๋ฒ์ ๋ฌด์์ธ๊ฐ์?
#
# - ์ฑ๋ฅ: ์ถ๋ก ๋ฐ ๋ฉ๋ชจ๋ฆฌ ์ค๋ฒํค๋๋ฅผ ์ค์ด๊ธฐ ์ํด 2:4 ํฌ์ ๊ฐ์ค์น๋ฅผ ์ด๋ป๊ฒ ๊ฐ์ํํ ์ ์๋๊ฐ?
#
#####################################################################
# .. math::
#
# \begin{bmatrix}
# 1 & 1 & 0 & 0 \\
# 0 & 0 & 1 & 1 \\
# 1 & 0 & 0 & 0 \\
# 0 & 0 & 1 & 1 \\
# \end{bmatrix}
#
# ์ด ๋ ๋ฌธ์ ์ฌ์ด์ ์์ฐ์ค๋ฌ์ด ์ฐ๊ฒฐ์ ์ 0์ผ๋ก ๋ ๋ฐ์ง ํ
์์
๋๋ค. ์ฐ๋ฆฌ์ ์ถ๋ก ์๋ฃจ์
์ ์ด๋ฌํ ํ์์
# ํ
์๋ฅผ ์์ถํ๊ณ ๊ฐ์ํ๋๋ก ์ค๊ณ๋์์ต๋๋ค. ์ด๋ ํ๋ฐํ ์ฐ๊ตฌ ๋ถ์ผ์ด๊ธฐ ๋๋ฌธ์ ๋ง์ ์ฌ์ฉ์๊ฐ ๋ง์ถคํ ๋ง์คํน
# ์๋ฃจ์
์ ๊ณ ์ํ ๊ฒ์ผ๋ก ์์๋ฉ๋๋ค.
#
# ๋ฐ๊ตฌ์กฐ์ ํฌ์์ฑ์ ๋ํด ์กฐ๊ธ ๋ ๋ฐฐ์ ์ผ๋, ์ด์ ์ง๋ฌธ ์๋ต ์์
์ธ SQuAD์ ๋ํด ํ์ต๋ BERT ๋ชจ๋ธ์ ์ด๋ฅผ
# ์ ์ฉํด ๋ด
์๋ค.
#
# ์๊ฐ & ์ค์
# -----------
#
# ํ์ํ ๋ชจ๋ ํจํค์ง๋ฅผ ๋ถ๋ฌ์ค๋ ๊ฒ์ผ๋ก ์์ํ๊ฒ ์ต๋๋ค.
#
# ๋ง์ฝ Google Colab์์ ์คํ ์ค์ด๋ผ๋ฉด, ๋ค์ ๋ช
๋ น์ด๋ฅผ ์คํํ์ธ์:
# .. code-block: python
#
# !pip install datasets transformers evaluate accelerate pandas
#
import os
os.environ["WANDB_DISABLED"] = "true"
import collections
import datasets
import evaluate
import numpy as np
import torch
import torch.utils.benchmark as benchmark
from torch import nn
from torch.sparse import to_sparse_semi_structured, SparseSemiStructuredTensor
from torch.ao.pruning import WeightNormSparsifier
import transformers
# ``cuSPARSELt``๊ฐ ์ฌ์ฉ ๋ถ๊ฐ๋ฅํ ๊ฒฝ์ฐ, ๊ฐ์ ๋ก CUTLASS๋ฅผ ์ฌ์ฉํฉ๋๋ค.
SparseSemiStructuredTensor._FORCE_CUTLASS = True
torch.manual_seed(100)
# ๊ธฐ๋ณธ ์ฅ์น๋ฅผ "cuda:0"์ผ๋ก ์ค์ ํฉ๋๋ค.
torch.set_default_device(torch.device("cuda:0" if torch.cuda.is_available() else "cpu"))
######################################################################
# ์ฐ๋ฆฌ๊ฐ ๋ค๋ฃจ๊ณ ์๋ ๋ฐ์ดํฐ์
/์์
์ ํนํ๋ ๋ช ๊ฐ์ง ๋ณด์กฐ ํจ์๋
# ์ ์ํด์ผ ํฉ๋๋ค. ์ด๋ฌํ ํจ์๋ค์ Hugging Face ์ฝ์ค์
# `์ด ์๋ฃ <https://huggingface.co/learn/nlp-course/chapter7/7?fw=pt>`__
# ๋ฅผ ์ฐธ๊ณ ํ์ฌ ์์ ๋์์ต๋๋ค.
#
def preprocess_validation_function(examples, tokenizer):
inputs = tokenizer(
[q.strip() for q in examples["question"]],
examples["context"],
max_length=384,
truncation="only_second",
return_overflowing_tokens=True,
return_offsets_mapping=True,
padding="max_length",
)
sample_map = inputs.pop("overflow_to_sample_mapping")
example_ids = []
for i in range(len(inputs["input_ids"])):
sample_idx = sample_map[i]
example_ids.append(examples["id"][sample_idx])
sequence_ids = inputs.sequence_ids(i)
offset = inputs["offset_mapping"][i]
inputs["offset_mapping"][i] = [
o if sequence_ids[k] == 1 else None for k, o in enumerate(offset)
]
inputs["example_id"] = example_ids
return inputs
def preprocess_train_function(examples, tokenizer):
inputs = tokenizer(
[q.strip() for q in examples["question"]],
examples["context"],
max_length=384,
truncation="only_second",
return_offsets_mapping=True,
padding="max_length",
)
offset_mapping = inputs["offset_mapping"]
answers = examples["answers"]
start_positions = []
end_positions = []
for i, (offset, answer) in enumerate(zip(offset_mapping, answers)):
start_char = answer["answer_start"][0]
end_char = start_char + len(answer["text"][0])
sequence_ids = inputs.sequence_ids(i)
# ๋ฌธ๋งฅ์ ์์๊ณผ ๋์ ์ฐพ๊ธฐ
idx = 0
while sequence_ids[idx] != 1:
idx += 1
context_start = idx
while sequence_ids[idx] == 1:
idx += 1
# ๋ต๋ณ์ด ๋ฌธ๋งฅ ์์ ์์ ํ ํฌํจ๋์ง ์๋ ๊ฒฝ์ฐ, (0, 0)์ผ๋ก ๋ ์ด๋ธ ์ง์
if offset[context_start][0] > end_char or offset[context_end][1] < start_char:
start_positions.append(0)
end_positions.append(0)
else:
# ๊ทธ๋ ์ง ์์ผ๋ฉด ์์ ๋ฐ ๋ ํ ํฐ ์์น๋ฅผ ์ง์
idx = context_start
while idx <= context_end and offset[idx][0] <= start_char:
idx += 1
start_positions.append(idx - 1)
idx = context_end
while idx >= context_start and offset[idx][1] >= end_char:
idx -= 1
end_positions.append(idx + 1)
inputs["start_positions"] = start_positions
inputs["end_positions"] = end_positions
return inputs
def compute_metrics(start_logits, end_logits, features, examples):
n_best = 20
max_answer_length = 30
metric = evaluate.load("squad")
example_to_features = collections.defaultdict(list)
for idx, feature in enumerate(features):
example_to_features[feature["example_id"]].append(idx)
predicted_answers = []
# ์๋ฅผ ๋ค์ด ``tqdm``(examples)์์:
for example in examples:
example_id = example["id"]
context = example["context"]
answers = []
# ํด๋น ์์ ์ ๊ด๋ จ๋ ๋ชจ๋ ํน์ฑ(feature)์ ๋ฐ๋ณต
for feature_index in example_to_features[example_id]:
start_logit = start_logits[feature_index]
end_logit = end_logits[feature_index]
offsets = features[feature_index]["offset_mapping"]
start_indexes = np.argsort(start_logit)[-1 : -n_best - 1 : -1].tolist()
end_indexes = np.argsort(end_logit)[-1 : -n_best - 1 : -1].tolist()
for start_index in start_indexes:
for end_index in end_indexes:
# ๋ฌธ๋งฅ์ ์์ ํ ํฌํจ๋์ง ์์ ๋ต๋ณ์ ๊ฑด๋๋
if offsets[start_index] is None or offsets[end_index] is None:
continue
# ๊ธธ์ด๊ฐ 0๋ณด๋ค ์๊ฑฐ๋
# max_answer_length๋ณด๋ค ํฐ ๋ต๋ณ์ ๊ฑด๋๋
if (
end_index < start_index
or end_index - start_index + 1 > max_answer_length
):
continue
answer = {
"text": context[
offsets[start_index][0] : offsets[end_index][1]
],
"logit_score": start_logit[start_index] + end_logit[end_index],
}
answers.append(answer)
# ๊ฐ์ฅ ๋์ ์ ์๋ฅผ ๊ฐ์ง ๋ต๋ณ์ ์ ํ
if len(answers) > 0:
best_answer = max(answers, key=lambda x: x["logit_score"])
predicted_answers.append(
{"id": example_id, "prediction_text": best_answer["text"]}
)
else:
predicted_answers.append({"id": example_id, "prediction_text": ""})
theoretical_answers = [
{"id": ex["id"], "answers": ex["answers"]} for ex in examples
]
return metric.compute(predictions=predicted_answers, references=theoretical_answers)
######################################################################
# ์ด์ ์ด๋ฌํ ํจ์๋ค์ด ์ ์๋์์ผ๋ฏ๋ก, ๋ชจ๋ธ์ ๋ฒค์น๋งํฌ๋ฅผ ๋์์ค ์ถ๊ฐ์ ์ธ ๋ณด์กฐ ํจ์ ํ๋๋ง ๋ ํ์ํฉ๋๋ค.
#
def measure_execution_time(model, batch_sizes, dataset):
dataset_for_model = dataset.remove_columns(["example_id", "offset_mapping"])
dataset_for_model.set_format("torch")
batch_size_to_time_sec = {}
for batch_size in batch_sizes:
batch = {
k: dataset_for_model[k][:batch_size].cuda()
for k in dataset_for_model.column_names
}
with torch.no_grad():
baseline_predictions = model(**batch)
timer = benchmark.Timer(
stmt="model(**batch)", globals={"model": model, "batch": batch}
)
p50 = timer.blocked_autorange().median * 1000
batch_size_to_time_sec[batch_size] = p50
model_c = torch.compile(model, fullgraph=True)
timer = benchmark.Timer(
stmt="model(**batch)", globals={"model": model_c, "batch": batch}
)
p50 = timer.blocked_autorange().median * 1000
batch_size_to_time_sec[f"{batch_size}_compile"] = p50
new_predictions = model_c(**batch)
return batch_size_to_time_sec
######################################################################
# ๋ชจ๋ธ๊ณผ ํ ํฌ๋์ด์ ๋ฅผ ๋ก๋ํ ํ, ๋ฐ์ดํฐ์
์ ์ค์ ํ๋ฉด์ ์์ํ๊ฒ ์ต๋๋ค.
#
# ๋ชจ๋ธ ๋ถ๋ฌ์ค๊ธฐ
model_name = "bert-base-cased"
tokenizer = transformers.AutoTokenizer.from_pretrained(model_name)
model = transformers.AutoModelForQuestionAnswering.from_pretrained(model_name)
print(f"Loading tokenizer: {model_name}")
print(f"Loading model: {model_name}")
# ํ์ต ๋ฐ ๊ฒ์ฆ ๋ฐ์ดํฐ์
์ค์
squad_dataset = datasets.load_dataset("squad")
tokenized_squad_dataset = {}
tokenized_squad_dataset["train"] = squad_dataset["train"].map(
lambda x: preprocess_train_function(x, tokenizer), batched=True
)
tokenized_squad_dataset["validation"] = squad_dataset["validation"].map(
lambda x: preprocess_validation_function(x, tokenizer),
batched=True,
remove_columns=squad_dataset["train"].column_names,
)
data_collator = transformers.DataCollatorWithPadding(tokenizer=tokenizer)
######################################################################
# ๊ธฐ์ค ์ฑ๋ฅ ์ค์
# =======================
#
# ๋ค์์ผ๋ก, SQuAD ๋ฐ์ดํฐ์
์์ ๋ชจ๋ธ์ ๋น ๋ฅธ ๊ธฐ์ค ์ฑ๋ฅ์ ํ์ต์์ผ ๋ณด๊ฒ ์ต๋๋ค. ์ด ์์
์ ๋ชจ๋ธ์ด ์ฃผ์ด์ง
# ๋ฌธ๋งฅ(์ํคํผ๋์ ๊ธฐ์ฌ)์์ ์ฃผ์ด์ง ์ง๋ฌธ์ ๋ํ ๋ต๋ณ์ด ๋๋ ํ
์คํธ์ ๋ฒ์ ๋๋ ๊ตฌ๊ฐ์ ์๋ณํ๋๋ก
# ์๊ตฌํฉ๋๋ค. ๋ค์ ์ฝ๋๋ฅผ ์คํํ๋ฉด F1 ์ ์๋ 86.9๊ฐ ๋์ต๋๋ค. ์ด๋ ๋ณด๊ณ ๋ NVIDIA ์ ์์ ๋งค์ฐ ๊ฐ๊น๊ณ ,
# ์ฐจ์ด๋ ์๋ง๋ BERT-base์ BERT-large ๋๋ ๋ฏธ์ธ ์กฐ์ ํ์ดํผํ๋ผ๋ฏธํฐ ๋๋ฌธ์ผ ๊ฒ์
๋๋ค.
#
training_args = transformers.TrainingArguments(
"trainer",
num_train_epochs=1,
lr_scheduler_type="constant",
per_device_train_batch_size=32,
per_device_eval_batch_size=256,
logging_steps=50,
# ํํ ๋ฆฌ์ผ ์คํ์ ์ํ ์ต๋ ๋จ๊ณ ์ ํ. ๋ณด๊ณ ๋ ์ ํ๋ ์์น๋ฅผ ๋ณด๋ ค๋ฉด ์๋ ์ค์ ์ญ์ ํ์ธ์.
max_steps=500,
report_to=None,
)
trainer = transformers.Trainer(
model,
training_args,
train_dataset=tokenized_squad_dataset["train"],
eval_dataset=tokenized_squad_dataset["validation"],
data_collator=data_collator,
tokenizer=tokenizer,
)
trainer.train()
# ํ๊ฐ๋ฅผ ์ํ ๋น๊ต ๋ฐฐ์น ํฌ๊ธฐ
batch_sizes = [4, 16, 64, 256]
# 2:4 ํฌ์์ฑ์ fp16์ ํ์๋ก ํ๋ฏ๋ก, ๊ณต์ ํ ๋น๊ต๋ฅผ ์ํด ์ฌ๊ธฐ์์ ์บ์คํ
ํจ
with torch.autocast("cuda"):
with torch.no_grad():
predictions = trainer.predict(tokenized_squad_dataset["validation"])
start_logits, end_logits = predictions.predictions
fp16_baseline = compute_metrics(
start_logits,
end_logits,
tokenized_squad_dataset["validation"],
squad_dataset["validation"],
)
fp16_time = measure_execution_time(
model,
batch_sizes,
tokenized_squad_dataset["validation"],
)
print("fp16", fp16_baseline)
print("cuda_fp16 time", fp16_time)
import pandas as pd
df = pd.DataFrame(trainer.state.log_history)
df.plot.line(x='step', y='loss', title="Loss vs. # steps", ylabel="loss")
######################################################################
# BERT๋ฅผ 2:4 ํฌ์์ฑ์ผ๋ก ๊ฐ์ง์น๊ธฐ
# -----------------------------
#
# ์ด์ ๊ธฐ์ค ์ฑ๋ฅ์ ์ค์ ํ์ผ๋, BERT๋ฅผ ๊ฐ์ง์น๊ธฐํ ์ฐจ๋ก์
๋๋ค. ๊ฐ์ง์น๊ธฐ์๋ ์ฌ๋ฌ ๊ฐ์ง ์ ๋ต์ด ์์ง๋ง,
# ๊ฐ์ฅ ์ผ๋ฐ์ ์ธ ๋ฐฉ๋ฒ ์ค ํ๋๋ **ํฌ๊ธฐ ๊ธฐ๋ฐ ๊ฐ์ง์น๊ธฐ**๋ก, ์ด๋ L1 norm์ด ๊ฐ์ฅ
# ๋ฎ์ ๊ฐ์ค์น๋ฅผ ์ ๊ฑฐํ๋ ๋ฐฉ๋ฒ์
๋๋ค. NVIDIA๋ ๋ชจ๋ ๊ฒฐ๊ณผ์์ ํฌ๊ธฐ ๊ธฐ๋ฐ ๊ฐ์ง์น๊ธฐ๋ฅผ ์ฌ์ฉํ์ผ๋ฉฐ, ์ด๋
# ์ผ๋ฐ์ ์ธ ๊ธฐ์ค ๋ฐฉ๋ฒ์
๋๋ค.
#
# ์ด๋ฅผ ์ํด ์ฐ๋ฆฌ๋ ``torch.ao.pruning`` ํจํค์ง๋ฅผ ์ฌ์ฉํ ๊ฒ์
๋๋ค. ์ด ํจํค์ง์๋ ๊ฐ์ค์น-norm ํฌ์ํ
# ๋๊ตฌ๊ฐ ํฌํจ๋์ด ์์ต๋๋ค. ์ด๋ฌํ ํฌ์ํ ๋๊ตฌ๋ ๋ชจ๋ธ์ ๊ฐ์ค์น ํ
์์ ๋ง์คํฌ ๋งค๊ฐ๋ณ์ํ๋ฅผ ์ ์ฉํ์ฌ
# ์๋ํฉ๋๋ค. ์ด๋ฅผ ํตํด ๊ฐ์ง์น๊ธฐ๋ ๊ฐ์ค์น๋ฅผ ๋ง์คํนํ์ฌ ํฌ์์ฑ์ ์๋ฎฌ๋ ์ด์
ํ ์ ์์ต๋๋ค.
#
# ๋ํ, ๋ชจ๋ธ์ ์ด๋ ๋ ์ด์ด์ ํฌ์์ฑ์ ์ ์ฉํ ์ง ๊ฒฐ์ ํด์ผ ํฉ๋๋ค. ์ด ๊ฒฝ์ฐ์๋ ๊ฐ๊ฐ์ ํ
์คํฌ ํค๋ ์ถ๋ ฅ์
# ์ ์ธํ ๋ชจ๋ ``nn.Linear`` ๋ ์ด์ด์ ์ ์ฉํฉ๋๋ค. ์ด๋ ๋ฐ๊ตฌ์กฐ์ ํฌ์์ฑ(semi-structured sparsity)์ด
# `ํ์ ์ ์ฝ <https://pytorch.org/docs/2.1/sparse.html#constructing-sparse-semi-structured-tensors>`_
# ์ ๊ฐ์ง๊ธฐ ๋๋ฌธ์ด๋ฉฐ, ๊ฐ๊ฐ์ task ``nn.Linear`` ๋ ์ด์ด๋ ์ด๋ฌํ ์ ์ฝ์ ์ถฉ์กฑํ์ง ์๊ธฐ ๋๋ฌธ์
๋๋ค.
#
sparsifier = WeightNormSparsifier(
# ๋ชจ๋ ๋ธ๋ก์ ํฌ์์ฑ ์ ์ฉ
sparsity_level=1.0,
# 4๊ฐ์ ์์๊ฐ ํ๋์ ๋ธ๋ก์ ํํ
sparse_block_shape=(1, 4),
# 4๊ฐ์ ๋ธ๋ก๋ง๋ค ๋ ๊ฐ์ 0์ด ํฌํจ๋จ
zeros_per_block=2
)
# BERT ๋ชจ๋ธ์ ``nn.Linear``๊ฐ ์๋ ๊ฒฝ์ฐ ์ค์ ์ ์ถ๊ฐ
sparse_config = [
{"tensor_fqn": f"{fqn}.weight"}
for fqn, module in model.named_modules()
if isinstance(module, nn.Linear) and "layer" in fqn
]
######################################################################
# ๋ชจ๋ธ์ ๋ชจ๋ธ์ ๊ฐ์ง์น๊ธฐ๋ฅผ ์ํ ์ฒซ๋ฒ์งธ ๋จ๊ณ๋ ๋ชจ๋ธ์ ๊ฐ์ค์น๋ฅผ ๋ง์คํนํ๊ธฐ ์ํ ๋งค๊ฐ๋ณ์ํ๋ฅผ ์ฝ์
ํ๋ ๊ฒ์
๋๋ค.
# ์ด๋ ์ค๋น ๋จ๊ณ์์ ์ํ๋ฉ๋๋ค. ์ด๋ ๊ฒ ํ๋ฉด ``.weight``์ ์ ๊ทผํ ๋๋ง๋ค ๋์ ``mask * weight``๋ฅผ
# ์ป๊ฒ ๋ฉ๋๋ค.
#
# ๋ชจ๋ธ์ ์ค๋นํ๊ณ , ํ์ต์ ์ํ ๊ฐ์ง ํฌ์์ฑ ๋งค๊ฐ๋ณ์์ํ๋ฅผ ์ฝ์
ํฉ๋๋ค.
sparsifier.prepare(model, sparse_config)
print(model.bert.encoder.layer[0].output)
######################################################################
# ๊ทธ ๋ค์, ๋จ์ผ ๊ฐ์ง์น๊ธฐ ๋จ๊ณ๋ฅผ ์ํํฉ๋๋ค. ๋ชจ๋ ๊ฐ์ง์น๊ธฐ ๋๊ตฌ(pruner)๋ ๊ฐ์ง์น๊ธฐ ๋๊ตฌ์ ๊ตฌํ
# ๋
ผ๋ฆฌ์ ๋ฐ๋ผ ๋ง์คํฌ๋ฅผ ์
๋ฐ์ดํธํ๋ ``update_mask()`` ๋ฉ์๋๋ฅผ ๊ตฌํํฉ๋๋ค. ์ด ๋จ๊ณ ๋ฉ์๋๋
# ํฌ์์ฑ ์ค์ (sparse config)์์ ์ง์ ๋ ๊ฐ์ค์น์ ๋ํด ์ด ``update_mask`` ํจ์๋ฅผ ํธ์ถํฉ๋๋ค.
#
# ๋ํ ๋ชจ๋ธ์ ํ๊ฐํ์ฌ ๋ฏธ์ธ ์กฐ์ /์ฌํ์ต ์์ด ๊ฐ์ง์น๊ธฐ(zero-shot) ๋๋ ๊ฐ์ง์น๊ธฐ์ ์ ํ๋ ์ ํ๋ฅผ ๋ณด์ฌ์ค ๊ฒ์
๋๋ค.
#
sparsifier.step()
with torch.autocast("cuda"):
with torch.no_grad():
predictions = trainer.predict(tokenized_squad_dataset["validation"])
pruned = compute_metrics(
*predictions.predictions,
tokenized_squad_dataset["validation"],
squad_dataset["validation"],
)
print("pruned eval metrics:", pruned)
######################################################################
# ์ด ์ํ์์ ๋ชจ๋ธ์ ๋ฏธ์ธ ์กฐ์ (fine-tuning)ํ์ฌ ๊ฐ์ง์น๊ธฐ๋์ง ์๋ ์์๋ค์ ์
๋ฐ์ดํธํ๊ณ , ์ ํ๋ ์์ค์
# ๋ณด์ํ ์ ์์ต๋๋ค. ๋ง์กฑํ ๋งํ ์ํ์ ๋๋ฌํ๋ฉด, ``squash_mask``๋ฅผ ํธ์ถํ์ฌ ๋ง์คํฌ์
# ๊ฐ์ค์น๋ฅผ ํ๋๋ก ๊ฒฐํฉํ ์ ์์ต๋๋ค. ์ด๋ ๊ฒ ํ๋ฉด ๋งค๊ฐ๋ณ์ํ๊ฐ ์ ๊ฑฐ๋๊ณ , 0์ผ๋ก ๋ 2:4 ๋ฐ์ง
# ๋ชจ๋ธ์ด ๋จ๊ฒ ๋ฉ๋๋ค.
#
trainer.train()
sparsifier.squash_mask()
torch.set_printoptions(edgeitems=4)
print(model.bert.encoder.layer[0].intermediate.dense.weight[:8, :8])
df["sparse_loss"] = pd.DataFrame(trainer.state.log_history)["loss"]
df.plot.line(x='step', y=["loss", "sparse_loss"], title="Loss vs. # steps", ylabel="loss")
######################################################################
# ์ถ๋ก ์ ์ํ 2:4 ํฌ์ ๋ชจ๋ธ ๊ฐ์ํ
# --------------------------------------------
#
# ์ด์ ์ด ํ์์ ๋ชจ๋ธ์ ์ป์์ผ๋ฏ๋ก, QuickStart ๊ฐ์ด๋์์์ฒ๋ผ ์ถ๋ก ์ ์ํด ๊ฐ์ํ ์ ์์ต๋๋ค.
#
model = model.cuda().half()
# ํฌ์์ฑ์ ์ํด ๊ฐ์ํ
for fqn, module in model.named_modules():
if isinstance(module, nn.Linear) and "layer" in fqn:
module.weight = nn.Parameter(to_sparse_semi_structured(module.weight))
with torch.no_grad():
predictions = trainer.predict(tokenized_squad_dataset["validation"])
start_logits, end_logits = predictions.predictions
metrics_sparse = compute_metrics(
start_logits,
end_logits,
tokenized_squad_dataset["validation"],
squad_dataset["validation"],
)
print("sparse eval metrics: ", metrics_sparse)
sparse_perf = measure_execution_time(
model,
batch_sizes,
tokenized_squad_dataset["validation"],
)
print("sparse perf metrics: ", sparse_perf)
######################################################################
# ํฌ๊ธฐ ๊ธฐ๋ฐ ๊ฐ์ง์น๊ธฐ ํ ๋ชจ๋ธ์ ์ฌํ์ตํ ๊ฒฐ๊ณผ, ๊ฐ์ง์น๊ธฐ ์ ์์ค๋์๋ F1
# ์ ์์ ๊ฑฐ์ ๋๋ถ๋ถ์ด ํ๋ณต๋์์ต๋๋ค. ๋์์ ``bs=16``์ ๋ฐฐ์น ํฌ๊ธฐ์์ 1.28๋ฐฐ์ ์๋ ํฅ์์
# ๋ฌ์ฑํ์ต๋๋ค. ํ์ง๋ง ๋ชจ๋ ํ์์ด ์ฑ๋ฅ ํฅ์์ ์ ํฉํ ๊ฒ์ ์๋๋๋ค. ๋ฐฐ์น ํฌ๊ธฐ๊ฐ ์๊ณ ๊ณ์ฐ์
# ์ฌ์ฉ๋๋ ์๊ฐ์ด ์ ํ์ ์ผ ๋๋ ํฌ์ ์ปค๋์ด ๋ฐ์ง ์ปค๋๋ณด๋ค ๋ ๋๋ฆด ์ ์์ต๋๋ค.
#
# ๋ฐ๊ตฌ์กฐ์ ํฌ์์ฑ(semi-structured sparsity)์ ํ
์์ ํ์ ํด๋์ค(subclass)๋ก ๊ตฌํ๋์ด ์๊ธฐ ๋๋ฌธ์,
# ``torch.compile``๊ณผ ํธํ๋ฉ๋๋ค. ``to_sparse_semi_structured``์ ํจ๊ป ์ฌ์ฉํ๋ฉด BERT์์ ์ด
# 2๋ฐฐ์ ์๋ ํฅ์์ ์ป์ ์ ์์ต๋๋ค.
#
# .. table::
#
# +--------------------+--------+--------------+-------------------+-----------+
# | Metrics | fp16 | 2:4 ํฌ์์ฑ | ๋ณํ / ์๋ ์ฆ๊ฐ์จ | ์ปดํ์ผ๋จ |
# +====================+========+==============+===================+===========+
# | ์ ํ๋ ์ผ์น (%) | 78.53 | 78.44 | -0.09 | |
# +--------------------+--------+--------------+-------------------+-----------+
# | F1 (%) | 86.93 | 86.49 | -0.44 | |
# +--------------------+--------+--------------+-------------------+-----------+
# | Time (bs=4) | 11.10 | 15.54 | 0.71x | no |
# +--------------------+--------+--------------+-------------------+-----------+
# | Time (bs=16) | 19.35 | 15.74 | 1.23x | no |
# +--------------------+--------+--------------+-------------------+-----------+
# | Time (bs=64) | 72.71 | 59.41 | 1.22x | no |
# +--------------------+--------+--------------+-------------------+-----------+
# | Time (bs=256) | 286.65 | 247.63 | 1.14x | no |
# +--------------------+--------+--------------+-------------------+-----------+
# | Time (bs=4) | 7.59 | 7.46 | 1.02x | yes |
# +--------------------+--------+--------------+-------------------+-----------+
# | Time (bs=16) | 11.47 | 9.68 | 1.18x | yes |
# +--------------------+--------+--------------+-------------------+-----------+
# | Time (bs=64) | 41.57 | 36.92 | 1.13x | yes |
# +--------------------+--------+--------------+-------------------+-----------+
# | Time (bs=256) | 159.22 | 142.23 | 1.12x | yes |
# +--------------------+--------+--------------+-------------------+-----------+
#
# ๊ฒฐ๋ก
# ==========
#
# ์ด ํํ ๋ฆฌ์ผ์์๋ BERT๋ฅผ 2:4 ํฌ์์ฑ์ผ๋ก ๊ฐ์ง์น๊ธฐํ๋ ๋ฐฉ๋ฒ๊ณผ 2:4 ํฌ์ ๋ชจ๋ธ์ ์ถ๋ก ์ฉ์ผ๋ก ๊ฐ์ํ๋
# ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ฃผ์์ต๋๋ค. ``SparseSemiStructuredTensor`` ํ์ ํด๋์ค๋ฅผ ํ์ฉํ์ฌ fp16 ๊ธฐ์ค ์ฑ๋ฅ์
# ๋นํด 1.3๋ฐฐ์ ์๋ ํฅ์์ ๋ฌ์ฑํ์ผ๋ฉฐ, ``torch.compile``์ ์ฌ์ฉํ๋ฉด ์ต๋ 2๋ฐฐ๊น์ง ์๋ ํฅ์์ ์ด๋ฃฐ
# ์ ์์์ต๋๋ค. ๋ํ, BERT๋ฅผ ๋ฏธ์ธ ์กฐ์ ํ์ฌ ์์ค๋ F1 ์ ์(๋ฐ์ง ๋ชจ๋ธ: 86.92 vs ํฌ์ ๋ชจ๋ธ: 86.48)๋ฅผ
# ํ๋ณตํ๋ ๊ณผ์ ์์ 2:4 ํฌ์์ฑ์ ์ด์ ์ ์
์ฆํ์ต๋๋ค.
#