# shinoharahare/llm-training architecture

> Architecture diagram of the GitHub repository shinoharahare/llm-training, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/shinoharahare/llm-training
- Repository: https://github.com/shinoharahare/llm-training

## Overview

##### Overview and Project Type
- This is a full-scale distributed training framework that targets LLMs such as LLaMA and Phi-3. It is designed to support various training modalities: pre-training, instruction tuning, DPO, and ORPO with additional support for tensor parallelism.
- The system leverages PyTorch Lightning for distributed training, and integrates configurations, model implementations, various data handlers, optimizer wrappers, LR schedulers, and custom ops.

##### Main Components
a. CLI/Command Interface:
- Located under “src/llm_training/cli” and “src/llm_training/lightning/cli”. This is the primary interface where the “llm-training fit” command gets invoked with a config file.
- Input handling, argument parsing, and initiation of the training pipeline.

b. Config Management:
- The “config” directory (and its example YAMLs) provides configuration files that control seeding, distributed strategy, hyper-parameters, model settings, data paths, etc.
- Representing how the user’s config file flows into the training setup and how it is consumed by various training modules.

c. Data Modules and Data Management:
- Under “src/llm_training/data” there are various data handlers for pre_training, instruction_tuning, preference_tuning, and dummy/hf_based datasets.
- How data modules and datacollators (for managing sequence packing and preventing cross-contamination) are responsible for feeding data to the model.
- Components for chat templates and dynamic data sampling to show that different configurations support various language model training scenarios.

d. Model Implementations:
- The “src/llm_training/models” directory holds abstract implementations (“base_model”) as well as concrete implementations for different architectures (e.g., LLaMA, Phi-3, HF Casual LM, HF Compat Model).

e. Training Engine & Distributed Setup:
- The training loop and orchestration reside in the “lightning” subdirectories. Include the CLI that invokes the Lightning trainer, the callbacks (for checkpointing, progress, redirection, etc.), and the distributed strategies like DeepSpeed and FSDP2.
- LR schedulers (“src/llm_training/lr_schedulers”) and optimizer wrappers (e.g., for maintaining full-precision parameters in mixed-precision training).

f. Low-Level Operations & Custom Ops:
- Under “src/llm_training/ops”, include the custom operations such as attention, cross entropy, rope operations, and the “liger_kernel” which provides optimized variants.
- As helper modules that plug into the model’s forward/backward passes to accelerate or customize specific computations.

g. Utilities and Logging:
- The “utils” and “metrics” directories support additional functionalities like logging (e.g., Wandb integration), metrics calculation (tokens, perplexity), and context management.
- These can be shown as peripheral components that monitor and report performance during training.

##### Relationships and Interactions
▪ User calls the training command via the CLI.
▪ The CLI loads the user-defined config (YAML file from config examples) and initializes the training pipeline.
▪ The training engine (Lightning Trainer) coordinates with the appropriate data modules, model providers, optimizers, LR schedulers, and custom ops.
▪ During training, callbacks log progress, maintain checkpoints, and handle adaptive strategies such as tensor parallelism.
▪ Finally, scripts (e.g., convert_to_hf.py) illustrate how trained models are exported to Hugging Face formats.

