Highly experimental
Browse files- segmentation.py +2 -52
segmentation.py
CHANGED
|
@@ -150,10 +150,11 @@ def q2k_proxy_cfg(clamp_val: float, cfg: HPCConfig):
|
|
| 150 |
"""Cheap phase1+2 proxy for Q2_K label iteration (GGML seeds + WLS)."""
|
| 151 |
from .q2k import codes_from_params_batch, dequantize_params_batch
|
| 152 |
from .seeds import build_seeds
|
|
|
|
| 153 |
|
| 154 |
def _proxy(xg: np.ndarray, wg: np.ndarray | None) -> np.ndarray:
|
| 155 |
seeds = build_seeds(xg, wg, cfg, clamp_val)
|
| 156 |
-
d, m =
|
| 157 |
sd = np.where(d > 1.0e-15, d, 1.0).astype(np.float32)
|
| 158 |
sm = np.where(m > 1.0e-15, m, 1.0).astype(np.float32)
|
| 159 |
ls = np.where(
|
|
@@ -172,57 +173,6 @@ def q2k_proxy_cfg(clamp_val: float, cfg: HPCConfig):
|
|
| 172 |
return _proxy
|
| 173 |
|
| 174 |
|
| 175 |
-
def _wls(xg, wg, seeds, cfg):
|
| 176 |
-
"""Row-level WLS (same iteration as pipeline._phase2_wls, kept local to
|
| 177 |
-
avoid a circular import)."""
|
| 178 |
-
from .fp16 import round_fp16
|
| 179 |
-
|
| 180 |
-
n = xg.shape[0]
|
| 181 |
-
per_blk_clamp = np.zeros((n, 1), dtype=np.float32)
|
| 182 |
-
clipped = xg.astype(np.float32)
|
| 183 |
-
base_d = seeds["d"].astype(np.float64).copy()
|
| 184 |
-
base_m = seeds["dmin"].astype(np.float64).copy()
|
| 185 |
-
ls = seeds["ls"].copy()
|
| 186 |
-
lm = seeds["lm"].copy()
|
| 187 |
-
scales = seeds["scales"].astype(np.float64)
|
| 188 |
-
mins = seeds["mins"].astype(np.float64)
|
| 189 |
-
ww = np.ones_like(xg, dtype=np.float64) if wg is None else wg.astype(np.float64)
|
| 190 |
-
|
| 191 |
-
from .q2k import codes_from_params_batch
|
| 192 |
-
|
| 193 |
-
for _ in range(5):
|
| 194 |
-
codes = codes_from_params_batch(clipped, base_d, base_m, ls, lm).astype(np.float64)
|
| 195 |
-
A = np.repeat(ls.astype(np.float64), 16, axis=1) * codes
|
| 196 |
-
B = np.repeat(lm.astype(np.float64), 16, axis=1)
|
| 197 |
-
Saa = np.sum(ww * A * A, axis=1)
|
| 198 |
-
Sab = np.sum(ww * A * B, axis=1)
|
| 199 |
-
Sbb = np.sum(ww * B * B, axis=1)
|
| 200 |
-
Sxa = np.sum(ww * clipped.astype(np.float64) * A, axis=1)
|
| 201 |
-
Sxb = np.sum(ww * clipped.astype(np.float64) * B, axis=1)
|
| 202 |
-
det = Saa * Sbb - Sab * Sab
|
| 203 |
-
valid = det > 1.0e-30
|
| 204 |
-
safe_det = np.where(valid, det, 1.0)
|
| 205 |
-
d_new = (Sbb * Sxa - Sab * Sxb) / safe_det
|
| 206 |
-
m_new = (Sab * Sxa - Saa * Sxb) / safe_det
|
| 207 |
-
upd_d = valid & (d_new > 0.0) & np.isfinite(d_new)
|
| 208 |
-
upd_m = valid & (m_new > 0.0) & np.isfinite(m_new)
|
| 209 |
-
base_d = np.where(upd_d, round_fp16(d_new.astype(np.float32)).astype(np.float64), base_d)
|
| 210 |
-
base_m = np.where(upd_m, round_fp16(m_new.astype(np.float32)).astype(np.float64), base_m)
|
| 211 |
-
safe_d = np.where(base_d > 1.0e-15, base_d, 1.0)
|
| 212 |
-
safe_m = np.where(base_m > 1.0e-15, base_m, 1.0)
|
| 213 |
-
ls = np.where(
|
| 214 |
-
base_d[:, None] > 1.0e-15,
|
| 215 |
-
np.clip(nearest_int(scales / safe_d[:, None]), 0, 15),
|
| 216 |
-
0,
|
| 217 |
-
).astype(np.uint8)
|
| 218 |
-
lm = np.where(
|
| 219 |
-
base_m[:, None] > 1.0e-15,
|
| 220 |
-
np.clip(nearest_int(mins / safe_m[:, None]), 0, 15),
|
| 221 |
-
0,
|
| 222 |
-
).astype(np.uint8)
|
| 223 |
-
return base_d, base_m
|
| 224 |
-
|
| 225 |
-
|
| 226 |
def q3k_proxy(xg: np.ndarray, wg: np.ndarray | None) -> np.ndarray:
|
| 227 |
"""Cheap per-16 sub-block 3-bit ladder proxy for Q3_K label iteration."""
|
| 228 |
from .seeds import make_qkx2_batch
|
|
|
|
| 150 |
"""Cheap phase1+2 proxy for Q2_K label iteration (GGML seeds + WLS)."""
|
| 151 |
from .q2k import codes_from_params_batch, dequantize_params_batch
|
| 152 |
from .seeds import build_seeds
|
| 153 |
+
from .pipeline import _phase2_wls
|
| 154 |
|
| 155 |
def _proxy(xg: np.ndarray, wg: np.ndarray | None) -> np.ndarray:
|
| 156 |
seeds = build_seeds(xg, wg, cfg, clamp_val)
|
| 157 |
+
d, m = _phase2_wls(xg, wg, seeds, cfg)
|
| 158 |
sd = np.where(d > 1.0e-15, d, 1.0).astype(np.float32)
|
| 159 |
sm = np.where(m > 1.0e-15, m, 1.0).astype(np.float32)
|
| 160 |
ls = np.where(
|
|
|
|
| 173 |
return _proxy
|
| 174 |
|
| 175 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
def q3k_proxy(xg: np.ndarray, wg: np.ndarray | None) -> np.ndarray:
|
| 177 |
"""Cheap per-16 sub-block 3-bit ladder proxy for Q3_K label iteration."""
|
| 178 |
from .seeds import make_qkx2_batch
|