LION.utils.spec¶
Specs: a LION component written as the call that builds it, in JSON.
LION writes them, as metadata, when it saves a component; they are not meant to
be written by hand. A spec is plain JSON in which a dict with "_target_"
means “call this”:
- {“_target_”: “LION.models.CNNs.UNets.Unet.UNet”,
- “model_parameters”: {“_target_”: “LION.models.LIONmodel.LIONModelParameter”,
“inChan”: 1, …}}
_target_ is the import path of a class, _args_ its positional arguments
and every other key a keyword argument. "_call_": false refers to a class or
function without calling it. A dict without _target_ (in older files) is a
LIONParameter. The keys follow Hydra’s instantiate convention.
to_spec(obj)writes the spec of an object. An object’s arguments are its attributes with the names of its constructor’s arguments (LION’s components keep each argument under its own name; torch losses and metrics do too). A class can definespec_kwargs()to give them itself. A torch optimizer is written with its hyperparameters; its parameters are given when building. A learning-rate scheduler is written with its arguments; its optimizer is given when building.build(spec)makes the object again. A spec names code to run, so only targets in allowed modules are imported, and only classes are called (plusnumpy.array); functions are only referred to. Add your own modules withallow=.X.instantiate(source), on LION’s components (LIONmodel,LIONDataset,Experiment), makes the component a saved file describes, throughinstantiate(spec, cls, base).X.load(source)also restores its state (a model’s weights).
Functions
|
Make the object a spec describes. |
|
A spec's target path and its built keyword arguments, without calling it (to call another class, e.g. the one |
|
A value as JSON: plain JSON types as they are, everything else as the spec of the call that makes it. |
|
|
|
The import path of a class or function. |
|
Make the component a spec describes, as |
|
Whether |
|
A value a record could only name ( |
|
What the component's |
|
Saved metadata, as a dict: the path of a |
|
The class or function at an import path, if it is allowed. |
|
The class to make for a spec naming |
|
The spec of an object: its class and the arguments that build it. |
|
Names in |
|
Warn about names in |
- LION.utils.spec.build(spec, allow=(), **kwargs)[source]¶
Make the object a spec describes.
allow: more module prefixes (e.g.["my_package."]) whose classes may be built.kwargsare added to the top-level call, for arguments that are live objects, e.g.build(optimizer_spec, params=model.parameters()).
- LION.utils.spec.build_arguments(spec, allow=())[source]¶
A spec’s target path and its built keyword arguments, without calling it (to call another class, e.g. the one
Model.load()is called on).
- LION.utils.spec.encode(value)[source]¶
A value as JSON: plain JSON types as they are, everything else as the spec of the call that makes it.
- LION.utils.spec.encode_or_name(value, what='it')[source]¶
encode(value), or, for a value a spec can’t hold (a lambda, a class that doesn’t keep its arguments),{"unsaved": repr, "reason": ...}with a warning. For records, which describe what they can and name the rest.
- LION.utils.spec.instantiate(spec, cls, base, allow=(), **arguments)[source]¶
Make the component a spec describes, as
cls(seetarget_class);argumentsreplace recorded ones. Used by theinstantiate()class methods of LION’s components.
- LION.utils.spec.is_allowed(path, allow=())[source]¶
Whether
buildmay importpath(ALLOWED_MODULES,ALLOWED_TARGETS, and the module prefixes inallow).
- LION.utils.spec.parameters(spec, name, cls, base, allow=())[source]¶
What the component’s
default_parameters()gives, as the spec records it (its argumentname, e.g."model_parameters"), to change and make the component with. The class’s defaults if the spec has none (it was made with them). Used by theparameters_from_file()class methods.
- LION.utils.spec.read(source)[source]¶
Saved metadata, as a dict: the path of a
.json(or of a model’s files, e.g."model.pt"or"model": its.jsonis read), a dict already read from one, or aLIONParameter(e.g. a record).
- LION.utils.spec.resolve(path, allow=())[source]¶
The class or function at an import path, if it is allowed.
- LION.utils.spec.target_class(path, cls, base, allow=())[source]¶
The class to make for a spec naming
path, whencls.instantiate(orcls.load) is called: the class the spec names ifclsis the base class (LIONmodel,LIONDataset,Experiment), which must derive from it; otherwisecls, with a warning if the spec names another class.
- LION.utils.spec.to_spec(obj)[source]¶
The spec of an object: its class and the arguments that build it.