Architectures / Recommendation
๐ DLRM
Meta's Deep Learning Recommendation Model โ bottom MLP for dense, embedding for sparse, feature interaction, top MLP
From Naumov et al. (2019). Deep Learning Recommendation Model for Personalization and Recommendation Systems. This page is the graph, not the PDF: open it, edit it, verify it, export it.
Every number on this page is computed from the graph by the same functions the app runs, not written by hand.
When to pick it
Structure
14 layers. Output shapes are propagated from the input shape, batch dimension excluded.
| Layer | Type | Parameters | Output shape | |
|---|---|---|---|---|
| 1 | Dense Features | Input | shape=[13] | 13 |
| 2 | Bottom MLP 1 | Linear | outFeatures=64, inFeatures=13 | 64 |
| 3 | ReLU | ReLU | 64 | |
| 4 | Bottom MLP 2 | Linear | outFeatures=64, inFeatures=64 | 64 |
| 5 | Sparse Features | Input | shape=[26] | 26 |
| 6 | EmbeddingBag | EmbeddingBag | vocabSize=1000000 | 26 ร 64 |
| 7 | Feature Interaction | Feature Interaction | 415 | |
| 8 | Top MLP 1 | Linear | outFeatures=512, inFeatures=415 | 512 |
| 9 | ReLU | ReLU | 512 | |
| 10 | Top MLP 2 | Linear | outFeatures=256, inFeatures=512 | 256 |
| 11 | ReLU | ReLU | 256 | |
| 12 | CTR Head | Linear | outFeatures=1, inFeatures=256 | 1 |
| 13 | Sigmoid | Sigmoid | 1 | |
| 14 | P(click) | Output | 1 |
What the verifier says
The same 43 single-graph checks that run on every edit in the app, on this graph.
output-activation
vanishing-gradient
deep-no-norm
init-activation-mismatch
The PyTorch it exports
Generated from the graph above. First 46 lines; the app exports the whole file, plus the training loop, the data contract and a deploy bundle.
# Architecture designed with Neurarch: https://neurarch.com
# PyTorch: compatible with Python 3.8+ and torch>=1.12
# Colab: pip install torch torchvision (usually pre-installed)
import torch
import torch.nn as nn
import torch.nn.functional as F
from typing import Tuple
class DLRM(nn.Module):
def __init__(self):
super().__init__()
self.linear_1 = nn.Linear(13, 64)
self.linear_2 = nn.Linear(64, 64)
self.embeddingBag_1 = nn.Embedding(1000000, 64)
self.linear_3 = nn.Linear(415, 512)
self.linear_4 = nn.Linear(512, 256)
self.linear_5 = nn.Linear(256, 1)
@staticmethod
def _dlrm_interact(dense, emb):
"""DLRM feature interaction: pairwise dots over [dense] + embedding rows,
concatenated back onto dense. Returns [B, D + C(N+1, 2)]."""
if emb.dim() == 2:
emb = emb.unsqueeze(0)
if dense.dim() == 1:
dense = dense.unsqueeze(0)
v = torch.cat([dense.unsqueeze(1), emb], dim=1) # [B, N+1, D]
z = torch.bmm(v, v.transpose(1, 2)) # [B, N+1, N+1]
i, j = torch.triu_indices(z.size(1), z.size(2), offset=1)
return torch.cat([dense, z[:, i, j]], dim=-1)
def forward(self, src, tgt=None):
# Dense Features shape: [13]
# Sparse Features shape: [26]
linear_ot_fc1 = self.linear_1(src)
relu__relu1 = F.relu(linear_ot_fc1)
linear_ot_fc2 = self.linear_2(relu__relu1)
embedding_bag_ed_bag = self.embeddingBag_1(tgt)
feature_interaction_teract = self._dlrm_interact(linear_ot_fc2, embedding_bag_ed_bag)
linear_op_fc1 = self.linear_3(feature_interaction_teract)
relu__relu1 = F.relu(linear_op_fc1)
linear_op_fc2 = self.linear_4(relu__relu1)
relu__relu2 = F.relu(linear_op_fc2)
linear_op_out = self.linear_5(relu__relu2)
For agents
This architecture is machine-readable end to end. An agent can list the set, fetch this graph, edit it, and have the edit verified before any GPU time is spent.