Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
395aa1e
Add experimental Megatron Lite package
ISEEKYAN May 20, 2026
ee0db9f
Update experimental Megatron Lite package
ISEEKYAN Jun 5, 2026
0a8c82b
Add Megatron Lite LoRA acknowledgement
ISEEKYAN Jun 5, 2026
82625c4
Add runnable VERL Megatron Lite SFT example
ISEEKYAN Jun 5, 2026
ed99d27
Fix lite Qwen3.5 TP layouts
ISEEKYAN Jun 5, 2026
d6bf6fa
Support FSDP2 offload in Megatron Lite
ISEEKYAN Jun 6, 2026
c29614e
Pin VERL example to latest release
ISEEKYAN Jun 6, 2026
61da9db
Merge pull request #1 from ISEEKYAN/lite_update
ISEEKYAN Jun 6, 2026
79bc8b3
[no-task] Add VERL GSM8K SFT and GRPO examples
ISEEKYAN Jun 7, 2026
e0fc935
[no-task] Add MLite skills foundation
ISEEKYAN Jun 7, 2026
a664980
Merge pull request #3 from ISEEKYAN/mlite-skills-foundation
ISEEKYAN Jun 7, 2026
9b2d48c
task:TASK-2.9.3.6 修复 MLite GRPO 权重导出和 offload 测试
ISEEKYAN Jun 7, 2026
c35889b
[no-task] Make dist_opt validation model-agnostic
ISEEKYAN Jun 7, 2026
142adcb
Merge pull request #6 from ISEEKYAN/mlite-dist-opt-model-agnostic
ISEEKYAN Jun 7, 2026
4de7adb
Fix MLite runtime checkpoint save/load
ISEEKYAN Jun 7, 2026
582aad3
[no-task] Split MLite validation-only test coverage
ISEEKYAN Jun 7, 2026
e08fdd1
Align checkpoint load return type
ISEEKYAN Jun 7, 2026
2776671
[no-task] Mark FSDP2 multi-rank offload sentinel
ISEEKYAN Jun 7, 2026
a473992
task:TASK-2.9.3.6 忽略GRPO验证输出
ISEEKYAN Jun 7, 2026
3299980
[no-task] Keep MLite validation markers local
ISEEKYAN Jun 8, 2026
eec0978
task:TASK-2.9.3.6 修复Qwen3.5导出TP重排
ISEEKYAN Jun 8, 2026
01c86b3
task:TASK-2.9.3.6 忽略VERL项目输出目录
ISEEKYAN Jun 8, 2026
caed937
[no-task] Fix Qwen3.5 HF weight export names
ISEEKYAN Jun 8, 2026
9d3022d
[no-task] Save MLite checkpoint RNG state
ISEEKYAN Jun 8, 2026
0c658b7
[no-task] Use bf16 rollout weight export for MLite
ISEEKYAN Jun 8, 2026
0977280
[no-task] Harden MLite checkpoint resume semantics
ISEEKYAN Jun 8, 2026
b6a4e09
Merge pull request #7 from ISEEKYAN/mlite-runtime-checkpoint-dcp-fix
ISEEKYAN Jun 8, 2026
c178e1c
Merge pull request #4 from ISEEKYAN/mlite-primitive-validation-suite
ISEEKYAN Jun 8, 2026
9944fa5
[no-task] Enable MLite optimizer continuity checks
ISEEKYAN Jun 8, 2026
8fb5a23
[no-task] Preserve runtime dtype during HF weight export
ISEEKYAN Jun 8, 2026
cbe68a9
[no-task] Add independent MLite primitive unit coverage
ISEEKYAN Jun 8, 2026
3465213
[no-task] Batch expert EP gather for HF export
ISEEKYAN Jun 8, 2026
93ebdba
[no-task] Export Qwen3.5 rollout weights for vLLM
ISEEKYAN Jun 8, 2026
f9abc77
[no-task] Use packed Qwen3.5 rollout expert export
ISEEKYAN Jun 8, 2026
b39746e
[no-task] Pack Qwen3.5 expert groups during export
ISEEKYAN Jun 8, 2026
baba6ab
[no-task] Align Qwen3.5 conv1d export with mbridge
ISEEKYAN Jun 8, 2026
a2cb7d1
Merge pull request #14 from ISEEKYAN/mlite-fsdp2-distopt-state-contin…
ISEEKYAN Jun 8, 2026
5efba6e
[no-task] Preserve MLite optimizer state in checkpoints
ISEEKYAN Jun 8, 2026
a1b09d4
[no-task] Keep primitive CPU tests independent
ISEEKYAN Jun 8, 2026
d3f373d
[no-task] Merge devlite into VERL examples
ISEEKYAN Jun 8, 2026
2697f36
Merge pull request #2 from ISEEKYAN/lite_verl_gsm8k_examples
ISEEKYAN Jun 8, 2026
2e655a8
Merge pull request #15 from ISEEKYAN/mlite-primitive-independent-tests
ISEEKYAN Jun 8, 2026
a8277f4
Add MLite bench example and Bridge runtime backend
ISEEKYAN Jun 7, 2026
bd5eb9d
[no-task] Add real MLite and Bridge benchmark evidence
ISEEKYAN Jun 7, 2026
aea844d
[no-task] Fix deterministic mbridge correctness parity
ISEEKYAN Jun 7, 2026
3459d91
[no-task] Document mbridge deterministic parity evidence
ISEEKYAN Jun 8, 2026
96c53af
[no-task] Align bridge runtime with Megatron-Bridge API
ISEEKYAN Jun 8, 2026
28b02ca
[no-task] Fix Qwen3.5 Megatron-Bridge runtime smoke
ISEEKYAN Jun 8, 2026
a1c46c6
[no-task] Set TE deterministic env for bridge backend
ISEEKYAN Jun 8, 2026
8929585
[no-task] Use distopt optimizer backend label
ISEEKYAN Jun 8, 2026
31122ae
[no-task] Add VERL MLite engine checkpoint tests
ISEEKYAN Jun 8, 2026
94d43cd
[no-task] Use native Qwen3.5 vision in MLite bench
ISEEKYAN Jun 8, 2026
704a6df
Merge pull request #16 from ISEEKYAN/verl-mlite-engine-checkpoint-tests
ISEEKYAN Jun 8, 2026
de95a6c
Merge pull request #5 from ISEEKYAN/mlite-bench-bridge-backend
ISEEKYAN Jun 8, 2026
091c571
Keep .gitignore changes within experimental/lite
ISEEKYAN Jun 8, 2026
2291932
Leave root .gitignore untouched
ISEEKYAN Jun 8, 2026
ac8acc2
[no-task] Refresh MLite documentation positioning
ISEEKYAN Jun 8, 2026
4a52710
Merge pull request #17 from ISEEKYAN/mlite-docs-review
ISEEKYAN Jun 9, 2026
9980930
[no-task] Fix MLite distopt checkpoint continuity via Megatron dist-c…
ISEEKYAN Jun 9, 2026
3b2227e
[no-task] Harden MLite distopt checkpoint reshard coverage
ISEEKYAN Jun 9, 2026
55799fa
[no-task] Add Qwen3MoE distopt checkpoint RNG-restore + tp2/pp2/ep2 c…
ISEEKYAN Jun 9, 2026
8ada150
Merge pull request #18 from ISEEKYAN/mlite-distopt-distckpt-continuity
ISEEKYAN Jun 9, 2026
886f4a1
[no-task] Harden MLite distopt MoE checkpoint continuity
ISEEKYAN Jun 9, 2026
fdb17a0
Merge pull request #19 from ISEEKYAN/mlite-distckpt-moe-continuity-fix
ISEEKYAN Jun 10, 2026
56cbbcf
Clean up and format Megatron Lite
ISEEKYAN Jun 10, 2026
7bd966c
Merge pull request #20 from ISEEKYAN/mlite-cleanup-format-validation-v2
ISEEKYAN Jun 10, 2026
cabdee8
Fix FSDP2 offload grad clipping on multi-rank
ISEEKYAN Jun 10, 2026
1c41eb0
Merge pull request #21 from ISEEKYAN/mlite-fsdp2-offload-grad-clip
ISEEKYAN Jun 10, 2026
574a572
Merge branch 'dev' into devlite
FDecaYed Jun 10, 2026
742d35b
Add MLite Python copyright headers
ISEEKYAN Jun 10, 2026
fd20995
Merge pull request #22 from ISEEKYAN/mlite-copyright-headers
ISEEKYAN Jun 10, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions experimental/lite/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
repos:
- repo: local
hooks:
- id: isort
name: isort
entry: isort
language: python
additional_dependencies: ["isort==5.13.2"]
files: ^experimental/lite/.*\.py$
- id: black
name: black
entry: black
language: python
additional_dependencies: ["black==24.4.2"]
files: ^experimental/lite/.*\.py$
args: ["--skip-magic-trailing-comma", "--skip-string-normalization"]
139 changes: 139 additions & 0 deletions experimental/lite/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
# Megatron Lite

