~/fba-lab/lab/qwen-c/09-attention

FBALab

Code · architecture · study mode

AboutRoadmapSpeedrun
FBALab

Study mode — no GPU required.

Interactive LLM training & inference lab.

Qwen CAboutContactTermsPrivacyCookiesCommunity

© 2026 FBA Lab

Contact · contact@bubblspace.com · +91 75061 55016

INFERENCE SIMULATION

Qwen3 Pure C — 9 · Attention & memory

running
◷run_viz.c▸Query, Key, Value◎learner⌁09-attention$no GPU
weights
Load the model
The 4 GB weight file is mapped into memory in milliseconds. No copying — the OS pages in only what inference needs.
run_viz.c · stage 01–02
load
runtime
Run inference
Your prompt flows through 28 layers of attention and computation, one token at a time.
stage 03–04
forward
output
Generate text
The model scores all 151,936 possible next words and picks one. Then repeats.
stage 05–06
1/3
Blocks
Quick summary

For each token the model builds three vectors — a Query (what am I looking for?), and Keys/Values (what does the past contain, and what should it pass on?).

Full explanation below the code →

fba-lab — run_viz.c · Query, Key, Valueexecuting
// block: Query, Key, Value · lines 363–367$ study run_viz.c --block attn_qkvFor each token the model builds three vectors — a Query (what am I looking for?), and Keys/Values (w… ✓
Explanation

Attention lets a token look back at every earlier token and decide *how much to care* about each one. For the token being processed right now, the model has already produced three projections (the Q/K/V matmuls from the previous stage):

- Query (Q) — "what am I looking for?" — the vector for the current token (line 365). - Key (K) — "what do I contain?" — one per past token, stored in the KV cache. - Value (V) — "what information do I pass on?" — also one per past token, in the cache.

The intuition (from the attention primer): treat the query as a search, score it against every key by similarity, softmax those scores into weights, then take the weighted average of the values. A query that matches a key strongly pulls in that key's value.

In the code, line 363 loops over the 16 heads (each head attends independently), line 365 grabs this head's query vector, and line 367 points at this head's row of the attention score buffer att. Everything that follows fills and uses that row.

Why it matters

"Query" here means the attention Q vector for a token position — not a user's question. Each token position produces exactly one query; one token being decoded ⇒ one query vector, which is precisely why decode attention is vector-shaped.

Think about

During decode of a single new token, how many query vectors exist for one head — and how many keys/values does that query attend over?

// architecture

Live diagram

100%
Q1 × dnew tokenK cacheT × dV cacheT × done query · many keys/valuesscores = Q · Kᵀ / √d[ s0 s1 s2 … sT ] (1 × T)÷ √head_dim keeps softmax stable1 query row ⇒ vector × matrix ⇒ GEMVsoftmax → weighted Σ Vout = Σ softmax(s)·V (1 × d)The memory wallEvery token re-reads the FULL K and V cache from VRAM — × 16 heads × 28 layers.Cheap math, huge memory traffic ⇒ memory-bound. FlashAttention tiles K/V through SRAM.
← 8 · Every op a kernel10 · Prefill vs decode →
Step 9 of 10Skill: Inference Systems
01Blueprint02Load weights03Buffers04Forward pass05Tokenizer06Generate loop07GEMV, not GEMM08Every op a kernel09Attention & memory10Prefill vs decode