nvalchemi.training.create_model_spec#
- nvalchemi.training.create_model_spec(target, **kwargs)[source]#
Build a
BaseSpecinstance fortargetwith the given kwargs.A new Pydantic model class is dynamically created via
pydantic.create_model(), one field per kwarg, each annotated by_resolve_annotation(). The resulting spec is JSON-serializable withmodel_dump_json()and reconstructible withcreate_model_spec_from_json().Non-empty
list/tuplekwargs containingBaseSpecitems are annotated so each dynamic spec schema survives JSON dump and rehydration, andBaseSpec.build()then rebuilds each spec item while preserving non-spec items. Empty collections are stored as-is. Nested collections (e.g.list[list[BaseSpec]]) are not traversed; wrap them in a serializable spec object or flatten the collection. A JSON round-trip preserves tuple-valued spec sequences when the target constructor annotates the parameter as a tuple; otherwise JSON arrays rehydrate as lists.- Parameters:
target (Any) – The target importable callable. Must accept all
**kwargsas keyword arguments and must not declare any positional-only parameters.**kwargs (Any) – Hyperparameters for
target. Registered types (torch.Tensor,torch.dtype,torch.device, and any user-registered types) are handled via the type-serializer registry. Other values must themselves be JSON-serializable by Pydantic.
- Returns:
A dynamically subclassed
BaseSpecinstance named"{target.__name__}Spec"with one field per kwarg plus the two metadata fields.- Return type:
- Raises:
TypeError – If
targethas positional-only parameters, or if**kwargscontains names absent from the signature while the signature has no**kwargsparameter.
Examples
>>> import torch.nn as nn >>> spec = create_model_spec(nn.Linear, in_features=8, out_features=4) >>> module = spec.build() >>> (module.in_features, module.out_features) (8, 4)