MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / debug_manual_step.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Debug the actual Q, K, attention values.""" |
| 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 | # Let me check the actual forward with careful instrumentation |
| 16 | # First, let's understand the model architecture |
| 17 | print(f"\nArchitecture: {e.arch_prefix}", flush=True) |
| 18 | print(f"n_layers: {e.n_layers}", flush=True) |
| 19 | 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) |
| 20 | print(f"n_ff: {e.n_ff}", flush=True) |
| 21 | print(f"rope_freq_base: {e.rope_freq_base}", flush=True) |
| 22 | print(f"rope_dim: {e.rope_dim if hasattr(e, 'rope_dim') else 'N/A'}", flush=True) |
| 23 | print(f"is_moe: {e.is_moe}", flush=True) |
| 24 | print(f"has _gqa_attn: {e._gqa_attn is not None}", flush=True) |
| 25 | |
| 26 | # Check first layer |
| 27 | lw = e._layers[0] |
| 28 | print(f"\nLayer 0:", flush=True) |
| 29 | print(f" attn_qk_use_c: {hasattr(lw, 'attn_qk_use_c') and lw.attn_qk_use_c}", flush=True) |
| 30 | print(f" attn_qkv_use_c: {hasattr(lw, 'attn_qkv_use_c') and lw.attn_qkv_use_c}", flush=True) |
| 31 | print(f" attn_q_use_c: {lw.attn_q_use_c if hasattr(lw, 'attn_q_use_c') else 'N/A'}", flush=True) |
| 32 | print(f" attn_out_use_c: {lw.attn_out_use_c if hasattr(lw, 'attn_out_use_c') else 'N/A'}", flush=True) |
| 33 | print(f" has_q_norm: {lw.has_q_norm if hasattr(lw, 'has_q_norm') else 'N/A'}", flush=True) |
| 34 | print(f" has_k_norm: {lw.has_k_norm if hasattr(lw, 'has_k_norm') else 'N/A'}", flush=True) |
| 35 | |
| 36 | # Check the first layer's kv cache |
| 37 | print(f"\nInitial kv_len[0] = {e.kv_len[0]}", flush=True) |
| 38 | |
| 39 | e.reset() |
| 40 | |
| 41 | # Manually trace through part of the first layer |
| 42 | b_x = e._x |
| 43 | b_x_norm = e._x_norm |
| 44 | b_q = e._q # view of _qk |
| 45 | b_k = e._k # view of _qk[NK:] |
| 46 | b_v = e._v |
| 47 | b_att = e._att_out |
| 48 | b_oproj = e._o_proj |
| 49 | |
| 50 | N = e.n_embd |
| 51 | NH = e.n_head |
| 52 | NKH = e.n_kv_head * e.head_dim |
| 53 | HD = e.head_dim |
| 54 | |
| 55 | print(f"\nN={N}, NKH={NKH}, HD={HD}", flush=True) |
| 56 | |
| 57 | # Embedding |
| 58 | np.copyto(b_x, e.emb[42]) |
| 59 | print(f"After embedding, b_x[:5] = {b_x[:5]}", flush=True) |
| 60 | print(f"After embedding, self._x[:5] = {e._x[:5]}", flush=True) |
| 61 | print(f"b_x is self._x: {b_x is e._x}", flush=True) |
| 62 | |
| 63 | import ctypes |
| 64 | cf = ctypes.POINTER(ctypes.c_float) |
| 65 | ci = ctypes.c_int |
| 66 | |
| 67 | ####### CALL 1 ####### |
| 68 | print("\n\n========== CALL 1 (token 42, pos=0) ==========", flush=True) |
| 69 | |
| 70 | # Reset state |
| 71 | e.reset() |
| 72 | print(f"After reset: pos={e.pos}, kv_len={e.kv_len[0]}", flush=True) |
| 73 | |
| 74 | # Embedding |
| 75 | np.copyto(b_x, e.emb[42]) |
| 76 | print(f"Embedding _x[:5] = {b_x[:5]}", flush=True) |
| 77 | |
| 78 | # Layer 0 |
| 79 | for layer_idx in [0]: # Just layer 0 |
| 80 | lw = e._layers[layer_idx] |
| 81 | |
| 82 | # Save residual |
| 83 | b_r = e._residual |
| 84 | b_r[:] = b_x |
| 85 | |
| 86 | # RMS norm |
| 87 | e._simd.rms_norm(e._p_x_norm, e._p_x, |
| 88 | lw.attn_norm_w.ctypes.data_as(cf), |
| 89 | N, e._eps_f) |
| 90 | print(f"After norm, x_norm[:5] = {e._x_norm[:5]}", flush=True) |
| 91 | |
| 92 | # Fused QK |
| 93 | e._kern.quant_matmul_omp(lw.attn_qk_raw, e._p_x_norm, e._p_qk, |
| 94 | lw.attn_qk_nr, lw.attn_qk_nc, lw.attn_qk_qt) |
| 95 | e._kern.quant_matmul_omp(lw.attn_v_raw, e._p_x_norm, e._p_v, |
| 96 | lw.attn_v_nr, lw.attn_v_nc, lw.attn_v_qt) |
| 97 | print(f"After QK matmul, _q[:5] = {e._q[:5]}", flush=True) |
| 98 | |
| 99 | # RoPE |
| 100 | rope_d = HD if not (hasattr(e, 'rope_dim') and e.rope_dim > 0) else e.rope_dim |
| 101 | print(f"rope_d = {rope_d}, pos = {e.pos}", flush=True) |
| 102 | |
| 103 | # Save Q and K before RoPE |
| 104 | q_before = e._q[:10].copy() |
| 105 | k_before = e._k[:10].copy() |
| 106 | |
| 107 | e._q[:] = e._apply_rope_fast(e._q, e.pos, NH, rope_dim=rope_d) |
| 108 | e._k[:] = e._apply_rope_fast(e._k, e.pos, e.n_kv_head, rope_dim=rope_d) |
| 109 | |
| 110 | q_after = e._q[:10].copy() |
| 111 | k_after = e._k[:10].copy() |
| 112 | |
| 113 | print(f"Q before RoPE[:5] = {q_before[:5]}", flush=True) |
| 114 | print(f"Q after RoPE[:5] = {q_after[:5]}", flush=True) |
| 115 | print(f"Q diff = {np.max(np.abs(q_before - q_after)):.6f}", flush=True) |
| 116 | |
| 117 | # KV cache |
| 118 | e.kv_k[layer_idx, e.kv_len[layer_idx], :NKH] = e._k[:NKH] |
| 119 | e.kv_v[layer_idx, e.kv_len[layer_idx], :NKH] = e._v[:NKH] |
| 120 | print(f"KV cache pos {e.kv_len[layer_idx]}: kv_k[0,:3] = {e.kv_k[layer_idx, e.kv_len[layer_idx], :3]}", flush=True) |
| 121 | |
| 122 | # GQA Attention |
| 123 | seq_len = e.kv_len[layer_idx] + 1 |
| 124 | print(f"GQA with seq_len={seq_len}", flush=True) |
| 125 | |
| 126 | # Save att_out before |
| 127 | att_before = e._att_out[:10].copy() |
| 128 | |
| 129 | e._gqa_attn.gqa_attention_decode( |
| 130 | e._q.ctypes.data_as(cf), |
| 131 | e.kv_k[layer_idx].ctypes.data_as(cf), |
| 132 | e.kv_v[layer_idx].ctypes.data_as(cf), |
| 133 | e._att_out.ctypes.data_as(cf), |
| 134 | ci(seq_len), ci(NH), ci(e.n_kv_head), ci(HD), |
| 135 | e._p_gqa_ws) |
| 136 | |
| 137 | att_after = e._att_out[:10].copy() |
| 138 | print(f"att_out[:5] = {att_after[:5]}", flush=True) |
| 139 | |
| 140 | e.kv_len[layer_idx] += 1 |
| 141 | |
| 142 | # Output projection |
| 143 | e._kern.quant_matmul_omp(lw.attn_out_raw, e._p_att_out, e._p_o_proj, |
| 144 | lw.attn_out_nr, lw.attn_out_nc, lw.attn_out_qt) |
| 145 | e._x[:N] = b_r[:N] + e._o_proj[:N] |
| 146 | print(f"After attn out proj, _x[:5] = {e._x[:5]}", flush=True) |
| 147 | |
| 148 | print(f"\npos after call 1: {e.pos}", flush=True) # Not incremented yet since we didn't call forward() |
| 149 | |
| 150 | # Check the KV cache state |
| 151 | print(f"\nKV cache after call 1:", flush=True) |
| 152 | print(f" kv_len[0] = {e.kv_len[0]}", flush=True) |
| 153 | print(f" kv_k[0,0,:3] = {e.kv_k[0,0,:3]}", flush=True) |
| 154 | |
| 155 | # Manually increment pos for call 2 |
| 156 | e.pos += 1 |
| 157 | |
| 158 | # Also save hidden state |
| 159 | hidden_call1 = e._x[:10].copy() |
| 160 | |
| 161 | ####### CALL 2 (simulated, with same token 42) ####### |
| 162 | print("\n\n========== CALL 2 (token 42, pos=1) ==========", flush=True) |
| 163 | |
| 164 | # Embedding - same token |
| 165 | np.copyto(b_x, e.emb[42]) |
| 166 | print(f"Embedding _x[:5] = {b_x[:5]}", flush=True) |
| 167 | |
| 168 | # Layer 0 |
| 169 | for layer_idx in [0]: |
| 170 | lw = e._layers[layer_idx] |
| 171 | |
| 172 | # Save residual |
| 173 | b_r = e._residual |
| 174 | b_r[:] = b_x |
| 175 | |
| 176 | # RMS norm |
| 177 | e._simd.rms_norm(e._p_x_norm, e._p_x, |
| 178 | lw.attn_norm_w.ctypes.data_as(cf), |
| 179 | N, e._eps_f) |
| 180 | print(f"After norm, x_norm[:5] = {e._x_norm[:5]}", flush=True) |
| 181 | |
| 182 | # Fused QK |
| 183 | e._kern.quant_matmul_omp(lw.attn_qk_raw, e._p_x_norm, e._p_qk, |
| 184 | lw.attn_qk_nr, lw.attn_qk_nc, lw.attn_qk_qt) |
| 185 | e._kern.quant_matmul_omp(lw.attn_v_raw, e._p_x_norm, e._p_v, |
| 186 | lw.attn_v_nr, lw.attn_v_nc, lw.attn_v_qt) |
| 187 | |
| 188 | # RoPE - NOW WITH pos=1 |
| 189 | rope_d = HD |
| 190 | print(f"rope_d = {rope_d}, pos = {e.pos}", flush=True) |
| 191 | |
| 192 | # Save Q and K before RoPE |
| 193 | q_before = e._q[:10].copy() |
| 194 | k_before = e._k[:10].copy() |
| 195 | |
| 196 | e._q[:] = e._apply_rope_fast(e._q, e.pos, NH, rope_dim=rope_d) |
| 197 | e._k[:] = e._apply_rope_fast(e._k, e.pos, e.n_kv_head, rope_dim=rope_d) |
| 198 | |
| 199 | q_after = e._q[:10].copy() |
| 200 | k_after = e._k[:10].copy() |
| 201 | |
| 202 | print(f"Q before RoPE[:5] = {q_before[:5]}", flush=True) |
| 203 | print(f"Q after RoPE[:5] = {q_after[:5]}", flush=True) |
| 204 | print(f"Q diff = {np.max(np.abs(q_before - q_after)):.6f}", flush=True) |
| 205 | |
| 206 | # KV cache - store at position 1 |
| 207 | print(f"kv_len before store: {e.kv_len[layer_idx]}", flush=True) |
| 208 | e.kv_k[layer_idx, e.kv_len[layer_idx], :NKH] = e._k[:NKH] |
| 209 | e.kv_v[layer_idx, e.kv_len[layer_idx], :NKH] = e._v[:NKH] |
| 210 | print(f"KV cache pos {e.kv_len[layer_idx]}: kv_k[0,:3] = {e.kv_k[layer_idx, e.kv_len[layer_idx], :3]}", flush=True) |
| 211 | print(f"KV cache pos 0: kv_k[0,:3] = {e.kv_k[layer_idx, 0, :3]}", flush=True) |
| 212 | |
| 213 | # GQA Attention |
| 214 | seq_len = e.kv_len[layer_idx] + 1 |
| 215 | print(f"GQA with seq_len={seq_len}", flush=True) |
| 216 | print(f"kv_len after store but before GQA: {e.kv_len[layer_idx]}", flush=True) |
| 217 | |
| 218 | # Save att_out before |
| 219 | att_before = e._att_out[:10].copy() |
| 220 | |
| 221 | e._gqa_attn.gqa_attention_decode( |
| 222 | e._q.ctypes.data_as(cf), |
| 223 | e.kv_k[layer_idx].ctypes.data_as(cf), |
| 224 | e.kv_v[layer_idx].ctypes.data_as(cf), |
| 225 | e._att_out.ctypes.data_as(cf), |
| 226 | ci(seq_len), ci(NH), ci(e.n_kv_head), ci(HD), |
| 227 | e._p_gqa_ws) |
| 228 | |
| 229 | att_after = e._att_out[:10].copy() |
| 230 | print(f"att_out[:5] = {att_after[:5]}", flush=True) |
| 231 | |
| 232 | e.kv_len[layer_idx] += 1 |
| 233 | |
| 234 | # Output projection |
| 235 | e._kern.quant_matmul_omp(lw.attn_out_raw, e._p_att_out, e._p_o_proj, |
| 236 | lw.attn_out_nr, lw.attn_out_nc, lw.attn_out_qt) |
| 237 | e._x[:N] = b_r[:N] + e._o_proj[:N] |
| 238 | print(f"After attn out proj, _x[:5] = {e._x[:5]}", flush=True) |
| 239 | |
| 240 | hidden_call2 = e._x[:10].copy() |
| 241 | |
| 242 | print(f"\n\n=== COMPARISON ===", flush=True) |
| 243 | print(f"Hidden call1[:10] = {hidden_call1}", flush=True) |
| 244 | print(f"Hidden call2[:10] = {hidden_call2}", flush=True) |
| 245 | print(f"Max diff: {np.max(np.abs(hidden_call1 - hidden_call2)):.8f}", flush=True) |