nvalchemi.training.create_model_spec_from_json#
- nvalchemi.training.create_model_spec_from_json(spec)[source]#
Rebuild a
BaseSpecfrom its JSON-dict form.Recursively rehydrates nested specs (detected as values that are
dictand contain a"cls_path"key). Lists of such dicts are rehydrated item-wise, preserving the collection order. Pydantic’sBeforeValidatorhooks on registered types handle the str →torch.dtype/torch.device/ dict →torch.Tensorconversions transparently.The original
timestampis preserved viaobject.__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
dictas produced bymodel_dump()or byjson.loads()on the output ofmodel_dump_json().- Returns:
A spec instance equivalent to the source, with the original
timestamppreserved.- Return type:
- Raises:
ValueError – If
specis missingcls_pathortimestamp, or ifcls_pathcannot 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