# nvidia-nemo/rl architecture

> Architecture diagram of the GitHub repository nvidia-nemo/rl, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-10-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nvidia-nemo/rl
- Repository: https://github.com/nvidia-nemo/rl

## Overview

##### Project type and purpose
- NeMo RL is an open-source post-training reinforcement-learning library for large language and vision-language models.
- It provides modular RL algorithms (GRPO, SFT, DPO, RM, distillation), multiple training/generation backends (DTensor, Megatron Core, vLLM), and a Ray-based distributed runner.

##### File structure
- Top-level directories:
- nemo_rl/: core Python package with subpackages (algorithms, data, distributed, environments, evals, experience, models, utils).
- examples/: entry-point scripts + YAML configs demonstrating common workflows.
- docs/: detailed design, API docs, user guides.
- 3rdparty/: Git submodules for Automodel, Megatron-Bridge, Megatron-LM, Penguin.
- tools/: helper scripts (config CLI, converters, build scripts).
- docker/: container definitions.
- tests/: unit and functional test suites.
- CI & workflow configurations (.github/, dockerignore, etc.).

##### README for additional insights
- Modular design: plug-in backends chosen via YAML config or CLI.
- Ray for actor-based distributed training.
- Seamless integration with PyTorch, Hugging Face, WandB, and container support.

- CLI & Example Runner
– uv run / python examples/*.py
– Config loader (pyproject.toml + YAML)
- Orchestrator
– Core “RL Runner”
– Reads config → initializes modules → launches training/eval
- Core Library Modules
– Algorithms (GRPO, DPO, SFT, RM, distillation)
– Data (datasets, collate, packing, chat templates)
– Environments (game, math, reward-model, VLM tools)
– Models (policy, generation backends vLLM & Megatron, HuggingFace, DTensor)
– Distributed (Ray actors, worker groups, collectives, virtual clusters)
– Experience (rollouts, replay buffers)
– Evaluator (parsing, metrics)
– Utils (checkpointing, logging, config, timers, nvml)
- Backends & Infrastructure
– Training: PyTorch–DTensor or Megatron Core
– Generation: vLLM or Megatron inference
– Ray cluster (local or Slurm/Kubernetes via ray.sub)
– External services: Hugging Face Hub, WandB
– Storage: local FS or network‐attached for checkpoints and logs
- CLI → Orchestrator: “start job with config”
- Orchestrator → Core Modules: “instantiate Algorithm, Data, Model, Env, Distributed”
- Orchestrator → Ray: “spawn actor pool”
- Ray Actors ↔ Environments: “rollout episodes”
- Ray Actors → Algorithms: “compute loss & policy update”
- Algorithms → Models: “forward/backward” via DTensor or Megatron
- Models → Generation Backend: “sample sequences”
- Orchestrator → Checkpointing: “save/load weights & config”
- Orchestrator → Logger: “push metrics to WandB or local logs”
- Example Scripts → Converters/Tools: optional pre/post-processing
c. Architectural patterns & principles:
- Plugin/Strategy pattern for backends (training & generation)
- Modular separation of concerns (algorithms vs. data vs. models)
- Actor model (Ray) for distributed scalability
- Config-driven orchestration (YAML + uv CLI)
- Environment isolation per actor (process boundaries)
d. Key technologies, frameworks, libraries:
- Python, PyTorch, Ray
- DTensor, Megatron Core, vLLM
- Hugging Face Hub
- WandB or other logger backends
- Docker & containerization

##### Detail
- Both training and generation flows.
- How configuration and environment isolation enable reproducibility and scaling.
- Don’t hesitate to show as many components as needed—this is a complex, highly modular system.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Interface Layer
    subgraph "Interface Layer"
        direction TB
        CLI["CLI & Example Runner"]:::interface
        ConfigCLI["Config Loader (tools/config_cli.py)"]:::interface
    end

    %% Orchestrator & Core
    subgraph "Orchestrator & Core Modules"
        direction TB
        Orchestrator["RL Runner (Orchestrator)"]:::orchestrator
        Logger["Logger"]:::orchestrator
        Checkpoint["Checkpointing"]:::orchestrator
        subgraph "Algorithms"
            direction TB
            GRPO["GRPO"]:::orchestrator
            SFT["SFT"]:::orchestrator
            DPO["DPO"]:::orchestrator
            RM["RM"]:::orchestrator
            Distill["Distillation"]:::orchestrator
        end
        subgraph "Data"
            direction TB
            Datasets["Datasets"]:::orchestrator
            Collate["Collate & Packing"]:::orchestrator
            ChatTemplates["Chat Templates"]:::orchestrator
        end
        subgraph "Environments"
            direction TB
            MathEnv["Math Environment"]:::orchestrator
            GameEnv["Game Environment"]:::orchestrator
            RMEnv["Reward-Model Env"]:::orchestrator
            VLMEnv["VLM Tools Env"]:::orchestrator
        end
        subgraph "Models"
            direction TB
            DTensorTrain["DTensor Training"]:::distributed
            MegatronTrain["Megatron Core Training"]:::distributed
            HFModel["HuggingFace Models"]:::orchestrator
            subgraph "Generation Backends"
                direction TB
                vLLM["vLLM"]:::distributed
            end
        end
        subgraph "Distributed"
            direction TB
            RayActors["Ray Actor Pool"]:::distributed
            WorkerGroups["Worker Groups"]:::distributed
            Collectives["Collectives"]:::distributed
            VirtualCluster["Virtual Clusters"]:::distributed
        end
        Experience["Experience (Rollouts)"]:::orchestrator
        Evaluator["Evaluator"]:::orchestrator
        Utils["Utils"]:::orchestrator
    end

    %% External & Storage
    subgraph "Backends & Infrastructure"
        direction TB
        PyTorch["PyTorch"]:::distributed
        RayCluster["Ray Cluster"]:::distributed
        HFHub["Hugging Face Hub"]:::external
        WandB["WandB"]:::external
        Storage["Storage"]:::external
    end

    %% Connections
    CLI -->|"start job with config"| Orchestrator
    ConfigCLI -->|loads YAML| Orchestrator
    Orchestrator -->|"instantiate modules"| GRPO & SFT & DPO & RM & Distill & Datasets & Collate & ChatTemplates & MathEnv & GameEnv & RMEnv & VLMEnv & HFModel & DTensorTrain & MegatronTrain & RayActors & Experience & Evaluator & Utils
    Orchestrator -->|"spawn actor pool"| RayCluster
    RayCluster -->|spawns| RayActors
    RayActors -->|"rollout episodes"| MathEnv & GameEnv & RMEnv & VLMEnv
    RayActors -->|"compute loss & update"| GRPO & SFT & DPO & RM & Distill
    GRPO & SFT & DPO & RM & Distill -->|"forward/backward"| DTensorTrain & MegatronTrain
    DTensorTrain & MegatronTrain -->|"sample sequences"| vLLM
    Orchestrator -->|"save/load"| Checkpoint
    Orchestrator -->|"push metrics"| Logger
    Logger -->|sends to| WandB
    Checkpoint -->|writes to| Storage
    HFModel -->|pull/push| HFHub

    %% Click events
    click CLI "https://github.com/nvidia-nemo/rl/blob/main/examples/run_grpo_math.py"
    click CLI "https://github.com/nvidia-nemo/rl/blob/main/examples/run_dpo.py"
    click CLI "https://github.com/nvidia-nemo/rl/blob/main/examples/run_sft.py"
    click CLI "https://github.com/nvidia-nemo/rl/blob/main/examples/run_eval.py"
    click ConfigCLI "https://github.com/nvidia-nemo/rl/blob/main/tools/config_cli.py"
    click Orchestrator "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/utils/config.py"
    click Logger "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/utils/logger.py"
    click Checkpoint "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/utils/checkpoint.py"
    click Checkpoint "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/utils/native_checkpoint.py"
    click GRPO "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/algorithms/grpo.py"
    click SFT "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/algorithms/sft.py"
    click DPO "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/algorithms/dpo.py"
    click RM "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/algorithms/rm.py"
    click Distill "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/algorithms/distillation.py"
    click Datasets "https://github.com/nvidia-nemo/rl/tree/main/nemo_rl/data/datasets/"
    click Collate "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/data/collate_fn.py"
    click Collate "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/data/packing/algorithms.py"
    click Collate "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/data/packing/metrics.py"
    click ChatTemplates "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/data/chat_templates.py"
    click MathEnv "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/environments/math_environment.py"
    click GameEnv "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/environments/games/sliding_puzzle.py"
    click RMEnv "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/environments/reward_model_environment.py"
    click VLMEnv "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/environments/vlm_environment.py"
    click DTensorTrain "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/models/dtensor/parallelize.py"
    click DTensorTrain "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/models/policy/dtensor_policy_worker.py"
    click DTensorTrain "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/models/policy/dtensor_policy_worker_v2.py"
    click MegatronTrain "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/models/megatron/common.py"
    click MegatronTrain "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/models/policy/megatron_policy_worker.py"
    click HFModel "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/models/huggingface/common.py"
    click vLLM "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/models/generation/vllm/vllm_backend.py"
    click vLLM "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/models/generation/vllm/vllm_generation.py"
    click vLLM "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/models/generation/vllm/vllm_worker.py"
    click vLLM "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/models/generation/vllm/vllm_worker_async.py"
    click RayActors "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/distributed/worker_groups.py"
    click WorkerGroups "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/distributed/worker_group_utils.py"
    click Collectives "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/distributed/collectives.py"
    click VirtualCluster "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/distributed/virtual_cluster.py"
    click Experience "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/experience/rollouts.py"
    click Evaluator "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/evals/eval.py"
    click Evaluator "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/evals/answer_parsing.py"
    click Utils "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/utils/timer.py"
    click Utils "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/utils/nvml.py"
    click Utils "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/utils/flops_tracker.py"
    click Utils "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/utils/automodel_checkpoint.py"
    click Utils "https://github.com/nvidia-nemo/rl/blob/main/nemo_rl/utils/config.py"
    click RayCluster "https://github.com/nvidia-nemo/rl/blob/main/ray.sub"
    click HFHub "https://github.com/nvidia-nemo/rl/tree/main/3rdparty/Automodel-workspace/Automodel"
    click HFHub "https://github.com/nvidia-nemo/rl/tree/main/3rdparty/Megatron-Bridge-workspace/Megatron-Bridge"
    click HFHub "https://github.com/nvidia-nemo/rl/tree/main/3rdparty/Megatron-LM-workspace/Megatron-LM"
    click HFHub "https://github.com/nvidia-nemo/rl/tree/main/3rdparty/Penguin-workspace"
    click Storage "https://github.com/nvidia-nemo/rl/tree/main/docker/Dockerfile"
    click Storage "https://github.com/nvidia-nemo/rl/blob/main/docker/Dockerfile.ngc_pytorch"
    click Storage "https://github.com/nvidia-nemo/rl/blob/main/docker/README.md"

    %% Styles
    classDef interface fill:#cce5ff,stroke:#0366d6;
    classDef orchestrator fill:#d4edda,stroke:#155724;
    classDef distributed fill:#fff3cd,stroke:#856404;
    classDef external fill:#e2e3e5,stroke:#6c757d;
```
