nvalchemi.training.create_model_spec#

nvalchemi.training.create_model_spec(target, **kwargs)[source]#

Build a BaseSpec instance for target with 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 with model_dump_json() and reconstructible with create_model_spec_from_json().

Non-empty list/tuple kwargs containing BaseSpec items are annotated so each dynamic spec schema survives JSON dump and rehydration, and BaseSpec.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 **kwargs as 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 BaseSpec instance named "{target.__name__}Spec" with one field per kwarg plus the two metadata fields.

Return type:

BaseSpec

Raises:

TypeError – If target has positional-only parameters, or if **kwargs contains names absent from the signature while the signature has no **kwargs parameter.

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)