Transformer Configuration
Phoenix uses a standard decoder-only transformer with several key architectural choices:grok.py:88-100
Grouped Query Attention: Phoenix uses
num_q_heads > num_kv_heads, which reduces memory usage and increases inference speed while maintaining model quality.Attention Masking for Candidate Isolation
The Problem
In standard transformer inference for sequences, each position can attend to all previous positions (causal masking). For recommendation ranking, this would allow candidates to see each other:The Solution: Custom Attention Mask
Phoenix implements a specialized attention mask usingmake_recsys_attn_mask:
grok.py:39-71
Attention Mask Visualization
Here’s the full attention pattern for Phoenix ranking:Key Insight: The bottom-right block (candidate-to-candidate attention) is diagonal only. Each candidate can attend to:
- ✓ User embedding
- ✓ All history positions
- ✓ Itself (self-attention)
- ✗ Other candidates
Transformer Forward Pass
The transformer applies the custom mask during attention computation:grok.py:516-586
Transformer Layer Components
1. Multi-Head Attention
Phoenix uses Rotary Position Embeddings (RoPE) for position-aware attention:grok.py:264-363
Rotary Position Embeddings
Rotary Position Embeddings
RoPE encodes position information by rotating the query and key vectors:This provides relative position information without adding explicit position embeddings.Source:
grok.py:205-261Attention Capping
Attention Capping
Phoenix applies tanh capping to attention logits for numerical stability:This prevents extremely large logits that can cause softmax overflow.
Grouped Query Attention
Grouped Query Attention
Instead of creating separate K/V heads for each Q head, Phoenix uses fewer K/V heads and reuses them:
- Reduces memory usage by ~50% (with 2:1 ratio)
- Faster inference due to fewer key-value computations
- Minimal quality degradation
2. Feed-Forward Network
Phoenix uses SwiGLU activation in the FFN:grok.py:414-440
The FFN size is computed to be a multiple of 8 for hardware efficiency:
grok.py:32-36
3. Layer Normalization
Phoenix uses RMS Normalization (Root Mean Square Layer Normalization):grok.py:162-194
RMSNorm is simpler and faster than LayerNorm because it doesn’t compute the mean (only the RMS). This provides similar normalization benefits with lower computational cost.
4. Decoder Layer Structure
Each transformer layer follows this pattern:grok.py:444-497
JAX and Haiku Implementation
Phoenix is implemented using:- JAX: For automatic differentiation and XLA compilation
- Haiku: For neural network modules and parameter management
Key Benefits
JAX
- Automatic differentiation
- XLA compilation for TPU/GPU
- Functional transformations (vmap, pmap)
- Efficient numerical computing
Haiku
- Clean module abstraction
- Explicit parameter management
- JAX-native design
- Transformer utilities (RMSNorm, etc.)
Example Usage
Performance Optimizations
bfloat16 Precision
bfloat16 Precision
Phoenix uses This reduces memory usage and increases throughput on modern accelerators (TPUs, A100s) while maintaining numerical stability.
bfloat16 for forward pass computation:Grouped Query Attention
Grouped Query Attention
Using fewer K/V heads than Q heads reduces the memory bandwidth bottleneck during inference, leading to faster decoding.
Efficient FFN Sizing
Efficient FFN Sizing
FFN dimensions are rounded to multiples of 8 for optimal hardware utilization on GPUs and TPUs.
XLA Compilation
XLA Compilation
JAX compiles the model to XLA, which performs aggressive optimizations like:
- Operator fusion
- Memory layout optimization
- Automatic parallelization
Comparison: Retrieval vs Ranking Masking
Retrieval User Tower
Ranking Model
Related Files
grok.py
Core transformer implementation
Transformerclassmake_recsys_attn_maskfunction- Attention and FFN layers
recsys_model.py
Phoenix ranking model
PhoenixModelclass- Input embedding construction
- Multi-action output projection
recsys_retrieval_model.py
Phoenix retrieval model
PhoenixRetrievalModelclass- User and candidate towers
- Similarity search
Next Steps
Overview
Return to Phoenix system overview
Ranking Model
Learn about the ranking model implementation
Retrieval Model
Explore the two-tower retrieval architecture