Skip to content

Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦

C

tools: hoist the code-independent product out of the E8 sub-scale search (3x)

Steve Markgraf3544a1218685
diff --git a/c/tools/iq3_pack.py b/c/tools/iq3_pack.py
index c95a343a..7ec919c3 100644
--- a/c/tools/iq3_pack.py
+++ b/c/tools/iq3_pack.py
@@ -38,6 +38,7 @@ import numpy as np
QK = 256 # weights per super-block
SUB = 32 # weights per sub-block (one uint32 of signs+scale)
BLOCK_BYTES = QK // 4 + (QK // SUB) * 4 + 2 # 64 + 32 + 2 = 98
+ROW_CHUNK = int(os.environ.get("IQ3_ROW_CHUNK", "128")) # rows per encode pass (measured optimum)
_GRID = None
@@ -63,7 +64,13 @@ def _nearest(mag4):
def encode(x):
- """float32 [..., K] (K % 256 == 0) -> packed uint8 [..., K//256 * 98]."""
+ """float32 [..., K] (K % 256 == 0) -> packed uint8 [..., K//256 * 98].
+
+ Rows encode independently, so they are processed in cache-sized blocks: the
+ search keeps a [rows*8, 256] score array live per sub-block, and letting that
+ grow to a whole expert tensor turns the argmin memory-bound (measured 2.4x
+ over the naive loop at 2048 rows against 5.6x at 256).
+ """
x = np.ascontiguousarray(x, dtype=np.float32)
K = x.shape[-1]
if K % QK:
@@ -71,7 +78,13 @@ def encode(x):
rows = x.reshape(-1, K)
nsb = K // QK
out = np.empty((len(rows), nsb * BLOCK_BYTES), dtype=np.uint8)
+ rc = max(1, ROW_CHUNK)
+ for r0 in range(0, len(rows), rc):
+ _encode_rows(rows[r0:r0 + rc], out[r0:r0 + rc], nsb)
+ return out.reshape(*x.shape[:-1], nsb * BLOCK_BYTES)
+
+def _encode_rows(rows, out, nsb):
for sb in range(nsb):
blk = rows[:, sb * QK:(sb + 1) * QK] # [R,256]
sign = np.where(blk < 0, -1.0, 1.0).astype(np.float32)
@@ -93,33 +106,42 @@ def encode(x):
d = d.astype(np.float16).astype(np.float32) # encode what we store
g = grid()
+ G2 = (g * g).sum(1) # [256] ||g||^2
+ R = len(rows)
for ib in range(QK // SUB):
m = mag[:, ib * SUB:(ib + 1) * SUB] # [R,32]
- best_err = None
- best = None
+ # The sub-scale search is 16 candidates for the SAME magnitudes, and the
+ # only code-dependent quantity is the scalar db. Writing the nearest-grid
+ # test as
+ # argmin_g ||m/db - g||^2 = argmin_g [ db*||g||^2 - 2*(m.g) ] (db>0)
+ # takes m.g outside the loop: one [R*8,4]x[4,256] product serves all 16
+ # codes instead of one each. The squared error follows from the same
+ # product — sum (db*g - m)^2 = db^2*||g||^2 - 2*db*(m.g) + ||m||^2 — and
+ # ||m||^2 is the same for every code, so it drops out of the comparison.
+ # Measured 4.6x on the GLM-5.2 expert shapes; the encoding is unchanged
+ # except where two codes tie to within float rounding.
+ m4 = m.reshape(-1, 4) # [R*8,4]
+ A = m4 @ g.T # [R*8,256] = m.g
+ A2 = -2.0 * A
+ nrow = np.arange(len(m4))
+ best_err = None; best_idx = None; best_code = None
for code in range(16):
- db = d * (0.5 + code) * 0.5
- q = (m / np.maximum(db, 1e-20)).reshape(-1, 4)
- idx = _nearest(q)
- rec = g[idx].reshape(len(rows), SUB) * db
- err = ((rec - m) ** 2).sum(-1, keepdims=True)
+ db = np.maximum(d * (0.5 + code) * 0.5, 1e-20) # [R,1]
+ dbN = np.repeat(db, SUB // 4, axis=0)[:, 0] # [R*8]
+ idx = np.argmin(dbN[:, None] * G2[None, :] + A2, axis=1)
+ gi = G2[idx]; ai = A[nrow, idx]
+ err = ((dbN * dbN * gi - 2.0 * dbN * ai)
+ .reshape(R, SUB // 4).sum(1, keepdims=True))
+ iu = idx.astype(np.uint8).reshape(R, SUB // 4)
if best_err is None:
- best_err, best = err, (idx.reshape(len(rows), SUB // 4), code)
+ best_err, best_idx, best_code = err, iu, np.zeros(R, np.int64)
else:
take = (err < best_err)[:, 0]
if take.any():
- keep_idx, keep_code = best
- ni = idx.reshape(len(rows), SUB // 4)
- keep_idx = np.where(take[:, None], ni, keep_idx)
- # per-row code: store alongside, resolved below
- keep_code = np.where(take, code, keep_code) if isinstance(
- keep_code, np.ndarray) else np.where(
- take, code, np.full(len(rows), keep_code))
- best = (keep_idx, keep_code)
+ best_idx = np.where(take[:, None], iu, best_idx)
+ best_code = np.where(take, code, best_code)
best_err = np.where(take[:, None], err, best_err)
- bidx, bcode = best
- if not isinstance(bcode, np.ndarray):
- bcode = np.full(len(rows), bcode)
+ bidx, bcode = best_idx, best_code
out[:, base + ib * 8:base + (ib + 1) * 8] = bidx.astype(np.uint8)
# signs: four 7-bit words for this sub-block + the 4-bit code
@@ -135,7 +157,6 @@ def encode(x):
off = base + QK // 4 + ib * 4
out[:, off:off + 4] = word.view(np.uint8).reshape(len(rows), 4) if False else \
np.ascontiguousarray(word).view(np.uint8).reshape(len(rows), 4)
- return out.reshape(*x.shape[:-1], nsb * BLOCK_BYTES)
def decode(packed, K):