Overview
ThePhoenixRetrievalModel implements a two-tower architecture for efficient retrieval of relevant posts. It uses the Phoenix transformer to encode user representations and a separate candidate tower for post embeddings, enabling fast approximate nearest neighbor (ANN) search.
Classes
PhoenixRetrievalModel
A two-tower retrieval model using the Phoenix transformer for user encoding.The underlying Grok transformer model for user encoding
Model configuration including embedding size and sequence lengths
Forward propagation data type for computation
Optional name for the model
PhoenixRetrievalModelConfig
Configuration for the Phoenix Retrieval Model. Uses the same transformer architecture as the Phoenix ranker for encoding user representations.Transformer architecture configuration
Embedding dimension size
Maximum length of user history sequence
Maximum number of candidate posts (used for batch processing)
Optional model name
Forward propagation data type
Hash configuration for multi-hash embeddings
Size of product surface vocabulary
CandidateTower
Candidate tower that projects post+author embeddings to a shared embedding space.Output embedding dimension
Optional name for the tower
Named Tuples
RetrievalOutput
Output of the retrieval model.L2-normalized user embedding [B, D]
Indices of top-k retrieved candidates [B, K]
Similarity scores for top-k candidates [B, K]
Methods
__call__
Batch containing hashes, actions, and product surfaces
Pre-looked-up embeddings from embedding tables
Normalized corpus candidate embeddings [N, D]
Number of candidates to retrieve
Optional mask for valid corpus entries [N]
Retrieval output containing user representation and top-k results
build_user_representation
Batch containing hashes, actions, and product surfaces
Pre-looked-up embeddings from embedding tables
L2-normalized user embedding [B, D]
Pre-normalization L2 norm [B, 1]
build_candidate_representation
Batch containing candidate hashes
Pre-looked-up embeddings containing candidate embeddings
L2-normalized candidate embeddings [B, C, D]
Valid candidate mask [B, C]
_retrieve_top_k
Normalized user embeddings [B, D]
Normalized corpus candidate embeddings [N, D]
Number of candidates to retrieve
Optional mask for valid corpus entries [N]
Indices of top-k candidates [B, K]
Similarity scores of top-k candidates [B, K]
CandidateTower Methods
__call__
Concatenated post and author embeddings. Shape: [B, C, num_hashes, D] or [B, num_hashes, D]
Normalized candidate representation. Shape: [B, C, D] or [B, D]
Architecture
The two-tower architecture consists of:-
User Tower: Encodes user features + history using the Phoenix transformer
- Processes user embeddings and interaction history
- Outputs a single L2-normalized user representation vector
-
Candidate Tower: Projects candidate embeddings to a shared space
- Two-layer MLP with SiLU activation
- Projects concatenated post+author embeddings
- Outputs L2-normalized candidate representations
-
Retrieval: Uses dot product similarity between normalized embeddings
- Enables efficient ANN search in production
- Returns top-k candidates based on similarity scores
Usage Notes
- Both user and candidate representations are L2-normalized
- Dot product similarity is used for retrieval (equivalent to cosine similarity for normalized vectors)
- The model is designed for efficient ANN search using libraries like FAISS or ScaNN
- Constants: EPS = 1e-12, INF = 1e12 for numerical stability