nemo_automodel.config.loader#

Module Contents#

Classes#

ConfigNode

A configuration node that wraps a dictionary (or parts of it) from a YAML file.

Functions#

translate_value

Convert a string token into the corresponding Python object.

_resolve_target

Resolve a dotted path to a Python object.

load_yaml_config

Load a YAML configuration file and convert it to a ConfigNode.

API#

nemo_automodel.config.loader.translate_value(v)[source]#

Convert a string token into the corresponding Python object.

This function first checks for a handful of special symbols (None/true/false), then falls back to ast.literal_eval, and finally to returning the original string if parsing fails.

Parameters:

v (str) – The raw string value to translate.

Returns:

  • None, True, or False for the special symbols

  • an int, float, tuple, list, dict, etc. if ast.literal_eval succeeds

  • the original string v if all parsing attempts fail

Return type:

The translated Python value, which may be

nemo_automodel.config.loader._resolve_target(dotted_path: str)[source]#

Resolve a dotted path to a Python object.

  1. Find the longest importable module prefix.

  2. getattr() the rest.

  3. If that fails, fall back to scanning sys.path for .py or package dirs.

class nemo_automodel.config.loader.ConfigNode(d)[source]#

A configuration node that wraps a dictionary (or parts of it) from a YAML file.

This class allows nested dictionaries and lists to be accessed as attributes and provides functionality to instantiate objects from configuration.

Initialization

Initialize the ConfigNode.

Parameters:

d (dict) – A dictionary representing configuration options.

_wrap(k, v)[source]#

Wrap a configuration value based on its type.

Parameters:
  • k (str) – The key corresponding to the value.

  • v – The value to be wrapped.

Returns:

The wrapped value.

instantiate(*args, **kwargs)[source]#

Instantiate the target object specified in the configuration.

This method looks for the “target” attribute in the configuration and resolves it to a callable function or class which is then instantiated.

Parameters:
  • *args – Positional arguments for the target instantiation.

  • **kwargs – Keyword arguments to override or add to the configuration values.

Returns:

The instantiated object.

Raises:

AttributeError – If no “target” attribute is found in the configuration.

_instantiate_value(v)[source]#

Recursively instantiate configuration values.

Parameters:

v – The configuration value.

Returns:

The instantiated value.

to_dict()[source]#

Convert the configuration node back to a dictionary.

Returns:

A dictionary representation of the configuration node.

Return type:

dict

_unwrap(v)[source]#

Recursively convert wrapped configuration values to basic Python types.

Parameters:

v – The configuration value.

Returns:

The unwrapped value.

get(key, default=None)[source]#

Retrieve a configuration value using a dotted key.

If any component of the path is missing, returns the specified default value.

Parameters:
  • key (str) – The dotted path key.

  • default – A default value to return if the key is not found.

Returns:

The configuration value or the default value.

set_by_dotted(dotted_key: str, value)[source]#

Set (or append) a value in the config using a dotted key.

e.g. set_by_dotted(“foo.bar.abc”, 1) will ensure self.foo.bar.abc == 1

__repr__(level=0)[source]#

Return a string representation of the configuration node with indentation.

Parameters:

level (int) – The current indentation level.

Returns:

An indented string representation of the configuration.

Return type:

str

_repr_value(value, level)[source]#

Format a configuration value for the string representation.

Parameters:
  • value – The configuration value.

  • level (int) – The indentation level.

Returns:

A formatted string representation of the value.

Return type:

str

__str__()[source]#

Return a string representation of the configuration node.

Returns:

The string representation.

Return type:

str

__contains__(key)[source]#

Check if a dotted key exists in the configuration.

Parameters:

key (str) – The dotted key to check.

Returns:

True if the key exists, False otherwise.

Return type:

bool

nemo_automodel.config.loader.load_yaml_config(path)[source]#

Load a YAML configuration file and convert it to a ConfigNode.

Parameters:

path (str) – The path to the YAML configuration file.

Returns:

A configuration node representing the YAML file.

Return type:

ConfigNode