Megatron Lite is an experimental, agentic-native training runtime and native
model implementation layer for Megatron. It is designed for work that needs to
move quickly without giving up Megatron-Core performance: small composable
primitives, explicit model/runtime protocols, and validation recipes that make
changes easy to review and easy to reproduce.

The source lives under `experimental/lite/megatron/lite`, and the public import
path is `megatron.lite`.

Do not import `experimental.lite` from user code. Examples and public APIs should
refer to `megatron.lite`.

## Scope

This initial drop contains:

- A lightweight runtime API in `megatron.lite.runtime`.
- Common training primitives in `megatron.lite.primitive`.
- Lite-only native model implementations for Qwen3 MoE and Qwen3.5 MoE.
- Hugging Face safetensors load/export helpers for the included models.
- Megatron-Core optimizer wrapping for the lite runtime.
- FSDP2 optimizer primitives for supported lite model protocols.
- Reference runtime backends for comparison runs: `mbridge` for the legacy
package and `bridge` for real Megatron-Bridge environments.
- A benchmark example that can dry-run or execute `mlite`, `mbridge`, and
`bridge` backends.

This initial drop intentionally does not include:

- Hybrid model implementations.
- Dense Qwen3 model support. The included Qwen3-family path is Qwen3 MoE only.

## Why MLite

