MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / .debug-context
| 1 | # MojoLlama Debug Context — GQA/Hidden State Bug |
| 2 | |
| 3 | ## The Bug |
| 4 | MojoLlama engine produces IDENTICAL output regardless of input token or KV cache state. |
| 5 | |
| 6 | Evidence: |
| 7 | - GPT-OSS-20B: Feeding token 42 twice gives IDENTICAL logits (max diff = 0.0000) even though KV cache has 2 different positions with different values |
| 8 | - First forward step works correctly (different tokens give different outputs) |
| 9 | - But subsequent steps ignore ALL state (KV cache, hidden state) |
| 10 | - Both Python and C batch forward paths exhibit this bug for ALL models (GPT-OSS, Qwen3.6) |
| 11 | |
| 12 | KV cache is correctly storing different values at different positions: |
| 13 | ``` |
| 14 | Step 1: kv_len[0]=1, KV[0,0,:3] = [-2.87, -1.02, 10.50] |
| 15 | Step 2: kv_len[0]=2, KV[0,1,:3] = [5.35, 1.12, 12.66] |
| 16 | ``` |
| 17 | |
| 18 | Run-to-run is deterministic (same input → same output). |
| 19 | |
| 20 | ## Key Files |
| 21 | - /onedev-workspace/work/src/mojollama/kernels/turbo_engine_v7_moe.py — main engine (forward() method around line 932) |
| 22 | - /onedev-workspace/work/src/mojollama/kernels/cengine_batch_instr.c — C engine |
| 23 | - /onedev-workspace/work/src/mojollama/kernels/quant_kernels_omp.c — kernels |
| 24 | - /onedev-workspace/work/src/mojollama/kernels/gqa_attention.c — GQA C implementation |
| 25 | |
| 26 | ## Hypothesis |
| 27 | The hidden state `self._x` might not propagate between layer iterations. Or the GQA attention function reads wrong KV cache positions. Or the `_logits` buffer aliases the `_x` buffer. |
| 28 | |
| 29 | ## Debugging tasks |
| 30 | 1. Run OMP_NUM_THREADS=1 to rule out threading |
| 31 | 2. Check if `self._x` AND `self._logits` buffers overlap (memory aliasing) |
| 32 | 3. Monkey-patch forward to print hidden state after each layer |
| 33 | 4. Test if GQA with seq_len=1 vs seq_len=2 produces different att output |
| 34 | 5. Check gqa_attention.c for bugs |
| 35 | |
| 36 | ## Test command |
| 37 | ```bash |
| 38 | cd /onedev-workspace/work/src/mojollama/kernels |
| 39 | OMP_NUM_THREADS=1 python3 -c " |
| 40 | import sys, numpy as np; sys.path.insert(0, '.') |
| 41 | from turbo_engine_v7_moe import TurboEngineV7MoE |
| 42 | e = TurboEngineV7MoE('/tmp/models/gpt-oss-20b-Q4_K_M.gguf', n_threads=1) |
| 43 | e.reset() |
| 44 | l1 = e.forward([42]).copy() |
| 45 | l2 = e.forward([42]).copy() |
| 46 | print(f'Same output: {np.max(np.abs(l1-l2)):.6f}') |
| 47 | " |
| 48 | ``` |