Skip to navigation

nemo_automodel.components.utils.yaml_utils

View as MarkdownOpen in Claude

Module Contents

Functions

NameDescription
_dotted_pathReturn <module>.<qualname> for a class or function.
_enum_representerRepresent enums in YAML.
_function_representerRepresent functions in YAML.
_generation_config_representerRepresent transformers GenerationConfig objects in YAML.
_partial_representerRepresent functools.partial objects in YAML.
_safe_object_representerGeneral object representer for YAML.
_torch_dtype_representerRepresent torch dtypes in YAML.
safe_yaml_representersContext manager for safely adding and removing custom YAML representers.

API

nemo_automodel.components.utils.yaml_utils._dotted_path(
obj: object
) -> str

Return <module>.<qualname> for a class or function.

Raises:

  • AttributeError: If obj has no __qualname__ or is not defined in an imported module. _safe_object_representer relies on this to fall back to the object’s class.
nemo_automodel.components.utils.yaml_utils._enum_representer(
dumper: yaml.SafeDumper,
data: enum.Enum
) -> yaml.Node

Represent enums in YAML.

nemo_automodel.components.utils.yaml_utils._function_representer(
dumper: yaml.SafeDumper,
data: types.FunctionType | type
) -> yaml.Node

Represent functions in YAML.

nemo_automodel.components.utils.yaml_utils._generation_config_representer(
dumper: yaml.SafeDumper,
data: transformers.GenerationConfig
) -> yaml.Node

Represent transformers GenerationConfig objects in YAML.

nemo_automodel.components.utils.yaml_utils._partial_representer(
dumper: yaml.SafeDumper,
data: functools.partial[object]
) -> yaml.Node

Represent functools.partial objects in YAML.

nemo_automodel.components.utils.yaml_utils._safe_object_representer(
dumper: yaml.SafeDumper,
data: object
) -> yaml.Node

General object representer for YAML.

This function is a fallback for objects that don’t have specific representers. If the object has qualname attr, the target is set to f”{inspect.getmodule(obj).name}.{obj.qualname}”. If the object does not have a qualname attr, the target is set from its class attr. The call key is used to indicate whether the target should be called to create an instance.

Parameters:

dumper
yaml.SafeDumper

The YAML dumper to use for serialization.

data
object

The object to serialize.

Returns: yaml.Node

The YAML representation of the data.

nemo_automodel.components.utils.yaml_utils._torch_dtype_representer(
dumper: yaml.SafeDumper,
data: torch.dtype
) -> yaml.Node

Represent torch dtypes in YAML.

nemo_automodel.components.utils.yaml_utils.safe_yaml_representers() -> collections.abc.Generator[None, None, None]

Context manager for safely adding and removing custom YAML representers.

Temporarily adds custom representers for functions, classes, and other objects to the YAML SafeDumper, and restores the original representers when exiting the context.

Usage: with safe_yaml_representers(): yaml_str = yaml.safe_dump(my_complex_object)