- **Agentic-native development surface.** Runtime, model, and primitive code are
split into reviewable contracts so agents and humans can make targeted changes
without touching unrelated Megatron subsystems.
- **Native MLite models, not wrapper models.** `backend="mlite"` builds native
`megatron.lite` model code; reference backends are used only for comparison.
- **Megatron-Core distopt parity.** In deterministic correctness runs against the
`mbridge` reference backend on the Megatron-Core distributed optimizer path,
MLite matched loss and grad-norm exactly (`max_abs=0.0`) with no mismatches;
post-step weights and eval logits were checked by SHA256 fingerprints.
- **Speed-aligned with the Core path.** On an 8x H100 Qwen3.5 MoE benchmark using
`distopt`, MLite measured 309.433 ms/step and 105,896.935 tokens/s, compared
with 332.201 ms/step and 98,639.089 tokens/s for `mbridge`, and 334.936
ms/step and 97,833.496 tokens/s for the real `bridge` path.

The `mbridge` benchmark line is the validated Megatron-Core/distopt reference
used for this PR. The `bridge` line is a separate Megatron-Bridge environment
check and should not be confused with the Core/distopt parity claim.

## Layout

```text
experimental/lite/
README.md
docs/ Design and usage notes
examples/ Optional integration and benchmark examples
skills/ Agent-agnostic maintenance skills
megatron/
lite/
runtime/ Runtime API, config, and backend registry
model/ Model registry and Qwen model implementations
primitive/ Parallel, checkpoint, optimizer, module, and op primitives
```

For local source-tree use:

```bash
export PYTHONPATH=/path/to/Megatron-LM/experimental/lite:$PYTHONPATH
```

## Public API

```python
from megatron.lite.runtime import MegatronLiteConfig, RuntimeConfig, create_runtime

cfg = RuntimeConfig(
backend="mlite",
hf_path="/path/to/hf-model",
backend_cfg=MegatronLiteConfig(model_name="qwen3_moe", impl="lite"),
)
runtime = create_runtime(cfg)
handle = runtime.build_model()
```

`backend="mlite"` selects the Megatron Lite runtime backend. `impl="lite"`
selects the model implementation inside the registered model family.
`backend="mbridge"` selects the legacy `mbridge` reference backend used by the
validated benchmark example. `backend="bridge"` selects the Megatron-Bridge
runtime backend and requires an environment where `import megatron.bridge` works
when the model is built.

Canonical model names currently registered by default:

- `qwen3_moe`: Qwen3 MoE lite implementation. Use this name in new configs.
- `qwen3_5`: Qwen3.5 MoE lite implementation.

Compatibility names:

- `qwen3`: legacy alias for the Qwen3 MoE implementation only. It does not mean
dense Qwen3 support. HF `model_type` values `qwen3_moe` and `qwen2_moe`
currently resolve through this compatibility path.

## Benchmark And Correctness Signoff

The validated benchmark and correctness commands live in
[`examples/bench/README.md`](examples/bench/README.md). The signoff setup uses
Qwen3.5 MoE, `optimizer_backend=distopt`, deterministic mode for strict
correctness, and identical synthetic input streams for paired performance runs.

