How shinoharahare/llm-training works
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.