nemo_rl.models.automodel.checkpoint#
Automodel checkpoint utilities for the policy workers.
This module provides a wrapper class around the nemo_automodel Checkpointer for saving and loading model checkpoints in Automodel policy workers.
Module Contents#
Classes#
Manages checkpointing for Automodel models using nemo_automodel’s Checkpointer. |
Functions#
Resolve a |
|
Build the Automodel checkpoint config for a worker. |
|
Re-add the Qwen2.5-VL |
Data#
API#
- nemo_rl.models.automodel.checkpoint.logger#
‘getLogger(…)’
- nemo_rl.models.automodel.checkpoint._resolve_lora_adapter_dir(restore_from: str) str#
Resolve a
lora_cfg.restore_frompath to the adapter directory.Accepts a NeMo RL checkpoint weights directory (
step_*/policy/weights), itsmodelsubdirectory, or any directory directly containingadapter_model.safetensors+adapter_config.json(HF PEFT layout).
- nemo_rl.models.automodel.checkpoint.build_checkpoint_config(
- automodel_cfg: collections.abc.Mapping[str, Any],
- *,
- model_repo_id: str,
- dequantize_base_checkpoint: bool,
- is_peft: bool,
- is_async: bool,
- skip_task_head_prefixes_for_base_model: Optional[list[str]] = None,
Build the Automodel checkpoint config for a worker.
Read NeMo-RL settings from the YAML-backed checkpoint block and forward them to Automodel. This integration boundary is shared by the two Automodel v2 workers; defaults belong in the exemplar configs.
- Parameters:
automodel_cfg – The worker’s
policy.automodel_cfg/value.automodel_cfgmapping. Automodel checkpoint settings are read from its nestedcheckpointblock; all other keys are ignored.model_repo_id – Forwarded to Automodel’s
CheckpointingConfig.model_repo_id.dequantize_base_checkpoint – Forwarded to
CheckpointingConfig.dequantize_base_checkpoint; only takes effect on a base-checkpoint init load, not on a resume load.is_peft – Forwarded to
CheckpointingConfig.is_peft.is_async – Forwarded to
CheckpointingConfig.is_async.skip_task_head_prefixes_for_base_model – Optional parameter-prefix list forwarded to
CheckpointingConfig.skip_task_head_prefixes_for_base_model(only set on the returned dict when not None); the value worker passes["score."]to skip the reward head on base-model init loads.
- Returns:
A dict of only the fields Automodel’s
CheckpointingConfigdataclass accepts, meant to be splatted intoAutomodelCheckpointingConfig(enabled=True, checkpoint_dir="", **result).
- nemo_rl.models.automodel.checkpoint._patch_qwen_vl_vision_key_mapping() None#
Re-add the Qwen2.5-VL
^visual->model.visualcheckpoint key rename.Workaround for a transformers v5.5.0 regression. transformers #44627 moved VLM checkpoint conversions into the main mapping, but copied the Qwen-VL visual key mapping incorrectly:
visual.*checkpoint keys no longer map tomodel.visual.*. transformers #45358 fixed those VLM mappings in v5.6, but the Automodel commit NeMo-RL can currently pin to still depends on transformers v5.5.0. Automodel’sget_combined_key_mappingmirrors the transformersWeightRenamingentries, so the bad v5.5.0 mapping leaves vision-tower checkpoint keys unmapped and FSDP2set_model_state_dict(strict=False)drops them inload_base_model. The vision tower is then left randomly initialized, making the training forward diverge from vLLM (token_mult_prob_error).This wraps
get_combined_key_mappingto inject the missing rule forqwen2_5_vl/qwen2_vl. It is idempotent: the rule is only added when no existing rule already targetsmodel.visual. Remove this after Automodel upgrades its transformers dependency to a version that includes #45358.
- class nemo_rl.models.automodel.checkpoint.AutomodelCheckpointManager(
- dp_mesh: torch.distributed.device_mesh.DeviceMesh,
- tp_mesh: torch.distributed.device_mesh.DeviceMesh,
- moe_mesh: Optional[torch.distributed.device_mesh.DeviceMesh] = None,
Manages checkpointing for Automodel models using nemo_automodel’s Checkpointer.
This class provides a clean interface for saving and loading model checkpoints, wrapping the nemo_automodel Checkpointer with configuration management.
.. attribute:: checkpointer
The underlying nemo_automodel Checkpointer instance.
Initialization
Initialize the AutomodelCheckpointManager.
- Parameters:
dp_mesh – The data parallel device mesh.
tp_mesh – The tensor parallel device mesh.
moe_mesh – Optional MoE device mesh.
- _get_dp_rank() int#
Get the data parallel rank.
- _get_tp_rank() int#
Get the tensor parallel rank.
- init_checkpointer(
- config_updates: Optional[dict[str, Any]] = None,
Initialize the Automodel Checkpointer if not already created.
This method creates a new Checkpointer instance with the provided configuration. If a checkpointer already exists, this method does nothing.
- Parameters:
config_updates – Automodel checkpoint fields to set during initialization.
- finalize_async_save() None#
Block until in-flight async checkpoint writes have landed on disk.
With
is_async=Truethe Automodel Checkpointer hands both the model and optimizer state todcp.async_save, which stages them and uploads from a separate process. Those writes address files by path, so the caller must not renametmp_step_Ntostep_Nuntil they finish – otherwise the writer re-createstmp_step_Nand the promoted checkpoint is missing its optimizer shards and.metadata.Safe to call when async saving is off or no save is in flight; both underlying calls are no-ops in that case.
- static _save_tokenizer_on_rank0(
- tokenizer: Any,
- tokenizer_path: str,
Save a tokenizer (or processor) to
tokenizer_pathfrom rank 0 only.Unlike model/optimizer state, the tokenizer is replicated rather than sharded: every rank holds an identical copy, and
save_pretrainedwrites to the same rank-independent filenames (tokenizer_config.json, …). Letting all ranks write means N-1 redundant writes racing on the same file.save_pretrainedis not atomic (it opens withO_TRUNC, writes, and may read files back), so concurrent writers can deadlock on the inode lock. On ahard-mounted NFS share this manifests as ranks stuck indefinitely in uninterruptible disk sleep, hanging the whole job at checkpoint time.This mirrors the rank-0 guard nemo_automodel applies to the same artifacts in
nemo_automodel.components.checkpoint.addons.ConsolidatedHFAddon.pre_save.Note this must NOT be applied to
dcp.save-based model/optimizer saves: those are collective calls whose output is already rank-disjoint, so guarding them would deadlock and drop every non-zero rank’s shard.- Parameters:
tokenizer – The tokenizer or processor to save.
tokenizer_path – Directory to save the tokenizer into.
- save_checkpoint(
- model: torch.nn.Module,
- weights_path: str,
- optimizer: Optional[torch.optim.Optimizer] = None,
- optimizer_path: Optional[str] = None,
- scheduler: Optional[torch.optim.lr_scheduler.LRScheduler] = None,
- tokenizer: Optional[transformers.AutoTokenizer] = None,
- tokenizer_path: Optional[str] = None,
- *,
- is_final_checkpoint: bool,
- peft_config: Optional[nemo_automodel.components._peft.lora.PeftConfig] = None,
Save a checkpoint of the model.
The optimizer states are saved only if
optimizerandoptimizer_pathare provided. Any previous async save is completed before a new one starts. When async saving is enabled, this method returns after model and optimizer staging is complete; upload and consolidation may continue in the background.- Parameters:
model – The model to save.
weights_path – Path to save model weights.
optimizer – Optional optimizer to save.
optimizer_path – Optional path to save optimizer state.
scheduler – Optional learning rate scheduler.
tokenizer – Optional tokenizer to save with the checkpoint.
tokenizer_path – Optional path to save tokenizer separately.
is_final_checkpoint – Whether this checkpoint completes the training run, either at the configured final step or after a deliberate early stop. Automodel’s
save_consolidated="final"mode consolidates these checkpoints. Timeout recovery checkpoints are resumable and are not considered final.peft_config – Optional PEFT configuration.
- load_lora_adapter(model: torch.nn.Module, adapter_dir: str) None#
Load validated donor adapter weights through the PEFT checkpointer path.
- Parameters:
model – Model whose adapters will be initialized.
adapter_dir – Adapter directory or checkpoint weights directory.
- load_checkpoint(
- model: torch.nn.Module,
- weights_path: str,
- optimizer: Optional[torch.optim.Optimizer] = None,
- optimizer_path: Optional[str] = None,
- scheduler: Optional[torch.optim.lr_scheduler.LRScheduler] = None,
Load a checkpoint into the model using Automodel Checkpointer.
- Parameters:
model – The model to load weights into.
weights_path – Path to the checkpoint weights.
optimizer – Optional optimizer to load state into.
optimizer_path – Optional path to optimizer checkpoint.
scheduler – Optional learning rate scheduler.
- check_master_weight_dtypes(
- model: torch.nn.Module,
- optimizer: torch.optim.Optimizer,
- optimizer_path: str,
Hard-error if a resumed optimizer’s on-disk master dtype has drifted.
Must run before
checkpointer.load_optimizer: legacy FP32 masters must not be silently cast into BF16 int16 remainders (or vice versa) just becausestore_param_remainders/master_weight_dtypechanged between save and resume.