CompressedGemma commited on
Commit
bc4e7a7
·
verified ·
1 Parent(s): 1f034b0

Upload 6 files

Browse files
Files changed (2) hide show
  1. gguf.py +5 -1
  2. q1_0.py +71 -70
gguf.py CHANGED
@@ -556,7 +556,11 @@ def target_quant_type(
556
  name, dims, include_embd=quantize_embd_q4, promote_attn=True, allow_q2k_tensors=True
557
  ):
558
  tgt = "Q4_0"
559
- elif is_q1_0_eligible(name, dims, include_embd=quantize_embd_q4):
 
 
 
 
560
  tgt = "Q1_0"
561
  elif enable_q4_fallback and is_q4_0_eligible(
562
  name, dims, include_embd=quantize_embd_q4, promote_attn=True, allow_q2k_tensors=True
 
556
  name, dims, include_embd=quantize_embd_q4, promote_attn=True, allow_q2k_tensors=True
557
  ):
558
  tgt = "Q4_0"
559
+ elif _is_embd_or_head(name) and quantize_embd_q4 and enable_q4_fallback and is_q4_0_eligible(
560
+ name, dims, include_embd=True, allow_q2k_tensors=True
561
+ ):
562
+ tgt = "Q4_0"
563
+ elif is_q1_0_eligible(name, dims, include_embd=not quantize_embd_q4):
564
  tgt = "Q1_0"
565
  elif enable_q4_fallback and is_q4_0_eligible(
566
  name, dims, include_embd=quantize_embd_q4, promote_attn=True, allow_q2k_tensors=True
q1_0.py CHANGED
@@ -152,42 +152,43 @@ def _quantize_q1_0_hpc_cpu(
152
  lam_vw = (float(cfg.vw_lambda) / QK1_0) if cfg.use_spectral else 0.0
153
  half = QK1_0 // 2
154
 
155
- # 1. Analog + WLS seed for d* (testing mean, weighted WLS, and companded analog):
156
  d_mean = np.mean(np.abs(X_f32), axis=1).astype(np.float32)
 
 
157
  wsum = np.maximum(np.sum(W_f32, axis=1, keepdims=True), 1.0e-30)
158
- d_wls_init = (np.sum(W_f32 * np.abs(X_f32), axis=1, keepdims=True) / wsum)[:, 0].astype(np.float32)
 
 
 
 
159
  mu = np.sum(W_f32 * X_f32, axis=1, keepdims=True) / wsum
160
  var = np.sum(W_f32 * (X_f32 - mu) ** 2, axis=1, keepdims=True) / wsum
161
  sig = np.sqrt(np.maximum(var, 1.0e-30))
162
  x_comp = mu + sig * 1.85 * np.tanh((X_f32 - mu) / (1.85 * sig))
163
- d_analog = (np.sum(W_f32 * np.abs(x_comp), axis=1, keepdims=True) / wsum)[:, 0].astype(np.float32)
 
 
 
 
164
 
 
 
165
  wls_d = d_mean.copy()
166
  best_seed_sse = np.full(B, np.inf, dtype=np.float64)
167
- for d_start in (d_mean, d_wls_init, d_analog, -d_analog):
168
- d = d_start.astype(np.float64)
169
- for _ in range(2):
170
- q = np.where(d[:, None] >= 0.0, X_f32 >= 0.0, X_f32 < 0.0).astype(np.int64)
171
- c = (2 * q - 1).astype(np.float64)
172
- num = np.sum(W_f32 * X_f32 * c, axis=1, dtype=np.float64)
173
- den = np.sum(W_f32, axis=1, dtype=np.float64)
174
- valid = den > 1.0e-30
175
- d = np.where(valid, num / np.where(valid, den, 1.0), 0.0)
176
- df = from_fp16_bits(to_fp16_bits(d.astype(np.float32)))
177
- q = np.where(df[:, None] >= 0.0, X_f32 >= 0.0, X_f32 < 0.0).astype(np.float32)
178
- c_f = 2.0 * q - 1.0
179
- e = X_f32 - df[:, None] * c_f
180
  sse = np.sum(W_f32 * e * e, axis=1, dtype=np.float64)
181
  take = sse < best_seed_sse
182
  best_seed_sse = np.where(take, sse, best_seed_sse)
183
  wls_d = np.where(take, df, wls_d)
184
 
185
- # 2. 24-Candidate Field with Extended Spectral Objective:
186
- cand_d16 = to_fp16_bits(wls_d[:, None] * MULTS_D[None, :]) # (B, 24)
 
187
  cand_d = from_fp16_bits(cand_d16) # (B, 24)
188
- q_c = np.where(cand_d[:, :, None] >= 0.0, X_f32[:, None, :] >= 0.0, X_f32[:, None, :] < 0.0).astype(np.float32)
189
- c_c = 2.0 * q_c - 1.0
190
- e_c = X_f32[:, None, :] - cand_d[:, :, None] * c_c # (B, 24, 128)
191
  sse_c = np.sum(W_f32[:, None, :] * e_c * e_c, axis=2)
192
  if cfg.use_spectral and (lam_dc > 0.0 or lam_vw > 0.0):
193
  v_c = e_c[:, :, :half] + e_c[:, :, half:]
@@ -201,19 +202,17 @@ 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, 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:]
@@ -226,9 +225,9 @@ def _quantize_q1_0_hpc_cpu(
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]
@@ -239,7 +238,8 @@ def _quantize_q1_0_hpc_cpu(
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])
@@ -278,42 +278,42 @@ def _quantize_q1_0_hpc_cuda(
278
  X = torch.from_numpy(X_np).to(dev, dtype=torch.float32)
279
  W = torch.from_numpy(W_np).to(dev, dtype=torch.float32)
280
 
281
- # 1. Analog + WLS seed (including negative-scale sign-flip starts):
282
  d_mean = torch.mean(torch.abs(X), dim=1)
 
 
283
  wsum = torch.clamp(torch.sum(W, dim=1, keepdim=True), min=1.0e-30)
284
- d_wls_init = (torch.sum(W * torch.abs(X), dim=1, keepdim=True) / wsum).squeeze(1)
 
 
 
 
285
  mu = torch.sum(W * X, dim=1, keepdim=True) / wsum
286
  var = torch.sum(W * (X - mu) ** 2, dim=1, keepdim=True) / wsum
287
  sig = torch.sqrt(torch.clamp(var, min=1.0e-30))
288
  x_comp = mu + sig * 1.85 * torch.tanh((X - mu) / (1.85 * sig))
289
- d_analog = (torch.sum(W * torch.abs(x_comp), dim=1, keepdim=True) / wsum).squeeze(1)
 
 
 
 
290
 
 
291
  wls_d = d_mean.clone()
292
  best_seed_sse = torch.full((B,), float("inf"), dtype=torch.float32, device=dev)
293
- for d_start in (d_mean, d_wls_init, d_analog, -d_analog):
294
- d = d_start.clone()
295
- for _ in range(2):
296
- q = torch.where(d[:, None] >= 0.0, X >= 0.0, X < 0.0)
297
- c = torch.where(q, 1.0, -1.0)
298
- num = torch.sum(W * X * c, dim=1)
299
- den = torch.sum(W, dim=1)
300
- valid = den > 1.0e-30
301
- d = torch.where(valid, num / torch.where(valid, den, torch.ones_like(den)), torch.zeros_like(den))
302
- df = d.to(torch.float16).to(torch.float32)
303
- q = torch.where(df[:, None] >= 0.0, X >= 0.0, X < 0.0)
304
- c = torch.where(q, 1.0, -1.0)
305
- e = X - df[:, None] * c
306
  sse = torch.sum(W * e * e, dim=1)
307
  take = sse < best_seed_sse
308
  best_seed_sse = torch.where(take, sse, best_seed_sse)
309
  wls_d = torch.where(take, df, wls_d)
310
 
311
- # 2. 24-Candidate Field with Extended Spectral Objective on GPU:
312
  mults = torch.tensor(MULTS_D, dtype=torch.float32, device=dev)
313
- cand_d = (wls_d[:, None] * mults[None, :]).to(torch.float16).to(torch.float32) # (B, 24)
314
- q_c = torch.where(cand_d[:, :, None] >= 0.0, X[:, None, :] >= 0.0, X[:, None, :] < 0.0)
315
- c_c = torch.where(q_c, 1.0, -1.0)
316
- e_c = X[:, None, :] - cand_d[:, :, None] * c_c
317
  sse_c = torch.sum(W[:, None, :] * e_c * e_c, dim=2)
318
  if cfg.use_spectral and (lam_dc > 0.0 or lam_vw > 0.0):
319
  v_c = e_c[:, :, :half] + e_c[:, :, half:]
@@ -328,22 +328,20 @@ def _quantize_q1_0_hpc_cuda(
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:]
@@ -356,9 +354,9 @@ def _quantize_q1_0_hpc_cuda(
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
@@ -368,7 +366,8 @@ def _quantize_q1_0_hpc_cuda(
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])
@@ -436,8 +435,10 @@ def quantize_tensor_q1_0(
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)
 
152
  lam_vw = (float(cfg.vw_lambda) / QK1_0) if cfg.use_spectral else 0.0
153
  half = QK1_0 // 2
154
 
155
+ # 1. Analog + WLS seed for d* (bounded around d_mean so outlier imatrix channels cannot collapse/explode d):
156
  d_mean = np.mean(np.abs(X_f32), axis=1).astype(np.float32)
157
+ d_lo = (0.80 * d_mean).astype(np.float32)
158
+ d_hi = (1.25 * d_mean).astype(np.float32)
159
  wsum = np.maximum(np.sum(W_f32, axis=1, keepdims=True), 1.0e-30)
160
+ d_wls_init = np.clip(
161
+ (np.sum(W_f32 * np.abs(X_f32), axis=1, keepdims=True) / wsum)[:, 0].astype(np.float32),
162
+ d_lo,
163
+ d_hi,
164
+ )
165
  mu = np.sum(W_f32 * X_f32, axis=1, keepdims=True) / wsum
166
  var = np.sum(W_f32 * (X_f32 - mu) ** 2, axis=1, keepdims=True) / wsum
167
  sig = np.sqrt(np.maximum(var, 1.0e-30))
168
  x_comp = mu + sig * 1.85 * np.tanh((X_f32 - mu) / (1.85 * sig))
169
+ d_analog = np.clip(
170
+ (np.sum(W_f32 * np.abs(x_comp), axis=1, keepdims=True) / wsum)[:, 0].astype(np.float32),
171
+ d_lo,
172
+ d_hi,
173
+ )
174
 
175
+ q_sign = (X_f32 >= 0.0).astype(np.float32)
176
+ c_sign = 2.0 * q_sign - 1.0
177
  wls_d = d_mean.copy()
178
  best_seed_sse = np.full(B, np.inf, dtype=np.float64)
179
+ for d_start in (d_mean, d_wls_init, d_analog):
180
+ df = from_fp16_bits(to_fp16_bits(np.clip(d_start, d_lo, d_hi).astype(np.float32)))
181
+ e = X_f32 - df[:, None] * c_sign
 
 
 
 
 
 
 
 
 
 
182
  sse = np.sum(W_f32 * e * e, axis=1, dtype=np.float64)
183
  take = sse < best_seed_sse
184
  best_seed_sse = np.where(take, sse, best_seed_sse)
185
  wls_d = np.where(take, df, wls_d)
186
 
187
+ # 2. 24-Candidate Field with Extended Spectral Objective (clamped to [0.80*d_mean, 1.25*d_mean]):
188
+ cand_raw = np.clip(wls_d[:, None] * MULTS_D[None, :], d_lo[:, None], d_hi[:, None]).astype(np.float32)
189
+ cand_d16 = to_fp16_bits(cand_raw) # (B, 24)
190
  cand_d = from_fp16_bits(cand_d16) # (B, 24)
191
+ e_c = X_f32[:, None, :] - cand_d[:, :, None] * c_sign[:, None, :] # (B, 24, 128)
 
 
192
  sse_c = np.sum(W_f32[:, None, :] * e_c * e_c, axis=2)
193
  if cfg.use_spectral and (lam_dc > 0.0 or lam_vw > 0.0):
194
  v_c = e_c[:, :, :half] + e_c[:, :, half:]
 
202
  rows = np.arange(B)
203
  d_sel = cand_d[rows, best_cand]
204
 
205
+ # 4. FP16-ULP Polish (+/-rad ULPs around d_sel, wls_d, and d_mean within [d_lo, d_hi]):
206
  rad = max(int(cfg.ulp_radius), 4)
207
  deltas = np.arange(-rad, rad + 1, dtype=np.int32)
208
+ lo_b = to_fp16_bits(d_lo).astype(np.int32)[:, None]
209
+ hi_b = np.maximum(to_fp16_bits(d_hi).astype(np.int32)[:, None], lo_b)
210
+ sel_b = np.clip(to_fp16_bits(np.abs(d_sel)).astype(np.int32)[:, None] + deltas[None, :], lo_b, hi_b).astype(np.uint16)
211
+ wls_b = np.clip(to_fp16_bits(np.abs(wls_d)).astype(np.int32)[:, None] + deltas[None, :], lo_b, hi_b).astype(np.uint16)
212
+ ref_b = np.clip(to_fp16_bits(np.abs(d_mean)).astype(np.int32)[:, None] + deltas[None, :], lo_b, hi_b).astype(np.uint16)
213
+ all_b = np.concatenate([sel_b, wls_b, ref_b], axis=1)
214
  all_d = from_fp16_bits(all_b)
215
+ e_u = X_f32[:, None, :] - all_d[:, :, None] * c_sign[:, None, :]
 
 
216
  sse_u = np.sum(W_f32[:, None, :] * e_u * e_u, axis=2, dtype=np.float64)
217
  if cfg.use_spectral and (lam_dc > 0.0 or lam_vw > 0.0):
218
  v_u = e_u[:, :, :half] + e_u[:, :, half:]
 
225
  pick_u = np.argmin(sse_u, axis=1)
226
  best_d16 = all_b[rows, pick_u]
227
  best_d = all_d[rows, pick_u]
228
+ best_q = (X_f32 >= 0.0).astype(np.int64)
229
 
230
+ # 5. Boundary-Guarded 1-D Quadratic Vesica + DC Bit-Flip Projection (only near-zero |x| <= 0.10*d):
231
  if cfg.use_spectral and (lam_vw > 0.0 or lam_dc > 0.0):
232
  c_f = (2 * best_q - 1).astype(np.float64)
233
  d_f64 = best_d.astype(np.float64)[:, None]
 
238
  v = e[:, :half] + e[:, half:]
239
  dc = np.sum(e, axis=1)
240
  denom = (W_f32 + lam_vw + lam_dc) * (step_f64 * step_f64)
241
+ near_zero = np.abs(X_f32) <= 0.10 * d_f64
242
+ act_b = (d_f64 > 1.0e-15) & near_zero
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])
 
