MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / .zaya-task
| 1 | # ZAYA1-8B C Batch Forward — Task |
| 2 | |
| 3 | ## Goal |
| 4 | Implement a C batch forward function for ZAYA1-8B in `cengine_batch_instr.c` that processes all 80 layers in a single C call, eliminating Python loop overhead. |
| 5 | |
| 6 | ## Architecture |
| 7 | - 80 layers (interleaved: even=attention, odd=MXFP4 MoE) |
| 8 | - N=2048, NH=8, n_kv_h=2, head_dim=128, rope_dim=64 |
| 9 | - Even layers: attention (QKV + output proj) + FFN |
| 10 | - Odd layers: MXFP4 MoE (router + gate/up/down experts) |
| 11 | - vocab=262272, n_experts=16, top_k=1, n_ff_expert=2048 |
| 12 | |
| 13 | ## Reference: Existing qwen36_batch_forward in cengine_batch_instr.c |
| 14 | The qwen36_batch_forward function handles 40 layers with SSM/attention hybrid. ZAYA is simpler — it's just attention + MoE with no SSM. |
| 15 | |
| 16 | ## Key considerations |
| 17 | 1. ZAYA uses MXFP4 (type 39) for ALL expert weights (gate/up/down) |
| 18 | 2. ZAYA uses Q4_K (type 12) for Q/K/V/O projections |
| 19 | 3. MXFP4 matmul needs the F32 workaround (mxfp4_row_dot_f32, not q8 variant) |
| 20 | 4. KV cache management: same as qwen36_batch_forward (kv_lens passed as pointer) |
| 21 | |
| 22 | ## Pattern to follow |
| 23 | Copy `qwen36_batch_forward` signature and modify for ZAYA: |
| 24 | - Remove SSM state (no SSM layers) |
| 25 | - Use `moe_forward_omp` for MoE (odd layers) |
| 26 | - Use `quant_matmul_omp` for attention Q/K/V/O (even layers) |
| 27 | - Use RMS_NORM_SIMD for all norms |
| 28 | - Use `mxfp4_matmul_omp_q8` -> actually use mxfp4_row_dot_f32 for MXFP4 (see workaround below) |
| 29 | |
| 30 | ## MXFP4 Workaround (2026-05-22) |
| 31 | mxfp4_row_dot_q8 produces NaN for large input distributions. Use mxfp4_row_dot_f32 instead. |
| 32 | In moe_forward_omp, gate/up path already changed to use f32 variant. |
| 33 | For ZAYA, ensure the moe_forward_omp call uses the fixed version. |
| 34 | |
| 35 | ## Files |
| 36 | - /onedev-workspace/work/src/mojollama/kernels/cengine_batch_instr.c (add zaya_batch_forward) |
| 37 | - /onedev-workspace/work/src/mojollama/kernels/quant_kernels_omp.c (already patched) |
| 38 | - /onedev-workspace/work/src/mojollama/kernels/forward/zaya.py (Python forward reference) |
| 39 | - /onedev-workspace/work/src/mojollama/kernels/forward/zaya_bc.py (backward compat reference) |