Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/MojoLlama/blob/main/trace_forward2.py # MojoLlama Archived Mojo/MAX quantizer and serving research repository (read-only history) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `MojoLlama / trace_forward2.py` 91 lines ยท 3003 bytes Raw Blame History 1 """Trace each layer to find the slow one.""" 2 import sys, time 3 sys.path.insert(0, 'src') 4 import numpy as np 5 from mojollama.model.inference import load_model 6 7 logf = open('/tmp/opencode/trace2.log', 'w') 8 def log(msg): 9 print(msg, flush=True) 10 logf.write(msg + '\n') 11 logf.flush() 12 13 model = load_model('test_model.gguf') 14 ids = model.encode('Hello') 15 h_ids = np.array(ids, dtype=np.int64) 16 17 embed = model._get_persistent('token_embd.weight') 18 x = embed[h_ids].astype(np.float32) 19 20 head_dim = model.n_embd // model.n_head 21 cos, sin = model.precompute_freqs_cis(head_dim, 1, model.rope_theta) 22 mask = np.triu(np.full((1, 1), -np.inf, dtype=np.float32), 1) 23 n_rep = model.n_head // model.n_kv_head 24 25 t_start = time.time() 26 for i in range(model.n_layers): 27 t0 = time.time() 28 ln1 = model._tensor(f'blk.{i}.attn_norm.weight') 29 q_w = model._tensor(f'blk.{i}.attn_q.weight') 30 k_w = model._tensor(f'blk.{i}.attn_k.weight') 31 v_w = model._tensor(f'blk.{i}.attn_v.weight') 32 o_w = model._tensor(f'blk.{i}.attn_output.weight') 33 q_b = model._tensor(f'blk.{i}.attn_q.bias') 34 k_b = model._tensor(f'blk.{i}.attn_k.bias') 35 v_b = model._tensor(f'blk.{i}.attn_v.bias') 36 ln2 = model._tensor(f'blk.{i}.ffn_norm.weight') 37 gate_w = model._tensor(f'blk.{i}.ffn_gate.weight') 38 up_w = model._tensor(f'blk.{i}.ffn_up.weight') 39 down_w = model._tensor(f'blk.{i}.ffn_down.weight') 40 load_time = time.time() - t0 41 42 r = x 43 x = model.rms_norm(x, ln1, model.norm_eps) 44 q = x @ q_w.T 45 k = x @ k_w.T 46 v = x @ v_w.T 47 if q_b is not None: q += q_b 48 if k_b is not None: k += k_b 49 if v_b is not None: v += v_b 50 q = q.reshape(1, model.n_head, head_dim) 51 k = k.reshape(1, model.n_kv_head, head_dim) 52 v = v.reshape(1, model.n_kv_head, head_dim) 53 q = model.apply_rope(q, cos, sin) 54 k = model.apply_rope(k, cos, sin) 55 if n_rep > 1: 56 k = np.repeat(k, n_rep, axis=1) 57 v = np.repeat(v, n_rep, axis=1) 58 att = np.einsum('ihd,jhd->hij', q, k) / np.sqrt(head_dim) 59 att = att + mask[np.newaxis, :, :] 60 am = np.max(att, axis=-1, keepdims=True) 61 att = np.exp(att - am) 62 att = att / np.sum(att, axis=-1, keepdims=True) 63 out = np.einsum('hij,jhd->ihd', att, v).reshape(1, model.n_embd) 64 out = out @ o_w.T 65 x = r + out 66 r = x 67 x = model.rms_norm(x, ln2, model.norm_eps) 68 gate = x @ gate_w.T 69 up = x @ up_w.T 70 x = (model.silu(gate) * up) @ down_w.T 71 x = r + x 72 73 del ln1, q_w, k_w, v_w, o_w, q_b, k_b, v_b, ln2, gate_w, up_w, down_w 74 elapsed = time.time() - t_start 75 log(f'layer {i}: {elapsed:.3f}s (load={load_time:.3f}s)') 76 77 norm_w = model._tensor('output_norm.weight') 78 t0 = time.time() 79 x = model.rms_norm(x, norm_w, model.norm_eps) 80 log(f'norm: {time.time()-t0:.3f}s') 81 82 t0 = time.time() 83 lm_w = model._get_persistent('output.weight') 84 log(f'lm_head load: {time.time()-t0:.3f}s') 85 86 t0 = time.time() 87 logits = x @ lm_w.T 88 log(f'lm_head matmul: {time.time()-t0:.3f}s') 89 log(f'Done! total={time.time()-t_start:.1f}s') 90 91 logf.close()