This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
LoongForge is large-scale transformer training framework built on top of Megatron-LM (as a patched fork: Loong-Megatron) and TransformerEngine. It supports LLMs, VLMs (Vision-Language Models), VLAs (Vision-Language-Action Models), and Diffusion Models across both NVIDIA GPUs and Kunlun XPUs. Training phases supported: pretrain and SFT (supervised fine-tuning).
git clone --recurse-submodules https://github.com/baidu-baige/LoongForge.git
# COMPILE_ENV: ampere | hopper | blackwell
docker build --build-arg COMPILE_ENV=hopper --build-arg ENABLE_LEROBOT=false \
-t loongforge:latest -f ./LoongForge/docker/Dockerfile .# 1. Clone with Megatron submodule
git clone --recurse-submodules https://github.com/baidu-baige/LoongForge.git
cd LoongForge
# 2. Install LoongForge + dependencies
uv pip install -e ".[gpu]" # NVIDIA GPU
uv pip install -e ".[xpu]" # Kunlun XPU
# 3. Setup TransformerEngine (clone, patch, build)
python setup_env.py --te-tag v2.9Note: setup_env.py only handles TransformerEngine. Megatron-LM (Loong-Megatron) is a git submodule at third_party/Loong-Megatron, initialized via --recurse-submodules.
sh build.sh # Creates output tarballE2E tests use a custom YAML-driven framework (tests/main.py), not pytest.
# Download test datasets first
bash tests/download_datasets.sh
# Run default CI test suite (all models in tests/configs/)
bash tests/main_start.sh
# Run optional regression tests (tests/optional_configs/)
bash tests/main_start.sh --optionalEdit variables in tests/main_start.sh:
# Run one model from tests/configs/
model_names="qwen3_14b"
# Run one model from tests/optional_configs/
model_names="deepseek_v2/deepseek_v2_lite"
include_optional=true
# Run an entire model series from optional_configs/
model_names="NONE"
optional_subdir="internvl2.5"
include_optional=trueTest configs: tests/configs/ (CI suite) and tests/optional_configs/ (regression, organized by model family). Each YAML defines model params and multi-step scenarios (checkpoint conversion + training).
Training scripts use torchrun for distributed execution. The PYTHONPATH must include both Megatron-LM and LoongForge:
PYTHONPATH=$MEGATRON_PATH:$LOONGFORGE_PATH:$PYTHONPATH \
torchrun --nproc_per_node 8 --nnodes $NNODES ... \
$LOONGFORGE_PATH/loongforge/train.py \
--model-name <model-name> \
--training-phase pretrain|sft \
...Key arguments: --model-name (maps to config via config_map.py) or --config-file (direct YAML path), --training-phase (pretrain/sft).
train.py— Entry point. Callsparse_train_args()thenbuild_model_trainer(args).train().train/parser.py— Argument parsing: merges Megatron CLI args with Hydra YAML configs (OmegaConf). Supports--model-name(looked up inconfig_map.py) or--config-file.train/trainer_builder.py— Registry-based trainer dispatch.register_model_trainer(model_family, training_phase)decorator registers training functions per model family and phase.train/megatron_trainer.py—MegatronTrainerwraps model_provider, dataset_provider, and forward_step into Megatron'spretrain()loop.train/training_utils.py— Extended Megatron pretrain loop (heavily customized).train/arguments.py— LoongForge-specific extra CLI arguments added on top of Megatron's.train/validators.py— Validation logic for Megatron and LoongForge args.train/pretrain/— Pretrain implementations for LLM and VLM.train/sft/— SFT implementations for LLM, VLM, InternVL, ERNIE.train/custom/— Custom model trainers (e.g., WAN diffusion, Pi0.5 VLA).
factory.py— Model registry.register_model_config(family, arch)registers model configs;register_model_provider(family)registers model provider functions (accepts a single family string or list of families). Lookups:get_model_config(),get_model_provider(),get_model_family().dispatch.py— Hardware-abstraction layer (MultiAccModules). Provides unified access to TransformerEngine or local linear/attention/norm implementations.foundation/— LLM backbone implementations: LLaMA, Qwen (all versions through Qwen3-Next), DeepSeek, InternLM, MiniMax, MIMO, GLM. Each defines a transformer spec and config dataclass.encoder/— Vision encoder implementations: base ViT, Qwen2-VL/3-VL, InternVL, LLaVA-OV, ERNIE-VL.omni_models/— Multi-modal model composition:OmniCombinationModelassembles encoder + projector + decoder into a unified pipeline, withmodel_chunk_schedule_plan.pyfor pipeline parallelism scheduling.common/— Shared layers (local norms, projectors, etc.).custom/— Non-standard models (WAN diffusion, Pi0.5).peft/— Parameter-efficient fine-tuning (LoRA) support.
configs/models/<family>/<model>.yaml— Hydra/OmegaConf YAML configs defining model architecture params. The_target_field maps to a Python config dataclass (e.g.,loongforge.models.foundation.LLaMAConfig).configs/data/— Data configuration templates.loongforge/utils/config_map.py—MODEL_CONFIG_REGISTRYmaps--model-namestrings to{"config_path": ..., "config_name": ...}dicts. Contains 80+ model entries.
- SFT datasets with sharegpt/alpaca format support, multimodal data handling, data packing, DP load balancing.
mm_plugin.py— Multi-modal data plugin for processing images/video.dp_balance/— Data-parallel load balancing for packed sequences.
Primary entry point: tools/convert_checkpoint/module_convertor/model.py.
For LLM models (single step):
python tools/convert_checkpoint/module_convertor/model.py \
--load_platform=huggingface --save_platform=mcore \
--config_file=<yaml> --convert_file=<json> \
--tensor_model_parallel_size=N --pipeline_model_parallel_size=M \
--load_ckpt_path=<hf_path> --save_ckpt_path=<mcore_path>For VLM models (multi-step pipeline): convert language model, vision encoder, adapter/projector separately, then merge via tools/convert_checkpoint/mcore/merge_megatron.py.
Additional tools: merge_megatron_expert.py (MoE expert merging), FP8 conversion support (bf16↔fp8). Example scripts in examples/<model>/checkpoint_convert/.
Custom CUDA kernels: sparse_mla_fwd/, sparse_mla_bwd/ (sparse MLA attention), lightning_indexer_bwd/.
Shell scripts for each supported model family with pretrain/SFT/checkpoint-conversion configs. Pattern: examples/<model>/{pretrain,sft,checkpoint_convert}/.
Kunlun XPU training scripts, mirroring examples/ structure.
- Create a config dataclass in
loongforge/models/foundation/(orencoder/for vision), decorated with@register_model_config(family, arch). - Create a model provider function decorated with
@register_model_provider(family). - Register a trainer function with
@register_model_trainer(family, training_phase). - Add YAML config under
configs/models/<family>/. - Add entry in
loongforge/utils/config_map.pyMODEL_CONFIG_REGISTRY. - Add example launch scripts under
examples/<model>/.
CLI args + Hydra YAML config -> parse_train_args() -> merged args namespace -> build_model_trainer(args) dispatches to registered trainer (looks up model_family from Hydra config's model_type) -> MegatronTrainer.train() runs the Megatron pretrain loop.
Defined in loongforge/utils/constants.py. These classes (inheriting _BaseFamilies) drive dispatch logic throughout the codebase:
LanguageModelFamilies: llama, llama2, llama3, llama3.1, qwen, qwen1.5, qwen2, qwen2.5, qwen3, qwen3_next, deepseek, internlm2.5, minimax, mimo, glmVisionLanguageModelFamilies: qwen2_vl, qwen2_5_vl, qwen3_vl, llava_ov_1_5, vlm, intern_vl, ernie4_5_vl, qwen3_5, kimi_k2_5CustomModelFamilies: wan2_2_i2vVisionLanguageActionModelFamilies: pi05, groot_n1_6
Megatron-LM is managed as a git submodule (third_party/Loong-Megatron → baidu-baige/Loong-Megatron). TransformerEngine is cloned and patched by setup_env.py. LoongForge itself is a Python package (pyproject.toml, hatchling build backend).
patches/TransformerEngine_v2.9/ contains patch files applied to upstream TransformerEngine during setup. These implement LoongForge-specific optimizations and fixes.
When asked to review a pull request or a diff (e.g. via @claude review this PR), follow skills/code-review/SKILL.md exactly: its checklist, severity tags (🔴 Critical / 🟠 Major / 🟡 Minor / 🟢 Nit), and output format are the authoritative contract for review output.
Posting findings — prefer inline comments. For every finding tied to specific code, call the mcp__github_inline_comment__create_inline_comment tool to post it on the exact file:line (or startLine–line range) instead of listing it in the summary. Use the top-level summary comment ONLY for: the overall Verdict, the 2–4 sentence Summary, the Tests line, and the Checklist table. Each inline comment body should follow the same severity-tag format as the SKILL spec (🔴 Critical: ..., 🟠 Major: ..., etc.). Set confirmed: true only on real review findings — never on probes or self-tests.