Reproduce the strict MLite vs Megatron-Core/distopt comparison with:

```bash
export MEGATRON_LITE_DETERMINISTIC=1
export CUBLAS_WORKSPACE_CONFIG=:4096:8

HF_PATH=/models/Qwen3.5-35B-A3B \
REFERENCE_BACKEND=mbridge \
DRY_RUN=0 \
bash experimental/lite/examples/bench/scripts/run_qwen35_correctness_pair.sh
```

## Docs

- [Architecture](docs/architecture.md)
- [Runtime](docs/runtime.md)
- [Models](docs/models.md)
- [Porting Notes](docs/porting.md)
- [Skills](skills/README.md)
- [Bench Example](examples/bench/README.md)

## Acknowledgements

The Qwen3 MoE LoRA adapter support follows Mind-Lab's PEFT/Mint-compatible
adapter work. Thanks to Mind-Lab for the reference implementation and guidance.
46 changes: 46 additions & 0 deletions experimental/lite/docs/architecture.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Architecture

Megatron Lite source lives under `experimental/lite/megatron/lite` and is split
into three layers:

- `runtime`: lifecycle and training-step orchestration.
- `model`: model registration plus model-specific build/load/export protocols.
- `primitive`: reusable lower-level pieces such as parallel state, tensor-parallel
layers, checkpoint conversion, MoE utilities, and optimizer wrapping.

The runtime does not know Qwen implementation details. It imports a model
protocol from the model registry, builds the typed implementation config, then
delegates model construction to that protocol.

## Import Boundary

The source root for local use is `experimental/lite`; adding that directory to
`PYTHONPATH` exposes the package as `megatron.lite`. Internal imports also use
`megatron.lite` so user-facing code matches the final package path.

## Runtime Boundary

The runtime API owns:

- Distributed initialization.
- Model protocol loading.
- Model checkpoint save/load dispatch.
- Forward/backward microbatch orchestration.
- Optimizer and learning-rate scheduler stepping.
- Optional model/optimizer offload hooks.

The model protocol owns:

- Architecture config creation.
- Model chunk construction.
- Model-specific recompute/offload wiring.
- Model-specific optimizer construction.
- HF checkpoint load/export mapping.

## Current Deliberate Omissions

This package currently includes only the lite model implementation path. It
intentionally excludes non-lite and hybrid model implementation packages. The
optional benchmark entrypoints under `examples/bench` are comparison tools and
are not imported by the package runtime. FSDP2 is included as an optimizer
primitive and can be selected by model protocols that support it.
58 changes: 58 additions & 0 deletions experimental/lite/docs/models.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Models

Model code lives under `megatron.lite.model`.

The model registry maps `(model_name, impl)` pairs to model protocol modules.
The runtime loads the protocol and expects the following required symbols:

- `ImplConfig`: dataclass for model-specific knobs.
- `build_model_config(source, **overrides)`: returns a typed model config.
- `build_model(model_cfg, *, impl_cfg)`: returns a `ModelBundle`.

Optional protocol symbols:

- `load_hf_weights(chunk, hf_path, model_cfg, ps)`
- `export_hf_weights(chunks, model_cfg, ps, **kwargs)`
- `vocab_size(model_cfg)`

## Included Models

`qwen3_moe` is the canonical name for the Qwen3 MoE lite implementation:

```text
megatron.lite.model.qwen3_moe.lite.protocol
```

`qwen3` is kept as a legacy compatibility alias for the same implementation. It
does not mean dense Qwen3 support. Hugging Face `model_type` values `qwen3_moe`
and `qwen2_moe` currently resolve through this compatibility path.

`qwen3_5` maps to the Qwen3.5 MoE lite implementation:

```text
megatron.lite.model.qwen3_5.lite.protocol
```

## Acknowledgements

The Qwen3 MoE LoRA adapter support follows Mind-Lab's PEFT/Mint-compatible
adapter work. Thanks to Mind-Lab for the reference implementation and guidance.

## Adding A Model

Add a model package under `model/`, then register it in
`model/registry.py`:

```python
register_model(
"my_model",
package="megatron.lite.model.my_model",
hf_model_types=["my_model"],
impls={
"lite": "megatron.lite.model.my_model.lite.protocol",
},
)
```

New models should keep heavyweight imports inside protocol functions when
possible so importing `megatron.lite` stays cheap.
43 changes: 43 additions & 0 deletions experimental/lite/docs/porting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Porting Notes

