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_identical_output.py

139 lines · 5613 bytes Raw Blame History
1#!/usr/bin/env python3
2"""Debug MojoLlama identical output bug."""
3import sys, os, numpy as np
4sys.path.insert(0, '/onedev-workspace/work/src/mojollama/kernels')
5
6os.environ['OMP_NUM_THREADS'] = '1'
7os.environ['OPENBLAS_NUM_THREADS'] = '1'
8
9from turbo_engine_v7_moe import TurboEngineV7MoE
10
11print("Loading model...", flush=True)
12e = TurboEngineV7MoE('/tmp/models/gpt-oss-20b-Q4_K_M.gguf', n_threads=1)
13print("Model loaded.", flush=True)
14
15# Store initial state
16print(f"Initial pos={e.pos}, kv_len[0]={e.kv_len[0]}", flush=True)
17print(f"Initial _x[:5] = {e._x[:5]}", flush=True)
18print(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
20N = e.n_embd
21NH = e.n_head
22NKH = e.n_kv_head * e.head_dim
23HD = e.head_dim
24print(f"N={N}, NH={NH}, NKH={NKH}, HD={HD}", flush=True)
25
26# First forward call with token 42
27print("\n=== Forward call 1 (token 42) ===", flush=True)
28l1 = e.forward(42).copy()
29print(f"pos after call 1: {e.pos}", flush=True)
30print(f"kv_len[0] after call 1: {e.kv_len[0]}", flush=True)
31print(f"kv_k[0,0,:3] after call 1: {e.kv_k[0,0,:3]}", flush=True)
32print(f"logits[:5] = {l1[:5]}", flush=True)
33
34# Second forward call with token 42 (same token)
35print("\n=== Forward call 2 (token 42) ===", flush=True)
36l2 = e.forward(42).copy()
37print(f"pos after call 2: {e.pos}", flush=True)
38print(f"kv_len[0] after call 2: {e.kv_len[0]}", flush=True)
39print(f"kv_k[0,0,:3] = {e.kv_k[0,0,:3]}", flush=True)
40print(f"kv_k[0,1,:3] = {e.kv_k[0,1,:3]}", flush=True)
41print(f"logits[:5] = {l2[:5]}", flush=True)
42
43max_diff = np.max(np.abs(l1 - l2))
44print(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
48print("\n=== Layer-by-layer diagnostic ===", flush=True)
49e.reset()
50print(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
53original_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
57print(f"_arch_forward is {e._arch_forward}", flush=True)
58print(f"arch_prefix = {e.arch_prefix if hasattr(e, 'arch_prefix') else 'N/A'}", flush=True)
59
60# Check buffer aliasing
61print(f"\nBuffer addresses:", flush=True)
62print(f" _x addr: {e._x.ctypes.data}", flush=True)
63print(f" _x_norm addr: {e._x_norm.ctypes.data}", flush=True)
64print(f" _residual addr: {e._residual.ctypes.data}", flush=True)
65print(f" _qk addr: {e._qk.ctypes.data}", flush=True)
66print(f" _v addr: {e._v.ctypes.data}", flush=True)
67print(f" _att_out addr: {e._att_out.ctypes.data}", flush=True)
68print(f" _o_proj addr: {e._o_proj.ctypes.data}", flush=True)
69
70# Check for buffer overlaps
71bufs = {
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
87for 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
101print("\n=== Testing with monkey-patched forward ===", flush=True)
102
103# Let me check if RoPE is working
104print(f"\nRope freq_base = {e.rope_freq_base}", flush=True)
105print(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
109e.reset()
110print(f"\nBefore any forward: _x[:5] = {e._x[:5]}", flush=True)
111print(f"pos = {e.pos}", flush=True)
112
113l1 = e.forward(42).copy()
114print(f"After call 1: _x[:5] = {e._x[:5]}", flush=True)
115print(f"pos = {e.pos}", flush=True)
116
117l2 = e.forward(43).copy()
118print(f"After call 2 (token 43): _x[:5] = {e._x[:5]}", flush=True)
119print(f"pos = {e.pos}", flush=True)
120
121print(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?
124e.reset()
125l1a = e.forward(42).copy()
126l2a = e.forward(99).copy()
127print(f"\nToken 42 step 1, token 99 step 2:", flush=True)
128print(f" Max diff: {np.max(np.abs(l1a - l2a)):.6f}", flush=True)
129
130e.reset()
131l1b = e.forward(42).copy()
132l2b = e.forward(42).copy()
133print(f"Token 42 step 1, token 42 step 2:", flush=True)
134print(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?
137print(f"\nl2a[:10] = {l2a[:10]}", flush=True)
138print(f"l2b[:10] = {l2b[:10]}", flush=True)
139print(f"Max diff l2a vs l2b: {np.max(np.abs(l2a - l2b)):.6f}", flush=True)