bridge.training.utils.checkpoint_utils#

Module Contents#

Functions#

file_exists

Check if a file exists.

ensure_directory_exists

Ensure that the directory for a given filename exists.

get_checkpoint_name

Determine the directory name for a specific checkpoint.

get_checkpoint_train_state_filename

Get the filename for the train state tracker file.

get_checkpoint_tracker_filename

Tracker file rescords the latest chckpoint during training to restart from.

_has_hf_weight_files

Check whether path contains any HuggingFace-style weight file.

is_hf_checkpoint_dir

Lightweight check for a local HuggingFace full-model directory.

is_checkpoint_iteration_directory

Check if path is a specific checkpoint iteration directory.

checkpoint_exists

Check if a checkpoint directory exists.

get_hf_source_from_model_config

Return the Hugging Face source id and revision recorded on a model config.

get_hf_model_id_from_checkpoint

Infer the HuggingFace model identifier recorded in a Megatron Bridge checkpoint.

read_train_state

Read the train state metadata from a YAML file (rank 0 only).

Data#

API#

bridge.training.utils.checkpoint_utils.__all__#

[‘CONFIG_FILE’, ‘get_checkpoint_run_config_filename’, ‘join_paths’, ‘read_run_config’]

bridge.training.utils.checkpoint_utils.TRAIN_STATE_FILE#

‘train_state.pt’

bridge.training.utils.checkpoint_utils.TRACKER_PREFIX#

‘latest’

bridge.training.utils.checkpoint_utils.file_exists(path: str) → bool#

Check if a file exists.

Parameters:

path – The path to the file. Can be a local path or an MSC URL.

Returns:

True if the file exists, False otherwise.

bridge.training.utils.checkpoint_utils.ensure_directory_exists(
filename: str,
check_parent: bool = True,
) → None#

Ensure that the directory for a given filename exists.

Parameters:
  • filename – The path whose directory should be checked/created.

  • check_parent – If True (default), checks the parent directory of the filename. If False, treats the filename itself as the directory path.

bridge.training.utils.checkpoint_utils.get_checkpoint_name(
checkpoints_path: str,
iteration: int,
release: bool = False,
) → str#

Determine the directory name for a specific checkpoint.

Constructs the path based on iteration number or release flag.

Parameters:
  • checkpoints_path – Base directory where checkpoints are stored.

  • iteration – The training iteration number.

  • release – If True, uses ‘release’ as the directory name instead of iteration.

Returns:

The full path to the checkpoint directory.

bridge.training.utils.checkpoint_utils.get_checkpoint_train_state_filename(
checkpoints_path: str,
prefix: Optional[str] = None,
) → str#

Get the filename for the train state tracker file.

This file typically stores metadata about the latest checkpoint, like the iteration number.

Parameters:
  • checkpoints_path – Base directory where checkpoints are stored.

  • prefix – Optional prefix (e.g., ‘latest’) to prepend to the filename.

Returns:

The full path to the train state tracker file.

bridge.training.utils.checkpoint_utils.get_checkpoint_tracker_filename(checkpoints_path: str) → str#

Tracker file rescords the latest chckpoint during training to restart from.

Supports checkpoints produced by Megatron-LM.

Parameters:

checkpoints_path – Base directory where checkpoints are stored.

Returns:

The full path to the checkpoint tracker file (e.g., latest_checkpointed_iteration.txt).

bridge.training.utils.checkpoint_utils._ITERATION_DIR_MARKERS#

()

bridge.training.utils.checkpoint_utils._HF_KNOWN_WEIGHT_FILES#

(‘model.safetensors’, ‘model.safetensors.index.json’, ‘pytorch_model.bin’, ‘pytorch_model.bin.index….

bridge.training.utils.checkpoint_utils._HF_WEIGHT_GLOB_PATTERNS#

(‘model--of-.safetensors’, ‘pytorch_model--of-.bin’)

bridge.training.utils.checkpoint_utils._has_hf_weight_files(path: str) → bool#

Check whether path contains any HuggingFace-style weight file.

bridge.training.utils.checkpoint_utils.is_hf_checkpoint_dir(path: Optional[str]) → bool#

Lightweight check for a local HuggingFace full-model directory.

This is a local directory-shape check used before entering the explicit HF pretrained initialization path. It is not a full compatibility validator. A path qualifies as HF when config.json is present and the directory contains at least one HuggingFace full-model weight file. Both consolidated (model.safetensors, pytorch_model.bin) and sharded (model-XXXXX-of-XXXXX.safetensors, pytorch_model-XXXXX-of-XXXXX.bin) layouts are recognised, with or without the matching *.index.json manifest.

Parameters:

path – Filesystem path to check (may be None).

Returns:

True when path looks like a HuggingFace full-model directory.

bridge.training.utils.checkpoint_utils.is_checkpoint_iteration_directory(path: Optional[str]) → bool#

Check if path is a specific checkpoint iteration directory.

An iteration directory (e.g. /checkpoints/iter_0001000/) contains the actual checkpoint payload as opposed to a parent checkpoint directory which holds tracker files and iter_* subdirectories.

Detection order:

  1. run_config.yaml — present in all Megatron Bridge checkpoints.

  2. train_state.pt — per-iteration state file written by Bridge.

  3. metadata.json — MCore distributed checkpoint (torch_dist).

  4. .metadata — PyTorch DCP checkpoint (fsdp_dtensor).

Parameters:

path – Filesystem path to check.

Returns:

True when path contains any of the recognised checkpoint markers.

bridge.training.utils.checkpoint_utils.checkpoint_exists(checkpoints_path: Optional[str]) → bool#

Check if a checkpoint directory exists.

Supports both parent checkpoint directories (containing tracker files) and specific iteration directories (containing checkpoint markers such as run_config.yaml, metadata.json, or .metadata).

Parameters:

checkpoints_path – Path to the potential checkpoint directory.

Returns:

True if the path exists, False otherwise.

bridge.training.utils.checkpoint_utils.get_hf_source_from_model_config(
model_config: Any,
) → tuple[str | None, str | None]#

Return the Hugging Face source id and revision recorded on a model config.

Builder-backed configs record them in extra_checkpoint_metadata; legacy providers in flat hf_model_id / hf_model_revision fields. The revision is read from the same record as the id.

Parameters:

model_config – Builder-backed model config or model provider.

Returns:

(hf_model_id, hf_model_revision), each None when not recorded.

bridge.training.utils.checkpoint_utils.get_hf_model_id_from_checkpoint(
path: str | os.PathLike[str],
) → str | None#

Infer the HuggingFace model identifier recorded in a Megatron Bridge checkpoint.

Parameters:

path – Path to a Megatron checkpoint directory. This can be either the root checkpoint directory containing iter_* subdirectories or a specific iteration directory.

Returns:

The HuggingFace model identifier/path if present, otherwise None.

Raises:
  • FileNotFoundError – If the provided path does not exist.

  • NotADirectoryError – If the provided path is not a directory.

bridge.training.utils.checkpoint_utils.read_train_state(
train_state_filename: str,
) → megatron.bridge.training.state.TrainState#

Read the train state metadata from a YAML file (rank 0 only).

Reads the file on rank 0 and broadcasts the result to other ranks if torch.distributed is initialized. Otherwise, loads the file locally.

Parameters:

train_state_filename – Path to the train state YAML file.

Returns:

An initialized TrainState object.