MCPcopy Create free account
hub / github.com/DevTechJr/turboquant_cutile / turboquant_compress_3bit

Function turboquant_compress_3bit

turboquant_cutile/compress.py:66–126  ·  view source on GitHub ↗

3-bit MSE (8 centroids) + 1-bit QJL. total_bits=4.

(
    K, Pi_T, Pi, S_T,
    Indices, Signs, Norms, RNorms,
    c0: float, c1: float, c2: float, c3: float,
    c4: float, c5: float, c6: float, c7: float,
    b1: float, b2: float, b3: float, b4: float,
    b5: float, b6: float, b7: float,
    seq_k: int,
)

Source from the content-addressed store, hash-verified

64
65@ct.kernel
66def turboquant_compress_3bit(
67 K, Pi_T, Pi, S_T,
68 Indices, Signs, Norms, RNorms,
69 c0: float, c1: float, c2: float, c3: float,
70 c4: float, c5: float, c6: float, c7: float,
71 b1: float, b2: float, b3: float, b4: float,
72 b5: float, b6: float, b7: float,
73 seq_k: int,
74):
75 """3-bit MSE (8 centroids) + 1-bit QJL. total_bits=4."""
76 block_id = ct.bid(0)
77 zero_pad = ct.PaddingMode.ZERO
78
79 k_tile = ct.load(K, index=(block_id, 0), shape=(BLOCK_S, HEAD_DIM),
80 padding_mode=zero_pad)
81
82 pi_t = ct.load(Pi_T, index=(0, 0), shape=(HEAD_DIM, HEAD_DIM))
83 pi = ct.load(Pi, index=(0, 0), shape=(HEAD_DIM, HEAD_DIM))
84 s_t = ct.load(S_T, index=(0, 0), shape=(HEAD_DIM, HEAD_DIM))
85
86 k_f32 = ct.astype(k_tile, ct.float32)
87 norms = ct.sqrt(ct.sum(k_f32 * k_f32, axis=1))
88 safe_norms = ct.where(norms > 1e-8, norms, 1e-8)
89 k_normed = k_f32 / ct.expand_dims(safe_norms, axis=1)
90
91 y = ct.mma(ct.astype(k_normed, ct.float16), pi_t,
92 ct.zeros((BLOCK_S, HEAD_DIM), dtype=ct.float32))
93
94 idx = ct.zeros((BLOCK_S, HEAD_DIM), dtype=ct.float32)
95 idx = ct.where(y > b1, 1.0, idx)
96 idx = ct.where(y > b2, 2.0, idx)
97 idx = ct.where(y > b3, 3.0, idx)
98 idx = ct.where(y > b4, 4.0, idx)
99 idx = ct.where(y > b5, 5.0, idx)
100 idx = ct.where(y > b6, 6.0, idx)
101 idx = ct.where(y > b7, 7.0, idx)
102
103 y_hat = ct.full((BLOCK_S, HEAD_DIM), c0, dtype=ct.float32)
104 y_hat = ct.where(idx > 0.5, c1, y_hat)
105 y_hat = ct.where(idx > 1.5, c2, y_hat)
106 y_hat = ct.where(idx > 2.5, c3, y_hat)
107 y_hat = ct.where(idx > 3.5, c4, y_hat)
108 y_hat = ct.where(idx > 4.5, c5, y_hat)
109 y_hat = ct.where(idx > 5.5, c6, y_hat)
110 y_hat = ct.where(idx > 6.5, c7, y_hat)
111
112 k_bar_hat = ct.mma(ct.astype(y_hat, ct.float16), pi,
113 ct.zeros((BLOCK_S, HEAD_DIM), dtype=ct.float32))
114 k_mse = k_bar_hat * ct.expand_dims(norms, axis=1)
115
116 residual = k_f32 - k_mse
117 r_norms = ct.sqrt(ct.sum(residual * residual, axis=1))
118
119 projected = ct.mma(ct.astype(residual, ct.float16), s_t,
120 ct.zeros((BLOCK_S, HEAD_DIM), dtype=ct.float32))
121 signs = ct.where(projected >= 0.0, 1.0, -1.0)
122
123 ct.store(Indices, index=(block_id, 0), tile=ct.astype(idx, ct.uint8))

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected