# DLRM

> Meta's Deep Learning Recommendation Model — bottom MLP for dense, embedding for sparse, feature interaction, top MLP

Pick when you have substantial dense + sparse features and need a production-validated CTR baseline. Bare-bones — needs feature engineering.

- Category: Recommendation
- Layers: 14
- Parameters: 32.35M
- Input shape (batchless): 13
- Output shape: 32 × 1
- Verifier verdict: pass
- Graph JSON: https://neurarch.com/templates/dlrm/model.json
- Open on the canvas: https://neurarch.com/?template=dlrm

## Structure

| # | 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=32, inFeatures=64 | 32 |
| 5 | Sparse Features | Input | shape=[26] | 26 |
| 6 | EmbeddingBag | EmbeddingBag | vocabSize=1000000 | 26 × 32 |
| 7 | Feature Interaction | Feature Interaction |  | 32 × 64 |
| 8 | Top MLP 1 | Linear | outFeatures=512, inFeatures=415 | 32 × 512 |
| 9 | ReLU | ReLU |  | 32 × 512 |
| 10 | Top MLP 2 | Linear | outFeatures=256, inFeatures=512 | 32 × 256 |
| 11 | ReLU | ReLU |  | 32 × 256 |
| 12 | CTR Head | Linear | outFeatures=1, inFeatures=256 | 32 × 1 |
| 13 | Sigmoid | Sigmoid |  | 32 × 1 |
| 14 | P(click) | Output |  | 32 × 1 |

## Verifier findings

- **info** `output-activation` at `Sigmoid`: "Sigmoid" feeds directly into Output. PyTorch's nn.CrossEntropyLoss already applies log-softmax internally, an explicit Softmax causes double-application and degrades training stability. Fix: Remove Softmax/Sigmoid for training. Restore it in a separate inference wrapper or ONNX export.
- **info** `vanishing-gradient` at `Sigmoid`: Sigmoid saturates to [0,1] / [-1,1], and its gradient approaches zero for large inputs. In networks deeper than 5 layers, this halts learning in early layers. Fix: Use ReLU, GELU, or SiLU for hidden layers. Keep Sigmoid only at binary classification outputs; Tanh in specific contexts (GAN generators, LSTM gates).
- **info** `deep-no-norm`: 11 layers with no BatchNorm, LayerNorm, or GroupNorm. Without normalization, activations can explode or vanish across layers, causing slow or unstable training. Fix: Add BatchNorm after Conv2d (CV tasks), LayerNorm after attention/FFN (NLP/LLM), or GroupNorm for small batch sizes.
- **info** `init-activation-mismatch` at `CTR Head`: PyTorch initializes Linear/Conv with Kaiming (He) init, which is derived for ReLU-family activations. Feeding a saturating activation (sigmoid/tanh) from a He-initialized layer starts training in the saturated tails, shrinking early gradients. Fix: Initialize these layers with Xavier instead: nn.init.xavier_uniform_(w, gain=nn.init.calculate_gain("sigmoid"|"tanh")), or switch the activation to a ReLU-family one.

## Exported PyTorch (first 46 lines)

```python
# 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, 32)
        self.embeddingBag_1 = nn.EmbeddingBag(10000, 32, mode='mean')
        self.linear_3 = nn.Linear(415, 512)
        self.linear_4 = nn.Linear(512, 256)
        self.linear_5 = nn.Linear(256, 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 = linear_ot_fc2  # feature interaction: implement FM/DCN manually
        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)
        sigmoid_sig = torch.sigmoid(linear_op_out)
        # Output
        return sigmoid_sig


if __name__ == '__main__':
    model = DLRM()
    model.eval()

    src = torch.randint(0, 13, (1, 13))  # (batch, src_seq_len)
    tgt = torch.randint(0, 26, (1, 26))  # (batch, tgt_seq_len)
    with torch.no_grad():
        output = model(src, tgt)
```

## Machine access

- Every architecture: https://neurarch.com/a/index.json
- Verify a graph of your own: `POST https://www.neurarch.com/api/v1/check` (see https://neurarch.com/developer.html)
- MCP server, so an agent edits the graph with the checks in the loop: https://neurarch.com/docs/mcp.md
