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 / SUMMARY.md

47 lines · 4051 bytes Raw Blame History Rendered
1## Goal
2- Profile and optimize a pure Mojo TinyLlama inference engine using Mojo performance features (SIMD, parallelize, FMA, batch inference).
3
4## Constraints & Preferences
5- Pure Mojo only (.mojo); exclude Python/C TurboEngine.
6- Profile before optimizing — let data decide.
7- f16 weight path is baseline; Q4_0/Q8_0 acceptable only if SIMD-vectorized decode beats f16 throughput.
8
9## Progress
10### Done
11- Benchmarked full scaling curve: B=1 (23.7 tok/s), B=2 (37.9 tok/s, 80% eff.), **B=4 (64.7 tok/s, 68% eff.)**, B=8 (84.1 tok/s, 44% eff.) — 50 gen tokens, sustained load.
12- Implemented **SIMD-vectorized GQA attention** (dot product + weighted sum inner loops, W=8): 10% total improvement.
13- Added **RoPE precompute** (HD/2 cos/sin per position, not per-head): eliminated ~101K pow() calls per B-set.
14- Rewrote both matmul functions with **explicit register accumulators** (no InlineArray spill): +6% (82.3 tok/s from 77.8).
15- All B=4 batch items produce **identical token sequences** vs single baseline ("2+2=" output matches exactly across items).
16- Benchmarked **Q4_0 matmul** (2048×2048, standalone): 213 µs vs f16's 161 µs — 32% slower. Scalar nibble extraction overhead outweighs 3.56× DRAM savings.
17- Identified **Q8_0 quantization** as better path: 1.88× compression, trivial SIMD decode (int8→float32 cast, no nibble extraction).
18- Implemented **Q8_0 matmul + weight converter** (`_mm_q8_batch`, `_mm_q8_2out_batch`), integrated into batched engine.
19- Fixed **h2f bitcast bug**: `Float32(UInt32)` in Mojo does **numeric conversion**, not bitcast. Correct form uses pointer aliasing (`alloc[UInt8](4)` → store UInt32 → read Float32 from same address). Original batch engine was correct (pointer-based); only the new Q8 file had the regression.
20- Fixed `f32_to_f16_bits` similarly (was reading f32 bits with numeric `UInt32(Float32)`).
21- Fixed Q8_0 in-place conversion (temp buffer for f16 row reads to avoid cross-row overlap).
22- Fixed Q8_0 sign encoding (uint8 with +128 offset + subtract 128 in matmul).
23- Fixed vocab parsing single-line for-loop bug (`off += plen` was inside loop body).
24- **Q8_0 B=4**: **88.1 tok/s** (+13% over f16's 77.8 tok/s). Conversion overhead: ~9.7s one-time.
25- All prior benchmarks valid — both `tinyllama_gen.mojo` and `tinyllama_gen_batch.mojo` already used correct pointer-based `h2f`.
26
27### In Progress
28- (none)
29
30### Known Issues
31- Q8_0 quantization error accumulation over 22 layers degrades output quality (garbled text). Performance gains are valid but accuracy is reduced vs f16.
32
33## Key Decisions
34- **B=4 is the sweet spot** for batch inference — 3.28× cumulative throughput over single with 68% scaling efficiency. B=8 gives only +30% more cumulative tok/s.
35- **Q4_0 deferred** — scalar nibble extraction makes decode 32% slower than f16. Without native shuffle intrinsics (vpshufb), SIMD interleave is infeasible in Mojo.
36- **Q8_0 implemented** — 1.88× compression with 13% throughput improvement (less than ideal because f16 already has per-weight `h2f` decode overhead, and Q8_0's extra arithmetic (uint8→float32, sub 128, mul scale) partially offsets bandwidth savings).
37- `Float32(UInt32)` does **numeric conversion**, not bitcast — all bitcasts must use pointer aliasing or `SIMD.reinterpret()`.
38- `comptime if` replaces deprecated `@parameter if` for compile-time dispatch.
39- `Int(-49.5)` truncates toward zero in Mojo (same as Python).
40
41## Relevant Files
42- `src/mojollama/mojo_engine/tinyllama_gen_batch.mojo`: Main batched inference engine (B=4, SIMD attention, explicit-register matmuls, correctness verification).
43- `src/mojollama/mojo_engine/tinyllama_gen.mojo`: Original single-token baseline (f16, max_gen=10).
44- `src/mojollama/mojo_engine/tinyllama_gen_q8.mojo`: Q8_0 quantized weights + batch B=4 (88.1 tok/s).
45- `src/mojollama/mojo_engine/bench_q4.mojo`: Q4_0 vs f16 standalone matmul benchmark.
46- `src/mojollama/mojo_engine/build_batch.sh` / `build_q8.sh`: Build scripts.
47- `src/mojollama/mojo_engine/bench_batch.sh`: Automation for B value scaling.