MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / src / mojollama / kernels / forward / zaya_bc.py
| 1 | """ZAYA BC struct builder for zaya_batch_forward.""" |
| 2 | import ctypes, numpy as np |
| 3 | |
| 4 | class BC(ctypes.Structure): |
| 5 | _fields_ = [ |
| 6 | ('L', ctypes.c_int), ('N', ctypes.c_int), ('NH', ctypes.c_int), |
| 7 | ('NKH', ctypes.c_int), ('HD', ctypes.c_int), ('FF', ctypes.c_int), |
| 8 | ('V', ctypes.c_int), ('eps', ctypes.c_float), |
| 9 | ('wQ', ctypes.POINTER(ctypes.c_void_p)), |
| 10 | ('wK', ctypes.POINTER(ctypes.c_void_p)), |
| 11 | ('wV', ctypes.POINTER(ctypes.c_void_p)), |
| 12 | ('wO', ctypes.POINTER(ctypes.c_void_p)), |
| 13 | ('wG', ctypes.POINTER(ctypes.c_void_p)), |
| 14 | ('wU', ctypes.POINTER(ctypes.c_void_p)), |
| 15 | ('wD', ctypes.POINTER(ctypes.c_void_p)), |
| 16 | ('wAN', ctypes.POINTER(ctypes.c_void_p)), |
| 17 | ('wFN', ctypes.POINTER(ctypes.c_void_p)), |
| 18 | ('nQ', ctypes.POINTER(ctypes.c_int)), |
| 19 | ('nK', ctypes.POINTER(ctypes.c_int)), |
| 20 | ('nV', ctypes.POINTER(ctypes.c_int)), |
| 21 | ('nO', ctypes.POINTER(ctypes.c_int)), |
| 22 | ('nG', ctypes.POINTER(ctypes.c_int)), |
| 23 | ('nU', ctypes.POINTER(ctypes.c_int)), |
| 24 | ('nD', ctypes.POINTER(ctypes.c_int)), |
| 25 | ('nc', ctypes.c_int), |
| 26 | ('emb', ctypes.POINTER(ctypes.c_float)), |
| 27 | ('onw', ctypes.POINTER(ctypes.c_float)), |
| 28 | ('wOut', ctypes.POINTER(ctypes.c_uint8)), |
| 29 | ('outNR', ctypes.c_int), ('outNC', ctypes.c_int), ('outQuant', ctypes.c_int), |
| 30 | ('kv_array', ctypes.POINTER(ctypes.c_void_p)), |
| 31 | ('logits', ctypes.POINTER(ctypes.c_float)), |
| 32 | ('n_experts', ctypes.c_int), ('n_experts_per_tok', ctypes.c_int), |
| 33 | ('moe_intermediate', ctypes.c_int), |
| 34 | ('w_gate_inp', ctypes.POINTER(ctypes.c_void_p)), |
| 35 | ('w_gate_exps', ctypes.POINTER(ctypes.c_void_p)), |
| 36 | ('w_up_exps', ctypes.POINTER(ctypes.c_void_p)), |
| 37 | ('w_down_exps', ctypes.POINTER(ctypes.c_void_p)), |
| 38 | ('gate_exp_quant', ctypes.c_int), ('up_exp_quant', ctypes.c_int), |
| 39 | ('down_exp_quant', ctypes.c_int), |
| 40 | ('q_quant', ctypes.POINTER(ctypes.c_int)), |
| 41 | ('k_quant', ctypes.POINTER(ctypes.c_int)), |
| 42 | ('v_quant', ctypes.POINTER(ctypes.c_int)), |
| 43 | ('o_quant', ctypes.POINTER(ctypes.c_int)), |
| 44 | ('g_quant', ctypes.POINTER(ctypes.c_int)), |
| 45 | ('u_quant', ctypes.POINTER(ctypes.c_int)), |
| 46 | ('d_quant', ctypes.POINTER(ctypes.c_int)), |
| 47 | ('emb_quant', ctypes.c_int), |
| 48 | ('wQK', ctypes.POINTER(ctypes.c_void_p)), |
| 49 | ('qk_quant', ctypes.POINTER(ctypes.c_int)), |
| 50 | ('cos_table', ctypes.POINTER(ctypes.c_float)), |
| 51 | ('sin_table', ctypes.POINTER(ctypes.c_float)), |
| 52 | ('max_ctx', ctypes.c_int), |
| 53 | ('workspace', ctypes.POINTER(ctypes.c_float)), |
| 54 | ('ws_size', ctypes.c_int), |
| 55 | ('rope_dim', ctypes.c_int), |
| 56 | ('full_attn_interval', ctypes.c_int), |
| 57 | ('wQKV', ctypes.POINTER(ctypes.c_void_p)), |
| 58 | ('qkv_quant', ctypes.POINTER(ctypes.c_int)), |
| 59 | ('wAttnG', ctypes.POINTER(ctypes.c_void_p)), |
| 60 | ('attnG_quant', ctypes.POINTER(ctypes.c_int)), |
| 61 | ('ssm_conv1d', ctypes.POINTER(ctypes.c_void_p)), |
| 62 | ('ssm_a', ctypes.POINTER(ctypes.c_void_p)), |
| 63 | ('ssm_dt_bias', ctypes.POINTER(ctypes.c_void_p)), |
| 64 | ('ssm_alpha', ctypes.POINTER(ctypes.c_void_p)), |
| 65 | ('ssm_beta', ctypes.POINTER(ctypes.c_void_p)), |
| 66 | ('ssm_norm', ctypes.POINTER(ctypes.c_void_p)), |
| 67 | ('wSsmOut', ctypes.POINTER(ctypes.c_void_p)), |
| 68 | ('ssm_out_quant', ctypes.POINTER(ctypes.c_int)), |
| 69 | ('ssm_state', ctypes.POINTER(ctypes.c_float)), |
| 70 | ('wSHexpG', ctypes.POINTER(ctypes.c_void_p)), |
| 71 | ('wSHexpU', ctypes.POINTER(ctypes.c_void_p)), |
| 72 | ('wSHexpD', ctypes.POINTER(ctypes.c_void_p)), |
| 73 | ('shexp_g_quant', ctypes.POINTER(ctypes.c_int)), |
| 74 | ('shexp_u_quant', ctypes.POINTER(ctypes.c_int)), |
| 75 | ('shexp_d_quant', ctypes.POINTER(ctypes.c_int)), |
| 76 | ('wShexpRouter', ctypes.POINTER(ctypes.c_void_p)), |
| 77 | ('layer_types', ctypes.POINTER(ctypes.c_int)), |
| 78 | ('n_layers_actual', ctypes.c_int), |
| 79 | # ZAYA-specific |
| 80 | ('w_zaya_ffn_gate_inp', ctypes.POINTER(ctypes.c_void_p)), |
| 81 | ('w_zaya_ffn_gate', ctypes.POINTER(ctypes.c_void_p)), |
| 82 | ('w_zaya_mlp2', ctypes.POINTER(ctypes.c_void_p)), |
| 83 | ('w_zaya_mlp4', ctypes.POINTER(ctypes.c_void_p)), |
| 84 | ('w_zaya_router_bias', ctypes.POINTER(ctypes.c_void_p)), |
| 85 | ('w_zaya_ffn_norm', ctypes.POINTER(ctypes.c_void_p)), |
| 86 | ('w_zaya_res_hs_w', ctypes.POINTER(ctypes.c_void_p)), |
| 87 | ('w_zaya_res_hs_b', ctypes.POINTER(ctypes.c_void_p)), |
| 88 | ('w_zaya_res_res_w', ctypes.POINTER(ctypes.c_void_p)), |
| 89 | ('w_zaya_res_res_b', ctypes.POINTER(ctypes.c_void_p)), |
| 90 | ('zaya_ffn_gate_inp_quant', ctypes.c_int), |
| 91 | ('zaya_ffn_gate_quant', ctypes.c_int), |
| 92 | ('zaya_mlp2_quant', ctypes.c_int), |
| 93 | ('zaya_mlp4_quant', ctypes.c_int), |
| 94 | ('has_moe_layer', ctypes.POINTER(ctypes.c_int)), |
| 95 | ('zaya_expert_n', ctypes.c_int), |
| 96 | ('zaya_moe_intermediate', ctypes.c_int), |
| 97 | ('w_cca_v1', ctypes.POINTER(ctypes.c_void_p)), |
| 98 | ('w_cca_v2', ctypes.POINTER(ctypes.c_void_p)), |
| 99 | ('w_ssm_conv1d', ctypes.POINTER(ctypes.c_void_p)), |
| 100 | ('cca_v1_quant', ctypes.c_int), |
| 101 | ('cca_v2_quant', ctypes.c_int), |
| 102 | ] |
| 103 | |
| 104 | def build_zaya_bc(engine): |
| 105 | """Build BC struct for ZAYA batch forward.""" |
| 106 | e = engine |
| 107 | cu8 = ctypes.POINTER(ctypes.c_uint8) |
| 108 | cf = ctypes.POINTER(ctypes.c_float) |
| 109 | ci = ctypes.POINTER(ctypes.c_int) |
| 110 | L = e.n_layers; N = e.n_embd |
| 111 | |
| 112 | def _pp(name): |
| 113 | """Get ctypes pointer from raw_weights for a weight name.""" |
| 114 | rw = e.raw_weights.get(name) |
| 115 | if rw is None: return ctypes.cast(0, cu8) |
| 116 | return rw.ctypes.data_as(cu8) |
| 117 | |
| 118 | def _fp(name): |
| 119 | """Get float pointer from weights.""" |
| 120 | w = e.weights.get(name) |
| 121 | if w is None: return ctypes.cast(0, cf) |
| 122 | return w.ctypes.data_as(cf) |
| 123 | |
| 124 | def _arr_pp(prefix, suffix, n): |
| 125 | """Array of uint8 pointers for each layer.""" |
| 126 | arr = (cu8 * n)() |
| 127 | for i in range(n): |
| 128 | arr[i] = _pp(f'{prefix}{i}{suffix}') |
| 129 | return ctypes.cast(arr, ctypes.POINTER(ctypes.c_void_p)) |
| 130 | |
| 131 | def _arr_fp(prefix, suffix, n): |
| 132 | """Array of float pointers for each layer.""" |
| 133 | arr = (cf * n)() |
| 134 | for i in range(n): |
| 135 | arr[i] = _fp(f'{prefix}{i}{suffix}') |
| 136 | return ctypes.cast(arr, ctypes.POINTER(ctypes.c_void_p)) |
| 137 | |
| 138 | def _arr_int(prefix, suffix, n, val_fn): |
| 139 | """Array of ints for each layer.""" |
| 140 | arr = (ctypes.c_int * n)() |
| 141 | for i in range(n): |
| 142 | arr[i] = val_fn(i) |
| 143 | return ctypes.cast(arr, ci) |
| 144 | |
| 145 | bc = BC() |
| 146 | bc.L = ctypes.c_int(L) |
| 147 | bc.N = ctypes.c_int(N) |
| 148 | bc.NH = ctypes.c_int(e.n_head) |
| 149 | bc.NKH = ctypes.c_int(e.n_kv_head) |
| 150 | bc.HD = ctypes.c_int(e.head_dim) |
| 151 | bc.FF = ctypes.c_int(getattr(e, 'n_ff', N)) |
| 152 | bc.V = ctypes.c_int(e.vocab_size) |
| 153 | bc.eps = ctypes.c_float(1e-5) |
| 154 | |
| 155 | bc.wQ = _arr_pp('blk.', '.attn_q.weight', L) |
| 156 | bc.wK = _arr_pp('blk.', '.attn_k.weight', L) |
| 157 | bc.wV = ctypes.cast(0, ctypes.POINTER(ctypes.c_void_p)) # V = K (no separate V) |
| 158 | bc.wO = _arr_pp('blk.', '.attn_output.weight', L) |
| 159 | bc.wG = ctypes.cast(0, ctypes.POINTER(ctypes.c_void_p)) # no separate gate |
| 160 | bc.wU = ctypes.cast(0, ctypes.POINTER(ctypes.c_void_p)) |
| 161 | bc.wD = ctypes.cast(0, ctypes.POINTER(ctypes.c_void_p)) |
| 162 | bc.wAN = _arr_fp('blk.', '.attn_norm.weight', L) |
| 163 | bc.wFN = _arr_fp('blk.', '.attn_norm.weight', L) # same as attn_norm for ZAYA |
| 164 | |
| 165 | wi = e.weight_info |
| 166 | def qv(n): info = wi.get(n); return info[3] if info else 0 |
| 167 | def nr(n): info = wi.get(n); return info[0] if info else 0 |
| 168 | def nc(n): info = wi.get(n); return info[1] if info else 0 |
| 169 | |
| 170 | bc.nQ = _arr_int('blk.', '.attn_q.weight', L, lambda i: nr(f'blk.{i}.attn_q.weight')) |
| 171 | bc.nK = _arr_int('blk.', '.attn_k.weight', L, lambda i: nr(f'blk.{i}.attn_k.weight')) |
| 172 | bc.nV = _arr_int('', '', L, lambda i: 0) |
| 173 | bc.nO = _arr_int('blk.', '.attn_output.weight', L, lambda i: nr(f'blk.{i}.attn_output.weight')) |
| 174 | bc.nG = _arr_int('', '', L, lambda i: 0) |
| 175 | bc.nU = _arr_int('', '', L, lambda i: 0) |
| 176 | bc.nD = _arr_int('', '', L, lambda i: 0) |
| 177 | |
| 178 | bc.q_quant = _arr_int('blk.', '.attn_q.weight', L, lambda i: qv(f'blk.{i}.attn_q.weight')) |
| 179 | bc.k_quant = _arr_int('blk.', '.attn_k.weight', L, lambda i: qv(f'blk.{i}.attn_k.weight')) |
| 180 | bc.v_quant = _arr_int('', '', L, lambda i: 8) |
| 181 | bc.o_quant = _arr_int('blk.', '.attn_output.weight', L, lambda i: qv(f'blk.{i}.attn_output.weight')) |
| 182 | bc.g_quant = _arr_int('', '', L, lambda i: 8) |
| 183 | bc.u_quant = _arr_int('', '', L, lambda i: 8) |
| 184 | bc.d_quant = _arr_int('', '', L, lambda i: 8) |
| 185 | bc.nc = ctypes.c_int(N) |
| 186 | bc.emb = e.emb.ctypes.data_as(cf) |
| 187 | bc.onw = _fp('output_norm.weight') |
| 188 | |
| 189 | # Output weight (embedding for tied ZAYA) |
| 190 | out_name = 'token_embd.weight' |
| 191 | out_info = wi.get(out_name) |
| 192 | if out_info: |
| 193 | bc.wOut = _pp(out_name) |
| 194 | bc.outNR = ctypes.c_int(out_info[0]) |
| 195 | bc.outNC = ctypes.c_int(out_info[1]) |
| 196 | bc.outQuant = ctypes.c_int(out_info[3]) |
| 197 | bc.emb_quant = ctypes.c_int(out_info[3]) |
| 198 | |
| 199 | bc.n_experts = ctypes.c_int(e.n_experts or 16) |
| 200 | bc.n_experts_per_tok = ctypes.c_int(1) |
| 201 | bc.moe_intermediate = ctypes.c_int(4096) # n_ff for ZAYA |
| 202 | |
| 203 | bc.cos_table = ctypes.cast(0, cf) # not used for ZAYA C path |
| 204 | bc.sin_table = ctypes.cast(0, cf) |
| 205 | bc.max_ctx = ctypes.c_int(4096) |
| 206 | bc.rope_dim = ctypes.c_int(64) |
| 207 | |
| 208 | # MoE expert weights |
| 209 | bc.w_gate_inp = _arr_fp('blk.', '.ffn_gate_inp.weight', L) |
| 210 | bc.w_gate_exps = _arr_pp('blk.', '.ffn_gate_up_exps.weight', L) |
| 211 | bc.w_up_exps = _arr_pp('blk.', '.ffn_gate_up_exps.weight', L) # same fused |
| 212 | bc.w_down_exps = _arr_pp('blk.', '.ffn_down_exps.weight', L) |
| 213 | bc.gate_exp_quant = ctypes.c_int(qv('blk.1.ffn_gate_up_exps.weight')) |
| 214 | bc.up_exp_quant = ctypes.c_int(qv('blk.1.ffn_gate_up_exps.weight')) |
| 215 | bc.down_exp_quant = ctypes.c_int(qv('blk.1.ffn_down_exps.weight')) |
| 216 | |
| 217 | # ZAYA-specific |
| 218 | bc.w_zaya_ffn_gate_inp = _arr_pp('blk.', '.ffn_gate_inp.weight', L) |
| 219 | bc.w_zaya_ffn_gate = _arr_pp('blk.', '.ffn_gate.weight', L) |
| 220 | bc.w_zaya_mlp2 = _arr_pp('blk.', '.zaya_router_mlp2.weight', L) |
| 221 | bc.w_zaya_mlp4 = _arr_pp('blk.', '.zaya_router_mlp4.weight', L) |
| 222 | bc.w_zaya_router_bias = _arr_fp('blk.', '.zaya_router_biases.weight', L) |
| 223 | bc.w_zaya_ffn_norm = _arr_fp('blk.', '.attn_norm.weight', L) |
| 224 | |
| 225 | bc.w_zaya_res_hs_w = _arr_fp('blk.', '.res_scale_hs.weight', L) |
| 226 | bc.w_zaya_res_hs_b = _arr_fp('blk.', '.res_scale_hs.bias', L) |
| 227 | bc.w_zaya_res_res_w = _arr_fp('blk.', '.res_scale_res.weight', L) |
| 228 | bc.w_zaya_res_res_b = _arr_fp('blk.', '.res_scale_res.bias', L) |
| 229 | |
| 230 | bc.zaya_ffn_gate_inp_quant = ctypes.c_int(qv('blk.1.ffn_gate_inp.weight')) |
| 231 | bc.zaya_ffn_gate_quant = ctypes.c_int(qv('blk.1.ffn_gate.weight')) |
| 232 | bc.zaya_mlp2_quant = ctypes.c_int(qv('blk.1.zaya_router_mlp2.weight')) |
| 233 | bc.zaya_mlp4_quant = ctypes.c_int(qv('blk.1.zaya_router_mlp4.weight')) |
| 234 | |
| 235 | has_moe = (ctypes.c_int * L)() |
| 236 | for i in range(L): has_moe[i] = ctypes.c_int(1 if i % 2 else 0) |
| 237 | bc.has_moe_layer = ctypes.cast(has_moe, ctypes.POINTER(ctypes.c_int)) |
| 238 | bc.zaya_expert_n = ctypes.c_int(e.n_experts or 16) |
| 239 | bc.zaya_moe_intermediate = ctypes.c_int(4096) |
| 240 | |
| 241 | return bc |
| 242 | |
| 243 | def run_zaya_batch_forward(engine, token_id): |
| 244 | """Run ZAYA batch forward via C engine.""" |
| 245 | import time |
| 246 | ce = getattr(engine, '_cengine', None) |
| 247 | if ce is None: |
| 248 | return None # not available |
| 249 | |
| 250 | if not hasattr(ce, '_zaya_bc'): |
| 251 | bc = build_zaya_bc(engine) |
| 252 | ce._zaya_bc = bc |
| 253 | |
| 254 | # Workspace: 50 * S floats where S = max(N, nq, nk, 8192) |
| 255 | S = max(engine.n_embd, engine.n_head * engine.head_dim, engine.n_kv_head * engine.head_dim, 8192) |
| 256 | ws_size = 44 * S |
| 257 | ce._zaya_ws = np.zeros(ws_size, dtype=np.float32) |
| 258 | |
| 259 | bc = ce._zaya_bc |
| 260 | ws = ce._zaya_ws |
| 261 | logits_out = engine._logits |
| 262 | |
| 263 | ce.zaya_batch_forward( |
| 264 | ctypes.byref(bc), |
| 265 | ctypes.byref(ctypes.c_int(token_id)), |
| 266 | ctypes.c_int(1), |
| 267 | ws.ctypes.data_as(ctypes.POINTER(ctypes.c_float)) |
| 268 | ) |
| 269 | |
| 270 | return engine._logits.copy() |