278
  X = torch.from_numpy(X_np).to(dev, dtype=torch.float32)
279
  W = torch.from_numpy(W_np).to(dev, dtype=torch.float32)
280
 
281
+ # 1. Analog + WLS seed (bounded around d_mean so outlier imatrix channels cannot collapse/explode d):
282
  d_mean = torch.mean(torch.abs(X), dim=1)
283
+ d_lo = 0.80 * d_mean
284
+ d_hi = 1.25 * d_mean
285
  wsum = torch.clamp(torch.sum(W, dim=1, keepdim=True), min=1.0e-30)
286
+ d_wls_init = torch.clamp(
287
+ (torch.sum(W * torch.abs(X), dim=1, keepdim=True) / wsum).squeeze(1),
288
+ min=d_lo,
289
+ max=d_hi,
290
+ )
291
  mu = torch.sum(W * X, dim=1, keepdim=True) / wsum
292
  var = torch.sum(W * (X - mu) ** 2, dim=1, keepdim=True) / wsum
293
  sig = torch.sqrt(torch.clamp(var, min=1.0e-30))
294
  x_comp = mu + sig * 1.85 * torch.tanh((X - mu) / (1.85 * sig))
295
+ d_analog = torch.clamp(
296
+ (torch.sum(W * torch.abs(x_comp), dim=1, keepdim=True) / wsum).squeeze(1),
297
+ min=d_lo,
298
+ max=d_hi,
299
+ )
300
 
