config_loader#
General-purpose YAML config loading with $import resolution.
This module provides the config loading infrastructure used by both
modelopt.recipe and modelopt.torch.quantization.config. It lives
in modelopt.torch.opt (the lowest dependency layer) to avoid circular
imports.
Functions
Load a YAML config and resolve all |
|
Return the |
- load_config(config_path: str | Path | Traversable, *, schema_type: type[_SchemaT]) _SchemaT#
- load_config(config_path: str | Path | Traversable, *, schema_type: type[list[_SchemaT]]) list[_SchemaT]
- load_config(config_path: str | Path | Traversable, *, schema_type: None = None) Any
Load a YAML config and resolve all
$importreferences.This is the primary config loading entry point. It loads the YAML file, resolves any
imports/$importdirectives, and returns either a validated instance of the schema (when one is known) or the raw resolved payload.The effective schema is selected as follows:
If
schema_typeis provided, it is used.Otherwise, the schema declared by the file’s
# modelopt-schema:comment (if any) is used.
When an effective schema is selected, the resolved payload is validated and returned as an instance of that schema — e.g., a Pydantic model instance for
BaseModelschemas, or a validated dict / list forTypedDict/list[TypedDict]schemas. If neither source supplies a schema, the raw resolved dict or list is returned unchanged.Imported snippets are stricter and must always declare
modelopt-schema; they are validated during import resolution regardless of the top-level selection above.- Parameters:
config_path (str | Path | Traversable)
schema_type (Any | None)
- Return type:
Any
- peek_declared_schema(config_file)#
Return the
# modelopt-schema:path a config file declares, if any.Reads only the comment preamble – no YAML parsing and no
$importresolution – so a caller can find out what kind of config a file is before it can be loaded.modelopt.recipe.load_recipe()uses this to pick a recipe’s schema class, which is why a recipe can inherit its body from another one via a top-level$import.- Parameters:
config_file (str | Path | Traversable)
- Return type:
str | None