nemo_automodel.components.config.loader

View as Markdown

Module Contents

Classes

NameDescription
ConfigNodeA configuration node that wraps a dictionary (or parts of it) from a YAML file.
_OrigValueStrString that keeps its original (unresolved) value for safe printing.

Functions

NameDescription
_is_allowed_moduleReturn True if a module is safe/allowed to import.
_is_safe_attr-
_is_safe_path-
_redact-
_resolve_targetResolve a dotted path to a Python object with safety checks.
config_to_yaml_strConvert a config object to a YAML string suitable for logging/printing.
load_module_from_fileDynamically imports a module from a given file path.
load_yaml_configLoad a YAML configuration file and convert it to a ConfigNode.
resolve_yaml_env_varsResolve env var references inside a YAML-loaded container.
set_enable_user_modulesEnable or disable loading user-defined code at runtime.
translate_valueConvert a string token into the corresponding Python object.

Data

ALLOWED_IMPORT_PREFIXES

ENABLE_USER_MODULES

SAFE_BASE_DIR

SENSITIVE_KEY_SUBSTRINGS

API

class nemo_automodel.components.config.loader.ConfigNode(
d: dict[str, typing.Any],
raise_on_missing_attr: bool = True
)

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.

_original_strings
dict[str, str] = {}
_raw_config
= deepcopy(d)
raw_config
dict[str, Any]

Get the raw configuration dictionary.

nemo_automodel.components.config.loader.ConfigNode.__contains__(
key: object
) -> bool

Check if a dotted key exists in the configuration.

Parameters:

key
str

The dotted key to check.

Returns: bool

True if the key exists, False otherwise.

nemo_automodel.components.config.loader.ConfigNode.__getattr__(
key: str
) -> typing.Any
nemo_automodel.components.config.loader.ConfigNode.__repr__() -> str
nemo_automodel.components.config.loader.ConfigNode.__str__() -> str
nemo_automodel.components.config.loader.ConfigNode._format(
level: int = 0,
use_orig_values: bool = True
) -> str

Return a string representation of the configuration node with indentation.

Parameters:

level
intDefaults to 0

The current indentation level.

use_orig_values
boolDefaults to True

If True, prefer original placeholder strings (e.g. ${VAR}) stored on _OrigValueStr values for safe logging.

nemo_automodel.components.config.loader.ConfigNode._instantiate_value(
v: typing.Any
) -> typing.Any

Recursively instantiate configuration values.

Parameters:

v
Any

The configuration value.

Returns: Any

The instantiated value.

nemo_automodel.components.config.loader.ConfigNode._repr_value(
value: typing.Any,
level: int,
use_orig_values: bool = True
) -> str
nemo_automodel.components.config.loader.ConfigNode._to_dotted_path(
obj: typing.Any
) -> str

Convert a callable/class/method object to a dotted path string.

Best-effort normalization for a few common cases to produce concise, user-friendly paths.

nemo_automodel.components.config.loader.ConfigNode._unwrap(
v: typing.Any
) -> typing.Any

Recursively convert wrapped configuration values to basic Python types.

Parameters:

v
Any

The configuration value.

Returns: Any

The unwrapped value.

nemo_automodel.components.config.loader.ConfigNode._wrap(
k: str,
v: typing.Any
) -> typing.Any

Wrap a configuration value based on its type.

Parameters:

k
str

The key corresponding to the value.

v
Any

The value to be wrapped.

Returns: Any

The wrapped value.

nemo_automodel.components.config.loader.ConfigNode.get(
key: str,
default: typing.Any = None
) -> typing.Any

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
AnyDefaults to None

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

Returns: Any

The configuration value or the default value.

nemo_automodel.components.config.loader.ConfigNode.get_as_string(
key: str,
default: str | None = None
) -> str

Get the string representation of a configuration value.

If the value is a function or class (resolved from an import path), returns the original import path string. Otherwise returns the value as a string.

Parameters:

key
str

The key to look up.

Returns: str

The string representation of the value, or None if key not found.

nemo_automodel.components.config.loader.ConfigNode.instantiate(
args: typing.Any = (),
kwargs: typing.Any = {}
) -> typing.Any

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
AnyDefaults to ()

Positional arguments for the target instantiation.

**kwargs
AnyDefaults to {}

Keyword arguments to override or add to the configuration values.

Returns: Any

The instantiated object.

Raises:

  • AttributeError: If no “target” attribute is found in the configuration.
nemo_automodel.components.config.loader.ConfigNode.instantiate_path(
dotted_path: str,
default: typing.Any = None,
args: typing.Any = (),
kwargs: typing.Any = {}
) -> typing.Any

Instantiate the target object specified in the configuration by path.

If the path is not found, returns the default value.

For example, this is useful when you want to do something like:

cfg_peft = self.cfg.get(“peft”, None) if cfg_peft is not None: cfg_peft = cfg_peft.instantiate()

In this case, you first check if the dotted path (in this case “peft”) is in the configuration. If it is, you instantiate it, otherwise you return the default value (in this case None).

With instantiate_path, you can do: cfg_peft = self.cfg.instantiate_path(“peft”, default=None)

