Upload 2 files
Browse files
gguf.py
CHANGED
|
@@ -462,12 +462,14 @@ def _is_attention_projection(name: str) -> bool:
|
|
| 462 |
|
| 463 |
def _is_tiny_ssm_gate(name: str, dims: tuple[int, ...]) -> bool:
|
| 464 |
"""True for low-rank recurrent step-size/decay gates (e.g. ssm_alpha, ssm_beta, ssm_f_a/b)."""
|
|
|
|
|
|
|
| 465 |
if any(
|
| 466 |
k in name
|
| 467 |
for k in ("ssm_alpha", "ssm_beta", "ssm_f_a", "ssm_f_b", "ssm_g_a", "ssm_g_b")
|
| 468 |
):
|
| 469 |
return True
|
| 470 |
-
if ".ssm_" in name and
|
| 471 |
return True
|
| 472 |
return False
|
| 473 |
|
|
@@ -517,6 +519,8 @@ def is_q1_0_eligible(
|
|
| 517 |
"""True for 128-aligned 2D/3D weight tensors eligible for Q1_0."""
|
| 518 |
if _is_skipped_metadata_tensor(name, dims):
|
| 519 |
return False
|
|
|
|
|
|
|
| 520 |
if _is_embd_or_head(name) and not include_embd:
|
| 521 |
return False
|
| 522 |
if len(dims) < 2 or dims[0] % QK1_0 != 0:
|
|
|
|
| 462 |
|
| 463 |
def _is_tiny_ssm_gate(name: str, dims: tuple[int, ...]) -> bool:
|
| 464 |
"""True for low-rank recurrent step-size/decay gates (e.g. ssm_alpha, ssm_beta, ssm_f_a/b)."""
|
| 465 |
+
if len(dims) < 2 or max(int(d) for d in dims) < 1024:
|
| 466 |
+
return False
|
| 467 |
if any(
|
| 468 |
k in name
|
| 469 |
for k in ("ssm_alpha", "ssm_beta", "ssm_f_a", "ssm_f_b", "ssm_g_a", "ssm_g_b")
|
| 470 |
):
|
| 471 |
return True
|
| 472 |
+
if ".ssm_" in name and min(int(dims[0]), int(dims[1])) <= 128:
|
| 473 |
return True
|
| 474 |
return False
|
| 475 |
|
|
|
|
| 519 |
"""True for 128-aligned 2D/3D weight tensors eligible for Q1_0."""
|
| 520 |
if _is_skipped_metadata_tensor(name, dims):
|
| 521 |
return False
|
| 522 |
+
if _is_tiny_ssm_gate(name, dims):
|
| 523 |
+
return False
|
| 524 |
if _is_embd_or_head(name) and not include_embd:
|
| 525 |
return False
|
| 526 |
if len(dims) < 2 or dims[0] % QK1_0 != 0:
|
q1_0.py
CHANGED
|
@@ -152,43 +152,39 @@ 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*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
)
|
|
|
|
| 174 |
|
| 175 |
q_sign = (X_f32 >= 0.0).astype(np.float32)
|
| 176 |
c_sign = 2.0 * q_sign - 1.0
|
| 177 |
-
wls_d =
|
| 178 |
-
|
| 179 |
-
|
| 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 =
|
| 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,17 +198,17 @@ def _quantize_q1_0_hpc_cpu(
|
|
| 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
|
| 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 =
|
| 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:]
|
|
@@ -232,13 +228,13 @@ def _quantize_q1_0_hpc_cpu(
|
|
| 232 |
c_f = (2 * best_q - 1).astype(np.float64)
|
| 233 |
d_f64 = best_d.astype(np.float64)[:, None]
|
| 234 |
step_f64 = 2.0 * d_f64
|
| 235 |
-
e =
|
| 236 |
sse_live = np.sum(W_f32 * e * e, axis=1, dtype=np.float64)
|
| 237 |
sse_cap = sse_live * (1.0 + cfg.sse_budget)
|
| 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(
|
| 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)
|
|
@@ -246,7 +242,7 @@ def _quantize_q1_0_hpc_cpu(
|
|
| 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)
|
| 249 |
-
e_new =
|
| 250 |
sse_new = np.sum(W_f32 * e_new * e_new, axis=1, dtype=np.float64)
|
| 251 |
v_new = e_new[:, :half] + e_new[:, half:]
|
| 252 |
dc_new = np.sum(e_new, axis=1)
|
|
@@ -278,42 +274,33 @@ 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
|
| 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 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
)
|
|
|
|
| 300 |
|
| 301 |
c_sign = torch.where(X >= 0.0, 1.0, -1.0)
|
| 302 |
-
wls_d =
|
| 303 |
-
|
| 304 |
-
|
| 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 =
|
| 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,20 +315,21 @@ 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. FP16-ULP Polish (+/-rad ULPs around d_sel, wls_d, and
|
| 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 |
-
|
|
|
|
| 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 =
|
| 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:]
|
|
@@ -360,13 +348,13 @@ def _quantize_q1_0_hpc_cuda(
|
|
| 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
|
| 363 |
-
e =
|
| 364 |
sse_live = torch.sum(W * e * e, dim=1)
|
| 365 |
sse_cap = sse_live * (1.0 + float(cfg.sse_budget))
|
| 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(
|
| 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)
|
|
@@ -378,7 +366,7 @@ def _quantize_q1_0_hpc_cuda(
|
|
| 378 |
torch.zeros_like(best_q),
|
| 379 |
)
|
| 380 |
q_new = torch.clamp(best_q + dq, 0, 1)
|
| 381 |
-
e_new =
|
| 382 |
sse_new = torch.sum(W * e_new * e_new, dim=1)
|
| 383 |
v_new = e_new[:, :half] + e_new[:, half:]
|
| 384 |
dc_new = torch.sum(e_new, dim=1)
|
|
|
|
| 152 |
lam_vw = (float(cfg.vw_lambda) / QK1_0) if cfg.use_spectral else 0.0
|
| 153 |
half = QK1_0 // 2
|
| 154 |
|
| 155 |
+
# 1. Iso-Energy / Geometric-Projection Analog + WLS seed for d*:
|
| 156 |
+
# Standard d_mean = mean(|x|) attenuates each linear projection by 2/pi (~0.637),
|
| 157 |
+
# shrinking SwiGLU FFN residual updates to 0.30x across layers.
|
| 158 |
+
# Projecting onto the geometric mean of the Iso-Energy RMS scale d_rms and the
|
| 159 |
+
# unbiased linear-projection scale d_proj = d_rms^2 / d_mean restores multi-layer
|
| 160 |
+
# residual norm (~0.82x == cos_theta) and improves multi-layer cosine similarity.
|
| 161 |
d_mean = np.mean(np.abs(X_f32), axis=1).astype(np.float32)
|
|
|
|
|
|
|
| 162 |
wsum = np.maximum(np.sum(W_f32, axis=1, keepdims=True), 1.0e-30)
|
| 163 |
+
d_wls_l1 = (np.sum(W_f32 * np.abs(X_f32), axis=1, keepdims=True) / wsum)[:, 0].astype(np.float32)
|
| 164 |
+
d_wls_rms = np.sqrt(np.maximum((np.sum(W_f32 * X_f32 * X_f32, axis=1, keepdims=True) / wsum)[:, 0], 0.0)).astype(np.float32)
|
| 165 |
+
if cfg.use_weight_analogs:
|
| 166 |
+
ratio = np.clip(d_wls_rms / np.maximum(d_wls_l1, 1.0e-30), 1.0, 1.45).astype(np.float32)
|
| 167 |
+
d_center = (d_wls_rms * np.sqrt(ratio)).astype(np.float32)
|
| 168 |
+
d_lo = (0.85 * d_wls_rms).astype(np.float32)
|
| 169 |
+
d_hi = (1.35 * d_wls_rms).astype(np.float32)
|
| 170 |
+
else:
|
| 171 |
+
d_center = d_wls_l1
|
| 172 |
+
d_lo = (0.80 * d_mean).astype(np.float32)
|
| 173 |
+
d_hi = (1.25 * d_mean).astype(np.float32)
|
| 174 |
+
|
| 175 |
+
d_center = np.clip(d_center, d_lo, d_hi).astype(np.float32)
|
| 176 |
+
gain = (d_center / np.maximum(d_wls_l1, 1.0e-30))[:, None].astype(np.float32)
|
| 177 |
+
X_target = (X_f32 * gain).astype(np.float32)
|
| 178 |
|
| 179 |
q_sign = (X_f32 >= 0.0).astype(np.float32)
|
| 180 |
c_sign = 2.0 * q_sign - 1.0
|
| 181 |
+
wls_d = from_fp16_bits(to_fp16_bits(d_center))
|
| 182 |
+
|
| 183 |
+
# 2. 24-Candidate Field with Extended Spectral Objective (clamped to [d_lo, d_hi]):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
cand_raw = np.clip(wls_d[:, None] * MULTS_D[None, :], d_lo[:, None], d_hi[:, None]).astype(np.float32)
|
| 185 |
cand_d16 = to_fp16_bits(cand_raw) # (B, 24)
|
| 186 |
cand_d = from_fp16_bits(cand_d16) # (B, 24)
|
| 187 |
+
e_c = X_target[:, None, :] - cand_d[:, :, None] * c_sign[:, None, :] # (B, 24, 128)
|
| 188 |
sse_c = np.sum(W_f32[:, None, :] * e_c * e_c, axis=2)
|
| 189 |
if cfg.use_spectral and (lam_dc > 0.0 or lam_vw > 0.0):
|
| 190 |
v_c = e_c[:, :, :half] + e_c[:, :, half:]
|
|
|
|
| 198 |
rows = np.arange(B)
|
| 199 |
d_sel = cand_d[rows, best_cand]
|
| 200 |
|
| 201 |
+
# 4. FP16-ULP Polish (+/-rad ULPs around d_sel, wls_d, and d_wls_rms within [d_lo, d_hi]):
|
| 202 |
rad = max(int(cfg.ulp_radius), 4)
|
| 203 |
deltas = np.arange(-rad, rad + 1, dtype=np.int32)
|
| 204 |
lo_b = to_fp16_bits(d_lo).astype(np.int32)[:, None]
|
| 205 |
hi_b = np.maximum(to_fp16_bits(d_hi).astype(np.int32)[:, None], lo_b)
|
| 206 |
sel_b = np.clip(to_fp16_bits(np.abs(d_sel)).astype(np.int32)[:, None] + deltas[None, :], lo_b, hi_b).astype(np.uint16)
|
| 207 |
wls_b = np.clip(to_fp16_bits(np.abs(wls_d)).astype(np.int32)[:, None] + deltas[None, :], lo_b, hi_b).astype(np.uint16)
|
| 208 |
+
ref_b = np.clip(to_fp16_bits(np.abs(d_wls_rms if cfg.use_weight_analogs else d_mean)).astype(np.int32)[:, None] + deltas[None, :], lo_b, hi_b).astype(np.uint16)
|
| 209 |
all_b = np.concatenate([sel_b, wls_b, ref_b], axis=1)
|
| 210 |
all_d = from_fp16_bits(all_b)
|
| 211 |
+
e_u = X_target[:, None, :] - all_d[:, :, None] * c_sign[:, None, :]
|
| 212 |
sse_u = np.sum(W_f32[:, None, :] * e_u * e_u, axis=2, dtype=np.float64)
|
| 213 |
if cfg.use_spectral and (lam_dc > 0.0 or lam_vw > 0.0):
|
| 214 |
v_u = e_u[:, :, :half] + e_u[:, :, half:]
|
|
|
|
| 228 |
c_f = (2 * best_q - 1).astype(np.float64)
|
| 229 |
d_f64 = best_d.astype(np.float64)[:, None]
|
| 230 |
step_f64 = 2.0 * d_f64
|
| 231 |
+
e = X_target.astype(np.float64) - d_f64 * c_f
|
| 232 |
sse_live = np.sum(W_f32 * e * e, axis=1, dtype=np.float64)
|
| 233 |
sse_cap = sse_live * (1.0 + cfg.sse_budget)
|
| 234 |
v = e[:, :half] + e[:, half:]
|
| 235 |
dc = np.sum(e, axis=1)
|
| 236 |
denom = (W_f32 + lam_vw + lam_dc) * (step_f64 * step_f64)
|
| 237 |
+
near_zero = np.abs(X_target) <= 0.10 * d_f64
|
| 238 |
act_b = (d_f64 > 1.0e-15) & near_zero
|
| 239 |
s_den = np.where(act_b & (denom > 1.0e-30), denom, 1.0)
|
| 240 |
v_full = np.concatenate([v, v], axis=1)
|
|
|
|
| 242 |
dq = np.where(act_b, nearest_int((grad / s_den).astype(np.float32)), 0)
|
| 243 |
q_new = np.clip(best_q + dq, 0, 1)
|
| 244 |
c_new = (2 * q_new - 1).astype(np.float64)
|
| 245 |
+
e_new = X_target.astype(np.float64) - d_f64 * c_new
|
| 246 |
sse_new = np.sum(W_f32 * e_new * e_new, axis=1, dtype=np.float64)
|
| 247 |
v_new = e_new[:, :half] + e_new[:, half:]
|
| 248 |
dc_new = np.sum(e_new, axis=1)
|
|
|
|
| 274 |
X = torch.from_numpy(X_np).to(dev, dtype=torch.float32)
|
| 275 |
W = torch.from_numpy(W_np).to(dev, dtype=torch.float32)
|
| 276 |
|
| 277 |
+
# 1. Iso-Energy / Geometric-Projection Analog + WLS seed on GPU:
|
| 278 |
d_mean = torch.mean(torch.abs(X), dim=1)
|
|
|
|
|
|
|
| 279 |
wsum = torch.clamp(torch.sum(W, dim=1, keepdim=True), min=1.0e-30)
|
| 280 |
+
d_wls_l1 = (torch.sum(W * torch.abs(X), dim=1, keepdim=True) / wsum).squeeze(1)
|
| 281 |
+
d_wls_rms = torch.sqrt(torch.clamp((torch.sum(W * X * X, dim=1, keepdim=True) / wsum).squeeze(1), min=0.0))
|
| 282 |
+
if cfg.use_weight_analogs:
|
| 283 |
+
ratio = torch.clamp(d_wls_rms / torch.clamp(d_wls_l1, min=1.0e-30), min=1.0, max=1.45)
|
| 284 |
+
d_center = d_wls_rms * torch.sqrt(ratio)
|
| 285 |
+
d_lo = 0.85 * d_wls_rms
|
| 286 |
+
d_hi = 1.35 * d_wls_rms
|
| 287 |
+
else:
|
| 288 |
+
d_center = d_wls_l1
|
| 289 |
+
d_lo = 0.80 * d_mean
|
| 290 |
+
d_hi = 1.25 * d_mean
|
| 291 |
+
|
| 292 |
+
d_center = torch.clamp(d_center, min=d_lo, max=d_hi)
|
| 293 |
+
gain = (d_center / torch.clamp(d_wls_l1, min=1.0e-30)).unsqueeze(1)
|
| 294 |
+
X_target = X * gain
|
| 295 |
|
| 296 |
c_sign = torch.where(X >= 0.0, 1.0, -1.0)
|
| 297 |
+
wls_d = d_center.to(torch.float16).to(torch.float32)
|
| 298 |
+
|
| 299 |
+
# 2. 24-Candidate Field with Extended Spectral Objective on GPU (clamped to [d_lo, d_hi]):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 300 |
mults = torch.tensor(MULTS_D, dtype=torch.float32, device=dev)
|
| 301 |
cand_raw = torch.clamp(wls_d[:, None] * mults[None, :], min=d_lo[:, None], max=d_hi[:, None])
|
| 302 |
cand_d = cand_raw.to(torch.float16).to(torch.float32) # (B, 24)
|
| 303 |
+
e_c = X_target[:, None, :] - cand_d[:, :, None] * c_sign[:, None, :]
|
| 304 |
sse_c = torch.sum(W[:, None, :] * e_c * e_c, dim=2)
|
| 305 |
if cfg.use_spectral and (lam_dc > 0.0 or lam_vw > 0.0):
|
| 306 |
v_c = e_c[:, :, :half] + e_c[:, :, half:]
|
|
|
|
| 315 |
rows = torch.arange(B, device=dev)
|
| 316 |
d_sel = cand_d[rows, best_cand_t]
|
| 317 |
|
| 318 |
+
# 4. FP16-ULP Polish (+/-rad ULPs around d_sel, wls_d, and d_wls_rms within [d_lo, d_hi]):
|
| 319 |
rad = max(int(cfg.ulp_radius), 4)
|
| 320 |
deltas = torch.arange(-rad, rad + 1, dtype=torch.int32, device=dev)
|
| 321 |
lo_bits = (d_lo.to(torch.float16).view(torch.int16).to(torch.int32) & 0x7FFF)[:, None]
|
| 322 |
hi_bits = torch.maximum((d_hi.to(torch.float16).view(torch.int16).to(torch.int32) & 0x7FFF)[:, None], lo_bits)
|
| 323 |
abs_bits = torch.abs(d_sel).to(torch.float16).view(torch.int16).to(torch.int32) & 0x7FFF
|
| 324 |
wls_bits = torch.abs(wls_d).to(torch.float16).view(torch.int16).to(torch.int32) & 0x7FFF
|
| 325 |
+
ref_src = d_wls_rms if cfg.use_weight_analogs else d_mean
|
| 326 |
+
ref_bits = torch.abs(ref_src).to(torch.float16).view(torch.int16).to(torch.int32) & 0x7FFF
|
| 327 |
pos_b = torch.clamp(abs_bits[:, None] + deltas[None, :], lo_bits, hi_bits)
|
| 328 |
wls_b = torch.clamp(wls_bits[:, None] + deltas[None, :], lo_bits, hi_bits)
|
| 329 |
mean_b = torch.clamp(ref_bits[:, None] + deltas[None, :], lo_bits, hi_bits)
|
| 330 |
all_b = torch.cat([pos_b, wls_b, mean_b], dim=1)
|
| 331 |
all_d = all_b.to(torch.int16).view(torch.float16).to(torch.float32)
|
| 332 |
+
e_u = X_target[:, None, :] - all_d[:, :, None] * c_sign[:, None, :]
|
| 333 |
sse_u = torch.sum(W[:, None, :] * e_u * e_u, dim=2)
|
| 334 |
if cfg.use_spectral and (lam_dc > 0.0 or lam_vw > 0.0):
|
| 335 |
v_u = e_u[:, :, :half] + e_u[:, :, half:]
|
|
|
|
| 348 |
if cfg.use_spectral and (lam_vw > 0.0 or lam_dc > 0.0):
|
| 349 |
d_col = best_d[:, None]
|
| 350 |
step_col = 2.0 * d_col
|
| 351 |
+
e = X_target - d_col * (2.0 * best_q.to(torch.float32) - 1.0)
|
| 352 |
sse_live = torch.sum(W * e * e, dim=1)
|
| 353 |
sse_cap = sse_live * (1.0 + float(cfg.sse_budget))
|
| 354 |
v = e[:, :half] + e[:, half:]
|
| 355 |
dc = torch.sum(e, dim=1)
|
| 356 |
denom = (W + lam_vw + lam_dc) * (step_col * step_col)
|
| 357 |
+
near_zero = torch.abs(X_target) <= 0.10 * d_col
|
| 358 |
act_b = (d_col > 1.0e-15) & near_zero
|
| 359 |
s_den = torch.where(act_b & (denom > 1.0e-30), denom, torch.ones_like(denom))
|
| 360 |
v_full = torch.cat([v, v], dim=1)
|
|
|
|
| 366 |
torch.zeros_like(best_q),
|
| 367 |
)
|
| 368 |
q_new = torch.clamp(best_q + dq, 0, 1)
|
| 369 |
+
e_new = X_target - d_col * (2.0 * q_new.to(torch.float32) - 1.0)
|
| 370 |
sse_new = torch.sum(W * e_new * e_new, dim=1)
|
| 371 |
v_new = e_new[:, :half] + e_new[:, half:]
|
| 372 |
dc_new = torch.sum(e_new, dim=1)
|