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 / trace_forward.py

87 lines · 2912 bytes Raw Blame History
1"""Trace the forward pass to find where it hangs."""
2import sys, time, gc
3sys.path.insert(0, 'src')
4import numpy as np
5from mojollama.model.inference import load_model
6
7logf = open('/tmp/opencode/trace.log', 'w')
8def log(msg):
9 print(msg, flush=True)
10 logf.write(msg + '\n')
11 logf.flush()
12
13model = load_model('test_model.gguf')
14ids = model.encode('Hello')
15log(f'ids={ids}')
16h_ids = np.array(ids, dtype=np.int64)
17
18embed = model._get_persistent('token_embd.weight')
19x = embed[h_ids].astype(np.float32)
20log(f'embed shape={x.shape}')
21
22head_dim = model.n_embd // model.n_head
23cos, sin = model.precompute_freqs_cis(head_dim, 1, model.rope_theta)
24mask = np.triu(np.full((1, 1), -np.inf, dtype=np.float32), 1)
25n_rep = model.n_head // model.n_kv_head
26
27for i in range(model.n_layers):
28 t0 = time.time()
29 ln1 = model._tensor(f'blk.{i}.attn_norm.weight')
30 q_w = model._tensor(f'blk.{i}.attn_q.weight')
31 k_w = model._tensor(f'blk.{i}.attn_k.weight')
32 v_w = model._tensor(f'blk.{i}.attn_v.weight')
33 o_w = model._tensor(f'blk.{i}.attn_output.weight')
34 q_b = model._tensor(f'blk.{i}.attn_q.bias')
35 k_b = model._tensor(f'blk.{i}.attn_k.bias')
36 v_b = model._tensor(f'blk.{i}.attn_v.bias')
37 ln2 = model._tensor(f'blk.{i}.ffn_norm.weight')
38 gate_w = model._tensor(f'blk.{i}.ffn_gate.weight')
39 up_w = model._tensor(f'blk.{i}.ffn_up.weight')
40 down_w = model._tensor(f'blk.{i}.ffn_down.weight')
41 load_time = time.time() - t0
42
43 t0 = time.time()
44 r = x
45 x = model.rms_norm(x, ln1, model.norm_eps)
46 q = x @ q_w.T
47 k = x @ k_w.T
48 v = x @ v_w.T
49 if q_b is not None: q += q_b
50 if k_b is not None: k += k_b
51 if v_b is not None: v += v_b
52 q = q.reshape(1, model.n_head, head_dim)
53 k = k.reshape(1, model.n_kv_head, head_dim)
54 v = v.reshape(1, model.n_kv_head, head_dim)
55 q = model.apply_rope(q, cos, sin)
56 k = model.apply_rope(k, cos, sin)
57 if n_rep > 1:
58 k = np.repeat(k, n_rep, axis=1)
59 v = np.repeat(v, n_rep, axis=1)
60 att = np.einsum('ihd,jhd->hij', q, k) / np.sqrt(head_dim)
61 att = att + mask[np.newaxis, :, :]
62 am = np.max(att, axis=-1, keepdims=True)
63 att = np.exp(att - am)
64 att = att / np.sum(att, axis=-1, keepdims=True)
65 out = np.einsum('hij,jhd->ihd', att, v).reshape(1, model.n_embd)
66 out = out @ o_w.T
67 x = r + out
68 r = x
69 x = model.rms_norm(x, ln2, model.norm_eps)
70 gate = x @ gate_w.T
71 up = x @ up_w.T
72 x = (model.silu(gate) * up) @ down_w.T
73 x = r + x
74 math_time = time.time() - t0
75
76 del ln1, q_w, k_w, v_w, o_w, q_b, k_b, v_b, ln2, gate_w, up_w, down_w
77
78 if i < 3 or i % 5 == 0:
79 log(f'layer {i}: load={load_time:.3f}s math={math_time:.3f}s')
80
81norm_w = model._tensor('output_norm.weight')
82x = model.rms_norm(x, norm_w, model.norm_eps)
83lm_w = model._get_persistent('output.weight')
84logits = x @ lm_w.T
85log(f'Done! logits shape={logits.shape}')
86
87logf.close()