Upload 8 files
Browse files- __main__.py +8 -0
- config.py +7 -0
- cuda_backend.py +25 -1
- gui.py +10 -1
- pipeline.py +39 -0
- q2k.py +34 -0
__main__.py
CHANGED
|
@@ -51,6 +51,8 @@ def _config_from_args(args) -> HPCConfig:
|
|
| 51 |
cfg.therm_passes = int(args.thermal)
|
| 52 |
if args.thermal_tilt is not None:
|
| 53 |
cfg.therm_lambda_tilt = float(args.thermal_tilt)
|
|
|
|
|
|
|
| 54 |
if args.no_q4_fallback:
|
| 55 |
cfg.enable_q4_fallback = False
|
| 56 |
if args.keep_embd:
|
|
@@ -296,6 +298,12 @@ def main() -> None:
|
|
| 296 |
help="zero-residual anneal: multiplier (>=1, 1=off) on dc/vw lambdas during thermal passes; "
|
| 297 |
"~100-1e3 drives the coherent residual (row DC + vesica) toward zero, spending weighted SSE",
|
| 298 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
parser.add_argument(
|
| 300 |
"--format",
|
| 301 |
choices=("mixed", "mixed_q1", "custom", "q1_0", "q3_k", "q4_0", "q2_k"),
|
|
|
|
| 51 |
cfg.therm_passes = int(args.thermal)
|
| 52 |
if args.thermal_tilt is not None:
|
| 53 |
cfg.therm_lambda_tilt = float(args.thermal_tilt)
|
| 54 |
+
if args.pin_dc:
|
| 55 |
+
cfg.pin_dc = True
|
| 56 |
if args.no_q4_fallback:
|
| 57 |
cfg.enable_q4_fallback = False
|
| 58 |
if args.keep_embd:
|
|
|
|
| 298 |
help="zero-residual anneal: multiplier (>=1, 1=off) on dc/vw lambdas during thermal passes; "
|
| 299 |
"~100-1e3 drives the coherent residual (row DC + vesica) toward zero, spending weighted SSE",
|
| 300 |
)
|
| 301 |
+
parser.add_argument(
|
| 302 |
+
"--pin-dc",
|
| 303 |
+
action="store_true",
|
| 304 |
+
help="zero-DC pin (Q2_K free lunch): re-solve (d, m) on the exact dc=0 line "
|
| 305 |
+
"after assembly/thermal; strictly lowers SSE while driving row-DC residual to ~0",
|
| 306 |
+
)
|
| 307 |
parser.add_argument(
|
| 308 |
"--format",
|
| 309 |
choices=("mixed", "mixed_q1", "custom", "q1_0", "q3_k", "q4_0", "q2_k"),
|
config.py
CHANGED
|
@@ -89,6 +89,13 @@ class HPCConfig:
|
|
| 89 |
# residual (row DC + vesica pairs) toward the lattice floor, leaving an
|
| 90 |
# (almost) spectrally white error. 1.0 = off.
|
| 91 |
therm_lambda_tilt: float = 1.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
# ---- seed clamp -----------------------------------------------------
|
| 94 |
clamp_kurtosis_off: float = 6.0
|
|
|
|
| 89 |
# residual (row DC + vesica pairs) toward the lattice floor, leaving an
|
| 90 |
# (almost) spectrally white error. 1.0 = off.
|
| 91 |
therm_lambda_tilt: float = 1.0
|
| 92 |
+
# Zero-DC pin (the "Q2_K free lunch"): after assembly (and any thermal
|
| 93 |
+
# refinement) re-solve (d, m) on the exact dc=0 constraint line — a joint
|
| 94 |
+
# least-squares translation whose closed form *lowers* SSE while driving
|
| 95 |
+
# each block's row-DC residual to ~0 (bounded only by fp16 storage of the
|
| 96 |
+
# newly fitted d/m). Structurally available only in formats with an
|
| 97 |
+
# additive dof (Q2_K); a no-op elsewhere.
|
| 98 |
+
pin_dc: bool = False
|
| 99 |
|
| 100 |
# ---- seed clamp -----------------------------------------------------
|
| 101 |
clamp_kurtosis_off: float = 6.0
|
cuda_backend.py
CHANGED
|
@@ -26,7 +26,7 @@ except ImportError: # pragma: no cover
|
|
| 26 |
from .config import HPCConfig
|
| 27 |
from .factored import MULTS_D, MULTS_M, N_CAND_D, N_CAND_M, TOTAL_CANDIDATES
|
| 28 |
from .pipeline import QuantizedTensor, TensorStats, _phase3_select
|
| 29 |
-
from .q2k import NS, QK_K, Q2KBlock
|
| 30 |
from .seeds import seed_clamp_value
|
| 31 |
from .spectral import fold_weights
|
| 32 |
|
|
@@ -1162,6 +1162,30 @@ def quantize_cuda(
|
|
| 1162 |
X_np, W_np, final_d, final_m, final_ls, final_lm, final_codes, cfg, row_width, dev
|
| 1163 |
)
|
| 1164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1165 |
scales = (final_ls & 0x0F) | ((final_lm & 0x0F) << np.uint8(4))
|
| 1166 |
qs = np.empty((n_blocks, QK_K // 4), dtype=np.uint8)
|
| 1167 |
for base in (0, 128):
|
|
|
|
| 26 |
from .config import HPCConfig
|
| 27 |
from .factored import MULTS_D, MULTS_M, N_CAND_D, N_CAND_M, TOTAL_CANDIDATES
|
| 28 |
from .pipeline import QuantizedTensor, TensorStats, _phase3_select
|
| 29 |
+
from .q2k import NS, QK_K, Q2KBlock, pin_q2k_dc_residual
|
| 30 |
from .seeds import seed_clamp_value
|
| 31 |
from .spectral import fold_weights
|
| 32 |
|
|
|
|
| 1162 |
X_np, W_np, final_d, final_m, final_ls, final_lm, final_codes, cfg, row_width, dev
|
| 1163 |
)
|
| 1164 |
|
| 1165 |
+
if cfg.pin_dc:
|
| 1166 |
+
_check_stop()
|
| 1167 |
+
t_pin = time.time()
|
| 1168 |
+
_emit(
|
| 1169 |
+
f"Phase 4.7: Zero-DC Pin ({gpu_name})",
|
| 1170 |
+
0,
|
| 1171 |
+
n_blocks,
|
| 1172 |
+
"re-solving (d, m) on the exact dc=0 constraint line "
|
| 1173 |
+
"(exact-JS zero-coherent-residual pin, SSE-nondecreasing)",
|
| 1174 |
+
sigma=sigma,
|
| 1175 |
+
temperature=temperature,
|
| 1176 |
+
)
|
| 1177 |
+
final_d, final_m = pin_q2k_dc_residual(
|
| 1178 |
+
final_d.copy(), final_m.copy(), final_ls, final_lm, final_codes, X_np.astype(np.float64)
|
| 1179 |
+
)
|
| 1180 |
+
_emit(
|
| 1181 |
+
f"Phase 4.7: Zero-DC Pin ({gpu_name})",
|
| 1182 |
+
n_blocks,
|
| 1183 |
+
n_blocks,
|
| 1184 |
+
f"done in {time.time() - t_pin:.1f}s",
|
| 1185 |
+
sigma=sigma,
|
| 1186 |
+
temperature=temperature,
|
| 1187 |
+
)
|
| 1188 |
+
|
| 1189 |
scales = (final_ls & 0x0F) | ((final_lm & 0x0F) << np.uint8(4))
|
| 1190 |
qs = np.empty((n_blocks, QK_K // 4), dtype=np.uint8)
|
| 1191 |
for base in (0, 128):
|
gui.py
CHANGED
|
@@ -183,6 +183,7 @@ class HPCQuantizeGUI:
|
|
| 183 |
self.sse_budget_var = tk.DoubleVar(value=0.0005)
|
| 184 |
self.thermal_var = tk.IntVar(value=0)
|
| 185 |
self.thermal_tilt_var = tk.DoubleVar(value=1.0)
|
|
|
|
| 186 |
|
| 187 |
self.cli_preview_var = tk.StringVar()
|
| 188 |
self.status_var = tk.StringVar(value="Ready.")
|
|
@@ -477,10 +478,15 @@ class HPCQuantizeGUI:
|
|
| 477 |
).grid(row=2, column=1, sticky=tk.EW, padx=4, pady=2)
|
| 478 |
ttk.Label(spec_f, text="Zero-Residual Tilt (1=off):").grid(row=2, column=2, sticky=tk.W, padx=4, pady=2)
|
| 479 |
ttk.Entry(spec_f, textvariable=self.thermal_tilt_var, width=8).grid(row=2, column=3, sticky=tk.EW, padx=4, pady=2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 480 |
ttk.Label(
|
| 481 |
spec_f, text="(λ multiplier ≥1 during thermal anneal; ~1e2–1e3 ≈ zero-coherent-residual '0-error' anneal)",
|
| 482 |
foreground="#93c5fd",
|
| 483 |
-
).grid(row=
|
| 484 |
|
| 485 |
# Right: GGUF Tensor Browser
|
| 486 |
browser_frame = ttk.LabelFrame(right_pane, text="3. GGUF Tensor Inspector & Live Status", padding=8)
|
|
@@ -809,6 +815,7 @@ class HPCQuantizeGUI:
|
|
| 809 |
sse_budget=float(self.sse_budget_var.get()),
|
| 810 |
therm_passes=max(0, int(self.thermal_var.get())),
|
| 811 |
therm_lambda_tilt=max(1.0, float(self.thermal_tilt_var.get())),
|
|
|
|
| 812 |
quant_format=self.quant_format_var.get(),
|
| 813 |
custom_quant_map=self._custom_map_for_plan() or {},
|
| 814 |
enable_q4_fallback=bool(self.q4_fallback_var.get()) and self.quant_format_var.get() not in ("q2_k", "q3_k"),
|
|
@@ -860,6 +867,8 @@ class HPCQuantizeGUI:
|
|
| 860 |
cmd += ["--thermal", str(int(self.thermal_var.get()))]
|
| 861 |
if self.thermal_tilt_var.get() > 1.0:
|
| 862 |
cmd += ["--thermal-tilt", str(float(self.thermal_tilt_var.get()))]
|
|
|
|
|
|
|
| 863 |
cmd.append("--verbose")
|
| 864 |
return " ".join(shlex.quote(part) for part in cmd)
|
| 865 |
|
|
|
|
| 183 |
self.sse_budget_var = tk.DoubleVar(value=0.0005)
|
| 184 |
self.thermal_var = tk.IntVar(value=0)
|
| 185 |
self.thermal_tilt_var = tk.DoubleVar(value=1.0)
|
| 186 |
+
self.pin_dc_var = tk.BooleanVar(value=False)
|
| 187 |
|
| 188 |
self.cli_preview_var = tk.StringVar()
|
| 189 |
self.status_var = tk.StringVar(value="Ready.")
|
|
|
|
| 478 |
).grid(row=2, column=1, sticky=tk.EW, padx=4, pady=2)
|
| 479 |
ttk.Label(spec_f, text="Zero-Residual Tilt (1=off):").grid(row=2, column=2, sticky=tk.W, padx=4, pady=2)
|
| 480 |
ttk.Entry(spec_f, textvariable=self.thermal_tilt_var, width=8).grid(row=2, column=3, sticky=tk.EW, padx=4, pady=2)
|
| 481 |
+
ttk.Checkbutton(
|
| 482 |
+
spec_f,
|
| 483 |
+
text="Pin DC residual to 0 (Q2_K free lunch)",
|
| 484 |
+
variable=self.pin_dc_var,
|
| 485 |
+
).grid(row=3, column=0, columnspan=2, sticky=tk.W, padx=4, pady=1)
|
| 486 |
ttk.Label(
|
| 487 |
spec_f, text="(λ multiplier ≥1 during thermal anneal; ~1e2–1e3 ≈ zero-coherent-residual '0-error' anneal)",
|
| 488 |
foreground="#93c5fd",
|
| 489 |
+
).grid(row=4, column=0, columnspan=4, sticky=tk.W, padx=4, pady=(0, 2))
|
| 490 |
|
| 491 |
# Right: GGUF Tensor Browser
|
| 492 |
browser_frame = ttk.LabelFrame(right_pane, text="3. GGUF Tensor Inspector & Live Status", padding=8)
|
|
|
|
| 815 |
sse_budget=float(self.sse_budget_var.get()),
|
| 816 |
therm_passes=max(0, int(self.thermal_var.get())),
|
| 817 |
therm_lambda_tilt=max(1.0, float(self.thermal_tilt_var.get())),
|
| 818 |
+
pin_dc=bool(self.pin_dc_var.get()),
|
| 819 |
quant_format=self.quant_format_var.get(),
|
| 820 |
custom_quant_map=self._custom_map_for_plan() or {},
|
| 821 |
enable_q4_fallback=bool(self.q4_fallback_var.get()) and self.quant_format_var.get() not in ("q2_k", "q3_k"),
|
|
|
|
| 867 |
cmd += ["--thermal", str(int(self.thermal_var.get()))]
|
| 868 |
if self.thermal_tilt_var.get() > 1.0:
|
| 869 |
cmd += ["--thermal-tilt", str(float(self.thermal_tilt_var.get()))]
|
| 870 |
+
if self.pin_dc_var.get():
|
| 871 |
+
cmd += ["--pin-dc"]
|
| 872 |
cmd.append("--verbose")
|
| 873 |
return " ".join(shlex.quote(part) for part in cmd)
|
| 874 |
|
pipeline.py
CHANGED
|
@@ -50,6 +50,7 @@ from .q2k import (
|
|
| 50 |
make_block,
|
| 51 |
pack_codes,
|
| 52 |
pack_scales,
|
|
|
|
| 53 |
unpack_codes,
|
| 54 |
weighted_sse,
|
| 55 |
)
|
|
@@ -1138,6 +1139,44 @@ class Q2KQuantizer:
|
|
| 1138 |
temperature=temperature,
|
| 1139 |
)
|
| 1140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1141 |
if cfg.apply_dc_carry and (cfg.dc_lambda > 0.0 or cfg.dc_decay > 0.0):
|
| 1142 |
_check_stop()
|
| 1143 |
_emit(
|
|
|
|
| 50 |
make_block,
|
| 51 |
pack_codes,
|
| 52 |
pack_scales,
|
| 53 |
+
pin_q2k_dc_residual,
|
| 54 |
unpack_codes,
|
| 55 |
weighted_sse,
|
| 56 |
)
|
|
|
|
| 1139 |
temperature=temperature,
|
| 1140 |
)
|
| 1141 |
|
| 1142 |
+
if cfg.pin_dc and n_blocks > 0:
|
| 1143 |
+
_check_stop()
|
| 1144 |
+
t_pin = time.time()
|
| 1145 |
+
_emit(
|
| 1146 |
+
"Phase 4.7: Zero-DC Pin (Q2_K Free Lunch)",
|
| 1147 |
+
0,
|
| 1148 |
+
n_blocks,
|
| 1149 |
+
"re-solving (d, m) on the exact dc=0 constraint line "
|
| 1150 |
+
"(exact-JS zero-coherent-residual pin, SSE-nondecreasing)",
|
| 1151 |
+
sigma=sigma,
|
| 1152 |
+
temperature=temperature,
|
| 1153 |
+
)
|
| 1154 |
+
pin_d = np.empty(n_blocks, dtype=np.float64)
|
| 1155 |
+
pin_m = np.empty(n_blocks, dtype=np.float64)
|
| 1156 |
+
pin_ls = np.empty((n_blocks, NS), dtype=np.uint8)
|
| 1157 |
+
pin_lm = np.empty((n_blocks, NS), dtype=np.uint8)
|
| 1158 |
+
pin_codes = np.empty((n_blocks, QK_K), dtype=np.uint8)
|
| 1159 |
+
for i, b in enumerate(blocks):
|
| 1160 |
+
pin_d[i] = b.d_float
|
| 1161 |
+
pin_m[i] = b.dmin_float
|
| 1162 |
+
pin_ls[i], pin_lm[i] = b.unpack_scales()
|
| 1163 |
+
pin_codes[i] = unpack_codes(b.qs)
|
| 1164 |
+
pin_d, pin_m = pin_q2k_dc_residual(
|
| 1165 |
+
pin_d, pin_m, pin_ls, pin_lm, pin_codes, X.astype(np.float64)
|
| 1166 |
+
)
|
| 1167 |
+
blocks = [
|
| 1168 |
+
make_block(pin_d[i], pin_m[i], pin_ls[i], pin_lm[i], pin_codes[i])
|
| 1169 |
+
for i in range(n_blocks)
|
| 1170 |
+
]
|
| 1171 |
+
_emit(
|
| 1172 |
+
"Phase 4.7: Zero-DC Pin (Q2_K Free Lunch)",
|
| 1173 |
+
n_blocks,
|
| 1174 |
+
n_blocks,
|
| 1175 |
+
f"done in {time.time() - t_pin:.1f}s",
|
| 1176 |
+
sigma=sigma,
|
| 1177 |
+
temperature=temperature,
|
| 1178 |
+
)
|
| 1179 |
+
|
| 1180 |
if cfg.apply_dc_carry and (cfg.dc_lambda > 0.0 or cfg.dc_decay > 0.0):
|
| 1181 |
_check_stop()
|
| 1182 |
_emit(
|
q2k.py
CHANGED
|
@@ -235,3 +235,37 @@ def weighted_sse_batch(
|
|
| 235 |
if w is None:
|
| 236 |
return np.sum(e * e, axis=1)
|
| 237 |
return np.sum(np.asarray(w, dtype=np.float64) * e * e, axis=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
if w is None:
|
| 236 |
return np.sum(e * e, axis=1)
|
| 237 |
return np.sum(np.asarray(w, dtype=np.float64) * e * e, axis=1)
|
| 238 |
+
|
| 239 |
+
|
| 240 |
+
def pin_q2k_dc_residual(d, m, ls, lm, codes, X):
|
| 241 |
+
"""Zero-DC pin: re-solve (d, m) on the exact ``sum(e) == 0`` line.
|
| 242 |
+
|
| 243 |
+
With e_i = X_i - d*ls_g*c_i + m*lm_g, requiring Σe = 0 makes m a line
|
| 244 |
+
m(d) = (d·Σa - ΣX)/Σb (a = ls·c, b = lm); subtracting the residual mean
|
| 245 |
+
along that line is a translation that can only *lower* SSE, so the
|
| 246 |
+
closed-form optimum d* = Σ(u·v)/Σ(v²) dominates the input state on both
|
| 247 |
+
counts and leaves dc ~ 0 (bounded below by the fp16 storage of the
|
| 248 |
+
``d``/``m`` fields). Blocks without a usable translation dof (Σb == 0
|
| 249 |
+
or Σv² == 0) keep their input parameters.
|
| 250 |
+
"""
|
| 251 |
+
ls64 = np.asarray(ls, dtype=np.float64)
|
| 252 |
+
lm64 = np.asarray(lm, dtype=np.float64)
|
| 253 |
+
c64 = np.asarray(codes, dtype=np.float64)
|
| 254 |
+
a_el = np.repeat(ls64, 16, axis=1) * c64
|
| 255 |
+
b_el = np.repeat(lm64, 16, axis=1)
|
| 256 |
+
sx0 = np.sum(np.asarray(X, dtype=np.float64), axis=1)
|
| 257 |
+
sa = a_el.sum(axis=1)
|
| 258 |
+
sb = b_el.sum(axis=1)
|
| 259 |
+
sb_safe = np.where(sb > 0.0, sb, np.nan)
|
| 260 |
+
u = np.asarray(X, dtype=np.float64) - b_el * (sx0 / sb_safe)[:, None]
|
| 261 |
+
v = a_el - b_el * (sa / sb_safe)[:, None]
|
| 262 |
+
vv = np.sum(v * v, axis=1)
|
| 263 |
+
uv = np.sum(u * v, axis=1)
|
| 264 |
+
ok = np.isfinite(vv) & (vv > 0.0) & np.isfinite(uv) & (sb > 0.0)
|
| 265 |
+
d_new = np.where(ok, uv / np.where(ok, vv, 1.0), np.asarray(d, dtype=np.float64))
|
| 266 |
+
m_new = (d_new * sa - sx0) / np.where(ok, sb, 1.0)
|
| 267 |
+
m_new = np.where(ok, m_new, np.asarray(m, dtype=np.float64))
|
| 268 |
+
# match the fp16 fields the block will actually carry in the file
|
| 269 |
+
d_new = from_fp16_bits(to_fp16_bits(d_new.astype(np.float32))).astype(np.float64)
|
| 270 |
+
m_new = from_fp16_bits(to_fp16_bits(m_new.astype(np.float32))).astype(np.float64)
|
| 271 |
+
return d_new, m_new
|