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 / .debug-context

48 lines · 2091 bytes Raw Blame History
1# MojoLlama Debug Context — GQA/Hidden State Bug
2
3## The Bug
4MojoLlama engine produces IDENTICAL output regardless of input token or KV cache state.
5
6Evidence:
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
12KV cache is correctly storing different values at different positions:
13```
14Step 1: kv_len[0]=1, KV[0,0,:3] = [-2.87, -1.02, 10.50]
15Step 2: kv_len[0]=2, KV[0,1,:3] = [5.35, 1.12, 12.66]
16```
17
18Run-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
27The 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
301. Run OMP_NUM_THREADS=1 to rule out threading
312. Check if `self._x` AND `self._logits` buffers overlap (memory aliasing)
323. Monkey-patch forward to print hidden state after each layer
334. Test if GQA with seq_len=1 vs seq_len=2 produces different att output
345. Check gqa_attention.c for bugs
35
36## Test command
37```bash
38cd /onedev-workspace/work/src/mojollama/kernels
39OMP_NUM_THREADS=1 python3 -c "
40import sys, numpy as np; sys.path.insert(0, '.')
41from turbo_engine_v7_moe import TurboEngineV7MoE
42e = TurboEngineV7MoE('/tmp/models/gpt-oss-20b-Q4_K_M.gguf', n_threads=1)
43e.reset()
44l1 = e.forward([42]).copy()
45l2 = e.forward([42]).copy()
46print(f'Same output: {np.max(np.abs(l1-l2)):.6f}')
47"
48```