forked from turboderp-org/exllamav2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadaptivegptq.py
375 lines (264 loc) · 10.9 KB
/
adaptivegptq.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
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
import torch
from torch import nn
import torch.nn.functional as F
import math
from exllamav2.ext import exllamav2_ext as ext_c, none_tensor
class AdaptiveQuantizer:
norm: float = 3.5
max_p: float = 1.0
min_p: float = 0.75
p_grid: int = 48
bits: int
scale_bits: int
scale_range: float = 1.0
scale: torch.tensor
qscale: torch.tensor
qscale_max: float
maxq: float
scale_maxq: float
qzero: float
def __init__(self,
bits: int = 4,
scale_bits: int = 4):
self.bits = bits
self.scale_bits = scale_bits
self.maxq = 2 ** bits - 1
self.qzero = (self.maxq + 1) / 2
self.scale_maxq = 2 ** scale_bits - 1
self.scale_maxq = (2 ** self.scale_bits) - 1
def find_params(self, x):
xmax, _ = torch.max(torch.abs(x), dim = 0)
xmax += 1e-12
base_scale = xmax / (self.maxq / 2)
qscale_max_t = torch.max(base_scale) * self.scale_range
scale_tp = base_scale / qscale_max_t
scale_tp = torch.sqrt(scale_tp)
scale_tp *= (self.scale_maxq + 1)
qscale_t = torch.clamp(torch.round(scale_tp), 1, self.scale_maxq + 1)
qscale_tw = qscale_t / (self.scale_maxq + 1)
qscale_tw = qscale_tw ** 2
qscale_tw *= qscale_max_t
q = torch.zeros((self.p_grid + 1, 128), dtype = torch.float, device = x.device)
ext_c.quantize_err(x, q, qscale_tw, self.qzero, self.maxq, self.norm, self.min_p, self.max_p, self.p_grid)
q = torch.sum(q, dim = 1)
best_pi = torch.argmin(q)
best_pif = best_pi / self.p_grid
best_p = self.max_p * best_pif + self.min_p * (1 - best_pif)
# best_p = 1.0
self.qscale = qscale_t.to(torch.short)
self.scale = qscale_tw * best_p
self.qscale_max = qscale_max_t * best_p
class AdaptiveGPTQ:
percdamp: float = 0.05
layer: nn.Linear
device: torch.device
group_size: int
bits: list
bits_groups: list
scale_bits: int
hot_bits: int
columns: int
rows: int
hessian: torch.tensor
total_groups: int
perm: torch.tensor = None
invperm: torch.tensor = None
g_idx: torch.tensor = None
scale: torch.tensor = None
qscale: torch.tensor = None
qscale_max: torch.tensor = None
qweight: torch.tensor = None
qgroups: torch.tensor = None
quant: torch.tensor = None
weights: torch.tensor = None
hessian: torch.tensor = None
hessian_inv: torch.tensor = None
num_samples: int = 0
num_batches: int = 0
def __init__(self,
layer: nn.Linear):
self.layer = layer
self.device = layer.weight.device
self.rows = self.layer.weight.data.shape[1]
self.columns = self.layer.weight.data.shape[0]
self.weights = self.layer.weight.data.T.clone().float().contiguous()
self.hessian = torch.zeros((self.rows, self.rows), device = self.device, dtype = torch.float)
self.num_samples = 0
self.num_batches = 0
def configure(self,
group_size: int = 128,
bits = None,
bits_prop = None,
scale_bits: int = 4
):
self.group_size = group_size
self.scale_bits = scale_bits
self.total_groups = (self.rows + self.group_size - 1) // self.group_size
if isinstance(bits, list):
self.bits = bits
g128 = (self.rows + 128 - 1) // 128
self.bits_groups = [max(round(g128 * p), 1) * 128 // self.group_size for p in bits_prop]
e = sum(self.bits_groups) - self.total_groups
self.bits_groups[-1] -= e
else:
self.bits = [bits]
self.bits_groups = [self.total_groups]
def num_bits(self, subtract_columns = 0):
gi = self.g_idx.numel() * 32
qs = self.qscale.numel() * self.scale_bits
qss = self.qscale_max.numel() * 16
w = 0
tr = self.rows
for g, b in zip(self.bits_groups, self.bits):
c = self.columns - subtract_columns
r = self.group_size * g
if r > tr: r = tr
tr -= r
w += r * c * b
return w + gi + qs + qss
def add_batch(self, inputs):
self.num_batches += 1
num_samples = len(inputs)
inputs = torch.cat(inputs, dim = 0)
inputs = inputs.view((-1, inputs.shape[-1])).float().T.to("cuda:0")
inputs *= math.sqrt(2 / num_samples)
self.hessian += inputs.matmul(inputs.T)
def prepare(self):
self.hessian /= self.num_batches
diagonal = torch.diag(self.hessian)
dead = diagonal == 0
self.hessian[dead, dead] = 1
self.weights[dead, :] = 0
# Activation order
self.perm = torch.argsort(diagonal, descending = True)
self.weights = self.weights[self.perm, :]
hessian = self.hessian[self.perm][:, self.perm]
# Damping
damp = self.percdamp * torch.mean(torch.diag(hessian))
d = torch.arange(self.rows, device = self.device)
hessian[d, d] += damp
# hessian = hessian.to(torch.float64)
# testq = torch.max(hessian)
# testr = torch.min(hessian)
# testd = torch.diagonal(hessian)
# testds, testds1 = torch.sort(testd)
# test = torch.allclose(hessian, hessian.T)
# eigenvalues = torch.linalg.eigvalsh(hessian)
# condition_number = eigenvalues[-1] / eigenvalues[0]
# print(condition_number)
# test2 = torch.any(eigenvalues <= 0)
# test3, test4 = torch.sort(eigenvalues)
# Inverse of H
hessian_inv = torch.linalg.cholesky(hessian)
hessian_inv = torch.cholesky_inverse(hessian_inv)
hessian_inv = torch.linalg.cholesky(hessian_inv, upper = True)
hessian_inv = hessian_inv.contiguous()
self.hessian_inv = hessian_inv
self.hessian = None
def quantize(self, keep_qweight = False):
weights = self.weights.clone()
self.quant = torch.zeros_like(self.weights)
if keep_qweight:
self.qweight = torch.zeros_like(weights, dtype = torch.short)
# Quantize groups
# self.scale = torch.zeros((self.total_groups, self.columns), dtype = torch.float, device = weights.device)
scale = []
qscale = []
qscale_max = []
qgroups = []
bits_idx = -1
bits_idx_r = 1
error = weights.clone()
for group in range(self.total_groups):
a = group * self.group_size
b = min(a + self.group_size, self.rows)
bits_idx_r -= 1
if bits_idx_r == 0:
bits_idx += 1
bits_idx_r = self.bits_groups[bits_idx]
bits = self.bits[bits_idx]
quantizer = AdaptiveQuantizer(bits = bits, scale_bits = self.scale_bits)
qgroups.append(bits)
qgroups.append(0)
quantizer.find_params(weights[a : b, :])
scale.append(quantizer.scale)
qscale.append(quantizer.qscale)
qscale_max.append(quantizer.qscale_max)
ext_c.quantize_range(self.quant,
quantizer.scale,
self.qweight if keep_qweight else none_tensor,
quantizer.qzero,
quantizer.maxq,
self.hessian_inv,
weights,
error,
a,
b)
# Create g_idx to store inverse activation order
rows = [i // self.group_size for i in range(self.rows)]
self.g_idx = torch.tensor(rows, dtype = torch.int32, device = self.device)
self.invperm = torch.argsort(self.perm)
self.g_idx = self.g_idx[self.invperm]
# Store scales
self.scale = torch.stack(scale, dim = 0)
self.qscale = torch.stack(qscale, dim = 0)
self.qscale_max = torch.tensor(qscale_max, dtype = torch.float16, device = self.device)
self.qgroups = torch.tensor(qgroups, dtype = torch.short, device = self.device)
def quant_error(self):
q = self.quant[self.invperm, :]
diff = torch.abs(q - self.layer.weight.data.T)
mat_error_1 = (diff > 0.01).sum().item() / diff.numel()
mat_error_5 = (diff > 0.05).sum().item() / diff.numel()
mat_error_10 = (diff > 0.10).sum().item() / diff.numel()
return mat_error_1, mat_error_5, mat_error_10
def apply_quant(self):
q = self.quant[self.invperm, :].T
# q = self.quant.T
self.layer.weight.data = q.reshape(self.layer.weight.shape).type_as(self.layer.weight.data)
def apply_temp(self):
q = self.quant[self.invperm, :].T
temp_layer = nn.Linear(self.layer.in_features, self.layer.out_features, False, device = "meta", dtype = torch.float16)
temp_layer.weight = nn.Parameter(q.reshape(self.layer.weight.shape).type_as(self.layer.weight.data))
return temp_layer
def pack(self, key, qparams):
assert qparams.scale_bits in [4]
# assert self.columns % 32 == 0
output = {}
output[key + ".q_invperm"] = self.invperm.to(torch.int)
output[key + ".q_scale_max"] = self.qscale_max
output[key + ".q_groups"] = self.qgroups
columns = self.columns
rem_rows = self.rows
padding = -columns % 32
if padding != 0:
print(f" !! Note: Padding quantized tensor {key}")
qst = F.pad(self.qscale, (0, padding)).contiguous()
qwt = F.pad(self.qweight, (0, padding)).contiguous()
qst_packed = torch.zeros((qst.shape[0], qst.shape[1] * qparams.scale_bits // 32), dtype = torch.int32, device = self.device)
if qparams.scale_bits == 4: ext_c.pack_rows_4(qst, qst_packed)
# if qparams.scale_bits == 6: ext_c.pack_rows_6(qst, qst_packed) # TODO:
output[key + ".q_scale"] = qst_packed
qwt_packed = []
i = 0
row = 0
out_row = 0
while i < self.qscale.shape[0]:
bits = self.qgroups[i * 2].item()
self.qgroups[i * 2 + 1] = out_row
i += 1
rows = min(self.group_size, rem_rows)
wpqr = 32 / bits
qrows = rows / wpqr
assert i == self.qgroups.shape[-1] or qrows == int(qrows)
qrows = math.ceil(qrows)
g_qwt = qwt[row:row+rows, :].contiguous()
g_qwt_packed = torch.zeros((qrows, columns + padding), dtype = torch.int32, device = self.device)
if padding > 0: g_qwt[:, -padding:] = 2 ** (bits - 1)
ext_c.pack_columns(g_qwt, g_qwt_packed, bits)
qwt_packed.append(g_qwt_packed)
row += rows
out_row += qrows
rem_rows -= rows
qwt_packed = torch.cat(qwt_packed, dim = 0)
output[key + ".q_weight"] = qwt_packed
return output