interplane.Get Omega
Read in your language

MojoLlama

Archived Mojo/MAX quantizer and serving research repository (read-only history)

Brought by drakestapleton

branch main Branches Tags History

MojoLlama / src / mojollama / moe-plan.md

69 lines · 3178 bytes Raw Blame History Rendered
1# MojoLlama MoE Engine: Implementation Plan
2
3## Goal
4Add MoE (Mixture of Experts) support to the C inference engine so GPT-OSS-20B and Qwen3-30B-A3B run at llama.cpp-level performance.
5
6## Current Engine
7- `/onedev-workspace/work/src/mojollama/kernels/cengine_batch.c` — AVX2 batched inference engine
8- Supports: Q4_0 and Q8_0 quant matmuls, GQA attention, RMS norm, RoPE, SiLU FFN
9- Compile: `gcc -O3 -mavx2 -mfma -mf16c -fopenmp -shared -fPIC -o cengine_batch.so cengine_batch.c -lm`
10- Server: `/onedev-workspace/work/src/mojollama/server_batch.py`
11
12## Architecture: GPT-OSS-20B
13File: `/tmp/models/gpt-oss-20b-Q4_K_M.gguf` (11GB)
14- Type: `gpt_oss`, MoE (32 experts, top-4 active)
15- 24 layers, hidden=2880, heads=64, KV_heads=8, head_dim=64
16- intermediate_size=2880, vocab=201088
17- **Non-standard attention**: Q=4096dim (64×64), K=V=512dim (8×64), O=4096→2880
18- **All attn projections have bias** (q, k, v, output)
19- **Packed experts**: `ffn_gate_exps.weight` shape=[2880, 2880, 32] (intermediate, hidden, experts)
20- **Router**: `ffn_gate_inp.weight` shape=[2880, 32] (hidden→experts)
21- Quant types: Q6_K (attn_q/k), Q8_0 (attn_v), Q4_K (attn_output), Q4_K_M (expert weights)
22
23## Architecture: Qwen3-30B-A3B
24File: `/tmp/models/Qwen3-30B-A3B-Instruct-2507-Q4_K_M.gguf` (18GB)
25- Type: `qwen3_moe`, MoE (128 experts, top-8 active + shared expert)
26- 48 layers, hidden=2048, heads=32, KV_heads=4, head_dim=64
27- intermediate_size=6144, vocab=151936
28- **Standard attention**: Q=2048dim, K=V=256dim
29- **Shared expert**: has `shared_expert.w1/w2/w3` + `shared_expert_gate.weight`
30- **Packed experts**: similar packed format but qwen3_moe layout
31- Quant: likely Q4_K_M throughout
32
33## Implementation Tasks (priority order)
34
35### 1. Add K-quant matmul support
36Current engine only has `q4_0_batch_matmul` and `q8_0_batch_matmul`. Need:
37- `q4_k_batch_matmul` — for Q4_K (type 12)
38- `q4_k_m_batch_matmul` — for Q4_K_M (type 39)
39- `q6_k_batch_matmul` — for Q6_K (type 6)
40- Block sizes: Q4_K/Q4_K_M=256 elements, Q6_K=256 elements
41- Use llama.cpp's block format as reference
42
43### 2. Add bias support to attention
44Current engine has no bias. GPT-OSS attn_q/k/v/output all have bias.
45
46### 3. Add packed-expert MoE FFN
47- Router: matmul `hidden @ ffn_gate_inp.weight` → softmax → top-K selection
48- Expert FFN: `silu(gate(e) @ hidden) * (up(e) @ hidden) → down(e) @ result`
49- For selected experts only (sparse computation)
50- Weighted combination by router probabilities
51
52### 4. Add Qwen3 shared expert
53- Shared expert is a dense FFN (always computed for every token)
54- Shared expert gate: learns how to mix shared + MoE outputs
55
56### 5. Update Python weight loading
57- Read per-layer tensor names
58- Quant type detection for correct matmul dispatch
59- Build BC struct with expert pointers
60
61## Files to modify
62- `kernels/cengine_batch.c` — add K-quant matmuls, MoE FFN, bias support, Qwen3 shared expert, non-standard attn dims
63- `server_batch.py` — add MoE weight loading, model config parser, dispatch
64
65## Test & Verification
661. Single-token forward match with llama.cpp output
672. Multi-token generation
683. Batch generation
694. Benchmark: should match llama.cpp tok/s within 15%