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_config

Load a YAML config and resolve all $import references.

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 $import references.

This is the primary config loading entry point. It loads the YAML file, resolves any imports / $import directives, 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:

  1. If schema_type is provided, it is used.

  2. 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 BaseModel schemas, or a validated dict / list for TypedDict / 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.