# pytorch/torchrec architecture

> Architecture diagram of the GitHub repository pytorch/torchrec, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/pytorch/torchrec
- Repository: https://github.com/pytorch/torchrec

## Overview

##### High-Level Components
First, organize the main components into these major sections:

a) Core Components:
- Sparse Operations (jagged_tensor)
- Embedding Modules
- Distributed Training Infrastructure
- Model Parallel Components
- Optimization Components

b) Supporting Systems:
- FBGEMM Integration (optimized kernels)
- PyTorch Foundation Layer
- GPU/CPU Execution Environment

##### Key Architectural Layers

Create distinct layers showing:

a) Model Layer:
- Models (dlrm, deepfm)
- Modules (embedding_modules, mlp, crossnet)
- Metrics (accuracy, auc, recall)

b) Distribution Layer:
- Sharding Components
- Planner System
- Model Parallel Infrastructure
- Communication Primitives

c) Data Layer:
- Sparse Features Handling
- Dataset Management
- Data Loading Pipeline

##### Integration Points

Connections between:
- Embedding tables and sharding strategies
- Distribution system and hardware resources
- Training pipeline and optimization components

##### Specific Implementation Details

- Different sharding strategies (DP, RW, TW, CW)
- Quantization support
- Inference optimization paths
- Pipeline stages (data loading, device transfer, computation)

##### Flow Directions

Show:
- Training data flow
- Model parallel distribution paths
- Optimization feedback loops
- Inference paths

##### Component Relationships

Highlight:
- How embedding tables are sharded
- How the planner interacts with sharding
- How the pipeline stages overlap
- Integration with PyTorch ecosystem

Key Considerations:

##### Scale
- How the system handles large embedding tables
- Multi-GPU/multi-node capabilities
- Demonstrate sharding flexibility

##### Performance Features
- Pipelined operations
- Optimization paths
- Quantization support

##### Integration Points
- PyTorch integration
- FBGEMM optimization
- External system connections

##### Practical Usage
- Training workflows
- Inference paths
- Development interfaces

Remember to include:
- Version compatibility notes
- Hardware requirements
- Key performance optimization points
- Critical path flows

This will create a comprehensive yet clear representation of TorchRec's architecture that highlights its capabilities in handling large-scale recommendation systems while showing how it integrates with the broader PyTorch ecosystem.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Core["TorchRec Core"]
        SO["Sparse Operations"]:::core
        EM["Embedding Modules"]:::core
        DTI["Distributed Training Infrastructure"]:::core
        MP["Model Parallel Components"]:::core
        OC["Optimization Components"]:::core
    end

    subgraph Supporting["Supporting Systems"]
        FB["FBGEMM Integration"]:::support
        PT["PyTorch Foundation"]:::support
        EX["GPU/CPU Execution"]:::support
    end

    subgraph ModelLayer["Model Layer"]
        Models["Models (DLRM, DeepFM)"]:::model
        Modules["Core Modules"]:::model
        Metrics["Metrics System"]:::model
    end

    subgraph DistLayer["Distribution Layer"]
        Sharding["Sharding Components"]:::dist
        Planner["Planner System"]:::dist
        ParallelInfra["Model Parallel Infrastructure"]:::dist
        CommPrim["Communication Primitives"]:::dist
    end

    subgraph DataLayer["Data Layer"]
        SparseFeatures["Sparse Features Handling"]:::data
        DatasetMgmt["Dataset Management"]:::data
        DataPipeline["Data Loading Pipeline"]:::data
    end

    subgraph ShardingStrategies["Sharding Strategies"]
        DP["Data Parallel"]:::shard
        RW["Row-Wise"]:::shard
        TW["Table-Wise"]:::shard
        CW["Column-Wise"]:::shard
    end

    %% Core Connections
    SO --> EM
    EM --> DTI
    DTI --> MP
    MP --> OC

    %% Supporting System Connections
    FB --> SO
    PT --> Core
    EX --> Core

    %% Layer Connections
    ModelLayer --> Core
    DistLayer --> Core
    DataLayer --> Core

    %% Sharding Strategy Connections
    ShardingStrategies --> Sharding

    %% Cross-Layer Connections
    DatasetMgmt --> Models
    Sharding --> ParallelInfra
    Planner --> ShardingStrategies

    %% Style Definitions
    classDef core fill:#3498db,stroke:#2980b9,color:white
    classDef support fill:#95a5a6,stroke:#7f8c8d,color:white
    classDef model fill:#e74c3c,stroke:#c0392b,color:white
    classDef dist fill:#f39c12,stroke:#d35400,color:white
    classDef data fill:#2ecc71,stroke:#27ae60,color:white
    classDef shard fill:#9b59b6,stroke:#8e44ad,color:white

    %% Click Events
    click SO "https://github.com/pytorch/torchrec/blob/main/torchrec/sparse/jagged_tensor.py"
    click EM "https://github.com/pytorch/torchrec/blob/main/torchrec/modules/embedding_modules.py"
    click DTI "https://github.com/pytorch/torchrec/tree/main/torchrec/distributed/"
    click Models "https://github.com/pytorch/torchrec/blob/main/torchrec/models/dlrm.py"
    click OC "https://github.com/pytorch/torchrec/tree/main/torchrec/optim/"
    click Metrics "https://github.com/pytorch/torchrec/tree/main/torchrec/metrics/"
    click Planner "https://github.com/pytorch/torchrec/tree/main/torchrec/distributed/planner/"
    click DatasetMgmt "https://github.com/pytorch/torchrec/tree/main/torchrec/datasets/"
    click Modules "https://github.com/pytorch/torchrec/blob/main/torchrec/modules/mlp.py"
    click Sharding "https://github.com/pytorch/torchrec/tree/main/torchrec/distributed/sharding/"
    click FB "https://github.com/pytorch/torchrec/blob/main/torchrec/distributed/fbgemm_qcomm_codec.py"
```
