-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathquantization.py
160 lines (151 loc) · 4.52 KB
/
quantization.py
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
"""A centralized registry of all existing quantization methods and their configurations."""
from typing import Any, Dict
from .awq_quantization import AWQQuantize
from .ft_quantization import FTQuantize
from .group_quantization import GroupQuantize
from .no_quantization import NoQuantize
from .per_tensor_quantization import PerTensorQuantize
Quantization = Any
"""Quantization is an object that represents an quantization algorithm. It is required to
have the following fields:
name : str
The name of the quantization algorithm, for example, "q4f16_1".
kind : str
The kind of quantization algorithm, for example, "group-quant", "faster-transformer".
It is also required to have the following method:
def quantize_model(self, module: nn.Module) -> nn.Module:
...
def quantize_weight(self, weight: tvm.runtime.NDArray) -> List[tvm.runtime.NDArray]:
...
"""
QUANTIZATION: Dict[str, Quantization] = {
"q0f16": NoQuantize(
name="q0f16",
kind="no-quant",
model_dtype="float16",
),
"q0f32": NoQuantize(
name="q0f32",
kind="no-quant",
model_dtype="float32",
),
"q3f16_0": GroupQuantize(
name="q3f16_0",
kind="group-quant",
group_size=40,
quantize_dtype="int3",
storage_dtype="uint32",
model_dtype="float16",
linear_weight_layout="KN",
quantize_embedding=True,
quantize_final_fc=True,
),
"q3f16_1": GroupQuantize(
name="q3f16_1",
kind="group-quant",
group_size=40,
quantize_dtype="int3",
storage_dtype="uint32",
model_dtype="float16",
linear_weight_layout="NK",
quantize_embedding=True,
quantize_final_fc=True,
),
"q4f16_0": GroupQuantize(
name="q4f16_0",
kind="group-quant",
group_size=32,
quantize_dtype="int4",
storage_dtype="uint32",
model_dtype="float16",
linear_weight_layout="KN",
quantize_embedding=True,
quantize_final_fc=True,
),
"q4f16_1": GroupQuantize(
name="q4f16_1",
kind="group-quant",
group_size=32,
quantize_dtype="int4",
storage_dtype="uint32",
model_dtype="float16",
linear_weight_layout="NK",
quantize_embedding=True,
quantize_final_fc=True,
),
"q4f32_1": GroupQuantize(
name="q4f32_1",
kind="group-quant",
group_size=32,
quantize_dtype="int4",
storage_dtype="uint32",
model_dtype="float32",
linear_weight_layout="NK",
quantize_embedding=True,
quantize_final_fc=True,
),
"q4f16_2": GroupQuantize(
name="q4f16_2",
kind="group-quant",
group_size=32,
quantize_dtype="int4",
storage_dtype="uint32",
model_dtype="float16",
linear_weight_layout="NK",
quantize_embedding=False,
quantize_final_fc=False,
),
"q4f16_autoawq": AWQQuantize(
name="q4f16_autoawq",
kind="awq",
group_size=128,
quantize_dtype="int4",
storage_dtype="uint32",
model_dtype="float16",
),
"q4f16_ft": FTQuantize(
name="q4f16_ft",
kind="ft-quant",
quantize_dtype="int4",
storage_dtype="int8",
model_dtype="float16",
),
"e5m2_e5m2_f16": PerTensorQuantize(
name="e5m2_e5m2_f16",
kind="per-tensor-quant",
activation_dtype="e5m2_float8",
weight_dtype="e5m2_float8",
storage_dtype="e5m2_float8",
model_dtype="float16",
quantize_final_fc=False,
quantize_embedding=False,
quantize_linear=True,
use_scale=False,
),
"e4m3_e4m3_f16": PerTensorQuantize(
name="e4m3_e4m3_f16",
kind="per-tensor-quant",
activation_dtype="e4m3_float8",
weight_dtype="e4m3_float8",
storage_dtype="e4m3_float8",
model_dtype="float16",
quantize_final_fc=False,
quantize_embedding=False,
quantize_linear=True,
use_scale=True,
calibration_mode="inference",
),
"e4m3_e4m3_f16_max_calibrate": PerTensorQuantize(
name="e4m3_e4m3_f16_max_calibrate",
kind="per-tensor-quant",
activation_dtype="e4m3_float8",
weight_dtype="e4m3_float8",
storage_dtype="e4m3_float8",
model_dtype="float16",
quantize_final_fc=False,
quantize_embedding=False,
quantize_linear=True,
use_scale=True,
calibration_mode="max",
),
}