nemo_automodel.components.checkpoint.conversion_mapping
nemo_automodel.components.checkpoint.conversion_mapping
Checkpoint conversion mappings for loading HuggingFace checkpoints.
This module provides conversion mappings for transforming checkpoint keys and tensors when loading models. It primarily uses the transformers library’s conversion_mapping module which handles both key renaming and tensor operations (merging/splitting).
For MoE models, the conversion handles:
- Key renaming from checkpoint format (e.g., block_sparse_moe.experts.X.w1) to model format (e.g., mlp.experts.gate_up_proj)
- Tensor merging for grouped expert formats (individual experts -> single 3D tensor)
The primary entry points are:
get_checkpoint_conversion_mapping(model_type): Get conversion rules for a model typeget_model_conversion_mapping(model, ...): Get all conversion rules for a model instancerequires_tensor_merging(model_type): Check if model needs tensor operations
Module Contents
Functions
Data
MODELS_REQUIRING_TENSOR_MERGING
API
Get the checkpoint conversion mapping for a given model type.
This returns a list of WeightConverter and/or WeightRenaming objects from transformers that define how to convert checkpoint keys and tensors to model state dict format.
Example
>>> mapping = get_checkpoint_conversion_mapping(“mixtral”) >>> # Returns list with WeightRenaming for gate and WeightConverter >>> # for merging individual expert weights into grouped format
Parameters:
The model type string (e.g., “mixtral”, “qwen2_moe”, “phimoe”)
Returns: list | None
A list of WeightConverter/WeightRenaming objects defining the conversion,
Get root key renames and optional scoped submodel renames.
This is a simpler alternative to get_model_conversion_mapping that only handles key renaming (not tensor operations). Useful when you just need to rename keys without merging tensors.
Note: For MoE models that require tensor merging, use get_model_conversion_mapping instead, which returns WeightConverter objects that handle both renaming and merging.
Parameters:
The model type string from config.model_type
Optional key mapping from the model’s
_checkpoint_conversion_mapping attribute
Optional model instance used to resolve class-specific root renames and scoped submodel renames. Omit for models whose state-dict adapter or tensor converters own checkpoint conversion.
Returns: dict[str, str] | Callable[[str], str] | None
Regex mapping, a callable that also applies nested-model renames in order,
Get all weight conversion mappings for a model instance.
This is the main entry point for getting conversion rules. It combines:
- Custom key_mapping if provided
- Model’s _checkpoint_conversion_mapping attribute (for VLMs)
- Model-type specific conversions (MoE merging, etc.)
- Legacy conversions (LayerNorm.gamma -> LayerNorm.weight, etc.)
- Quantizer-specific conversions if provided
Example
>>> from transformers import AutoModelForCausalLM >>> model = AutoModelForCausalLM.from_pretrained(“mistralai/Mixtral-8x7B”) >>> conversions = get_model_conversion_mapping(model) >>> # Use conversions to transform checkpoint state dict
Parameters:
The model instance to get conversions for
Optional custom key mapping (source -> target patterns)
Optional HuggingFace quantizer with additional conversions
Whether to include legacy LayerNorm conversions (default True)
Returns: list
List of WeightConverter/WeightRenaming objects defining all conversions.
Check if transformers conversion mapping is available.
Returns: bool
True if transformers library with conversion_mapping module is available.
Check if a model type requires tensor merging during checkpoint loading.
Some MoE models store expert weights in grouped format (single 3D tensor for all experts) but checkpoints store individual expert weights. These models require tensor merging that cannot be done via simple key renaming.
Parameters:
The model type string from config.model_type
Returns: bool
True if the model type requires tensor merging during loading.