nvalchemi.training.create_model_spec_from_json#

nvalchemi.training.create_model_spec_from_json(spec)[source]#

Rebuild a BaseSpec from its JSON-dict form.

Recursively rehydrates nested specs (detected as values that are dict and contain a "cls_path" key). Lists of such dicts are rehydrated item-wise, preserving the collection order. Pydantic’s BeforeValidator hooks on registered types handle the str → torch.dtype / torch.device / dict → torch.Tensor conversions transparently.

The original timestamp is preserved via object.__setattr__() rather than stamped fresh, so that a round-tripped spec remains byte-identical (up to JSON-whitespace) with its source.

Parameters:

spec (dict[str, Any]) – A dict as produced by model_dump() or by json.loads() on the output of model_dump_json().

Returns:

A spec instance equivalent to the source, with the original timestamp preserved.

Return type:

BaseSpec

Raises:

ValueError – If spec is missing cls_path or timestamp, or if cls_path cannot be imported / resolves to a non-callable. The underlying exception is preserved as __cause__.

Examples

>>> import json, torch.nn as nn
>>> s = create_model_spec(nn.Linear, in_features=4, out_features=2)
>>> dumped = json.loads(s.model_dump_json())
>>> s2 = create_model_spec_from_json(dumped)
>>> s2.timestamp == s.timestamp
True