301
+ c_sign = torch.where(X >= 0.0, 1.0, -1.0)
302
  wls_d = d_mean.clone()
303
  best_seed_sse = torch.full((B,), float("inf"), dtype=torch.float32, device=dev)
304
+ for d_start in (d_mean, d_wls_init, d_analog):
305
+ df = torch.clamp(d_start, min=d_lo, max=d_hi).to(torch.float16).to(torch.float32)
306
+ e = X - df[:, None] * c_sign
 
 
 
 
 
 
 
 
 
 
307
  sse = torch.sum(W * e * e, dim=1)
308
  take = sse < best_seed_sse
309
  best_seed_sse = torch.where(take, sse, best_seed_sse)
310
  wls_d = torch.where(take, df, wls_d)
311
 
312
+ # 2. 24-Candidate Field with Extended Spectral Objective on GPU (clamped to [0.80*d_mean, 1.25*d_mean]):
313
  mults = torch.tensor(MULTS_D, dtype=torch.float32, device=dev)
314
+ cand_raw = torch.clamp(wls_d[:, None] * mults[None, :], min=d_lo[:, None], max=d_hi[:, None])
315
+ cand_d = cand_raw.to(torch.float16).to(torch.float32) # (B, 24)
316
+ e_c = X[:, None, :] - cand_d[:, :, None] * c_sign[:, None, :]
 
