MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / debug_identical_output.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Debug MojoLlama identical output bug.""" |
| 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 | # Store initial state |
| 16 | print(f"Initial pos={e.pos}, kv_len[0]={e.kv_len[0]}", flush=True) |
| 17 | print(f"Initial _x[:5] = {e._x[:5]}", flush=True) |
| 18 | print(f"n_embd={e.n_embd}, n_head={e.n_head}, n_kv_head={e.n_kv_head}, head_dim={e.head_dim}", flush=True) |
| 19 | |
| 20 | N = e.n_embd |
| 21 | NH = e.n_head |
| 22 | NKH = e.n_kv_head * e.head_dim |
| 23 | HD = e.head_dim |
| 24 | print(f"N={N}, NH={NH}, NKH={NKH}, HD={HD}", flush=True) |
| 25 | |
| 26 | # First forward call with token 42 |
| 27 | print("\n=== Forward call 1 (token 42) ===", flush=True) |
| 28 | l1 = e.forward(42).copy() |
| 29 | print(f"pos after call 1: {e.pos}", flush=True) |
| 30 | print(f"kv_len[0] after call 1: {e.kv_len[0]}", flush=True) |
| 31 | print(f"kv_k[0,0,:3] after call 1: {e.kv_k[0,0,:3]}", flush=True) |
| 32 | print(f"logits[:5] = {l1[:5]}", flush=True) |
| 33 | |
| 34 | # Second forward call with token 42 (same token) |
| 35 | print("\n=== Forward call 2 (token 42) ===", flush=True) |
| 36 | l2 = e.forward(42).copy() |
| 37 | print(f"pos after call 2: {e.pos}", flush=True) |
| 38 | print(f"kv_len[0] after call 2: {e.kv_len[0]}", flush=True) |
| 39 | print(f"kv_k[0,0,:3] = {e.kv_k[0,0,:3]}", flush=True) |
| 40 | print(f"kv_k[0,1,:3] = {e.kv_k[0,1,:3]}", flush=True) |
| 41 | print(f"logits[:5] = {l2[:5]}", flush=True) |
| 42 | |
| 43 | max_diff = np.max(np.abs(l1 - l2)) |
| 44 | print(f"\nMax diff between call 1 and call 2: {max_diff:.6f}", flush=True) |
| 45 | |
| 46 | # Now let's trace what happens layer by layer |
| 47 | # Reset and instrument |
| 48 | print("\n=== Layer-by-layer diagnostic ===", flush=True) |
| 49 | e.reset() |
| 50 | print(f"After reset: pos={e.pos}, kv_len[0]={e.kv_len[0]}", flush=True) |
| 51 | |
| 52 | # We'll monkey-patch to trace hidden state after each layer |
| 53 | original_forward = e.forward.__func__ if hasattr(e.forward, '__func__') else None |
| 54 | |
| 55 | # Instead, let's create a diagnostic using available hooks |
| 56 | # Check if _arch_forward is being used |
| 57 | print(f"_arch_forward is {e._arch_forward}", flush=True) |
| 58 | print(f"arch_prefix = {e.arch_prefix if hasattr(e, 'arch_prefix') else 'N/A'}", flush=True) |
| 59 | |
| 60 | # Check buffer aliasing |
| 61 | print(f"\nBuffer addresses:", flush=True) |
| 62 | print(f" _x addr: {e._x.ctypes.data}", flush=True) |
| 63 | print(f" _x_norm addr: {e._x_norm.ctypes.data}", flush=True) |
| 64 | print(f" _residual addr: {e._residual.ctypes.data}", flush=True) |
| 65 | print(f" _qk addr: {e._qk.ctypes.data}", flush=True) |
| 66 | print(f" _v addr: {e._v.ctypes.data}", flush=True) |
| 67 | print(f" _att_out addr: {e._att_out.ctypes.data}", flush=True) |
| 68 | print(f" _o_proj addr: {e._o_proj.ctypes.data}", flush=True) |
| 69 | |
| 70 | # Check for buffer overlaps |
| 71 | bufs = { |
| 72 | '_x': (e._x.ctypes.data, e._x.nbytes), |
| 73 | '_x_norm': (e._x_norm.ctypes.data, e._x_norm.nbytes), |
| 74 | '_residual': (e._residual.ctypes.data, e._residual.nbytes), |
| 75 | '_qk': (e._qk.ctypes.data, e._qk.nbytes), |
| 76 | '_v': (e._v.ctypes.data, e._v.nbytes), |
| 77 | '_att_out': (e._att_out.ctypes.data, e._att_out.nbytes), |
| 78 | '_o_proj': (e._o_proj.ctypes.data, e._o_proj.nbytes), |
| 79 | '_gate': (e._gate.ctypes.data, e._gate.nbytes), |
| 80 | '_up': (e._up.ctypes.data, e._up.nbytes), |
| 81 | '_silu_gate': (e._silu_gate.ctypes.data, e._silu_gate.nbytes), |
| 82 | '_ffn_out': (e._ffn_out.ctypes.data, e._ffn_out.nbytes), |
| 83 | '_logits': (e._logits.ctypes.data, e._logits.nbytes), |
| 84 | 'kv_k[0]': (e.kv_k[0].ctypes.data, e.kv_k[0].nbytes), |
| 85 | } |
| 86 | |
| 87 | for name1, (addr1, sz1) in bufs.items(): |
| 88 | for name2, (addr2, sz2) in bufs.items(): |
| 89 | if name1 < name2: |
| 90 | # Check if they overlap |
| 91 | start1, end1 = addr1, addr1 + sz1 |
| 92 | start2, end2 = addr2, addr2 + sz2 |
| 93 | if start1 < end2 and start2 < end1: |
| 94 | print(f" *** OVERLAP: {name1} and {name2} overlap!") |
| 95 | overlap_start = max(start1, start2) |
| 96 | overlap_end = min(end1, end2) |
| 97 | print(f" Overlap of {overlap_end - overlap_start} bytes") |
| 98 | |
| 99 | # Test: call forward with intermediate value tracing |
| 100 | # First, save the original forward method |
| 101 | print("\n=== Testing with monkey-patched forward ===", flush=True) |
| 102 | |
| 103 | # Let me check if RoPE is working |
| 104 | print(f"\nRope freq_base = {e.rope_freq_base}", flush=True) |
| 105 | print(f"Rope dim = {e.rope_dim if hasattr(e, 'rope_dim') else 'N/A'}", flush=True) |
| 106 | |
| 107 | # Let me manually test with a simple approach |
| 108 | # After the first forward, let's check _x |
| 109 | e.reset() |
| 110 | print(f"\nBefore any forward: _x[:5] = {e._x[:5]}", flush=True) |
| 111 | print(f"pos = {e.pos}", flush=True) |
| 112 | |
| 113 | l1 = e.forward(42).copy() |
| 114 | print(f"After call 1: _x[:5] = {e._x[:5]}", flush=True) |
| 115 | print(f"pos = {e.pos}", flush=True) |
| 116 | |
| 117 | l2 = e.forward(43).copy() |
| 118 | print(f"After call 2 (token 43): _x[:5] = {e._x[:5]}", flush=True) |
| 119 | print(f"pos = {e.pos}", flush=True) |
| 120 | |
| 121 | print(f"\nMax diff btwn token 42 and 43 (call 1 vs call 2): {np.max(np.abs(l1 - l2)):.6f}", flush=True) |
| 122 | |
| 123 | # Now check: what if we call with diff token on step 2? |
| 124 | e.reset() |
| 125 | l1a = e.forward(42).copy() |
| 126 | l2a = e.forward(99).copy() |
| 127 | print(f"\nToken 42 step 1, token 99 step 2:", flush=True) |
| 128 | print(f" Max diff: {np.max(np.abs(l1a - l2a)):.6f}", flush=True) |
| 129 | |
| 130 | e.reset() |
| 131 | l1b = e.forward(42).copy() |
| 132 | l2b = e.forward(42).copy() |
| 133 | print(f"Token 42 step 1, token 42 step 2:", flush=True) |
| 134 | print(f" Max diff: {np.max(np.abs(l1b - l2b)):.6f}", flush=True) |
| 135 | |
| 136 | # So the question is: does call 2 always give the same output regardless of input? |
| 137 | print(f"\nl2a[:10] = {l2a[:10]}", flush=True) |
| 138 | print(f"l2b[:10] = {l2b[:10]}", flush=True) |
| 139 | print(f"Max diff l2a vs l2b: {np.max(np.abs(l2a - l2b)):.6f}", flush=True) |