CompressedGemma commited on
Commit
336c058
·
verified ·
1 Parent(s): dc0a265

Upload 6 files

Browse files
Files changed (2) hide show
  1. gui.py +16 -0
  2. q1_0.py +39 -18
gui.py CHANGED
@@ -30,6 +30,22 @@ from tkinter import filedialog, messagebox, ttk
30
 
31
  import numpy as np
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  from hpc_quantize_v2 import (
34
  GGUFReader,
35
  HPCConfig,
 
30
 
31
  import numpy as np
32
 
33
+ _HERE = Path(__file__).resolve().parent
34
+ if not (_HERE / "hpc_quantize_v2").is_dir() and (_HERE / "__init__.py").is_file():
35
+ import importlib.util
36
+ import sys
37
+
38
+ if "hpc_quantize_v2" not in sys.modules:
39
+ _spec = importlib.util.spec_from_file_location(
40
+ "hpc_quantize_v2",
41
+ _HERE / "__init__.py",
42
+ submodule_search_locations=[str(_HERE)],
43
+ )
44
+ if _spec is not None and _spec.loader is not None:
45
+ _mod = importlib.util.module_from_spec(_spec)
46
+ sys.modules["hpc_quantize_v2"] = _mod
47
+ _spec.loader.exec_module(_mod)
48
+
49
  from hpc_quantize_v2 import (
50
  GGUFReader,
51
  HPCConfig,
q1_0.py CHANGED
@@ -201,37 +201,48 @@ def _quantize_q1_0_hpc_cpu(
201
  rows = np.arange(B)
202
  d_sel = cand_d[rows, best_cand]
203
 
204
- # 4. Asymmetric Sign-Flip fp16-ULP Polish (+/-8 ULPs around +d_sel, -d_sel, and d_mean):
205
  rad = max(int(cfg.ulp_radius), 4)
206
  deltas = np.arange(-rad, rad + 1, dtype=np.int32)
207
  pos_b = np.clip(to_fp16_bits(np.abs(d_sel)).astype(np.int32)[:, None] + deltas[None, :], 0, 0x7BFF).astype(np.uint16)
208
  neg_b = (pos_b | np.uint16(0x8000)).astype(np.uint16)
 
 
209
  ref_b = np.clip(to_fp16_bits(np.abs(d_mean)).astype(np.int32)[:, None] + deltas[None, :], 0, 0x7BFF).astype(np.uint16)
210
- all_b = np.concatenate([pos_b, neg_b, ref_b], axis=1)
211
  all_d = from_fp16_bits(all_b)
212
  q_u = np.where(all_d[:, :, None] >= 0.0, X_f32[:, None, :] >= 0.0, X_f32[:, None, :] < 0.0).astype(np.uint8)
213
  c_u = 2.0 * q_u.astype(np.float32) - 1.0
214
  e_u = X_f32[:, None, :] - all_d[:, :, None] * c_u
215
  sse_u = np.sum(W_f32[:, None, :] * e_u * e_u, axis=2, dtype=np.float64)
216
- pick_u = np.argmin(sse_u, axis=1)
 
 
 
 
 
 
 
 
217
  best_d16 = all_b[rows, pick_u]
218
  best_d = all_d[rows, pick_u]
219
  best_q = q_u[rows, pick_u].astype(np.int64)
220
 
221
- # 5. Closed-Form 1-D Quadratic Vesica + DC Bit-Flip Projection within sse_budget:
222
  if cfg.use_spectral and (lam_vw > 0.0 or lam_dc > 0.0):
223
  c_f = (2 * best_q - 1).astype(np.float64)
224
  d_f64 = best_d.astype(np.float64)[:, None]
 
225
  e = X_f32.astype(np.float64) - d_f64 * c_f
226
  sse_live = np.sum(W_f32 * e * e, axis=1, dtype=np.float64)
227
  sse_cap = sse_live * (1.0 + cfg.sse_budget)
228
  v = e[:, :half] + e[:, half:]
229
  dc = np.sum(e, axis=1)
230
- denom = 2.0 * (W_f32 + lam_vw + lam_dc) * (d_f64 * d_f64)
231
  act_b = np.abs(d_f64) > 1.0e-15
232
  s_den = np.where(act_b & (denom > 1.0e-30), denom, 1.0)
233
  v_full = np.concatenate([v, v], axis=1)
234
- grad = d_f64 * (W_f32 * e + lam_vw * v_full + lam_dc * dc[:, None])
235
  dq = np.where(act_b, nearest_int((grad / s_den).astype(np.float32)), 0)
236
  q_new = np.clip(best_q + dq, 0, 1)
237
  c_new = (2 * q_new - 1).astype(np.float64)
@@ -317,38 +328,50 @@ def _quantize_q1_0_hpc_cuda(
317
  rows = torch.arange(B, device=dev)
318
  d_sel = cand_d[rows, best_cand_t]
319
 
320
- # 4. Asymmetric Sign-Flip fp16-ULP Polish (+/-8 ULPs around +d_sel, -d_sel, and d_mean):
321
  rad = max(int(cfg.ulp_radius), 4)
322
  deltas = torch.arange(-rad, rad + 1, dtype=torch.int32, device=dev)
323
  abs_bits = torch.abs(d_sel).to(torch.float16).view(torch.int16).to(torch.int32) & 0x7FFF
 
324
  ref_bits = torch.abs(d_mean).to(torch.float16).view(torch.int16).to(torch.int32) & 0x7FFF
325
  pos_b = torch.clamp(abs_bits[:, None] + deltas[None, :], 0, 0x7BFF)
326
  neg_b = pos_b | 0x8000
 
 
327
  mean_b = torch.clamp(ref_bits[:, None] + deltas[None, :], 0, 0x7BFF)
328
- all_b = torch.cat([pos_b, neg_b, mean_b], dim=1)
329
  all_d = all_b.to(torch.int16).view(torch.float16).to(torch.float32)
330
  q_u = torch.where(all_d[:, :, None] >= 0.0, X[:, None, :] >= 0.0, X[:, None, :] < 0.0).to(torch.uint8)
331
  c_u = 2.0 * q_u.to(torch.float32) - 1.0
332
  e_u = X[:, None, :] - all_d[:, :, None] * c_u
333
  sse_u = torch.sum(W[:, None, :] * e_u * e_u, dim=2)
334
- pick_u = torch.argmin(sse_u, dim=1)
 
 
 
 
 
 
 
 
335
  best_d16 = all_b[rows, pick_u]
336
  best_d = all_d[rows, pick_u]
337
  best_q = q_u[rows, pick_u].to(torch.int64)
338
 
339
- # 5. Closed-Form 1-D Quadratic Vesica + DC Bit-Flip Projection within sse_budget:
340
  if cfg.use_spectral and (lam_vw > 0.0 or lam_dc > 0.0):
341
  d_col = best_d[:, None]
 
342
  e = X - d_col * (2.0 * best_q.to(torch.float32) - 1.0)
343
  sse_live = torch.sum(W * e * e, dim=1)
344
  sse_cap = sse_live * (1.0 + float(cfg.sse_budget))
345
  v = e[:, :half] + e[:, half:]
346
  dc = torch.sum(e, dim=1)
347
- denom = 2.0 * (W + lam_vw + lam_dc) * (d_col * d_col)
348
  act_b = torch.abs(d_col) > 1.0e-15
349
  s_den = torch.where(act_b & (denom > 1.0e-30), denom, torch.ones_like(denom))
350
  v_full = torch.cat([v, v], dim=1)
351
- grad = d_col * (W * e + lam_vw * v_full + lam_dc * dc[:, None])
352
  sh_dq = (grad / s_den) + 12582912.0
353
  dq = torch.where(
354
  act_b,
@@ -409,14 +432,12 @@ def quantize_tensor_q1_0(
409
  for start in range(0, n_blocks, chunk_blocks):
410
  stop = min(start + chunk_blocks, n_blocks)
411
  Xb = X[start:stop]
412
- sig2 = np.mean(Xb * Xb, axis=1, keepdims=True)
413
  if W_raw is None:
414
- Wb = np.sqrt(sig2 + Xb * Xb)
415
  else:
416
- wr = W_raw[start:stop]
417
- Wb = np.sqrt(np.maximum(wr, 1.0e-6)) * np.sqrt(sig2 + Xb * Xb) if cfg.temper_imatrix else wr
418
- wm = np.mean(Wb, axis=1, keepdims=True)
419
- Wb = np.where(wm > 1.0e-30, Wb / np.maximum(wm, 1.0e-30), 1.0).astype(np.float32)
420
 
421
  if use_cuda and dev is not None:
422
  raw_b, sse_b = _quantize_q1_0_hpc_cuda(Xb, Wb, dev, cfg)
 
201
  rows = np.arange(B)
202
  d_sel = cand_d[rows, best_cand]
203
 
204
+ # 4. Asymmetric Sign-Flip fp16-ULP Polish (+/-8 ULPs around +d_sel, -d_sel, wls_d, and d_mean):
205
  rad = max(int(cfg.ulp_radius), 4)
206
  deltas = np.arange(-rad, rad + 1, dtype=np.int32)
207
  pos_b = np.clip(to_fp16_bits(np.abs(d_sel)).astype(np.int32)[:, None] + deltas[None, :], 0, 0x7BFF).astype(np.uint16)
208
  neg_b = (pos_b | np.uint16(0x8000)).astype(np.uint16)
209
+ wls_b = np.clip(to_fp16_bits(np.abs(wls_d)).astype(np.int32)[:, None] + deltas[None, :], 0, 0x7BFF).astype(np.uint16)
210
+ wls_neg_b = (wls_b | np.uint16(0x8000)).astype(np.uint16)
211
  ref_b = np.clip(to_fp16_bits(np.abs(d_mean)).astype(np.int32)[:, None] + deltas[None, :], 0, 0x7BFF).astype(np.uint16)
212
+ all_b = np.concatenate([pos_b, neg_b, wls_b, wls_neg_b, ref_b], axis=1)
213
  all_d = from_fp16_bits(all_b)
214
  q_u = np.where(all_d[:, :, None] >= 0.0, X_f32[:, None, :] >= 0.0, X_f32[:, None, :] < 0.0).astype(np.uint8)
215
  c_u = 2.0 * q_u.astype(np.float32) - 1.0
216
  e_u = X_f32[:, None, :] - all_d[:, :, None] * c_u
217
  sse_u = np.sum(W_f32[:, None, :] * e_u * e_u, axis=2, dtype=np.float64)
218
+ if cfg.use_spectral and (lam_dc > 0.0 or lam_vw > 0.0):
219
+ v_u = e_u[:, :, :half] + e_u[:, :, half:]
220
+ dc_u = np.sum(e_u, axis=2, dtype=np.float64)
221
+ sse_min_u = np.min(sse_u, axis=1, keepdims=True)
222
+ obj_u = sse_u + lam_vw * np.sum(v_u * v_u, axis=2, dtype=np.float64) + lam_dc * dc_u * dc_u
223
+ obj_u = np.where(sse_u <= sse_min_u * (1.0 + cfg.sse_budget), obj_u, np.inf)
224
+ pick_u = np.argmin(obj_u, axis=1)
225
+ else:
226
+ pick_u = np.argmin(sse_u, axis=1)
227
  best_d16 = all_b[rows, pick_u]
228
  best_d = all_d[rows, pick_u]
229
  best_q = q_u[rows, pick_u].astype(np.int64)
230
 
231
+ # 5. Closed-Form 1-D Quadratic Vesica + DC Bit-Flip Projection (step = 2*d) within sse_budget:
232
  if cfg.use_spectral and (lam_vw > 0.0 or lam_dc > 0.0):
233
  c_f = (2 * best_q - 1).astype(np.float64)
234
  d_f64 = best_d.astype(np.float64)[:, None]
235
+ step_f64 = 2.0 * d_f64
236
  e = X_f32.astype(np.float64) - d_f64 * c_f
237
  sse_live = np.sum(W_f32 * e * e, axis=1, dtype=np.float64)
238
  sse_cap = sse_live * (1.0 + cfg.sse_budget)
239
  v = e[:, :half] + e[:, half:]
240
  dc = np.sum(e, axis=1)
241
+ denom = (W_f32 + lam_vw + lam_dc) * (step_f64 * step_f64)
242
  act_b = np.abs(d_f64) > 1.0e-15
243
  s_den = np.where(act_b & (denom > 1.0e-30), denom, 1.0)
244
  v_full = np.concatenate([v, v], axis=1)
245
+ grad = step_f64 * (W_f32 * e + lam_vw * v_full + lam_dc * dc[:, None])
246
  dq = np.where(act_b, nearest_int((grad / s_den).astype(np.float32)), 0)
247
  q_new = np.clip(best_q + dq, 0, 1)
248
  c_new = (2 * q_new - 1).astype(np.float64)
 
328
  rows = torch.arange(B, device=dev)
329
  d_sel = cand_d[rows, best_cand_t]
330
 
331
+ # 4. Asymmetric Sign-Flip fp16-ULP Polish (+/-8 ULPs around +d_sel, -d_sel, wls_d, and d_mean):
332
  rad = max(int(cfg.ulp_radius), 4)
333
  deltas = torch.arange(-rad, rad + 1, dtype=torch.int32, device=dev)
334
  abs_bits = torch.abs(d_sel).to(torch.float16).view(torch.int16).to(torch.int32) & 0x7FFF
335
+ wls_bits = torch.abs(wls_d).to(torch.float16).view(torch.int16).to(torch.int32) & 0x7FFF
336
  ref_bits = torch.abs(d_mean).to(torch.float16).view(torch.int16).to(torch.int32) & 0x7FFF
337
  pos_b = torch.clamp(abs_bits[:, None] + deltas[None, :], 0, 0x7BFF)
338
  neg_b = pos_b | 0x8000
339
+ wls_b = torch.clamp(wls_bits[:, None] + deltas[None, :], 0, 0x7BFF)
340
+ wls_neg_b = wls_b | 0x8000
341
  mean_b = torch.clamp(ref_bits[:, None] + deltas[None, :], 0, 0x7BFF)
342
+ all_b = torch.cat([pos_b, neg_b, wls_b, wls_neg_b, mean_b], dim=1)
343
  all_d = all_b.to(torch.int16).view(torch.float16).to(torch.float32)
344
  q_u = torch.where(all_d[:, :, None] >= 0.0, X[:, None, :] >= 0.0, X[:, None, :] < 0.0).to(torch.uint8)
345
  c_u = 2.0 * q_u.to(torch.float32) - 1.0
346
  e_u = X[:, None, :] - all_d[:, :, None] * c_u
347
  sse_u = torch.sum(W[:, None, :] * e_u * e_u, dim=2)
348
+ if cfg.use_spectral and (lam_dc > 0.0 or lam_vw > 0.0):
349
+ v_u = e_u[:, :, :half] + e_u[:, :, half:]
350
+ dc_u = torch.sum(e_u, dim=2)
351
+ sse_min_u = torch.min(sse_u, dim=1, keepdim=True).values
352
+ obj_u = sse_u + lam_vw * torch.sum(v_u * v_u, dim=2) + lam_dc * dc_u * dc_u
353
+ obj_u = torch.where(sse_u <= sse_min_u * (1.0 + float(cfg.sse_budget)), obj_u, torch.full_like(obj_u, float("inf")))
354
+ pick_u = torch.argmin(obj_u, dim=1)
355
+ else:
356
+ pick_u = torch.argmin(sse_u, dim=1)
357
  best_d16 = all_b[rows, pick_u]
358
  best_d = all_d[rows, pick_u]
359
  best_q = q_u[rows, pick_u].to(torch.int64)
360
 
361
+ # 5. Closed-Form 1-D Quadratic Vesica + DC Bit-Flip Projection (step = 2*d) within sse_budget:
362
  if cfg.use_spectral and (lam_vw > 0.0 or lam_dc > 0.0):
363
  d_col = best_d[:, None]
364
+ step_col = 2.0 * d_col
365
  e = X - d_col * (2.0 * best_q.to(torch.float32) - 1.0)
366
  sse_live = torch.sum(W * e * e, dim=1)
367
  sse_cap = sse_live * (1.0 + float(cfg.sse_budget))
368
  v = e[:, :half] + e[:, half:]
369
  dc = torch.sum(e, dim=1)
370
+ denom = (W + lam_vw + lam_dc) * (step_col * step_col)
371
  act_b = torch.abs(d_col) > 1.0e-15
372
  s_den = torch.where(act_b & (denom > 1.0e-30), denom, torch.ones_like(denom))
373
  v_full = torch.cat([v, v], dim=1)
374
+ grad = step_col * (W * e + lam_vw * v_full + lam_dc * dc[:, None])
375
  sh_dq = (grad / s_den) + 12582912.0
376
  dq = torch.where(
377
  act_b,
 
432
  for start in range(0, n_blocks, chunk_blocks):
433
  stop = min(start + chunk_blocks, n_blocks)
434
  Xb = X[start:stop]
 
435
  if W_raw is None:
436
+ Wb = np.ones_like(Xb, dtype=np.float32)
437
  else:
438
+ wr = np.maximum(W_raw[start:stop], 1.0e-6)
439
+ wm = np.mean(wr, axis=1, keepdims=True)
440
+ Wb = np.where(wm > 1.0e-30, wr / np.maximum(wm, 1.0e-30), 1.0).astype(np.float32)
 
441
 
442
  if use_cuda and dev is not None:
443
  raw_b, sse_b = _quantize_q1_0_hpc_cuda(Xb, Wb, dev, cfg)