-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcoder.py
More file actions
356 lines (287 loc) · 11.5 KB
/
Copy pathcoder.py
File metadata and controls
356 lines (287 loc) · 11.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
import torch
from tqdm import tqdm
import seaborn as sns
from matplotlib import pyplot as plt
from e8 import encode_e8, G, G_inv, generate_dither
from utils import matrix_to_pieces, pieces_to_matrix
# Set default dtype if you want (optional):
# torch.set_default_dtype(torch.float32)
def encode(x, q, z, beta):
# x, z expected to be torch tensors
# encode_e8 expects x: [N,8]
x = x / beta
if z is not None:
x = x + z
t = encode_e8(x)
y = torch.round(t @ G_inv.T.to(t.device)).to(torch.int64)
res = torch.remainder(y, q)
if z is not None:
t = t - z
lambda_c = q * encode_e8(t / q)
# err: sum over axis=1
err = torch.sum(lambda_c * lambda_c, dim=-1) > 1e-9
return res, err
def decode(enc, q, z, beta):
# enc: int tensor
# Convert to float
encf = enc.float()
y_tilde = encf @ G.T.to(encf.device)
if z is not None:
y_tilde = y_tilde - z
x = beta * (y_tilde - q * encode_e8(y_tilde / q))
return x
def encode_matrix_first_beta(A, q, betas, seed, use_dither=True):
device = A.device
pieces = matrix_to_pieces(A)
rows = pieces.shape[0]
beta_id = torch.full((rows,), -1, dtype=torch.int8, device=device)
enc = torch.zeros(pieces.shape, dtype=torch.int64, device=device)
generator = torch.Generator(device=device)
generator.manual_seed(seed)
for i, beta_0 in enumerate(betas):
beta = beta_0 / q
mask = (beta_id == -1)
pieces_to_quantize = pieces[mask]
z = generate_dither(pieces_to_quantize.shape[0], generator, device) if use_dither else None
cur_enc, err = encode(pieces_to_quantize, q, z, beta)
masked_beta_id = beta_id[mask]
masked_beta_id[~err] = i
beta_id[mask] = masked_beta_id
enc[mask] = cur_enc
assert not (beta_id == -1).any()
return {
"shape": A.shape,
"enc": enc,
"beta_id": beta_id,
}
def encode_matrix_try_all(A, q, betas, seed, use_dither=True):
device = A.device
pieces = matrix_to_pieces(A)
rows = pieces.shape[0]
beta_id = torch.full((rows,), 0, dtype=torch.int8, device=device)
enc = torch.zeros(pieces.shape, dtype=torch.int64, device=device)
generator = torch.Generator(device=device)
generator.manual_seed(seed)
INF = 1e9
error = torch.full((rows,), INF, device=device, dtype=torch.float32)
no_overflow = torch.full((rows,), False, dtype=torch.bool, device=device)
for i, beta_0 in enumerate(betas):
beta = beta_0 / q
z = generate_dither(pieces.shape[0], generator, device) if use_dither else None
cur_enc, err = encode(pieces, q, z, beta)
recon = decode(cur_enc, q, z, beta)
no_overflow[~err] = True
cur_error = ((recon - pieces) ** 2).sum(dim=1)
replace_mask = cur_error < error
enc[replace_mask] = cur_enc[replace_mask]
beta_id[replace_mask] = i
error[replace_mask] = cur_error[replace_mask]
# assert no_overflow.all()
return {
"shape": A.shape,
"enc": enc,
"beta_id": beta_id,
}
def decode_matrix_first_beta(A_enc, q, betas, seed, use_dither=True):
enc = A_enc["enc"]
device = enc.device
result_pieces = torch.zeros(enc.shape, device=device, dtype=torch.float32)
generator = torch.Generator(device=device)
generator.manual_seed(seed)
for i, beta_0 in enumerate(betas):
beta = beta_0 / q
mask_ge = A_enc["beta_id"] >= i
mask = A_enc["beta_id"] == i
rel_mask = mask[mask_ge]
# Tricky: when we generated dither, we didn't know which entries would be actually used
z = generate_dither(mask_ge.sum().item(), generator, device)[rel_mask] if use_dither else None
cur = enc[mask]
cur_dec = decode(cur, q, z, beta)
result_pieces[mask] = cur_dec
return pieces_to_matrix(result_pieces, A_enc["shape"])
def decode_matrix_try_all(A_enc, q, betas, seed, use_dither=True):
enc = A_enc["enc"]
device = enc.device
result_pieces = torch.zeros(enc.shape, device=device, dtype=torch.float32)
generator = torch.Generator(device=device)
generator.manual_seed(seed)
for i, beta_0 in enumerate(betas):
beta = beta_0 / q
mask = A_enc["beta_id"] == i
z = generate_dither(enc.shape[0], generator, device)[mask] if use_dither else None
cur = enc[mask]
recon = decode(cur, q, z, beta)
result_pieces[mask] = recon
return pieces_to_matrix(result_pieces, A_enc["shape"])
# copied from QuIP# paper
def block_LDL(H, b, check_nan=True):
n = H.shape[0]
assert (n % b == 0)
m = n // b
try:
L = torch.linalg.cholesky(H)
except:
return None
DL = torch.diagonal(L.reshape(m, b, m, b), dim1=0, dim2=2).permute(2, 0, 1)
D = (DL @ DL.permute(0, 2, 1)).cpu()
DL = torch.linalg.inv(DL)
L = L.view(n, m, b)
for i in range(m):
L[:, i, :] = L[:, i, :] @ DL[i, :, :]
if check_nan and L.isnan().any():
return None
L = L.reshape(n, n)
return (L, D.to(DL.device))
def encode_matrix_LDLQ_first_beta(A, H, q, betas, seed, use_dither=True):
# A -- shape (m, n)
# H -- shape (n, n); H[i, j] is (d obj) / dH_{ki}H_{kj} forall k
m, n = A.shape
device = A.device
assert n % 8 == 0
# A_hat - A
L, D = block_LDL(H, 8)
beta_id = torch.full((m, n // 8), -1, dtype=torch.int8, device=device)
enc = torch.zeros((m, n), dtype=torch.int64, device=device)
generator = torch.Generator(device=device)
generator.manual_seed(seed)
decoded = A.clone()
new_A = A.clone()
for c in range(n - 8, -8, -8):
block_idx = c // 8
# Quantize column block c:c+8
WXWX = A[:, c:c+8] + (A[:, c+8:n] - decoded[:, c+8:n]) @ L[c+8:n, c:c+8]
decoded[:, c:c+8] = WXWX
new_A[:, c:c+8] = WXWX
for i, beta_0 in enumerate(betas):
beta = beta_0 / q
mask = (beta_id[:, block_idx] == -1)
rows_to_quantize = WXWX[mask]
z = generate_dither(rows_to_quantize.shape[0], generator, device) if use_dither else None
cur_enc, err = encode(rows_to_quantize, q, z, beta)
masked_beta_id = beta_id[mask, block_idx]
masked_beta_id[~err] = i
beta_id[mask, block_idx] = masked_beta_id
enc[mask, c:c+8] = cur_enc
decoded[mask, c:c+8] = decode(cur_enc, q, z, beta)
if (beta_id == -1).any():
bad_mask = (beta_id == -1)
pos = torch.nonzero(bad_mask)[0].tolist()
print("Bad vector piece")
print("pos:", pos[0], pos[1])
print(new_A[pos[0], pos[1] * 8: (pos[1] + 1) * 8])
assert False
return {
"enc": enc,
"beta_id": beta_id,
}
def decode_matrix_LDLQ_first_beta(A_enc, q, betas, seed, use_dither=True):
enc = A_enc["enc"]
beta_id = A_enc["beta_id"]
device = enc.device
result = torch.zeros(enc.shape, device=device, dtype=torch.float32)
generator = torch.Generator(device=device)
generator.manual_seed(seed)
(m, n) = enc.shape
for c in range(n-8, -8, -8):
block_idx = c // 8
for i, beta_0 in enumerate(betas):
beta = beta_0 / q
mask_ge = beta_id[:, block_idx] >= i
mask = beta_id[:, block_idx] == i
rel_mask = mask[mask_ge]
# Tricky: when we generated dither, we didn't know which entries would be actually used
z = generate_dither(mask_ge.sum().item(), generator, device)[rel_mask] if use_dither else None
cur = enc[mask, c:c+8]
cur_dec = decode(cur, q, z, beta)
result[mask, c:c+8] = cur_dec
return result
# from torch.distributed import get_rank
def encode_matrix_LDLQ_try_all(A, H, q, betas, seed, use_dither=True):
# A -- shape (m, n)
# H -- shape (n, n); H[i, j] is (d obj) / dH_{ki}H_{kj} forall k
# print("rank:", get_rank(), A.device, H.device)
m, n = A.shape
device = A.device
assert n % 8 == 0
# A_hat - A
result = block_LDL(H, 8)
if result is None:
print("LDL decomposition of H does not exist")
H = torch.eye(H.shape[0], device=H.device, dtype=H.dtype)
result = block_LDL(H, 8)
assert result is not None
L, D = result
beta_id = torch.full((m, n // 8), 0, dtype=torch.int8, device=device)
enc = torch.zeros((m, n), dtype=torch.int64, device=device)
generator = torch.Generator(device=device)
generator.manual_seed(seed)
decoded = A.clone()
for c in range(n - 8, -8, -8):
block_idx = c // 8
# Quantize column block c:c+8
WXWX = A[:, c:c+8] + (A[:, c+8:n] - decoded[:, c+8:n]) @ L[c+8:n, c:c+8]
decoded[:, c:c+8] = WXWX
INF = 1e9
error = torch.full((A.shape[0],), INF, device=device, dtype=torch.float32)
no_overflow = torch.full((A.shape[0],), False, dtype=torch.bool, device=device)
for i, beta_0 in enumerate(betas):
beta = beta_0 / q
z = generate_dither(WXWX.shape[0], generator, device) if use_dither else None
cur_enc, err = encode(WXWX, q, z, beta)
recon = decode(cur_enc, q, z, beta)
no_overflow[~err] = True
cur_error = ((recon - WXWX) ** 2).sum(dim=1)
replace_mask = cur_error < error
enc[replace_mask, c:c+8] = cur_enc[replace_mask]
beta_id[replace_mask, block_idx] = i
error[replace_mask] = cur_error[replace_mask]
decoded[replace_mask, c:c+8] = recon[replace_mask]
if not no_overflow.all():
print("Warning, overflow")
if (beta_id == -1).any():
print("This should not be possible, given the previous assert")
assert False
return {
"enc": enc,
"beta_id": beta_id,
}
def decode_matrix_LDLQ_try_all(A_enc, q, betas, seed, use_dither=True):
enc = A_enc["enc"]
beta_id = A_enc["beta_id"]
device = enc.device
result = torch.zeros(enc.shape, device=device, dtype=torch.float32)
generator = torch.Generator(device=device)
generator.manual_seed(seed)
(m, n) = enc.shape
for c in range(n-8, -8, -8):
block_idx = c // 8
for i, beta_0 in enumerate(betas):
beta = beta_0 / q
mask = beta_id[:, block_idx] == i
z = generate_dither(enc.shape[0], generator, device)[mask] if use_dither else None
cur = enc[mask, c:c+8]
cur_dec = decode(cur, q, z, beta)
result[mask, c:c+8] = cur_dec
return result
def encode_matrix(A, q, betas, seed, use_dither=True, try_all=False, H=None):
if H is None:
if try_all:
return encode_matrix_try_all(A, q, betas, seed, use_dither=use_dither)
else:
return encode_matrix_first_beta(A, q, betas, seed, use_dither=use_dither)
else:
if try_all:
return encode_matrix_LDLQ_try_all(A, H, q, betas, seed, use_dither=use_dither)
else:
return encode_matrix_LDLQ_first_beta(A, H, q, betas, seed, use_dither=use_dither)
def decode_matrix(A, q, betas, seed, use_dither=True, try_all=False, use_ldlq=False):
if not use_ldlq:
if try_all:
return decode_matrix_try_all(A, q, betas, seed, use_dither=use_dither)
else:
return decode_matrix_first_beta(A, q, betas, seed, use_dither=use_dither)
else:
if try_all:
return decode_matrix_LDLQ_try_all(A, q, betas, seed, use_dither=use_dither)
else:
return decode_matrix_LDLQ_first_beta(A, q, betas, seed, use_dither=use_dither)