Transformer Block #
Implements a complete transformer layer combining attention and feed-forward network.
Architecture #
Input
│
├─────────────────────────┐
│ │
▼ │
[RMSNorm] │ Pre-attention normalization
│ │
▼ │
[Multi-Head Attention] │ Self-attention mechanism
│ │
▼ │
[Add] ←─────────────────────┘ Residual connection
│
├─────────────────────────┐
│ │
▼ │
[RMSNorm] │ Pre-FFN normalization
│ │
▼ │
[FFN: Gate + Up/Down] │ Feed-forward network
│ │
▼ │
[Add] ←─────────────────────┘ Residual connection
│
▼
Output
Feed-Forward Network (FFN) #
BitNet uses a gated FFN similar to LLaMA:
gate = ReLU²(x @ W_gate) # Gating signal (BitNet uses relu_sqr, NOT silu)
up = x @ W_up # Value signal
hidden = gate × up # Element-wise gating
output = hidden @ W_down # Down-projection
Why gating?
- Non-linear mixing of features
- Selective information flow
- Better gradient properties than simple ReLU
Dimensions (BitNet-3B):
Input: [batch, seq, 2560]
Gate: [2560, 10240] (4× expansion)
Up: [2560, 10240]
Down: [10240, 2560] (back to original)
Memory Layout #
Per-layer weights (TQ2_0 quantized):
Attention:
- W_q, W_k, W_v: 3 × 2560² × 0.25 bytes = 4.9 MB
- W_o: 2560² × 0.25 bytes = 1.6 MB
FFN:
- W_gate, W_up: 2 × (2560 × 10240) × 0.25 bytes = 13.1 MB
- W_down: (10240 × 2560) × 0.25 bytes = 6.5 MB
RMSNorm:
- attn_norm, ffn_norm: 2 × 2560 × 4 bytes = 20 KB
Total: ~26 MB per layer
32 layers: ~832 MB
Performance #
Compute per token (BitNet-3B):
Attention: 285 GFLOPS
FFN:
- Gate projection: 52.4 GFLOPS
- Up projection: 52.4 GFLOPS
- SiLU + multiply: 0.05 GFLOPS
- Down projection: 104.8 GFLOPS
FFN Total: 209.7 GFLOPS
RMSNorm: 0.02 GFLOPS (negligible)
Total per layer: ~495 GFLOPS
References #
- LLaMA: https://github.com/facebookresearch/llama
- BitNet: https://arxiv.org/abs/2402.17764
- llama.cpp: llama.cpp (llm_load_tensors, llm_build_graph)
Configuration #
Equations
- One or more equations did not get rendered due to their size.
Instances For
Pre-allocated Buffers #
Pre-allocated buffers for transformer block forward pass. Avoids ~21 GPU buffer allocations per layer per token (9 block + 12 attention).
- normedBuf : BufT
- attnOutBuf : BufT
- residual1Buf : BufT
- normed2Buf : BufT
- gateBuf : BufT
- upBuf : BufT
- ffnOutBuf : BufT
- ffnNormedBuf : BufT
- rmsTempBuf : BufT
- attnBufs : Attention.AttentionBuffers BufT
Instances For
Create pre-allocated buffers for one transformer block forward pass
Equations
- One or more equations did not get rendered due to their size.
Instances For
Layer Structure #
Complete transformer block
- config : Config
- attnNorm : RMSNorm.RMSNorm BufT CacheT
- attention : Attention.Attention BufT CacheT KernelT
- attnSubNorm : RMSNorm.RMSNorm BufT CacheT
- ffnNorm : RMSNorm.RMSNorm BufT CacheT
- ffnGate : BitLinear.BitLinear BufT CacheT KernelT
- ffnUp : BitLinear.BitLinear BufT CacheT KernelT
- ffnSubNorm : RMSNorm.RMSNorm BufT CacheT
- ffnDown : BitLinear.BitLinear BufT CacheT KernelT
Instances For
Layer Creation #
Create transformer block from GGUF tensors
@param device WebGPU device @param config Block configuration @param attnNormData Pre-attention RMSNorm scale parameters @param ffnNormData Pre-FFN RMSNorm scale parameters @param attnWeights Attention layer weight data (Q, K, V, O + scales) @param ffnGateData FFN gate projection weights + scales @param ffnUpData FFN up projection weights + scales @param ffnDownData FFN down projection weights + scales
Equations
- One or more equations did not get rendered due to their size.
Instances For
Create transformer block with pre-built BitLinear layers
@param device WebGPU device @param config Block configuration @param attnNormData Pre-attention RMSNorm scale parameters @param ffnNormData Pre-FFN RMSNorm scale parameters @param attnWeights Already-created Attention layer @param ffnGateLayer Already-created FFN gate BitLinear @param ffnUpLayer Already-created FFN up BitLinear @param ffnDownLayer Already-created FFN down BitLinear
Equations
- One or more equations did not get rendered due to their size.
Instances For
Forward Pass #
Execute transformer block forward pass
Algorithm:
# Attention sub-layer
1. normed = RMSNorm(input)
2. attn_out = MultiHeadAttention(normed)
3. residual1 = input + attn_out # Residual connection
# FFN sub-layer
4. normed2 = RMSNorm(residual1)
5. gate = W_gate @ normed2
6. up = W_up @ normed2
7. hidden = ReLU²(gate) × up # Gated activation (BitNet uses relu_sqr)
8. ffn_out = W_down @ hidden
9. residual2 = residual1 + ffn_out # Residual connection
return residual2
@param device WebGPU device @param block Transformer block @param inputBuf Input tensor [batch, seq_len, dim] @param outputBuf Output tensor [batch, seq_len, dim] @param batchSize Batch size @param seqLen Sequence length
Equations
- One or more equations did not get rendered due to their size.
Instances For
Pre-allocated Buffers for Cached Inference #
Pre-allocated buffers for single-token forward pass with KV cache
- normedBuf : BufT
- attnOutBuf : BufT
- residual1Buf : BufT
- normed2Buf : BufT
- gateBuf : BufT
- upBuf : BufT
- ffnOutBuf : BufT
- ffnNormedBuf : BufT
- rmsTempBuf : BufT
- attnBufs : Attention.CachedAttentionBuffers BufT CacheT
Instances For
Create pre-allocated buffers for cached single-token inference
Equations
- One or more equations did not get rendered due to their size.
Instances For
Per-Layer Fused PreparedDispatch Refs #
Create per-layer fused PreparedDispatch refs
Equations
- Hesper.Layers.TransformerBlock.createFusedLayerRefs = do let __do_lift ← Hesper.GPUBackend.newCacheRef pure { fusedGateUpRelu := __do_lift }
Instances For
Forward Pass with KV Cache #
Execute single-token transformer block forward pass with KV cache.
Same algorithm as forward but uses cached attention for O(1) per token.
All dimensions are for single token (batchSize=1, seqLen=1).
@param pos Current token position (0-indexed) @param kvCache KV cache for this layer's attention @param fusedRefs Per-layer fused PreparedDispatch refs (optional)
Equations
- One or more equations did not get rendered due to their size.
Instances For
Cached forward pass WITH LoRA corrections on attention Q/V.
Same as forwardWithCache but uses Attention.forwardWithCacheLoRA
to inject LoRA before RoPE.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Integration with GGUF #
Create transformer block from GGUF file
Loads all tensors for a specific transformer layer.
Example tensor names in GGUF:
blk.{idx}.attn_norm.weight- Pre-attention RMSNormblk.{idx}.attn_q.weight- Attention Q projectionblk.{idx}.attn_k.weight- Attention K projectionblk.{idx}.attn_v.weight- Attention V projectionblk.{idx}.attn_output.weight- Attention output projectionblk.{idx}.ffn_norm.weight- Pre-FFN RMSNormblk.{idx}.ffn_gate.weight- FFN gate projectionblk.{idx}.ffn_up.weight- FFN up projectionblk.{idx}.ffn_down.weight- FFN down projection
@param device WebGPU device @param gguf Loaded GGUF file @param config Block configuration
Equations
- One or more equations did not get rendered due to their size.