MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / src / mojollama / kernels / .debug-context
| 1 | # Debug Log: NaN in qwen3_forward_c at layer 7 |
| 2 | |
| 3 | ## Bug |
| 4 | C fused forward function qwen3_forward_c produces all-NaN output for Qwen3-30B 7+ layers. Layers 1-6 work correctly (max_logit ~17), layer 7+ produces NaN. |
| 5 | |
| 6 | ## Model Architecture |
| 7 | - Qwen3-30B: 48 layers, N=2048, NH=32, n_kv_h=4, HD=128, FF_total=6144 |
| 8 | - 128 experts, top_k=8, n_ff_expert=768 |
| 9 | - Attention weights: Q4_K (type 12), K: Q4_K, V: Q4_K, O: Q4_K |
| 10 | - Expert gate/up: Q4_K (type 12), Expert down: Q6_K (type 14) |
| 11 | - Output weight: Q8_0 (type 8), shape [151936, 2048] |
| 12 | |
| 13 | ## The C Function |
| 14 | A single #pragma omp parallel region wraps all 48 layers. Inside: |
| 15 | - RMS norm (omp for reduction) |
| 16 | - QKV projection (omp for per-row quant_matmul_omp calls) |
| 17 | - RoPE (omp for) |
| 18 | - KV cache update (omp single) |
| 19 | - GQA inline attention (omp for over kv_heads) |
| 20 | - Output projection (omp single calls quant_matmul_omp) |
| 21 | - RMS norm again (omp for reduction) |
| 22 | - MoE via moe_forward_omp (omp single calls moe_forward_omp) |
| 23 | |
| 24 | ## What's Been Checked |
| 25 | 1. Individual quant_matmul_omp calls work from Python ✓ |
| 26 | 2. L=1..6 work correctly in the C function ✓ |
| 27 | 3. NaN detection after norm, QKV, GQA all show NO NaN ✓ |
| 28 | 4. NaN detection AFTER the MoE (ffn_out check with abort()) also shows NO NaN |
| 29 | |
| 30 | ## Critical Unresolved |
| 31 | The NaN check AFTER `x[i] = res[i] + ffn_out[i]` (after MoE residual) also shows NO NaN with abort(). |
| 32 | But the final logits are ALL NaN (151936 elements). |
| 33 | This means NaN appears in the OUTPUT PROJECTION section (after the parallel region ends). |
| 34 | The output projection does: |
| 35 | 1. Final RMS norm on x |
| 36 | 2. quantize_q8_0_c(x, xn_q8, N) |
| 37 | 3. quant_matmul_omp(w_out, x, logits, 151936, 2048, 8) |
| 38 | The output weight is Q8_0 type 8 with shape [151936, 2048]. |
| 39 | |
| 40 | ## Task |
| 41 | Read cengine_batch_instr.c (the qwen3_forward_c function at the end) |
| 42 | Read quant_kernels_omp.c (quant_matmul_omp and q8_0_matmul_omp) |
| 43 | Find why the output projection produces NaN when x, xn_q8 are valid. |