nemo_automodel.components.config.loader
nemo_automodel.components.config.loader
Module Contents
Classes
Functions
Data
API
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.
Get the raw configuration dictionary.
Check if a dotted key exists in the configuration.
Parameters:
The dotted key to check.
Returns: bool
True if the key exists, False otherwise.
Return a string representation of the configuration node with indentation.
Parameters:
The current indentation level.
If True, prefer original placeholder strings (e.g. ${VAR})
stored on _OrigValueStr values for safe logging.
Recursively instantiate configuration values.
Parameters:
The configuration value.
Returns: Any
The instantiated value.
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.
Recursively convert wrapped configuration values to basic Python types.
Parameters:
The configuration value.
Returns: Any
The unwrapped value.
Wrap a configuration value based on its type.
Parameters:
The key corresponding to the value.
The value to be wrapped.
Returns: Any
The wrapped value.
Retrieve a configuration value using a dotted key.
If any component of the path is missing, returns the specified default value.
Parameters:
The dotted path key.
A default value to return if the key is not found.
Returns: Any
The configuration value or the default value.
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:
The key to look up.
Returns: str
The string representation of the value, or None if key not found.
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:
Positional arguments for the target instantiation.
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.
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:
The path to the target object (e.g., “model.config”).
A default value to return if the path is not found.
Positional arguments for the target instantiation.
Keyword arguments to override or add to the configuration values.
Returns: Any
The instantiated object.
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
Convert the configuration node back to a dictionary.
Returns: dict[str, Any]
A dictionary representation of the configuration node.
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:
If True, resolve ${oc.env:VAR} interpolations in the returned dict.
This does not mutate the in-memory config.
If True, redact values for keys that look sensitive (token/secret/etc).
If True, prefer _orig_value (when present) for safe printing/logging.
Bases: str
String that keeps its original (unresolved) value for safe printing.
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).
Resolve a dotted path to a Python object with safety checks.
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.
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.
Load a YAML configuration file and convert it to a ConfigNode.
Parameters:
The path to the YAML configuration file.
Returns: ConfigNode
A configuration node representing the YAML file.
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}
Enable or disable loading user-defined code at runtime.
Users can also set environment variable NEMO_ENABLE_USER_MODULES=1 to enable.
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:
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_evalsucceeds - the original string
vif all parsing attempts fail