unified_export_megatron#
Code that export quantized Megatron Core models for deployment.
Functions
Export Megatron Core GPTModel to unified checkpoint and save to export_dir. |
|
Import GPTModel state_dict from supported HuggingFace pretrained model path. |
- export_mcore_gpt_to_hf(model, pretrained_model_name_or_path, export_extra_modules=False, dtype=torch.bfloat16, export_dir='/tmp', trust_remote_code=False, moe_router_dtype=None, clamp_kv_cache_scales=True)#
Export Megatron Core GPTModel to unified checkpoint and save to export_dir.
- Parameters:
model (Module) – The Megatron Core GPTModel instance.
pretrained_model_name_or_path (str | PathLike) – Can be either: the model id of a pretrained model hosted inside a model repo on huggingface.co; or a directory containing model weights saved using [~PreTrainedModel.save_pretrained], e.g., ./my_model_directory/. The source’s non-model files (tokenizer, processor, generation config, remote code) are copied into the export from a local directory only; for a model ID they are skipped with a warning. Get a local copy first with modelopt.torch.export.ensure_local_checkpoint to include them.
export_extra_modules (bool) – If True, export extra modules like eagle_module. Otherwise, only export the base model.
dtype (dtype) – The weights data type to export the unquantized layers.
export_dir (Path | str) – The target export path.
clamp_kv_cache_scales (bool) – Whether to clamp FP8 KV cache scaling factors to at least 1.0.
trust_remote_code (bool)
moe_router_dtype (dtype | None)
Deprecated since version 0.50.0: Planned for removal in 0.51.0. Where it supports the checkpoint, export with Megatron-Bridge’s
AutoBridge.save_hf_pretrained_modeloptinstead.examples/megatron_bridge/export_quantized_megatron_to_hf.pydoes this and copies the source’s non-model files. It checks only what Megatron-Bridge can export: otherwise it falls back to this function, which exports the checkpoint only within its own limits (see below).save_hf_pretrained_modeloptneeds a Megatron-Bridge newer than the one innemo:26.10; ModelOpt tests it at the commitexamples/megatron_bridge/README.mdnames. To call it directly, from the quantized model chunks and the local HF source checkpoint:bridge = AutoBridge.from_hf_pretrained(hf_path) bridge.save_hf_pretrained_modelopt(model_chunks, export_dir, source_path=hf_path) # Megatron-Bridge saves only the config, tokenizer and remote code; carry over the # source's other non-model files (chat templates, assets) as this function does. if torch.distributed.get_rank() == 0: copy_non_model_files(hf_path, export_dir)
Megatron-Bridge writes the model as it is in memory, so
model_chunksmust hold the whole loaded model. For a VLM checkpoint that holds only the language model, load the HF weights first (bridge.load_hf_weights(model_chunks, hf_path)) and the checkpoint over them, as the example does; unlike this function, Megatron-Bridge doesn’t copy the vision tower fromsource_path.Megatron-Bridge doesn’t export these yet:
GGML / IQ formats;
EAGLE modules (
export_extra_modules=True);models whose Megatron-Bridge bridge post-processes the exported weights (e.g. Nemotron 3.5 Super VL);
KV-cache quantizers, with a Megatron-Bridge older than NVIDIA-NeMo/Megatron-Bridge#7057 (including the commit the README names).
This function covers them only for the architectures in its rule tables (
modelopt/torch/export/plugins/mcore_*.py) and at TP=1 (GGML / IQ formats also at PP=1). A quantized base model also needs a weight format it records in the quantization metadata; the separate EAGLE draft export doesn’t. For example, it has no rules for Gemma3-VL or Megatron-Bridge 0.8’s hybrid Qwen3.5 (which it refuses), and writes no quantization metadata when only KV-cache quantizers are enabled.Neither path exports these:
Medusa heads: Megatron-Bridge won’t add them, and this function fails for a Medusa model with
export_extra_modules=True.Uniform INT8 weight-only checkpoints: Megatron-Bridge can’t pack INT8 weight-only layers, and this function writes no quantization metadata for such a checkpoint.
- import_mcore_gpt_from_hf(model, pretrained_model_path, workspace_dir=None, dtype=torch.bfloat16, trust_remote_code=False, moe_router_dtype=None)#
Import GPTModel state_dict from supported HuggingFace pretrained model path.
- Parameters:
model (Module) – The Megatron Core GPTModel instance.
pretrained_model_path (str) – A path to a directory containing model weights saved using [~PreTrainedModel.save_pretrained], e.g., ./my_model_directory/.
workspace_dir (str | None) – The directory to save the workspace.
dtype (dtype) – The weights data type to import.
trust_remote_code (bool) – If True, this allows importing from a wider range of sources.
moe_router_dtype (dtype | None) – The data type to import the moe router weights.
Deprecated since version 0.49.0: Use Megatron-Bridge’s
AutoBridgeto load Hugging Face checkpoints instead.