This tree is prepared as an experimental Megatron package. The package code
lives under `experimental/lite/megatron/lite`, so users can add
`experimental/lite` to `PYTHONPATH` and import `megatron.lite`.

## Naming Rules

- Use `Megatron Lite` for the component name in docs and comments.
- Use `megatron.lite` for public and internal imports.
- Do not introduce project-specific legacy branding.
- Use `mlite` for the runtime backend key.
- Use `lite` for model implementation names.

## Included Surface

Keep the PR focused on the lite model implementation path:

- Runtime backend: `mlite`.
- Reference comparison backends: `mbridge` for the validated legacy
Megatron-Core/distopt path and `bridge` for real Megatron-Bridge environments.
- Models: Qwen3 MoE and Qwen3.5 MoE. Dense Qwen3 is not included.
- Model implementations: `lite` only.
- Optimizer primitives: Megatron-Core optimizer wrapping and FSDP2.
- Optional examples: benchmark and VERL launchers under `experimental/lite/examples`.

Keep these out of the first PR unless the scope changes:

- Hybrid model implementation packages.
- Megatron-Bridge model implementation packages.
- Undocumented experiment-specific entrypoints.

## Package Integration

No repository-level packaging changes are made in this experimental drop. The
current layout is importable from source with:

```bash
export PYTHONPATH=/path/to/Megatron-LM/experimental/lite:$PYTHONPATH
```

A future integration step can decide whether to keep the experimental location
or move the tree into the final package location.
83 changes: 83 additions & 0 deletions experimental/lite/docs/runtime.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Runtime

The public runtime entrypoint is `megatron.lite.runtime`.

```python
from megatron.lite.runtime import MegatronLiteConfig, ParallelConfig, RuntimeConfig, create_runtime

cfg = RuntimeConfig(
backend="mlite",
hf_path="/path/to/hf-model",
backend_cfg=MegatronLiteConfig(
model_name="qwen3_moe",
impl="lite",
parallel=ParallelConfig(tp=1, pp=1, cp=1, ep=1),
),
)
runtime = create_runtime(cfg)
handle = runtime.build_model()
```

## API Tiers

All runtime backends implement the pretraining tier:

- `build_model`
- `save_checkpoint`
- `load_checkpoint`
- `train_mode`
- `eval_mode`
- `forward_backward`
- `zero_grad`
- `optimizer_step`
- `lr_scheduler_step`

The lite runtime also implements `export_weights` and `to` when the underlying
model and optimizer support those operations.

The `mbridge` runtime implements the same runtime contract through the legacy
`mbridge` package and Megatron-Core optimizer/checkpoint helpers. The benchmark
example currently uses this backend for validated reference runs.

The `bridge` runtime is the real Megatron-Bridge path. It imports
`megatron.bridge` lazily from `build_model()`, so config construction and dry-run
examples can execute without Megatron-Bridge installed.

## Config Types

`RuntimeConfig` selects the backend and carries the Hugging Face model path.

`MegatronLiteConfig` carries `mlite` backend settings:

- `model_name`: `qwen3_moe` or `qwen3_5` for new configs. `qwen3` remains
accepted as a legacy alias for `qwen3_moe` only; dense Qwen3 is not included.
- `impl`: currently only `lite`.
- `parallel`: tensor, expert, pipeline, virtual pipeline, and context sizes.
- `optimizer`: Megatron-Core optimizer settings.
- `impl_cfg`: model-specific options consumed by each model protocol.

`BridgeConfig` carries shared `mbridge` and `bridge` backend settings:

- `model_name`: optional model identifier used for benchmark metadata.
- `parallel`: tensor, expert, pipeline, virtual pipeline, and context sizes.
- `optimizer`: Megatron-Core optimizer settings.
- `override_ddp_config`, `override_transformer_config`, and
`override_optimizer_config`: explicit reference-backend/Core override maps.
- `param_offload` and `optimizer_offload`: offload model/optimizer state between
train/eval contexts.

## Backend Registry

The built-in backend keys are `mlite`, `mbridge`, and `bridge`. Model
implementations for the native runtime remain selected through
`MegatronLiteConfig.impl`, which currently supports `impl="lite"`.

Custom runtime backends can be registered with:

```python
from megatron.lite.runtime import register_runtime

register_runtime("my_backend", "my_package.my_runtime")
```

The target module must expose `create(hf_path, cfg)`.
2 changes: 2 additions & 0 deletions experimental/lite/examples/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
"""Megatron Lite examples package."""
1 change: 1 addition & 0 deletions experimental/lite/examples/bench/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
outputs/
Loading
Loading