This repository was archived by the owner on Feb 24, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathmatmul_dequantize_impl.py
More file actions
977 lines (892 loc) · 35.5 KB
/
matmul_dequantize_impl.py
File metadata and controls
977 lines (892 loc) · 35.5 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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# pre-transformed tir expression of matmul
from bitblas import tvm
from tvm import te, DataType
from tvm.tir import IndexMap
from bitblas.ops.operator import TransformKind
from bitblas.gpu.matmul_analysis import get_propagate_map
from bitblas.quantization import (
_tir_packed_int_to_int_convert,
_tir_packed_to_signed_convert,
_tir_packed_to_unsigned_convert,
_tir_u32_to_f4_to_f16,
_tir_u8_to_f8_e4m3_to_f16,
_tir_packed_to_unsigned_convert_with_zeros,
)
from typing import Union
class MatMulNTDequantizeEmitter:
def __init__(
self,
M,
N,
K,
in_dtype="float16",
out_dtype="float16",
accum_dtype="float16",
bit=4,
storage_dtype="int8",
source_format="uint",
with_scaling=False,
with_zeros=False,
group_size=-1,
fast_decoding=False,
with_bias=False,
zeros_mode="original",
propagate_a: TransformKind = TransformKind.NonTransform,
propagate_b: TransformKind = TransformKind.NonTransform,
):
self.M = self._validate_dimension(M, "M")
self.N = N
self.K = K
self.in_dtype = in_dtype
self.out_dtype = out_dtype
self.accum_dtype = accum_dtype
self.bit = bit
self.storage_dtype = storage_dtype
self.source_format = source_format
self.with_scaling = with_scaling
self.with_zeros = with_zeros
self.group_size = group_size if group_size != -1 else K
self.fast_decoding = fast_decoding
self.with_bias = with_bias
self.zeros_mode = zeros_mode
self.propagate_a = self._legalize_transform_kind(propagate_a)
self.propagate_b = self._legalize_transform_kind(propagate_b)
self._validate_bit()
self._validate_layout()
@staticmethod
def _validate_dimension(dim, name):
if not isinstance(dim, int):
return tvm.te.var(name.lower())
return dim
def _validate_bit(self):
if self.bit not in [1, 2, 4, 8]:
raise ValueError(f"Unsupported bit: {self.bit}")
def _validate_layout(self):
# TODO: extend the dequantize operators into General Layout
pass
def _legalize_group_size(self):
if self.group_size == -1:
self.group_size = self.K
def _legalize_transform_kind(self, propagate):
if propagate is None:
return TransformKind.NonTransform
if isinstance(propagate, bool):
return (TransformKind.IntraWarpTransform if propagate else TransformKind.NonTransform)
elif isinstance(propagate, int):
return TransformKind(propagate)
def _create_placeholders(self):
storage_dtype = self.storage_dtype
storage_nbit = int("".join(c for c in storage_dtype if c.isdigit()))
in_dtype = self.in_dtype
bit = self.bit
l = r = 16 # noqa: E741
if in_dtype in ["int8", "e4m3_float8", "e5m2_float8"]:
l, r = 16, 32 # noqa: E741
A = te.placeholder((self.M, self.K), name="A", dtype=in_dtype)
B = te.placeholder((self.N, self.K // storage_nbit * bit), name="B", dtype=storage_dtype)
if self.propagate_a:
A = te.placeholder((self.M // l, self.K // r, l, r), name="A", dtype=in_dtype)
if self.propagate_b:
target_dtype = DataType(in_dtype)
scaling_factor = 1
if bit > 0 and bit < target_dtype.bits:
scaling_factor = ((target_dtype.bits // bit) * DataType(storage_dtype).bits //
target_dtype.bits)
qr = r * bit // storage_nbit
B = te.placeholder((self.N // l, (self.K // scaling_factor) // qr, l, qr),
name="B",
dtype=storage_dtype)
LUT = te.placeholder((1 << bit,), name="LUT", dtype=in_dtype)
Scale = te.placeholder((self.N, self.K // self.group_size), name="Scale", dtype=in_dtype)
Zeros = te.placeholder((self.N, self.K // self.group_size), name="Zeros", dtype=in_dtype)
QZeros = te.placeholder(((self.K // self.group_size), self.N // storage_nbit * bit),
name="QZeros",
dtype=self.storage_dtype)
Bias = te.placeholder((self.N,), name="Bias", dtype=in_dtype)
return A, B, LUT, Scale, Zeros, QZeros, Bias
def _propagate_input(self, tensor, transform_kind=TransformKind.NonTransform, matrix_name="A"):
if transform_kind == TransformKind.NonTransform:
return tensor
in_dtype = self.in_dtype
l = r = 16 # noqa: E741
if in_dtype in ["int8", "e4m3_float8", "e5m2_float8"]:
l, r = 16, 32 # noqa: E741
_, inversed_index_map = get_propagate_map(
trans=False, dtype=in_dtype, matrix_name=matrix_name)
def fcompute(i, j):
warp_i, warp_j = i % l, j % r
spatial_args = i // l, j // r
if transform_kind >= TransformKind.IntraWarpTransform:
warp_i, warp_j = inversed_index_map.map_indices([warp_i, warp_j])
new_index = (*spatial_args, warp_i, warp_j)
return tensor[new_index]
return te.compute(
(self.M, self.K),
fcompute,
name=f"{matrix_name}_reindex",
)
def _propagage_weight(self, tensor, transform_kind=TransformKind.NonTransform, matrix_name="B"):
if transform_kind == TransformKind.NonTransform:
return tensor
in_dtype = self.in_dtype
bit = self.bit
storage_dtype = self.storage_dtype
storage_nbit = int("".join(c for c in self.storage_dtype if c.isdigit()))
l = r = 16 # noqa: E741
if in_dtype in ["int8", "e4m3_float8", "e5m2_float8"]:
l, r = 16, 32 # noqa: E741
_, inversed_index_map = get_propagate_map(
trans=True, dtype=in_dtype, matrix_name=matrix_name)
target_dtype = DataType(in_dtype)
scaling_factor = 1
if bit > 0 and bit < target_dtype.bits:
scaling_factor = ((target_dtype.bits // bit) * DataType(storage_dtype).bits //
target_dtype.bits)
initial_indices = inversed_index_map.initial_indices
scaling_final_indices = inversed_index_map.map_indices(
initial_indices[:-1] + [initial_indices[-1] * scaling_factor])
scaling_final_indices = scaling_final_indices[:-1] + [
scaling_final_indices[-1] // scaling_factor
]
inversed_index_map = IndexMap(
initial_indices,
scaling_final_indices,
None,
)
qr = r * bit // storage_nbit
def fcompute(i, j):
warp_i, warp_j = i % l, j % qr
spatial_args = i // l, j // qr
if transform_kind >= TransformKind.IntraWarpTransform:
warp_i, warp_j = inversed_index_map.map_indices([warp_i, warp_j])
new_index = (*spatial_args, warp_i, warp_j)
return tensor[new_index]
return te.compute(
(self.N, self.K // storage_nbit * bit),
fcompute,
name=f"{matrix_name}_reindex",
)
def _decode_func(self, B, LUT, Scale, Zeros, QZeros):
bit = self.bit
in_dtype = self.in_dtype
storage_dtype = self.storage_dtype
storage_nbit = int("".join(c for c in storage_dtype if c.isdigit()))
storage_type = str("".join(c for c in storage_dtype if not c.isdigit()))
n_float_per_elem = storage_nbit // bit
# TODO: Move the decode function into a more general place
def decode(n, k):
w = None
if self.with_zeros and self.zeros_mode == "quantized":
qzeros_dequantize = _tir_packed_to_unsigned_convert(storage_type, storage_nbit)(
bit,
QZeros[k, n // n_float_per_elem],
n % n_float_per_elem,
dtype=self.storage_dtype,
)
w = _tir_packed_to_unsigned_convert_with_zeros(storage_type, storage_nbit)(
bit,
B[n, k // n_float_per_elem],
k % n_float_per_elem,
qzeros_dequantize,
dtype=in_dtype,
)
elif self.source_format == "uint":
if bit == 8:
w = B[n, k].astype(in_dtype)
w = _tir_packed_to_unsigned_convert(storage_type, storage_nbit)(
bit, B[n, k // n_float_per_elem], k % n_float_per_elem, dtype=in_dtype)
elif self.source_format == "int":
if bit == 1:
w = _tir_packed_int_to_int_convert(storage_type, storage_nbit)(
bit, B[n, k // n_float_per_elem], k % n_float_per_elem, dtype=in_dtype)
if bit == 8:
w = B[n, k].astype(in_dtype)
w = _tir_packed_to_signed_convert(storage_type, storage_nbit)(
bit, B[n, k // n_float_per_elem], k % n_float_per_elem, dtype=in_dtype)
elif self.source_format == "fp":
w = _tir_u32_to_f4_to_f16(
bit, B[n, k // n_float_per_elem], k % n_float_per_elem, dtype=in_dtype)
elif self.source_format == "fp_e4m3":
w = _tir_u8_to_f8_e4m3_to_f16(bit, B[n, k], dtype=in_dtype)
elif self.source_format == "nf":
index = _tir_packed_to_unsigned_convert(storage_type, storage_nbit)(
bit,
B[n, k // n_float_per_elem],
k % n_float_per_elem,
dtype="int32",
)
w = LUT[index]
else:
raise ValueError(f"Unsupported source_format: {self.source_format}")
assert w is not None, "w is None"
group_size = self.group_size
zeros_mode = self.zeros_mode
if not self.with_scaling:
return w
if not self.with_zeros:
return w * Scale[n, k // group_size]
if zeros_mode == "original":
w = (w - Zeros[n, k // group_size]) * Scale[n, k // group_size]
elif zeros_mode == "rescale":
w = w * Scale[n, k // group_size] - Zeros[n, k // group_size]
elif zeros_mode == "quantized":
w = w * Scale[n, k // group_size]
else:
raise ValueError("Unsupported zeros_mode: {}".format(zeros_mode))
return w
return te.compute((self.N, self.K), decode, name="B_decode")
def _compute_matmul(self, A, B_decode):
k = te.reduce_axis((0, self.K), name="k")
C = te.compute(
(self.M, self.N),
lambda i, j: te.sum(
A[i, k].astype(self.accum_dtype) * B_decode[j, k].astype(self.accum_dtype), axis=k),
name="C",
)
return C
def _convert_dtype(self, tensor):
if self.accum_dtype != self.out_dtype:
return te.compute((self.M, self.N),
lambda i, j: tensor[i, j].astype(self.out_dtype),
name="D")
return tensor
def _apply_bias(self, tensor, Bias):
if self.with_bias:
return te.compute((self.M, self.N), lambda i, j: tensor[i, j] + Bias[j], name="E")
return tensor
def emit(self):
A, B, LUT, Scale, Zeros, QZeros, Bias = self._create_placeholders()
A_reindex = self._propagate_input(A, self.propagate_a, "A")
B_reindex = self._propagage_weight(B, self.propagate_b, "B")
B_decode = self._decode_func(B_reindex, LUT, Scale, Zeros, QZeros)
C = self._compute_matmul(A_reindex, B_decode)
D = self._convert_dtype(C)
last_output = self._apply_bias(D, Bias)
args = [A, B]
if self.source_format == "nf":
args.append(LUT)
if self.with_scaling:
args.append(Scale)
if self.with_zeros:
args.append(QZeros if self.zeros_mode == "quantized" else Zeros)
if self.with_bias:
args.append(Bias)
args.append(last_output)
func = te.create_prim_func(args).with_attr(
"dequantize_info",
{
"B_decode": {
"decode_block": "B_decode",
"fast_decoding": self.fast_decoding,
"source_format": {
"bits": self.bit,
"format": self.source_format,
},
"storage_dtype": self.storage_dtype,
"target_format": self.in_dtype,
"with_zeros": self.with_zeros,
"zeros_mode": self.zeros_mode,
"with_scaling": self.with_scaling,
"group_size": self.group_size,
}
},
)
if self.propagate_a:
func = func.with_attr("input_transform_kind", self.propagate_a.value)
if self.propagate_b:
func = func.with_attr("weight_transform_kind", self.propagate_b.value)
return tvm.IRModule.from_expr(func)
def matmul_nt_dequantize_b(
M,
N,
K,
in_dtype="float16",
out_dtype="float16",
accum_dtype="float16",
bit=4,
storage_dtype="int8",
source_format="uint",
with_scaling=False,
with_zeros=False,
group_size=-1,
fast_decoding=False,
with_bias=False,
zeros_mode="original",
):
assert bit in [1, 2, 4, 8], "Unsupported bit: {}".format(bit)
if not isinstance(M, int):
M = tvm.te.var("m")
storage_nbit = int("".join(c for c in storage_dtype if c.isdigit()))
storage_type = str("".join(c for c in storage_dtype if not c.isdigit()))
n_float_per_elem = storage_nbit // bit
if group_size == -1:
group_size = K
A = te.placeholder((M, K), name="A", dtype=in_dtype)
B = te.placeholder((N, K // storage_nbit * bit), name="B", dtype=storage_dtype)
LUT = te.placeholder((1 << bit,), name="LUT", dtype=in_dtype)
Scale = te.placeholder((N, K // group_size), name="Scale", dtype=in_dtype)
Zeros = te.placeholder((N, K // group_size), name="Zeros", dtype=in_dtype)
QZeros = te.placeholder(((K // group_size), N // storage_nbit * bit),
name="QZeros",
dtype=storage_dtype)
Bias = te.placeholder((N,), name="Bias", dtype=in_dtype)
def qzeros_dequantize(k, n):
return _tir_packed_to_unsigned_convert(storage_type, storage_nbit)(
bit,
QZeros[k, n // n_float_per_elem],
n % n_float_per_elem,
dtype=storage_dtype,
)
Dequantize_qzeros = None
if with_zeros and zeros_mode == "quantized":
Dequantize_qzeros = te.compute(
(K // group_size, N),
qzeros_dequantize,
name="Dequantize_zeros",
)
def decode_func(n, k):
if with_zeros and zeros_mode == "quantized":
assert Dequantize_qzeros is not None, "Dequantize_zeros is None"
w = _tir_packed_to_unsigned_convert_with_zeros(storage_type, storage_nbit)(
bit,
B[n, k // n_float_per_elem],
k % n_float_per_elem,
Dequantize_qzeros[k // group_size, n],
dtype=in_dtype,
)
elif source_format == "uint":
if bit == 8:
# 8 bit does not need to be compressed
w = B[n, k].astype(in_dtype)
else:
w = _tir_packed_to_unsigned_convert(storage_type, storage_nbit)(
bit, B[n, k // n_float_per_elem], k % n_float_per_elem, dtype=in_dtype)
elif source_format == "int":
if bit == 1:
# Dequantize int1 to -1 and 1. Without this step, the values would be 0 and 1, identical to uint1.
w = _tir_packed_int_to_int_convert(storage_type, storage_nbit)(
bit, B[n, k // n_float_per_elem], k % n_float_per_elem, dtype=in_dtype)
elif bit == 8:
# 8 bit does not need to be compressed
w = B[n, k].astype(in_dtype)
else:
w = _tir_packed_to_signed_convert(storage_type, storage_nbit)(
bit, B[n, k // n_float_per_elem], k % n_float_per_elem, dtype=in_dtype)
elif source_format == "fp":
w = _tir_u32_to_f4_to_f16(
bit, B[n, k // n_float_per_elem], k % n_float_per_elem, dtype=in_dtype)
elif source_format == "fp_e4m3":
w = _tir_u8_to_f8_e4m3_to_f16(bit, B[n, k], dtype=in_dtype)
elif source_format == "nf":
w = LUT[_tir_packed_to_unsigned_convert(storage_type, storage_nbit)(
bit,
B[n, k // n_float_per_elem],
k % n_float_per_elem,
dtype="int32", # assume the index data type is int32
)]
else:
raise ValueError("Unsupported source_format: {}".format(source_format))
if not with_scaling:
return w
if not with_zeros:
return w * Scale[n, k // group_size]
if zeros_mode == "original":
w = (w - Zeros[n, k // group_size]) * Scale[n, k // group_size]
elif zeros_mode == "rescale":
w = w * Scale[n, k // group_size] - Zeros[n, k // group_size]
elif zeros_mode == "quantized":
w = w * Scale[n, k // group_size]
else:
raise ValueError("Unsupported zeros_mode: {}".format(zeros_mode))
return w
B_decode = te.compute((N, K), decode_func, name="B_decode")
# Describe the matrix multiplication in TE
k = te.reduce_axis((0, K), name="k")
C = te.compute(
(M, N),
lambda i, j: te.sum(
A[i, k].astype(accum_dtype) * B_decode[j, k].astype(accum_dtype), axis=k),
name="C",
)
last_output = C
if accum_dtype != out_dtype:
D = te.compute((M, N), lambda i, j: C[i, j].astype(out_dtype), name="D")
last_output = D
args = [A, B]
if source_format == "nf":
args.append(LUT)
if with_scaling:
args.append(Scale)
if with_zeros:
if zeros_mode == "quantized":
args.append(QZeros)
else:
args.append(Zeros)
if with_bias:
last_output = te.compute((M, N), lambda i, j: last_output[i, j] + Bias[j], name="E")
args.append(Bias)
args.append(last_output)
func = te.create_prim_func(args).with_attr(
"dequantize_info",
{
"B_decode": {
"decode_block": "B_decode",
"fast_decoding": fast_decoding,
"source_format": {
"bits": bit,
"format": source_format,
},
"storage_dtype": storage_dtype,
"target_format": in_dtype,
"with_scaling": with_scaling,
"with_zeros": with_zeros,
"zeros_mode": zeros_mode,
"group_size": group_size,
}
},
)
return tvm.IRModule.from_expr(func)
def matmul_nt_dequantize_b_propagate_b(
M,
N,
K,
in_dtype="float16",
out_dtype="float16",
accum_dtype="float16",
bit=4,
storage_dtype="int8",
source_format="uint",
with_scaling=False,
with_zeros=False,
group_size=-1,
fast_decoding=False,
with_bias=False,
zeros_mode="original",
transform_kind: Union[int, TransformKind] = TransformKind.NonTransform,
):
if isinstance(transform_kind, int):
transform_kind = TransformKind(transform_kind)
assert bit in [1, 2, 4, 8], "Unsupported bit: {}".format(bit)
if not isinstance(M, int):
M = tvm.te.var("m")
l = r = 16 # noqa: E741
if in_dtype in ["int8", "e4m3_float8", "e5m2_float8"]:
l, r = 16, 32 # noqa: E741
_, inverse_indexmap = get_propagate_map(trans=True, dtype=in_dtype, matrix_name="B")
target_dtype = DataType(in_dtype)
scaling_factor = 1
if bit > 0 and bit < target_dtype.bits:
scaling_factor = ((target_dtype.bits // bit) * DataType(storage_dtype).bits //
target_dtype.bits)
initial_indices = inverse_indexmap.initial_indices
scaling_final_indices = inverse_indexmap.map_indices(initial_indices[:-1] +
[initial_indices[-1] * scaling_factor])
scaling_final_indices = scaling_final_indices[:-1] + [
scaling_final_indices[-1] // scaling_factor
]
inverse_indexmap = IndexMap(
initial_indices,
scaling_final_indices,
None,
)
storage_nbit = int("".join(c for c in storage_dtype if c.isdigit()))
storage_type = str("".join(c for c in storage_dtype if not c.isdigit()))
n_float_per_elem = storage_nbit // bit
if group_size == -1:
group_size = K
qr = r * bit // storage_nbit
A = te.placeholder((M, K), name="A", dtype=in_dtype)
B = te.placeholder((N // l, (K // scaling_factor) // qr, l, qr), name="B", dtype=storage_dtype)
LUT = te.placeholder((1 << bit,), name="LUT", dtype=in_dtype)
Scale = te.placeholder((N, K // group_size), name="Scale", dtype=in_dtype)
Zeros = te.placeholder((N, K // group_size), name="Zeros", dtype=in_dtype)
Bias = te.placeholder((N,), name="Bias", dtype=in_dtype)
def fcompute(i, j):
warp_i, warp_j = i % l, j % qr
spatial_args = i // l, j // qr
if transform_kind >= TransformKind.IntraWarpTransform:
warp_i, warp_j = inverse_indexmap.map_indices([warp_i, warp_j])
new_index = (*spatial_args, warp_i, warp_j)
return B[new_index]
B_reindex = te.compute(
(N, K // storage_nbit * bit),
fcompute,
name="B_reindex",
)
def decode_func(n, k):
if source_format == "uint":
if bit == 8:
# 8 bit does not need to be compressed
w = B_reindex[n, k].astype(in_dtype)
else:
w = _tir_packed_to_unsigned_convert(storage_type, storage_nbit)(
bit,
B_reindex[n, k // n_float_per_elem],
k % n_float_per_elem,
dtype=in_dtype,
)
elif source_format == "int":
if bit == 1:
# Dequantize int1 to -1 and 1. Without this step, the values would be 0 and 1, identical to uint1.
w = _tir_packed_int_to_int_convert(storage_type, storage_nbit)(
bit, B_reindex[n, k // n_float_per_elem], k % n_float_per_elem, dtype=in_dtype)
elif bit == 8:
# 8 bit does not need to be compressed
w = B_reindex[n, k].astype(in_dtype)
else:
w = _tir_packed_to_signed_convert(storage_type, storage_nbit)(
bit,
B_reindex[n, k // n_float_per_elem],
k % n_float_per_elem,
dtype=in_dtype,
)
elif source_format == "fp":
w = _tir_u32_to_f4_to_f16(
bit,
B_reindex[n, k // n_float_per_elem],
k % n_float_per_elem,
dtype=in_dtype,
)
elif source_format == "fp_e4m3":
w = _tir_u8_to_f8_e4m3_to_f16(bit, B_reindex[n, k], dtype=in_dtype)
elif source_format == "nf":
w = LUT[_tir_packed_to_unsigned_convert(storage_type, storage_nbit)(
bit,
B_reindex[n, k // n_float_per_elem],
k % n_float_per_elem,
dtype="int32", # assume the index data type is int32
)]
else:
raise ValueError("Unsupported source_format: {}".format(source_format))
if not with_scaling:
return w
if not with_zeros:
return w * Scale[n, k // group_size]
if zeros_mode == "original":
w = (w - Zeros[n, k // group_size]) * Scale[n, k // group_size]
elif zeros_mode == "rescale":
w = w * Scale[n, k // group_size] - Zeros[n, k // group_size]
else:
raise ValueError("Unsupported zeros_mode: {}".format(zeros_mode))
return w
B_decode = te.compute((N, K), decode_func, name="B_decode")
# Describe the matrix multiplication in TE
k = te.reduce_axis((0, K), name="k")
C = te.compute(
(M, N),
lambda i, j: te.sum(
A[i, k].astype(accum_dtype) * B_decode[j, k].astype(accum_dtype), axis=k),
name="C",
)
last_output = C
if accum_dtype != out_dtype:
D = te.compute((M, N), lambda i, j: C[i, j].astype(out_dtype), name="D")
last_output = D
args = [A, B]
if source_format == "nf":
args.append(LUT)
if with_scaling:
args.append(Scale)
if with_zeros:
args.append(Zeros)
if with_bias:
last_output = te.compute((M, N), lambda i, j: last_output[i, j] + Bias[j], name="E")
args.append(Bias)
args.append(last_output)
func = te.create_prim_func(args).with_attr(
"dequantize_info",
{
"B_decode": {
"decode_block": "B_decode",
"fast_decoding": fast_decoding,
"source_format": {
"bits": bit,
"format": source_format,
},
"storage_dtype": storage_dtype,
"target_format": in_dtype,
"with_zeros": with_zeros,
"zeros_mode": zeros_mode,
"with_scaling": with_scaling,
"group_size": group_size,
}
},
)
func = func.with_attr("weight_transform_kind", transform_kind.value)
return tvm.IRModule.from_expr(func)
def matmul_nt_dequantize_b_propagate_a_propagate_b(
M,
N,
K,
in_dtype="float16",
out_dtype="float16",
accum_dtype="float16",
bit=4,
storage_dtype="int8",
source_format="uint",
with_scaling=False,
with_zeros=False,
group_size=-1,
fast_decoding=False,
with_bias=False,
zeros_mode="original",
transform_kind_input: Union[int, TransformKind] = TransformKind.NonTransform,
transform_kind_weight: Union[int, TransformKind] = TransformKind.NonTransform,
):
if isinstance(transform_kind_input, int):
transform_kind_input = TransformKind(transform_kind_input)
if isinstance(transform_kind_weight, int):
transform_kind_weight = TransformKind(transform_kind_weight)
assert bit in [1, 2, 4, 8], "Unsupported bit: {}".format(bit)
if not isinstance(M, int):
M = tvm.te.var("m")
l = r = 16 # noqa: E741
if in_dtype in ["int8", "e4m3_float8", "e5m2_float8"]:
l, r = 16, 32 # noqa: E741
_, inversed_index_map = get_propagate_map(trans=False, dtype=in_dtype, matrix_name="A")
A = te.placeholder((M // l, K // r, l, r), name="A", dtype=in_dtype)
def fcompute(i, j):
warp_i, warp_j = i % l, j % r
spatial_args = i // l, j // r
if transform_kind_input >= TransformKind.IntraWarpTransform:
warp_i, warp_j = inversed_index_map.map_indices([warp_i, warp_j])
new_index = (*spatial_args, warp_i, warp_j)
return A[new_index]
A_reindex = te.compute(
(M, K),
fcompute,
name="A_reindex",
)
_, inversed_index_map = get_propagate_map(trans=True, dtype=in_dtype, matrix_name="B")
target_dtype = DataType(in_dtype)
scaling_factor = 1
if bit > 0 and bit < target_dtype.bits:
scaling_factor = ((target_dtype.bits // bit) * DataType(storage_dtype).bits //
target_dtype.bits)
initial_indices = inversed_index_map.initial_indices
scaling_final_indices = inversed_index_map.map_indices(
initial_indices[:-1] + [initial_indices[-1] * scaling_factor])
scaling_final_indices = scaling_final_indices[:-1] + [
scaling_final_indices[-1] // scaling_factor
]
inversed_index_map = IndexMap(
initial_indices,
scaling_final_indices,
None,
)
storage_nbit = int("".join(c for c in storage_dtype if c.isdigit()))
storage_type = str("".join(c for c in storage_dtype if not c.isdigit()))
n_float_per_elem = storage_nbit // bit
if group_size == -1:
group_size = K
qr = r * bit // storage_nbit
B = te.placeholder((N // l, (K // scaling_factor) // qr, l, qr), name="B", dtype=storage_dtype)
LUT = te.placeholder((1 << bit,), name="LUT", dtype=in_dtype)
Scale = te.placeholder((N, K // group_size), name="Scale", dtype=in_dtype)
Zeros = te.placeholder((N, K // group_size), name="Zeros", dtype=in_dtype)
Bias = te.placeholder((N,), name="Bias", dtype=in_dtype)
def fcompute(i, j):
warp_i, warp_j = i % l, j % qr
spatial_args = i // l, j // qr
if transform_kind_weight >= TransformKind.IntraWarpTransform:
warp_i, warp_j = inversed_index_map.map_indices([warp_i, warp_j])
new_index = (*spatial_args, warp_i, warp_j)
return B[new_index]
B_reindex = te.compute(
(N, K // storage_nbit * bit),
fcompute,
name="B_reindex",
)
def decode_func(n, k):
if source_format == "uint":
if bit == 8:
# 8 bit does not need to be compressed
w = B_reindex[n, k].astype(in_dtype)
else:
w = _tir_packed_to_unsigned_convert(storage_type, storage_nbit)(
bit,
B_reindex[n, k // n_float_per_elem],
k % n_float_per_elem,
dtype=in_dtype,
)
elif source_format == "int":
# Dequantize int1 to -1 and 1. Without this step, the values would be 0 and 1, identical to uint1.
if bit == 1:
w = _tir_packed_int_to_int_convert(storage_type, storage_nbit)(
bit, B_reindex[n, k // n_float_per_elem], k % n_float_per_elem, dtype=in_dtype)
elif bit == 8:
# 8 bit does not need to be compressed
w = B_reindex[n, k].astype(in_dtype)
else:
w = _tir_packed_to_signed_convert(storage_type, storage_nbit)(
bit,
B_reindex[n, k // n_float_per_elem],
k % n_float_per_elem,
dtype=in_dtype,
)
elif source_format == "fp":
w = _tir_u32_to_f4_to_f16(
bit,
B_reindex[n, k // n_float_per_elem],
k % n_float_per_elem,
dtype=in_dtype,
)
elif source_format == "fp_e4m3":
w = _tir_u8_to_f8_e4m3_to_f16(bit, B_reindex[n, k], dtype=in_dtype)
elif source_format == "nf":
w = LUT[_tir_packed_to_unsigned_convert(storage_type, storage_nbit)(
bit,
B_reindex[n, k // n_float_per_elem],
k % n_float_per_elem,
dtype="int32", # assume the index data type is int32
)]
else:
raise ValueError("Unsupported source_format: {}".format(source_format))
if not with_scaling:
return w
if not with_zeros:
return w * Scale[n, k // group_size]
if zeros_mode == "original":
w = (w - Zeros[n, k // group_size]) * Scale[n, k // group_size]
elif zeros_mode == "rescale":
w = w * Scale[n, k // group_size] - Zeros[n, k // group_size]
else:
raise ValueError("Unsupported zeros_mode: {}".format(zeros_mode))
return w
B_decode = te.compute((N, K), decode_func, name="B_decode")
# Describe the matrix multiplication in TE
k = te.reduce_axis((0, K), name="k")
C = te.compute(
(M, N),
lambda i, j: te.sum(
A_reindex[i, k].astype(accum_dtype) * B_decode[j, k].astype(accum_dtype),
axis=k,
),
name="C",
)
last_output = C
if accum_dtype != out_dtype:
D = te.compute((M, N), lambda i, j: C[i, j].astype(out_dtype), name="D")
last_output = D
args = [A, B]
if source_format == "nf":
args.append(LUT)
if with_scaling:
args.append(Scale)
if with_zeros:
args.append(Zeros)
if with_bias:
last_output = te.compute((M, N), lambda i, j: last_output[i, j] + Bias[j], name="E")
args.append(Bias)
args.append(last_output)
func = te.create_prim_func(args).with_attr(
"dequantize_info",
{
"B_decode": {
"decode_block": "B_decode",
"fast_decoding": fast_decoding,
"source_format": {
"bits": bit,
"format": source_format,
},
"storage_dtype": storage_dtype,
"target_format": in_dtype,
"with_zeros": with_zeros,
"zeros_mode": zeros_mode,
"with_scaling": with_scaling,
"group_size": group_size,
}
},
)
func = func.with_attr("input_transform_kind", transform_kind_input.value)
func = func.with_attr("weight_transform_kind", transform_kind_weight.value)
return tvm.IRModule.from_expr(func)
# Should be refactored with Emitter
def select_implementation(
M=None,
N=1024,
K=1024,
in_dtype="float16",
out_dtype="float16",
accum_dtype="float16",
bit=4,
storage_dtype="int8",
source_format="uint",
with_scaling=False,
with_zeros=False,
group_size=-1,
fast_decoding=False,
with_bias=False,
layout="nt",
zeros_mode="original",
propagate_a=False,
propagate_b=False,
):
if layout == "nn":
raise ValueError(
"Currently only support propagate_a=False and propagate_b=False for layout=nn in Dequantize Implementation"
)
elif layout == "nt":
if propagate_a and propagate_b:
return matmul_nt_dequantize_b_propagate_a_propagate_b(
M,
N,
K,
in_dtype,
out_dtype,
accum_dtype,
bit,
storage_dtype,
source_format,
with_scaling,
with_zeros,
group_size,
fast_decoding,
with_bias,
zeros_mode,
transform_kind_input=propagate_a,
transform_kind_weight=propagate_b,
)
elif propagate_a:
raise NotImplementedError
elif propagate_b:
return matmul_nt_dequantize_b_propagate_b(
M,
N,
K,
in_dtype,
out_dtype,
accum_dtype,
bit,
storage_dtype,
source_format,
with_scaling,
with_zeros,
group_size,
fast_decoding,
with_bias,
zeros_mode,
transform_kind=propagate_b,
)
else:
return matmul_nt_dequantize_b(
M,
N,
K,
in_dtype,
out_dtype,
accum_dtype,
bit,
storage_dtype,
source_format,
with_scaling,
with_zeros,
group_size,
fast_decoding,
with_bias,
zeros_mode,
)
else:
raise ValueError(f"Unsupported layout: {layout}")