MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / src / mojollama / mojo_engine / gqa_attention.mojo
| 1 | # MojoLlama GQA Attention — Pure Mojo Grouped Query Attention |
| 2 | # WHAT: GQA attention decode for single token. Replaces gqa_attention.c (135L) |
| 3 | # which depended on cblas_sgemm (OpenBLAS). Pure Mojo SIMD + parallelize. |
| 4 | # WHY: Remove OpenBLAS dependency. GQA is used by Llama 3, Gemma, Mistral, Qwen2. |
| 5 | # WHEN: May 2026 — pure Mojo port. Uses scalar softmax + parallel KV heads. |
| 6 | from std import math |
| 7 | from std.algorithm.backend.cpu.parallelize import parallelize |
| 8 | |
| 9 | comptime W: Int = 8 |
| 10 | |
| 11 | fn clamp_f32(x: Float32, lo: Float32, hi: Float32) -> Float32: |
| 12 | if x < lo: return lo |
| 13 | if x > hi: return hi |
| 14 | return x |
| 15 | |
| 16 | fn softmax(v: UnsafePointer[Float32, MutExternalOrigin], n: Int): |
| 17 | """In-place softmax on v[0:n]. Numerically stable: subtracts max first.""" |
| 18 | # Find max |
| 19 | var m: Float32 = -1e30 |
| 20 | for i in range(n): |
| 21 | if v.load(i) > m: m = v.load(i) |
| 22 | # Compute exp(x - max) and sum |
| 23 | var sum_exp: Float32 = 0.0 |
| 24 | for i in range(n): |
| 25 | var e = math.exp(clamp_f32(v.load(i) - m, -80.0, 80.0)) |
| 26 | v.store(i, e) |
| 27 | sum_exp += e |
| 28 | # Normalize |
| 29 | var inv_sum = 1.0 / (sum_exp + 1e-10) |
| 30 | for i in range(n): v.store(i, v.load(i) * inv_sum) |
| 31 | |
| 32 | def gqa_attention_decode( |
| 33 | q: UnsafePointer[Float32, MutExternalOrigin], |
| 34 | k_cache: UnsafePointer[Float32, MutExternalOrigin], |
| 35 | v_cache: UnsafePointer[Float32, MutExternalOrigin], |
| 36 | out_ptr: UnsafePointer[Float32, MutExternalOrigin], |
| 37 | seq_len: Int, n_head: Int, n_kv_head: Int, head_dim: Int): |
| 38 | """GQA attention for single-token decode. Pure Mojo, no cblas_sgemm.""" |
| 39 | @extern("malloc") |
| 40 | def _alc(sz: Int64) abi("C") -> Int64: ... |
| 41 | var gqa_rep = n_head // n_kv_head |
| 42 | var scale = 1.0 / math.sqrt(Float32(head_dim)) |
| 43 | var kv_stride = n_kv_head * head_dim |
| 44 | var scores = UnsafePointer[Float32, MutExternalOrigin](unsafe_from_address=Int(_alc(Int64(n_head * seq_len * 4)))) |
| 45 | |
| 46 | def head_worker(kv_h: Int) capturing -> None: |
| 47 | var qh = q + kv_h * gqa_rep * head_dim |
| 48 | var kh = k_cache + kv_h * head_dim |
| 49 | var vh = v_cache + kv_h * head_dim |
| 50 | for qi in range(gqa_rep): |
| 51 | var q_off = qi * head_dim; var h_idx = kv_h * gqa_rep + qi |
| 52 | # scores[seq_len] = Q[HD] @ K[seq_len, HD]^T * scale |
| 53 | for t in range(seq_len): |
| 54 | var acc: Float32 = 0.0 |
| 55 | for d in range(head_dim): |
| 56 | acc += qh.load(q_off + d) * k_cache.load(kv_h * head_dim + t * kv_stride + d) |
| 57 | scores.store(h_idx * seq_len + t, acc * scale) |
| 58 | # Softmax |
| 59 | softmax(scores + h_idx * seq_len, seq_len) |
| 60 | # out[HD] = scores[seq_len] @ V[seq_len, HD] |
| 61 | for d in range(head_dim): |
| 62 | var acc: Float32 = 0.0 |
| 63 | for t in range(seq_len): |
| 64 | acc += (scores + h_idx * seq_len).load(t) * v_cache.load(kv_h * head_dim + t * kv_stride + d) |
| 65 | out_ptr.store(h_idx * head_dim + d, acc) |
| 66 | parallelize[func=head_worker](num_work_items=n_kv_head) |