##### Architectural Patterns and Design Principles
- The design is highly modular, following a separation-of-concerns approach:
▪ Each training method (pre-training, instruction tuning, preference tuning, DPO, ORPO) is handled in distinct namespaces with their own data and training logic.
▪ Reinforce that the modular design enhances extensibility (e.g., supporting multiple model architectures and tuning methods).
- Usage of custom ops and distributed training strategies integrated with a well-known framework (Lightning), which fulfills scalability, extensibility, and performance efficiency.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global nodes
    User["User"]:::cli

    %% Frontend Interfaces
    subgraph "Frontend Interfaces"
        CLI["LLM-Training CLI"]:::cli
        LCLI["Lightning CLI"]:::cli
        Config["Config Files"]:::config
    end

    %% Data Handling
    subgraph "Data Handling"
        Pre["Data: Pre-Training"]:::data
        Instr["Data: Instruction Tuning"]:::data
        Pref["Data: Preference Tuning"]:::data
        Dummy["Data: Dummy Dataset"]:::data
        HF["Data: HF-based"]:::data
        Chat["Chat Templates"]:::data
    end

    %% Model Implementations & Custom Ops
    subgraph "Model & Ops"
        Base["Abstract Base Model"]:::model
        Llama["LLaMA Model"]:::model
        Phi["Phi-3 Model"]:::model
        HFCasl["HF Causal LM"]:::model
        HFCompat["HF Compat Model"]:::model
        Provider["Model Provider Abstraction"]:::model
        Ops["Custom Ops (Attention, CrossEntropy, Rope, etc.)"]:::ops
        Liger["Liger Kernel Ops"]:::ops
    end

    %% Training Engine & Distributed Setup
    subgraph "Training Engine"
        Trainer["Lightning Trainer"]:::train
        Callbacks["Callbacks (Checkpointing, Redirection)"]:::train
        Dist["Distributed Strategies (DeepSpeed, FSDP2)"]:::train
        LRS["LR Schedulers"]:::train
        Optim["Optimizer Wrappers"]:::train
    end

    %% Utilities & Logging
    subgraph "Utilities & Logging"
        Utils["Utilities"]:::utils
        Logger["Logging (Wandb)"]:::utils
        Metrics["Metrics Calculation"]:::utils
    end

    %% Output Integration
    Export["Model Export (Conversion to HF)"]:::train

    %% Relationships and Data Flow
    User -->|"invokes"| CLI
    CLI -->|"triggers"| LCLI
    CLI -->|"reads"| Config
    LCLI -->|"initiates"| Trainer
    Config -->|"configures"| Trainer

    Pre -->|"feeds data"| Trainer
    Instr -->|"feeds data"| Trainer
    Pref -->|"feeds data"| Trainer
    Dummy -->|"feeds data"| Trainer
    HF -->|"feeds data"| Trainer
    Chat -->|"provides templates"| Trainer

    Provider -->|"selects model"| Base
    Base -->|"implements"| Llama
    Base -->|"implements"| Phi
    Base -->|"implements"| HFCasl
    Base -->|"implements"| HFCompat
    Trainer -->|"loads model"| Provider

    Trainer -->|"executes callbacks"| Callbacks
    Trainer -->|"applies"| Dist
    Trainer -->|"schedules LR"| LRS
    Trainer -->|"wraps optimizers"| Optim

    Trainer -->|"logs progress"| Logger
    Trainer -->|"utilizes"| Utils
    Trainer -->|"monitors"| Metrics
    Trainer -->|"exports"| Export

    Ops -->|"integrates with"| Base
    Liger -->|"optimizes ops for"| Base

    %% Click events for CLI/Command Interface
    click CLI "https://github.com/shinoharahare/llm-training/blob/main/src/llm_training/cli/main.py"
    click LCLI "https://github.com/shinoharahare/llm-training/blob/main/src/llm_training/lightning/cli/cli.py"
    click LCLI "https://github.com/shinoharahare/llm-training/blob/main/src/llm_training/lightning/cli/trainer.py"
    click LCLI "https://github.com/shinoharahare/llm-training/blob/main/src/llm_training/lightning/cli/utils.py"

    %% Click event for Config Management
    click Config "https://github.com/shinoharahare/llm-training/tree/main/config"

    %% Click events for Data Modules
    click Pre "https://github.com/shinoharahare/llm-training/tree/main/src/llm_training/data/pre_training"
    click Instr "https://github.com/shinoharahare/llm-training/tree/main/src/llm_training/data/instruction_tuning"
    click Pref "https://github.com/shinoharahare/llm-training/tree/main/src/llm_training/data/preference_tuning"
    click Dummy "https://github.com/shinoharahare/llm-training/tree/main/src/llm_training/data/dummy"
    click HF "https://github.com/shinoharahare/llm-training/tree/main/src/llm_training/data/hf_based"
    click Chat "https://github.com/shinoharahare/llm-training/tree/main/src/llm_training/data/chat_templates"

    %% Click events for Model Implementations
    click Base "https://github.com/shinoharahare/llm-training/tree/main/src/llm_training/models/base_model"
    click Llama "https://github.com/shinoharahare/llm-training/tree/main/src/llm_training/models/llama"
    click Phi "https://github.com/shinoharahare/llm-training/tree/main/src/llm_training/models/phi3"
    click HFCasl "https://github.com/shinoharahare/llm-training/tree/main/src/llm_training/models/hf_causal_lm"
    click HFCompat "https://github.com/shinoharahare/llm-training/tree/main/src/llm_training/models/hf_compat_model"
    click Provider "https://github.com/shinoharahare/llm-training/blob/main/src/llm_training/lms/model_provider.py"

    %% Click events for Training Engine & Distributed Setup
    click Trainer "https://github.com/shinoharahare/llm-training/tree/main/src/llm_training/lightning"
    click Callbacks "https://github.com/shinoharahare/llm-training/tree/main/src/llm_training/lightning/callbacks"
    click Dist "https://github.com/shinoharahare/llm-training/tree/main/src/llm_training/lightning/strategy/deepspeed"
    click Dist "https://github.com/shinoharahare/llm-training/tree/main/src/llm_training/lightning/strategy/fsdp2"
    click LRS "https://github.com/shinoharahare/llm-training/tree/main/src/llm_training/lr_schedulers"
    click Optim "https://github.com/shinoharahare/llm-training/tree/main/src/llm_training/optim"

    %% Click events for Utilities and Logging
    click Utils "https://github.com/shinoharahare/llm-training/tree/main/src/llm_training/utils"
    click Logger "https://github.com/shinoharahare/llm-training/tree/main/src/llm_training/lightning/loggers"
    click Metrics "https://github.com/shinoharahare/llm-training/tree/main/src/llm_training/metrics"

    %% Styling Classes
    classDef cli fill:#ADD8E6,stroke:#000,stroke-width:1px;
    classDef config fill:#90EE90,stroke:#000,stroke-width:1px;
    classDef data fill:#FFFFE0,stroke:#000,stroke-width:1px;
    classDef model fill:#FFB6C1,stroke:#000,stroke-width:1px;
    classDef train fill:#FFA07A,stroke:#000,stroke-width:1px;
    classDef ops fill:#DDA0DD,stroke:#000,stroke-width:1px;
    classDef utils fill:#E6E6FA,stroke:#000,stroke-width:1px;
```
