Skip to navigation

nemo_automodel.components.models.common.hf_checkpointing_mixin

View as MarkdownOpen in Claude

HuggingFace-compatible checkpointing mixin for NeMo Automodel.

This module provides a mixin class that gives models HuggingFace-compatible save_pretrained() and from_pretrained() methods while using NeMo’s checkpointing infrastructure internally.

Key design principle: We do NOT override state_dict() or load_state_dict(). PyTorch’s DCP expects these to behave like standard nn.Module methods. HF format conversions happen only in save_pretrained() and from_pretrained() via Checkpointer.

Checkpointer is passed explicitly (dependency injection) - no global state.

Module Contents

Classes

NameDescription
HFCheckpointingMixinMixin providing HF-compatible API using NeMo’s checkpointing infrastructure.

Data

logger

API

class nemo_automodel.components.models.common.hf_checkpointing_mixin.HFCheckpointingMixin()

Mixin providing HF-compatible API using NeMo’s checkpointing infrastructure.

Provides save_pretrained() and from_pretrained() methods that use Checkpointer for unified distributed/async support with HF format conversion.

Key design: We do NOT override state_dict() or load_state_dict() because PyTorch’s DCP expects these to behave like standard nn.Module methods.

For PreTrainedModel subclasses:

  • super().from_pretrained() handles: downloads, quantization config, meta device init
  • Checkpointer.load_base_model() handles: actual weight loading with format conversion

For nn.Module subclasses (no parent from_pretrained):

  • Falls back to manual config loading + Checkpointer

For PreTrainedModel subclasses, a vocabulary resize keeps the model’s LMHead.

nemo_automodel.components.models.common.hf_checkpointing_mixin.HFCheckpointingMixin._get_resized_lm_head(
old_lm_head: torch.nn.Linear,
new_num_tokens: int | None = None,
transposed: bool = False,
mean_resizing: bool = True
) -> torch.nn.Linear

Resize the output head as Transformers does, keeping an LMHead and its settings.

Transformers builds a plain nn.Linear for the resized vocabulary. An LMHead takes that module’s resized parameters instead, so its class, logits_dtype and softcap survive the resize.

Parameters:

old_lm_head
nn.Linear

The output head, with weight of shape [vocab, hidden].

new_num_tokens
int | NoneDefaults to None

New vocabulary size; None returns the head unchanged.

transposed
boolDefaults to False

Whether the head’s weight is stored as [hidden, vocab].

mean_resizing
boolDefaults to True

Initialize added rows from the mean and covariance of the existing rows.

Returns: nn.Linear

The resized head, with weight of shape [new_num_tokens, hidden].

nemo_automodel.components.models.common.hf_checkpointing_mixin.HFCheckpointingMixin.save_pretrained(
save_directory: str,
tokenizer: typing.Optional[transformers.tokenization_utils.PreTrainedTokenizerBase] = None,
kwargs = {}
) -> None

Save model in HF-compatible format using Checkpointer infrastructure.

Supports distributed saving, sharding, and async checkpointing.

Parameters:

save_directory
str

Output path

checkpointer
Optional[Checkpointer]Defaults to None

Checkpointer instance. Uses self._checkpointer if not provided.

tokenizer
Optional[PreTrainedTokenizerBase]Defaults to None

Optional tokenizer to save alongside model

**kwargs
Defaults to {}

Additional arguments, including peft_config and is_final_checkpoint. Direct callers that do not have recipe step-scheduler context default is_final_checkpoint to False.

nemo_automodel.components.models.common.hf_checkpointing_mixin.logger = logging.getLogger(__name__)