MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / trace_2tokens.py
| 1 | """Trace forward pass with 2 tokens.""" |
| 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 | model = load_model('test_model.gguf') |
| 8 | |
| 9 | # Use 2 tokens |
| 10 | ids = [model.bos_id, model.bos_id] |
| 11 | seq_len = len(ids) |
| 12 | h_ids = np.array(ids, dtype=np.int64) |
| 13 | |
| 14 | embed = model._get_persistent('token_embd.weight') |
| 15 | x = embed[h_ids].astype(np.float32) |
| 16 | print(f'embed: {x.shape}') |
| 17 | |
| 18 | head_dim = model.n_embd // model.n_head |
| 19 | cos, sin = model.precompute_freqs_cis(head_dim, seq_len, model.rope_theta) |
| 20 | n_rep = model.n_head // model.n_kv_head |
| 21 | mask = np.triu(np.full((seq_len, seq_len), -np.inf, dtype=np.float32), 1) |
| 22 | |
| 23 | t_start = time.time() |
| 24 | for i in range(model.n_layers): |
| 25 | t0 = time.time() |
| 26 | ln1 = model._tensor(f'blk.{i}.attn_norm.weight') |
| 27 | q_w = model._tensor(f'blk.{i}.attn_q.weight') |
| 28 | k_w = model._tensor(f'blk.{i}.attn_k.weight') |
| 29 | v_w = model._tensor(f'blk.{i}.attn_v.weight') |
| 30 | o_w = model._tensor(f'blk.{i}.attn_output.weight') |
| 31 | q_b = model._tensor(f'blk.{i}.attn_q.bias') |
| 32 | k_b = model._tensor(f'blk.{i}.attn_k.bias') |
| 33 | v_b = model._tensor(f'blk.{i}.attn_v.bias') |
| 34 | ln2 = model._tensor(f'blk.{i}.ffn_norm.weight') |
| 35 | gate_w = model._tensor(f'blk.{i}.ffn_gate.weight') |
| 36 | up_w = model._tensor(f'blk.{i}.ffn_up.weight') |
| 37 | down_w = model._tensor(f'blk.{i}.ffn_down.weight') |
| 38 | load_time = time.time() - t0 |
| 39 | |
| 40 | r = x |
| 41 | x = model.rms_norm(x, ln1, model.norm_eps) |
| 42 | q = x @ q_w.T; k = x @ k_w.T; v = x @ v_w.T |
| 43 | if q_b is not None: q += q_b |
| 44 | if k_b is not None: k += k_b |
| 45 | if v_b is not None: v += v_b |
| 46 | q = q.reshape(seq_len, model.n_head, head_dim) |
| 47 | k = k.reshape(seq_len, model.n_kv_head, head_dim) |
| 48 | v = v.reshape(seq_len, model.n_kv_head, head_dim) |
| 49 | q = model.apply_rope(q, cos, sin) |
| 50 | k = model.apply_rope(k, cos, sin) |
| 51 | if n_rep > 1: k = np.repeat(k, n_rep, axis=1); v = np.repeat(v, n_rep, axis=1) |
| 52 | att = np.einsum('ihd,jhd->hij', q, k) / np.sqrt(head_dim) |
| 53 | att = att + mask[np.newaxis, :, :] |
| 54 | am = np.max(att, axis=-1, keepdims=True) |
| 55 | att = np.exp(att - am) |
| 56 | att = att / np.sum(att, axis=-1, keepdims=True) |
| 57 | out = np.einsum('hij,jhd->ihd', att, v).reshape(seq_len, model.n_embd) |
| 58 | out = out @ o_w.T; x = r + out |
| 59 | r = x |
| 60 | x = model.rms_norm(x, ln2, model.norm_eps) |
| 61 | gate = x @ gate_w.T; up = x @ up_w.T |
| 62 | x = (model.silu(gate) * up) @ down_w.T; x = r + x |
| 63 | del ln1, q_w, k_w, v_w, o_w, q_b, k_b, v_b, ln2, gate_w, up_w, down_w |
| 64 | |
| 65 | elapsed = time.time() - t_start |
| 66 | if i < 3 or i % 5 == 0: |
| 67 | print(f'layer {i}: {elapsed:.3f}s (load={load_time:.3f}s)') |
| 68 | |
| 69 | norm_w = model._tensor('output_norm.weight') |
| 70 | x = model.rms_norm(x, norm_w, model.norm_eps) |
| 71 | lm_w = model._get_persistent('output.weight') |
| 72 | logits = x @ lm_w.T |
| 73 | print(f'Done! {time.time()-t_start:.1f}s logits={logits.shape}') |