Skip to main content
This page provides detailed technical information about the Phoenix transformer architecture, including the critical attention masking mechanism that enables candidate isolation.

Transformer Configuration

Phoenix uses a standard decoder-only transformer with several key architectural choices:
Source: 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:
This creates batch composition effects: the score for Cand2 depends on whether Cand1 is in the batch.

The Solution: Custom Attention Mask

Phoenix implements a specialized attention mask using make_recsys_attn_mask:
Source: 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:
Source: grok.py:516-586
The candidate_start_offset parameter controls the masking behavior:
  • None: Standard causal mask (for language modeling or retrieval user tower)
  • int: Custom candidate isolation mask (for ranking)

Transformer Layer Components

1. Multi-Head Attention

Phoenix uses Rotary Position Embeddings (RoPE) for position-aware attention:
Source: grok.py:264-363
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-261
Phoenix applies tanh capping to attention logits for numerical stability:
This prevents extremely large logits that can cause softmax overflow.
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:
Source: grok.py:414-440 The FFN size is computed to be a multiple of 8 for hardware efficiency:
Source: grok.py:32-36

3. Layer Normalization

Phoenix uses RMS Normalization (Root Mean Square Layer Normalization):
Source: 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:
Source: grok.py:444-497
Phoenix applies layer norm before and after each sub-layer, which differs from standard “Pre-LN” transformers. This “sandwich” normalization improves training stability.

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

Phoenix uses bfloat16 for forward pass computation:
This reduces memory usage and increases throughput on modern accelerators (TPUs, A100s) while maintaining numerical stability.
Using fewer K/V heads than Q heads reduces the memory bandwidth bottleneck during inference, leading to faster decoding.
FFN dimensions are rounded to multiples of 8 for optimal hardware utilization on GPUs and TPUs.
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

User and history tokens use standard bidirectional causal attention. No candidates in this model.

Ranking Model

Candidates isolated from each other but can attend to user + history.

grok.py

Core transformer implementation
  • Transformer class
  • make_recsys_attn_mask function
  • Attention and FFN layers

recsys_model.py

Phoenix ranking model
  • PhoenixModel class
  • Input embedding construction
  • Multi-action output projection

recsys_retrieval_model.py

Phoenix retrieval model
  • PhoenixRetrievalModel class
  • 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