Skip to content

[megatron] add opt-in distributed HF checkpoint export#1831

Open
dinhxuanvu wants to merge 2 commits into
NovaSky-AI:mainfrom
dinhxuanvu:vdinh/megatron-distributed-hf-export
Open

[megatron] add opt-in distributed HF checkpoint export#1831
dinhxuanvu wants to merge 2 commits into
NovaSky-AI:mainfrom
dinhxuanvu:vdinh/megatron-distributed-hf-export

Conversation

@dinhxuanvu

Copy link
Copy Markdown
Contributor

What

Add an opt-in policy.megatron_config.hf_export_config with distributed_save and save_every_n_ranks, threaded through the Megatron strategy's save_hf_model into AutoBridge.save_hf_weights.

Why

The Megatron path currently calls save_hf_weights with the bridge default distributed_save=False, so a single rank writes the entire HF checkpoint serially. For large models this dominates the export and stalls every rank on each periodic save — a ~1 TB 550B checkpoint takes 40+ minutes to write.

distributed_save=True fans the shard writes across ranks (one saver per save_every_n_ranks, e.g. 8 = one writer per 8-GPU node). The on-disk result is the same standard HF sharded safetensors + model.safetensors.index.json layout, so from_pretrained loads it unchanged — only the write is parallelized, not the format.

Behavior

  • Defaults to distributed_save=False, save_every_n_ranks=1 — no change for existing users; strictly opt-in.
  • Megatron-only; FSDP and other backends are untouched.

Notes

  • distributed_save only partitions which rank writes which standard shard file; rank 0 still writes a single consolidated index. It is not a Megatron distributed (DCP) checkpoint and needs no conversion to reload.

The Megatron save path calls AutoBridge.save_hf_weights with the bridge
default distributed_save=False, so a single rank writes the entire HF
checkpoint serially. For large models this dominates the export and stalls
every rank on each periodic save (a ~1 TB 550B export takes 40+ minutes).

Add policy.megatron_config.hf_export_config with distributed_save and
save_every_n_ranks, threaded through the Megatron strategy's save_hf_model
into save_hf_weights. distributed_save fans shard writes across ranks (one
saver per save_every_n_ranks) while producing the same standard HF sharded
safetensors + index layout, so from_pretrained loads it unchanged.

Defaults keep the serial behavior (distributed_save=False); strictly opt-in
and Megatron-only.

Signed-off-by: Vu Dinh <vudinh@outlook.com>

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces parallelized Hugging Face model exporting by adding a new configuration class MegatronHFExportConfig with options for distributed_save and save_every_n_ranks. These configurations are propagated from the worker down to the Megatron strategy to allow fanning out the safetensors export across multiple ranks. Feedback suggests adding validation to ensure save_every_n_ranks is at least 1, both in the configuration's post-initialization and defensively at the start of the saving process, to prevent potential division-by-zero errors.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread skyrl/train/config/config.py
A value < 1 raises ZeroDivisionError in the bridge's distributed save
(modulo/floor-div over ranks). Fail fast at config construction.

Signed-off-by: Vu Dinh <vudinh@outlook.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant