Skip to content

Commit 50c80c0

Browse files
committed
Demonstrates very basic example of how to use a DLRM for making predictions using TorchRec capabilities. (pytorch#3043)
Summary: Pull Request resolved: pytorch#3043 examples demonstrating how to use TorchRec for recommendation model predictions: * **DLRM (Deep Learning Recommendation Model)**: Implementation using TorchRec's EmbeddingBagCollection and KeyedJaggedTensor * **Two Tower Model**: Alternative architecture for recommendation systems Both examples include complete implementations with training, evaluation, and prediction capabilities. Differential Revision: D75989524
1 parent c8495ec commit 50c80c0

File tree

8 files changed

+2404
-0
lines changed

8 files changed

+2404
-0
lines changed

examples/prediction/__init__.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) Meta Platforms, Inc. and affiliates.
3+
# All rights reserved.
4+
#
5+
# This source code is licensed under the BSD-style license found in the
6+
# LICENSE file in the root directory of this source tree.
7+
8+
# pyre-strict
9+
10+
"""
11+
TorchRec recommendation model examples.
12+
13+
This package contains examples of different recommendation models implemented using TorchRec:
14+
- DLRM (Deep Learning Recommendation Model)
15+
- Two-Tower Model
16+
17+
Each model is organized in its own subdirectory with complete implementation, tests, and documentation.
18+
"""
19+
20+
# Import main components from DLRM
21+
from torchrec.github.examples.prediction.dlrm.predict_using_dlrm import (
22+
create_kjt_from_batch,
23+
DLRMRatingWrapper,
24+
RecommendationDataset as DLRMDataset,
25+
TorchRecDLRM,
26+
)
27+
28+
# Import main components from Two-Tower
29+
from torchrec.github.examples.prediction.twoTower.predict_using_twotower import (
30+
create_kjt_from_ids,
31+
RecommendationDataset as TwoTowerDataset,
32+
TwoTowerModel,
33+
TwoTowerRatingWrapper,
34+
)
35+
36+
__all__ = [
37+
# DLRM components
38+
"DLRMRatingWrapper",
39+
"DLRMDataset",
40+
"TorchRecDLRM",
41+
"create_kjt_from_batch",
42+
# Two-Tower components
43+
"TwoTowerModel",
44+
"TwoTowerRatingWrapper",
45+
"TwoTowerDataset",
46+
"create_kjt_from_ids",
47+
]

examples/prediction/dlrm/README.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# DLRM Prediction Example
2+
3+
This example demonstrates how to use a Deep Learning Recommendation Model (DLRM) for making predictions using TorchRec capabilities. The code includes:
4+
5+
1. A DLRM implementation using TorchRec's EmbeddingBagCollection and KeyedJaggedTensor
6+
2. Training with random data
7+
3. Evaluation
8+
4. Making sample predictions
9+
10+
## DLRM Architecture
11+
12+
The Deep Learning Recommendation Model (DLRM) is a state-of-the-art architecture for recommendation systems that combines:
13+
14+
- **Bottom MLP**: Processes dense features
15+
- **Embedding Tables**: Process sparse features
16+
- **Feature Interaction**: Computes dot products between embeddings and dense features
17+
- **Top MLP**: Processes the combined features to produce predictions
18+
19+
This architecture is particularly effective for CTR prediction and ranking tasks in recommendation systems.
20+
21+
## TorchRec Integration
22+
23+
This implementation uses TorchRec's capabilities:
24+
- Uses `KeyedJaggedTensor` for sparse features
25+
- Uses `EmbeddingBagCollection` for embedding tables
26+
- Follows the DLRM architecture as described in the paper: https://arxiv.org/abs/1906.00091
27+
28+
The example demonstrates how to leverage TorchRec's efficient sparse feature handling for recommendation models.
29+
30+
## Dependencies
31+
32+
Install the required dependencies:
33+
34+
```bash
35+
# Install PyTorch
36+
pip install torch torchvision
37+
38+
# Install NumPy
39+
pip install numpy
40+
41+
# Install TorchRec
42+
pip install torchrec
43+
```
44+
45+
**Important**: This implementation requires torchrec to run, as it uses TorchRec's specialized modules for recommendation systems.
46+
47+
## Running the Example Locally
48+
49+
1. Download the `predict_using_dlrm.py` file to your local machine.
50+
51+
2. Run the example:
52+
53+
```bash
54+
python3 predict_using_dlrm.py
55+
```
56+
57+
3. If you're using a different Python environment:
58+
59+
```bash
60+
# For conda environments
61+
conda activate your_environment_name
62+
python predict_using_dlrm.py
63+
64+
# For virtual environments
65+
source your_venv/bin/activate
66+
python predict_using_dlrm.py
67+
```
68+
69+
## What to Expect
70+
71+
When you run the example, you'll see:
72+
73+
1. Training progress for 10 epochs with loss and learning rate information
74+
2. Evaluation results showing MSE and RMSE metrics
75+
3. Sample predictions for a specific user on multiple items
76+
77+
## Implementation Details
78+
79+
This example uses TorchRec's capabilities to implement a DLRM model that:
80+
81+
- Takes dense features and sparse features (as KeyedJaggedTensor) as input
82+
- Processes dense features through a bottom MLP
83+
- Processes sparse features through EmbeddingBagCollection
84+
- Computes feature interactions using dot products
85+
- Processes the interactions through a top MLP
86+
- Outputs rating predictions on a 0-5 scale
87+
88+
The implementation demonstrates how to use TorchRec's specialized modules for recommendation systems, making it more efficient and scalable than a custom implementation.
89+
90+
## Key TorchRec Components Used
91+
92+
1. **KeyedJaggedTensor**: Efficiently represents sparse features with variable lengths
93+
2. **EmbeddingBagConfig**: Configures embedding tables with parameters like dimensions and feature names
94+
3. **EmbeddingBagCollection**: Manages multiple embedding tables for different categorical features
95+
96+
## Comparison with Two-Tower Model
97+
98+
While both DLRM and Two-Tower models are used for recommendation systems, they have different architectures and use cases:
99+
100+
- **DLRM**: Combines multiple categorical features and dense features with feature interactions, suitable for CTR prediction and ranking tasks.
101+
- **Two-Tower**: Separates user and item processing into distinct towers, making it more suitable for retrieval tasks and large-scale recommendation systems.
102+
103+
## Troubleshooting
104+
105+
If you encounter any issues:
106+
107+
1. **Python version**: This code has been tested with Python 3.8+. Make sure you're using a compatible version.
108+
109+
2. **PyTorch and TorchRec installation**: If you have issues with PyTorch or TorchRec, try installing specific versions:
110+
```bash
111+
pip install torch==2.0.0 torchvision==0.15.0
112+
pip install torchrec==0.5.0
113+
```
114+
115+
3. **Memory issues**: If you run out of memory, try reducing the batch size by modifying this line in the code:
116+
```python
117+
batch_size = 256 # Try a smaller value like 64 or 32
118+
```
119+
120+
4. **CPU vs GPU**: The code automatically uses CUDA if available. To force CPU usage, modify:
121+
```python
122+
device = torch.device("cpu")
123+
```
124+
125+
5. **TorchRec compatibility**: If you encounter compatibility issues with TorchRec, make sure you're using compatible versions of PyTorch and TorchRec.

examples/prediction/dlrm/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) Meta Platforms, Inc. and affiliates.
3+
# All rights reserved.
4+
#
5+
# This source code is licensed under the BSD-style license found in the
6+
# LICENSE file in the root directory of this source tree.
7+
8+
# pyre-strict

0 commit comments

Comments
 (0)