# habanaai/megatron-lm architecture

> Architecture diagram of the GitHub repository habanaai/megatron-lm, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/habanaai/megatron-lm
- Repository: https://github.com/habanaai/megatron-lm

## Overview

##### Main Components
- Megatron-LM: Acts as the research framework that orchestrates training. It includes pretraining scripts (e.g., pretrain_bert.py, pretrain_gpt.py, etc.) along with tasks and experiment workflows.
- Megatron-Core: A library of composable building blocks (transformer layers, attention mechanisms, embedding modules, normalization, checkpointing, optimizer utilities, etc.) that are separately organized in a “core” directory and spread over subdirectories (like models, datasets, distributed, tensor_parallel, transformer, dist_checkpointing, etc.).
- Distributed Computation Infrastructure: Distributed training components are central to the design. They include modules for distributed data parallelism, tensor and pipeline parallelism, asynchronous checkpointing, and inter-node communication.
- Data Handling and Preprocessing: Modules and examples for processing datasets, building tokenizers (see the datasets directories and tasks/data_utils.py).
- Training and Inference Pipelines: Separate modules for training, fine-tuning (the tasks/ directory), and inference (megatron/inference and the examples/inference directory).
- Integration & Deployment Components: Dockerfiles (e.g., Dockerfile.ci, Dockerfile.linting), CI/CD pipelines (GitHub/GitLab workflows), and scripts to set environment variables.
- Testing and Validation: A large suite of unit and functional tests under various directories to verify behavior at both module and system levels.

##### Relationships and Data Flow
- User/Orchestrator and Environment Setup: Capture how users interact via Docker containers or custom environments (as mentioned in the README under Setup) and trigger distributed training jobs.
- Data Pipeline: Show data flows from raw source inputs (handled under tools/openwebtext, tasks, and datasets modules) into preprocessing, tokenization, and batching.
- Training Engine: At the heart, the training engine (Megatron-LM scripts) coordinates with Megatron-Core’s building blocks. Illustrate components such as:
- Model Architecture Subsystem (transformer blocks, attention, and embedding modules from megatron/core/models).
- Parallelism Modules – distributed data parallel, tensor parallel, and pipeline parallel systems (see megatron/core/distributed, tensor_parallel, pipeline_parallel).
- Optimizer and Scheduler: Modules for distributed optimization (optimizer, optimizer_param_scheduler, grad_scaler, etc.).
- Distributed Checkpointing: Illustrate how checkpointing flows are managed (using modules in megatron/core/dist_checkpointing).
- Inference and Post-Processing: Outline how the inference API (megatron/inference and accompanying CLI tools in tools/) uses the trained models.
- Testing and CI/CD: Include a section that briefly shows the testing infrastructure (under tests/ directories) and how automated workflows (GitLab CI and GitHub workflows) integrate with continuous deployment.

##### Architectural Patterns and Design Principles
- Extensibility: Note the presence of configurable tasks, pretraining scripts, and conversion tools (for Hugging Face checkpoints, for example) to allow future expansions.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User / Environment Setup
    UserEnv["User / Environment Setup:\nDocker, Shell Scripts, Env Variables"]:::userStyle

    %% Data Pipeline Cluster
    subgraph "Data Pipeline"
        direction TB
        DatasetsModule["Datasets Module\n(Ingestion, Tokenization)"]:::dataStyle
        DataPreprocessing["Data Preprocessing\n(Cleaning, Batching)"]:::dataStyle
        DatasetsModule --> DataPreprocessing
    end

    %% Training Engine Cluster
    subgraph "Training Engine"
        direction TB
        MegatronLM["Megatron-LM (Training Scripts)\n(pretrain_* scripts)"]:::trainingStyle
        MegatronCore["Megatron-Core (Core Library)\n(Transformers, Optimizers)"]:::trainingStyle
        MegatronLM -->|"uses"| MegatronCore
    end

    %% Distributed Infrastructure Cluster
    subgraph "Distributed Infrastructure"
        direction TB
        DDP["Distributed Data Parallel\n(Communication)"]:::distStyle
        Checkpointing["Distributed Checkpointing\n(Async Saving)"]:::distStyle
        DDP --> Checkpointing
    end

    %% Inference & Conversion Cluster
    subgraph "Inference & Conversion"
        direction TB
        Inference["Inference Pipeline\n(API, Conversion Tools)"]:::inferenceStyle
    end

    %% Testing & CI/CD Cluster
    subgraph "Testing & CI/CD"
        direction TB
        Testing["Testing Suite\n(Unit, Integration Tests)"]:::testStyle
    end

    %% Integration & Deployment Cluster
    subgraph "Integration & Deployment"
        direction TB
        Integration["Integration & Deployment\n(Docker, CI/CD Config)"]:::integrationStyle
    end

    %% Connect Clusters Vertically
    UserEnv --> DatasetsModule
    DataPreprocessing --> MegatronLM
    MegatronCore --> DDP
    Checkpointing --> Inference
    Inference --> Testing
    Testing --> Integration

    %% Click Events
    click DatasetsModule "https://github.com/habanaai/megatron-lm/blob/main/megatron/core/datasets/bert_dataset.py"
    click DataPreprocessing "https://github.com/habanaai/megatron-lm/blob/main/tasks/data_utils.py"
    click MegatronLM "https://github.com/habanaai/megatron-lm/blob/main/megatron/training/training.py"
    click MegatronCore "https://github.com/habanaai/megatron-lm/tree/main/megatron/core"
    click DDP "https://github.com/habanaai/megatron-lm/blob/main/megatron/core/distributed/distributed_data_parallel.py"
    click Checkpointing "https://github.com/habanaai/megatron-lm/tree/main/megatron/core/dist_checkpointing"
    click Inference "https://github.com/habanaai/megatron-lm/blob/main/examples/inference/gpt/simple_gpt_batch_inference.py"
    click Testing "https://github.com/habanaai/megatron-lm/tree/main/tests/"
    click Integration "https://github.com/habanaai/megatron-lm/blob/main/Dockerfile.ci"

    %% Styles
    classDef userStyle fill:#FFFAE6,stroke:#FFA500,stroke-width:2px;
    classDef dataStyle fill:#E6F7FF,stroke:#1E90FF,stroke-width:2px;
    classDef trainingStyle fill:#E6FFE6,stroke:#008000,stroke-width:2px;
    classDef distStyle fill:#F0E6FF,stroke:#800080,stroke-width:2px;
    classDef inferenceStyle fill:#FFF0E6,stroke:#FF8C00,stroke-width:2px;
    classDef testStyle fill:#FFE6E6,stroke:#FF0000,stroke-width:2px;
    classDef integrationStyle fill:#F0F0F0,stroke:#808080,stroke-width:2px;
```
