Megatron-Bridge
AGENTS.md976 starsChanged 22 days agoApache-2.0View on GitHub
Project: PyTorch-native bridge between Hugging Face and Megatron-Core. Bidirectional checkpoint conversion, pretraining, SFT, and LoRA recipes with optimized NVIDIA GPU performance. Package: megatron.bridge (Python 3.12). The skills/ directory contains structured guides for common tasks (adding models, running experiments, debugging multi-node jobs, performance tuning, etc.). Always read the relevant SKILL.md before starting any task it covers — skills are mandatory context, not optional background reading. Workflow — mandatory order for every task: 1. Pull information first. Read the commit, PR, error…
- Reads credentials
We split this AGENTS.md into the rules it gives an agent
18 rules. About 0 are common, 7 are distinctive, and 11 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈7
Distinctive · estimate
This author's own choices. Worth a look.
11
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 15
Pull information first: Read the commit, PR, error log, file, or whatever artifact the task is about.
- Distinctive: line 19
Answer or implement: Only after the skill is loaded, use its context to reason, diagnose, or write code.
- Distinctive: line 22
Never skip or reorder these steps.
- Distinctive: line 29
Run the full test suite — run only the specific tests relevant to your change
- Distinctive: line 30
Add required (non-optional) dependencies — use optional extras.
- Very specific: line 31
Commit secrets, tokens, .env files, or environment-specific paths / account names (e.g.
- Very specific: line 32
Use bare print() — use logging.getLogger(__name__) or print_rank_0()
- Very specific: line 35
Before adding any new dependency to pyproject.toml
- Very specific: line 36
Before modifying CI workflows (.github/workflows/)
- Very specific: line 37
Before changing public API signatures in models/conversion/
- Very specific: line 40
Run uv run pre-commit run --all-files before committing
- Very specific: line 41
Add NVIDIA copyright headers to new Python files (except under tests/)
- Very specific: line 43
Use uv run python -m pytest and uv run python -m torch.distributed.run, not bare pytest / torchrun
- Distinctive: line 44
Use the current year (2026) in generated content — do not default to 2025 or any past year
- Very specific: line 67
Use * separator for functions with multiple same-type parameters
- Distinctive: line 68
No arbitrary defaults for config values — be explicit
- Very specific: line 78
Commit and PR title format (Conventional Commits: <type>(<scope>): <description>)
- Very specific: line 107
Use scripts/switch_mcore.sh to switch versions.
≈ “Common” is an estimate: at least 1 in 5 of the 36,368 AGENTS.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
AGENTS.md — Megatron Bridge
Project: PyTorch-native bridge between Hugging Face and Megatron-Core. Bidirectional checkpoint conversion, pretraining, SFT, and LoRA recipes with optimized NVIDIA GPU performance. Package:
megatron.bridge(Python 3.12).
Skills
The skills/ directory contains structured guides for common tasks (adding
models, running experiments, debugging multi-node jobs, performance tuning,
etc.). Always read the relevant SKILL.md before starting any task it
covers — skills are mandatory context, not optional background reading.
Workflow — mandatory order for every task:
- Pull information first. Read the commit, PR, error log, file, or whatever artifact the task is about. Do not reason about it yet.
- Select and invoke the skill. Based on what you just read, identify the relevant skill and invoke it before forming any answer or plan.
- Answer or implement. Only after the skill is loaded, use its context to reason, diagnose, or write code.
Never skip or reorder these steps. Do not wait for the user to name the right skill keyword — infer it from the artifact you read.
Boundaries
NEVER:
- Modify files inside
3rdparty/Megatron-LM/— changes go through the upstream repo - Run the full test suite — run only the specific tests relevant to your change
- Add required (non-optional) dependencies — use optional extras; submit dependency changes as a separate PR
- Commit secrets, tokens,
.envfiles, or environment-specific paths / account names (e.g./home/yuya/…, usernames, cluster hostnames) - Use bare
print()— uselogging.getLogger(__name__)orprint_rank_0()
ASK FIRST:
- Before adding any new dependency to
pyproject.toml - Before modifying CI workflows (
.github/workflows/) - Before changing public API signatures in
models/conversion/
ALWAYS:
- Run
uv run pre-commit run --all-filesbefore committing - Add NVIDIA copyright headers to new Python files (except under
tests/) - Sign off commits:
git commit -s -m "message" - Use
uv run python -m pytestanduv run python -m torch.distributed.run, not barepytest/torchrun - Use the current year (2026) in generated content — do not default to 2025 or any past year
Toolchain
| Action | Command | Config |
| Install deps | uv sync | pyproject.toml, uv.lock |
| Install dev tools | uv sync --group dev | |
| Lint + format | uv run pre-commit run --all-files | ruff.toml, .pre-commit-config.yaml |
| Unit tests | uv run python -m pytest tests/unit_tests/ | pyproject.toml [tool.pytest] |
| Distributed run | uv run python -m torch.distributed.run --nproc_per_node=2 script.py | |
| Type check | uv run mypy --strict path/to/file.py | |
| Regen lock file | uv lock | |
| Init submodule | git submodule update --init | .gitmodules |
Code Style
Lint and format are enforced by pre-commit hooks (ruff). See @ruff.toml for the authoritative rules. For judgment calls not covered by tooling, see @skills/linting-and-formatting/SKILL.md. Key points the linter cannot catch:
- Type hints required on all public API functions (
X | None, notOptional[X]) - Google-style docstrings on public classes and functions
- Use
*separator for functions with multiple same-type parameters - No arbitrary defaults for config values — be explicit
Testing
- No foreign
setattron config dataclasses in tests. When a test applies overrides to a recipe config viasetattr(config_obj, key, value), always guard withif not hasattr(config_obj, key): raise ValueError(...)first. Setting an attribute that does not exist on the dataclass silently creates a phantom field — the test passes but the recipe would fail for a real user who never sets that key. This applies to all override patterns (model_overrides,checkpoint_overrides,config_overrides, etc.) intests/functional_tests/.
Contributing
See @CONTRIBUTING.md for the full contributor guide, including:
- Commit and PR title format (Conventional Commits:
<type>(<scope>): <description>) - PR labeling taxonomy (type, area, state, risk labels)
- Testing conventions (unit preferred, functional max 2 GPUs, L0/L1/L2 tiers)
- Dependency management policy
- DCO sign-off requirements
- CI triggering (
/ok to test <commit-SHA>)
Architecture
Bridge Pattern
Each supported model family lives under src/megatron/bridge/models/<family>/ with:
| File | Role |
bridge.py | HF ↔ Megatron conversion logic |
config_mapping.py | Maps HF config → Megatron config |
param_mapping.py | Maps parameter names between formats |
hf_pretrained/ | HF model definition provider |
Recipes live in src/megatron/bridge/recipes/<family>/.
AutoBridge (models/conversion/auto_bridge.py) auto-selects the correct
bridge from a HF model name or path.
Megatron-Core Submodule
3rdparty/Megatron-LM/ is a git submodule pinned to a specific commit,
installed as an editable package via [tool.uv.sources] in pyproject.toml.
Use scripts/switch_mcore.sh to switch versions.
Tool Compatibility
This file is the single source of agent instructions. For Claude Code
compatibility, create a symlink: ln -s AGENTS.md CLAUDE.md
Where files are headingNew AGENTS.md files have more rules about keeping secrets out of the code, and fewer about code style.
Of the AGENTS.md files we read, the 28,693 first seen in the last six months, against the older ones: the kind of rule that gained most share, and the kind that lost most. About files like this in general, not this one, and an estimate, since a kind is a pattern in the words.
In this repository35 other files it gives its agents.
- adding-model-supportSkill
- build-and-dependencySkill
- bump-dependencySkill
- cicdSkill
- create-model-verification-cardSkill
- linting-and-formattingSkill
- nemo-mbridge-memory-snapshot-analysisSkill
- nemo-mbridge-mlm-bridge-trainingSkill
- nemo-mbridge-multi-node-slurmSkill
- nemo-mbridge-perf-activation-recomputeSkill
- nemo-mbridge-perf-cpu-offloadingSkill
- nemo-mbridge-perf-cuda-graphsSkill
- nemo-mbridge-perf-expert-parallel-overlapSkill
- nemo-mbridge-perf-hierarchical-context-parallelSkill
- nemo-mbridge-perf-megatron-fsdpSkill
- nemo-mbridge-perf-memory-tuningSkill
- nemo-mbridge-perf-moe-comm-overlapSkill
- nemo-mbridge-perf-moe-dispatcher-selectionSkill
- nemo-mbridge-perf-moe-hardware-configsSkill
- nemo-mbridge-perf-moe-long-contextSkill
- nemo-mbridge-perf-moe-optimization-workflowSkill
- nemo-mbridge-perf-moe-vlm-trainingSkill
- nemo-mbridge-perf-nsys-analysisSkill
- nemo-mbridge-perf-parallelism-strategiesSkill
- nemo-mbridge-perf-sequence-packingSkill
- nemo-mbridge-perf-tp-dp-comm-overlapSkill
- nemo-mbridge-perf-vision-dp-over-cpSkill
- nemo-mbridge-recipe-recommenderSkill
- nemo-mbridge-resiliencySkill
- nemo-rl-e2e-testingSkill
- parity-testingSkill
- pr-reviewSkill
- review-prSkill
- testingSkill
- verl-e2e-testingSkill
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