317
  sse_c = torch.sum(W[:, None, :] * e_c * e_c, dim=2)
318
  if cfg.use_spectral and (lam_dc > 0.0 or lam_vw > 0.0):
319
  v_c = e_c[:, :, :half] + e_c[:, :, half:]
 
328
  rows = torch.arange(B, device=dev)
329
  d_sel = cand_d[rows, best_cand_t]
330
 
331
+ # 4. FP16-ULP Polish (+/-rad ULPs around d_sel, wls_d, and d_mean within [d_lo, d_hi]):
332
  rad = max(int(cfg.ulp_radius), 4)
333
  deltas = torch.arange(-rad, rad + 1, dtype=torch.int32, device=dev)
334
+ lo_bits = (d_lo.to(torch.float16).view(torch.int16).to(torch.int32) & 0x7FFF)[:, None]
335
+ hi_bits = torch.maximum((d_hi.to(torch.float16).view(torch.int16).to(torch.int32) & 0x7FFF)[:, None], lo_bits)
336
  abs_bits = torch.abs(d_sel).to(torch.float16).view(torch.int16).to(torch.int32) & 0x7FFF
337
  wls_bits = torch.abs(wls_d).to(torch.float16).view(torch.int16).to(torch.int32) & 0x7FFF
338
  ref_bits = torch.abs(d_mean).to(torch.float16).view(torch.int16).to(torch.int32) & 0x7FFF
339
+ pos_b = torch.clamp(abs_bits[:, None] + deltas[None, :], lo_bits, hi_bits)
340
+ wls_b = torch.clamp(wls_bits[:, None] + deltas[None, :], lo_bits, hi_bits)
341
+ mean_b = torch.clamp(ref_bits[:, None] + deltas[None, :], lo_bits, hi_bits)
342
+ all_b = torch.cat([pos_b, wls_b, mean_b], dim=1)
 
 
343
  all_d = all_b.to(torch.int16).view(torch.float16).to(torch.float32)
344
+ e_u = X[:, None, :] - all_d[:, :, None] * c_sign[:, None, :]
 
 
345
  sse_u = torch.sum(W[:, None, :] * e_u * e_u, dim=2)
346
  if cfg.use_spectral and (lam_dc > 0.0 or lam_vw > 0.0):
347
  v_u = e_u[:, :, :half] + e_u[:, :, half:]
 
354
  pick_u = torch.argmin(sse_u, dim=1)
355
  best_d16 = all_b[rows, pick_u]
356
  best_d = all_d[rows, pick_u]
357
+ best_q = (X >= 0.0).to(torch.int64)
358
 
359
+ # 5. Boundary-Guarded 1-D Quadratic Vesica + DC Bit-Flip Projection (only near-zero |x| <= 0.10*d):
360
  if cfg.use_spectral and (lam_vw > 0.0 or lam_dc > 0.0):
361
  d_col = best_d[:, None]
362
  step_col = 2.0 * d_col
 
366
  v = e[:, :half] + e[:, half:]
367
  dc = torch.sum(e, dim=1)
368
  denom = (W + lam_vw + lam_dc) * (step_col * step_col)
369
+ near_zero = torch.abs(X) <= 0.10 * d_col
370
+ act_b = (d_col > 1.0e-15) & near_zero
371
  s_den = torch.where(act_b & (denom > 1.0e-30), denom, torch.ones_like(denom))
372
  v_full = torch.cat([v, v], dim=1)
373
  grad = step_col * (W * e + lam_vw * v_full + lam_dc * dc[:, None])
 
435
  Wb = np.ones_like(Xb, dtype=np.float32)
436
  else:
437
  wr = np.maximum(W_raw[start:stop], 1.0e-6)
438
+ med = np.maximum(np.median(wr, axis=1, keepdims=True), 1.0e-6)
439
+ wt = np.clip((wr / med) ** 0.25 if cfg.temper_imatrix else wr / med, 0.5, 2.5)
440
+ wm = np.mean(wt, axis=1, keepdims=True)
441
+ Wb = np.where(wm > 1.0e-30, wt / np.maximum(wm, 1.0e-30), 1.0).astype(np.float32)
442
 
443
  if use_cuda and dev is not None:
444
  raw_b, sse_b = _quantize_q1_0_hpc_cuda(Xb, Wb, dev, cfg)