MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / debug_trace_gqa.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Check workspace buffer overflow and other potential issues.""" |
| 3 | import sys, os, numpy as np |
| 4 | sys.path.insert(0, '/onedev-workspace/work/src/mojollama/kernels') |
| 5 | |
| 6 | os.environ['OMP_NUM_THREADS'] = '1' |
| 7 | os.environ['OPENBLAS_NUM_THREADS'] = '1' |
| 8 | |
| 9 | from turbo_engine_v7_moe import TurboEngineV7MoE |
| 10 | |
| 11 | print("Loading model...", flush=True) |
| 12 | e = TurboEngineV7MoE('/tmp/models/gpt-oss-20b-Q4_K_M.gguf', n_threads=1) |
| 13 | print("Model loaded.", flush=True) |
| 14 | |
| 15 | N = e.n_embd; NH = e.n_head; NKH = e.n_kv_head * e.head_dim; HD = e.head_dim |
| 16 | gqa_rep = NH // e.n_kv_head |
| 17 | |
| 18 | print(f"\nModel dimensions:", flush=True) |
| 19 | print(f" N={N}, NH={NH}, NKH={NKH}, HD={HD}, gqa_rep={gqa_rep}", flush=True) |
| 20 | |
| 21 | # Check workspace size |
| 22 | ws_size = e._gqa_ws_size |
| 23 | print(f"\nWorkspace allocated: {ws_size} floats ({ws_size * 4 / 1024:.0f} KB)", flush=True) |
| 24 | print(f" For seq_len=4096: needs NH*seq_len + gqa_rep*seq_len = {NH*4096 + gqa_rep*4096}", flush=True) |
| 25 | if ws_size < NH * 4096 + gqa_rep * 4096: |
| 26 | print(f" *** WORKSPACE OVERFLOW for seq_len=4096! Missing {(NH + gqa_rep)*4096 - ws_size} floats", flush=True) |
| 27 | else: |
| 28 | print(f" Workspace OK for seq_len=4096", flush=True) |
| 29 | |
| 30 | # Check: does _p_gqa_ws point to the right place? |
| 31 | print(f"\n_gqa_workspace addr: {e._gqa_workspace.ctypes.data}", flush=True) |
| 32 | print(f"_p_gqa_ws addr: {e._p_gqa_ws}", flush=True) |
| 33 | |
| 34 | # Check batch_forward path — is there a bug there? |
| 35 | print(f"\nChecking batch forward availability:", flush=True) |
| 36 | print(f" _c_batch_ready: {getattr(e, '_c_batch_ready', 'N/A')}", flush=True) |
| 37 | print(f" Has batch forward: {hasattr(e, 'forward_c_batch')}", flush=True) |
| 38 | |
| 39 | # Check if there's a forward_c_batch for gpt-oss |
| 40 | if hasattr(e, '_kern') and e._kern is not None: |
| 41 | for attr in dir(e._kern): |
| 42 | if 'batch' in attr.lower() or 'forward' in attr.lower(): |
| 43 | print(f" C function: {attr}", flush=True) |
| 44 | |
| 45 | # Check if pos is correctly handled |
| 46 | print(f"\npos = {e.pos}", flush=True) |
| 47 | |
| 48 | # CRITICAL TEST: monkey-patch to check the actual seq_len passed to GQA |
| 49 | import ctypes |
| 50 | cf = ctypes.POINTER(ctypes.c_float) |
| 51 | ci = ctypes.c_int |
| 52 | |
| 53 | original_gqa = e._gqa_attn.gqa_attention_decode |
| 54 | |
| 55 | call_count = [0] |
| 56 | def traced_gqa(q, k, v, out, seq_len, n_head, n_kv_head, head_dim, ws): |
| 57 | call_count[0] += 1 |
| 58 | print(f" GQA call {call_count[0]}: seq_len={seq_len}, n_head={n_head}, n_kv_head={n_kv_head}, head_dim={head_dim}", flush=True) |
| 59 | return original_gqa(q, k, v, out, seq_len, n_head, n_kv_head, head_dim, ws) |
| 60 | |
| 61 | e._gqa_attn.gqa_attention_decode = traced_gqa |
| 62 | |
| 63 | # Now do 2 forward calls |
| 64 | e.reset() |
| 65 | print(f"\n=== Forward call 1 (token 42) ===", flush=True) |
| 66 | l1 = e.forward(42).copy() |
| 67 | print(f"pos after call 1: {e.pos}", flush=True) |
| 68 | |
| 69 | print(f"\n=== Forward call 2 (token 99) ===", flush=True) |
| 70 | l2 = e.forward(99).copy() |
| 71 | print(f"pos after call 2: {e.pos}", flush=True) |
| 72 | |
| 73 | print(f"\n=== Forward call 3 (token 42) ===", flush=True) |
| 74 | l3 = e.forward(42).copy() |
| 75 | print(f"pos after call 3: {e.pos}", flush=True) |
| 76 | |
| 77 | # Check: is l2 (token 99 with context) different from just token 99 with no context? |
| 78 | e.reset() |
| 79 | l99_alone = e.forward(99).copy() |
| 80 | print(f"\nToken 99 alone vs with context (token 42 then 99): {np.max(np.abs(l2 - l99_alone)):.6f}") |
| 81 | |
| 82 | # Check: is l3 (token 42 with 42 and 99 in cache) different from l1 (token 42 alone)? |
| 83 | print(f"Token 42 alone vs with 2 tokens context: {np.max(np.abs(l3 - l1)):.6f}") |
| 84 | |
| 85 | # The KEY question: is the output on step 3 different from step 1? |
| 86 | # If V values for token 42 are the same at all positions (which they are for the same layer input), |
| 87 | # then the attention output would be the same, and the hidden state propagation would make |
| 88 | # everything identical. |
| 89 | # |
| 90 | # BUT! After step 2 with token 99, the hidden state at step 3 should be DIFFERENT from |
| 91 | # the hidden state at step 1 (because step 2's output was different, affecting subsequent layers). |
| 92 | # |
| 93 | # So: step 3 output should be different from step 1! |
| 94 | |
| 95 | print(f"\nCRITICAL: Is step 3 (token 42 after token 99) different from step 1 (token 42 alone)?") |
| 96 | print(f" Step 1 logits[:5] = {l1[:5]}") |
| 97 | print(f" Step 3 logits[:5] = {l3[:5]}") |
| 98 | print(f" Diff = {np.max(np.abs(l3 - l1)):.6f}") |
| 99 | print(f" Different? {np.max(np.abs(l3 - l1)) > 0.001}") |