Parameters:

dotted_path
str

The path to the target object (e.g., “model.config”).

default
AnyDefaults to None

A default value to return if the path is not found.

*args
AnyDefaults to ()

Positional arguments for the target instantiation.

**kwargs
AnyDefaults to {}

Keyword arguments to override or add to the configuration values.

Returns: Any

The instantiated object.

nemo_automodel.components.config.loader.ConfigNode.set_by_dotted(
dotted_key: str,
value: typing.Any
) -> None

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

nemo_automodel.components.config.loader.ConfigNode.to_dict() -> dict[str, typing.Any]

Convert the configuration node back to a dictionary.

Returns: dict[str, Any]

A dictionary representation of the configuration node.

nemo_automodel.components.config.loader.ConfigNode.to_yaml_dict(
resolve_env: bool = False,
redact_sensitive: bool = False,
use_orig_values: bool = False
) -> dict[str, typing.Any]

Convert configuration to a YAML-ready dictionary:

  • Preserves typed scalars (ints, floats, bools)
  • Converts callables/classes/methods (e.g., target, *_fn) to dotted path strings
  • Recurses through nested ConfigNodes and lists

Parameters:

resolve_env
boolDefaults to False

If True, resolve ${oc.env:VAR} interpolations in the returned dict. This does not mutate the in-memory config.

redact_sensitive
boolDefaults to False

If True, redact values for keys that look sensitive (token/secret/etc).

use_orig_values
boolDefaults to False

If True, prefer _orig_value (when present) for safe printing/logging.

class nemo_automodel.components.config.loader._OrigValueStr()

Bases: str

String that keeps its original (unresolved) value for safe printing.

_no_env_resolve
bool
_orig_value
str
nemo_automodel.components.config.loader._is_allowed_module(
module_name: str
) -> bool

Return True if a module is safe/allowed to import.

Security policy (balanced for functionality and tests):

  • If user modules are explicitly enabled, allow everything.
  • Always allow modules that are already imported in this process.
  • Allow modules that are importable from the current PYTHONPATH/sys.path. This keeps behavior intuitive for local/test modules while still blocking truly unknown targets.
  • Fallback to explicit allowlist as a final gate (mostly relevant if find_spec returns None for the top-level name).
nemo_automodel.components.config.loader._is_safe_attr(
name: str
) -> bool
nemo_automodel.components.config.loader._is_safe_path(
p: pathlib.Path
) -> bool
nemo_automodel.components.config.loader._redact(
obj: typing.Any
) -> typing.Any
nemo_automodel.components.config.loader._resolve_target(
dotted_path: str
) -> typing.Any

Resolve a dotted path to a Python object with safety checks.

nemo_automodel.components.config.loader.config_to_yaml_str(
cfg_obj: typing.Any,
use_orig_values: bool = True
) -> str

Convert a config object to a YAML string suitable for logging/printing.

Uses original placeholder strings (e.g. ${VAR}) and original target/*_fn strings when use_orig_values is True; never includes internal keys like _original_strings. If cfg_obj is a ConfigNode, uses to_yaml_dict(); otherwise falls back to to_dict() or a plain dict.

nemo_automodel.components.config.loader.load_module_from_file(
file_path: str | pathlib.Path
) -> types.ModuleType

Dynamically imports a module from a given file path.

Intentionally permissive to support test/temporary modules. Caller is responsible for any higher-level policy checks.

nemo_automodel.components.config.loader.load_yaml_config(
path: str | pathlib.Path
) -> nemo_automodel.components.config.loader.ConfigNode

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

Parameters:

path
str

The path to the YAML configuration file.

Returns: ConfigNode

A configuration node representing the YAML file.

nemo_automodel.components.config.loader.resolve_yaml_env_vars(
obj: typing.Any
) -> typing.Any

Resolve env var references inside a YAML-loaded container.

Supported forms inside strings:

  • ${VAR} / ${VAR,default}
  • ${var.dot.var} (dots are treated as part of the env var name)
  • $VAR / $var.dot.var
  • Back-compat: ${oc.env:VAR} / ${oc.env:VAR,default}
nemo_automodel.components.config.loader.set_enable_user_modules(
allow: bool
) -> None

Enable or disable loading user-defined code at runtime.

Users can also set environment variable NEMO_ENABLE_USER_MODULES=1 to enable.

nemo_automodel.components.config.loader.translate_value(
v: typing.Any
) -> typing.Any

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: Any

The translated Python value, which may be:

  • 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
nemo_automodel.components.config.loader.ALLOWED_IMPORT_PREFIXES = ('nemo_automodel', 'torch', 'transformers', 'torchdata', 'torchao', 'liger_kerne...
nemo_automodel.components.config.loader.ENABLE_USER_MODULES = os.environ.get('NEMO_ENABLE_USER_MODULES', '').lower() in ('1', 'true', 'yes')
nemo_automodel.components.config.loader.SAFE_BASE_DIR = Path(__file__).resolve().parents[2]
nemo_automodel.components.config.loader.SENSITIVE_KEY_SUBSTRINGS = ('password', 'secret', 'token', 'apikey', 'api_key', 'authorization', 'auth')