chuchichaestli.models.spec
Recording how an object was constructed, so it can be rebuilt exactly.
Classes:
| Name | Description |
|---|---|
C3liSpecError |
Raised when a spec names something it may not load. |
ModelSpec |
What it takes to rebuild a model. |
ModelSpecMixin |
Records every constructor argument, so |
Functions:
| Name | Description |
|---|---|
load_model |
Rebuild a model from a file and load its weights. |
read_model_state |
Read a state dict and the spec stored beside it, |
read_spec |
Return the spec a file carries, or |
write_model_state |
Write a state dict with the spec that rebuilds its model, if any. |
C3liSpecError
Bases: ValueError
Raised when a spec names something it may not load.
ModelSpec
dataclass
ModelSpec(cls: str, kwargs: Mapping[str, Any] = dict())
What it takes to rebuild a model.
Attributes:
| Name | Type | Description |
|---|---|---|
cls |
str
|
Importable name of the class, as |
kwargs |
Mapping[str, Any]
|
Every constructor argument, defaults included. |
json_limit |
int
|
Most characters a spec is written as or read from. |
Methods:
| Name | Description |
|---|---|
allow |
Let a spec from an untrusted file name |
build |
Construct the object this spec describes. |
decode_arg |
Rebuild whatever |
encode_arg |
Reduce a constructor argument to something JSON can hold. |
from_dict |
Rebuild a spec from its mapping form. |
from_json |
Rebuild a spec from its JSON form. |
import_class |
Import the class an importable name points at. |
import_name |
Return a |
qualname |
Return an importable |
resolve |
Return the object an import name refers to, if a spec may name it. |
to_dict |
Return a JSON-serializable mapping of the spec. |
to_json |
Return the spec as one JSON string, for file metadata. |
allow
classmethod
allow(obj: Any) -> Any
Let a spec from an untrusted file name obj, and return it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Class, function or enum to allow. |
required |
Source code in src/chuchichaestli/models/spec.py
149 150 151 152 153 154 155 156 157 | |
build
build(*, trusted: bool = False, **overrides: Any) -> Any
Construct the object this spec describes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trusted
|
bool
|
Import any name, as for a file from a trusted source. |
False
|
overrides
|
Any
|
Arguments to replace, for rebuilding a variant. |
{}
|
Source code in src/chuchichaestli/models/spec.py
335 336 337 338 339 340 341 342 343 344 | |
decode_arg
classmethod
decode_arg(value: Any, trusted: bool = False) -> Any
Rebuild whatever encode_arg reduced, nested specs included.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Encoded argument to restore. |
required |
trusted
|
bool
|
Import any name, as for a file from a trusted source. |
False
|
Source code in src/chuchichaestli/models/spec.py
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | |
encode_arg
classmethod
encode_arg(value: Any) -> Any
Reduce a constructor argument to something JSON can hold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Argument to reduce. |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Source code in src/chuchichaestli/models/spec.py
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |
from_dict
classmethod
from_dict(state: Mapping[str, Any]) -> ModelSpec
Rebuild a spec from its mapping form.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
Mapping[str, Any]
|
Mapping as returned by |
required |
Source code in src/chuchichaestli/models/spec.py
366 367 368 369 370 371 372 373 | |
from_json
classmethod
from_json(text: str) -> ModelSpec
Rebuild a spec from its JSON form.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
String as returned by |
required |
Raises:
| Type | Description |
|---|---|
C3liSpecError
|
If |
Source code in src/chuchichaestli/models/spec.py
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 | |
import_class
staticmethod
import_class(path: str) -> type
Import the class an importable name points at.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Name of the form |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/chuchichaestli/models/spec.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
import_name
staticmethod
import_name(obj: Any) -> str
Return a "module:QualName" that import_class resolves to obj itself.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Class, function or other named object. |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If no name imports back to |
Source code in src/chuchichaestli/models/spec.py
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | |
qualname
staticmethod
qualname(obj: type) -> str
Return an importable "module:QualName" for a class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
type
|
Class to name. |
required |
Source code in src/chuchichaestli/models/spec.py
100 101 102 103 104 105 106 107 | |
resolve
classmethod
resolve(path: str, trusted: bool = False) -> Any
Return the object an import name refers to, if a spec may name it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Name of the form |
required |
trusted
|
bool
|
Import any name, as for a file from a trusted source. |
False
|
Raises:
| Type | Description |
|---|---|
C3liSpecError
|
If |
Source code in src/chuchichaestli/models/spec.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | |
to_dict
to_dict() -> dict[str, Any]
Return a JSON-serializable mapping of the spec.
Source code in src/chuchichaestli/models/spec.py
346 347 348 349 | |
to_json
to_json() -> str
Return the spec as one JSON string, for file metadata.
Raises:
| Type | Description |
|---|---|
TypeError
|
If the string is longer than |
Source code in src/chuchichaestli/models/spec.py
351 352 353 354 355 356 357 358 359 360 361 362 363 364 | |
ModelSpecMixin
Bases: InitArgMixin
Records every constructor argument, so .spec can rebuild the object.
Attributes:
| Name | Type | Description |
|---|---|---|
spec |
ModelSpec
|
Return what it takes to rebuild this object. |
spec
property
spec: ModelSpec
Return what it takes to rebuild this object.
Raises:
| Type | Description |
|---|---|
AttributeError
|
If the arguments were never recorded, which means
the class defines no |
load_model
load_model(
path: Path,
strict: bool = True,
trusted: bool = False,
**overrides: Any,
) -> torch.nn.Module
Rebuild a model from a file and load its weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
File to read. |
required |
strict
|
bool
|
Whether every key must match, as for |
True
|
trusted
|
bool
|
Let the spec name anything, for a file you trust. |
False
|
overrides
|
Any
|
Constructor arguments to replace before building. Changing
one that affects a tensor's shape makes the weights unloadable,
which |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the file carries no spec, since the architecture cannot be inferred from weights alone. |
Source code in src/chuchichaestli/models/spec.py
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 | |
read_model_state
read_model_state(
path: Path,
) -> tuple[dict[str, torch.Tensor], ModelSpec | None]
Read a state dict and the spec stored beside it, None if there is none.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
File to read. |
required |
Source code in src/chuchichaestli/models/spec.py
432 433 434 435 436 437 438 439 | |
read_spec
read_spec(path: Path) -> ModelSpec | None
Return the spec a file carries, or None if it carries none.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
File to read. |
required |
Source code in src/chuchichaestli/models/spec.py
422 423 424 425 426 427 428 429 | |
write_model_state
write_model_state(
path: Path,
state: dict[str, torch.Tensor],
spec: ModelSpec | None = None,
) -> None
Write a state dict with the spec that rebuilds its model, if any.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
File to write. |
required |
state
|
dict[str, torch.Tensor]
|
Tensors to store. |
required |
spec
|
ModelSpec | None
|
How to rebuild the model, stored beside the tensors. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the suffix names no known format. |
Source code in src/chuchichaestli/models/spec.py
442 443 444 445 446 447 448 449 450 451 452 453 454 455 | |