bridge.training.utils.config_utils#

Module Contents#

Classes#

_ConfigContainerBase

Base configuration container for Megatron Bridge configurations.

Functions#

apply_run_config_backward_compat

Apply backward compatibility transformations to run config.

_sanitize_dataclass_config

Remove init=False fields from a dataclass config dict for backward compatibility.

_get_init_false_fields

Get the set of field names with init=False for a dataclass.

_resolve_target_class

Resolve a target string to a class.

Data#

API#

bridge.training.utils.config_utils.logger#

‘getLogger(…)’

bridge.training.utils.config_utils.T#

‘TypeVar(…)’

bridge.training.utils.config_utils.apply_run_config_backward_compat(
config_dict: dict[str, Any],
) dict[str, Any]#

Apply backward compatibility transformations to run config.

This function handles dataclass config fields that should not be passed to the constructor when loading older checkpoints. It automatically detects init=False fields by inspecting the target class.

The entire config is sanitized recursively to handle init=False fields in any part of the configuration hierarchy.

Parameters:

config_dict – The full run configuration dictionary.

Returns:

The config dictionary with backward compatibility fixes applied.

bridge.training.utils.config_utils._sanitize_dataclass_config(
config: dict[str, Any],
_visited: set | None = None,
) dict[str, Any]#

Remove init=False fields from a dataclass config dict for backward compatibility.

This function automatically detects fields with init=False by inspecting the target class specified in the config’s target field. This handles cases where older checkpoints serialized computed fields that should not be passed to init.

The function recursively processes nested dicts that may also be dataclass configs.

Parameters:
  • config – A configuration dictionary, potentially with a target field.

  • _visited – Internal set to track visited objects and prevent infinite recursion.

Returns:

The sanitized configuration with init=False fields removed.

bridge.training.utils.config_utils._get_init_false_fields(target_class: type) frozenset[str]#

Get the set of field names with init=False for a dataclass.

Parameters:

target_class – A dataclass type to inspect.

Returns:

A frozenset of field names that have init=False.

bridge.training.utils.config_utils._resolve_target_class(target: str) type | None#

Resolve a target string to a class.

Parameters:

target – A fully qualified class path (e.g., “module.submodule.ClassName”).

Returns:

The resolved class, or None if resolution fails.

class bridge.training.utils.config_utils._ConfigContainerBase#

Base configuration container for Megatron Bridge configurations.

Provides:

  • Custom validation

  • Versioning metadata

  • YAML/Dict serialization and deserialization

  • Dictionary-style attribute access (config[“attr”] and config.get(“attr”, default))

__version__: str#

‘0.1.0’

classmethod from_dict(
config_dict: dict[str, Any],
mode: megatron.bridge.utils.instantiate_utils.InstantiationMode = InstantiationMode.STRICT,
) bridge.training.utils.config_utils.T#

Create a config container from a dictionary using instantiate.

Parameters:
  • config_dict – Dictionary containing configuration

  • mode – Serialization mode (strict or lenient)

Returns:

A new instance of this class initialized with the dictionary values

classmethod from_yaml(
yaml_path: str,
mode: megatron.bridge.utils.instantiate_utils.InstantiationMode = InstantiationMode.LENIENT,
) bridge.training.utils.config_utils.T#

Create a config container from a YAML file.

Parameters:
  • yaml_path – Path to the YAML file

  • mode – Serialization mode (strict or lenient)

Returns:

A new instance of this class initialized with the YAML file values

to_dict() dict[str, Any]#

Convert the config container to a dictionary.

Also converts any nested dataclasses (both ConfigContainer and regular dataclasses) to dictionaries recursively.

Returns:

Dictionary representation of this config

classmethod _convert_value_to_dict(value: Any) Any#

Recursively convert a value to a dictionary representation.

Handles:

  • ConfigContainer instances (using to_dict)

  • Classes which implement a to_cfg_dict method

  • Regular dataclasses (converting each non-private field)

  • Lists and tuples (converting each element)

  • Dictionaries (converting each value)

  • Other types (kept as-is)

Parameters:

value – The value to convert

Returns:

The converted value

to_yaml(yaml_path: Optional[str] = None) None#

Save the config container to a YAML file.

Parameters:

yaml_path – Path where to save the YAML file. If None, prints to stdout.

.. note::

Printing to stdout is deprecated and will be removed in a future version. Use print_yaml() instead.

print_yaml() None#

Print the config container to the console in YAML format.

__deepcopy__(memo)#

Support for deep copying.