unitorch.models¤
CheckpointMixin¤
Mixin that adds checkpoint save/load and pretrained-weight loading to a model.
replace_keys_in_state_dict
class-attribute
instance-attribute
¤
replace_keys_in_state_dict: Dict[str, str] = {}
prefix_keys_in_state_dict
class-attribute
instance-attribute
¤
prefix_keys_in_state_dict: Dict[str, str] = {}
from_checkpoint ¤
from_checkpoint(
ckpt_dir: str,
weight_name: Optional[str] = None,
**kwargs
) -> None
Load model weights from ckpt_dir.
Source code in src/unitorch/models/__init__.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | |
save_checkpoint ¤
save_checkpoint(
ckpt_dir: str,
weight_name: Optional[str] = None,
**kwargs
) -> None
Save model weights to ckpt_dir.
Source code in src/unitorch/models/__init__.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |
from_pretrained ¤
from_pretrained(
weight_path: Optional[Union[str, List[str]]] = None,
state_dict: Optional[Union[Dict, List[Dict]]] = None,
replace_keys: Optional[Dict[str, str]] = None,
prefix_keys: Optional[Dict[str, str]] = None,
) -> None
Load pretrained weights into the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weight_path
|
Optional[Union[str, List[str]]]
|
Path(s) to pretrained weight file(s). |
None
|
state_dict
|
Optional[Union[Dict, List[Dict]]]
|
Pretrained state dict(s) to load from. |
None
|
replace_keys
|
Optional[Dict[str, str]]
|
Regex substitution rules |
None
|
prefix_keys
|
Optional[Dict[str, str]]
|
Regex prefix rules |
None
|
Source code in src/unitorch/models/__init__.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |
GenericModel¤
Bases: Module, CheckpointMixin
Base class for all unitorch models.
Source code in src/unitorch/models/__init__.py
140 141 | |
_init_weights ¤
_init_weights(module: Module) -> None
Source code in src/unitorch/models/__init__.py
143 144 145 146 147 148 149 150 | |
init_weights ¤
init_weights() -> None
Initialise all submodule weights with the default scheme.
Source code in src/unitorch/models/__init__.py
152 153 154 | |
HfTextGenerationProcessor¤
Processor for encoder-decoder text generation tasks.
Source code in src/unitorch/models/processing_utils.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | |
generation_inputs ¤
generation_inputs(
text: str, max_seq_length: Optional[int] = None
) -> GenericOutputs
Tokenise text into padded encoder input IDs and attention mask.
Source code in src/unitorch/models/processing_utils.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | |
generation_labels ¤
generation_labels(
text: str, max_gen_seq_length: Optional[int] = None
) -> GenericOutputs
Tokenise text into padded decoder label IDs and attention mask.
Source code in src/unitorch/models/processing_utils.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | |
generation ¤
generation(
text: str,
text_pair: str,
max_seq_length: Optional[int] = None,
max_gen_seq_length: Optional[int] = None,
) -> GenericOutputs
Return encoder inputs, decoder inputs, and decoder labels for a text pair.
Source code in src/unitorch/models/processing_utils.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | |
detokenize ¤
detokenize(
sequences: Tensor, skip_special_tokens: bool = True
) -> list
Decode a 2-D or 3-D token-ID tensor back to strings.
Source code in src/unitorch/models/processing_utils.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |
HfTextClassificationProcessor¤
Processor for BERT-style text classification tasks.
Source code in src/unitorch/models/processing_utils.py
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | |
classification ¤
classification(
text: str,
text_pair: Optional[str] = None,
max_seq_length: Optional[int] = None,
) -> GenericOutputs
Tokenise text (and optional pair) for sequence classification.
Source code in src/unitorch/models/processing_utils.py
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 | |
HfImageClassificationProcessor¤
Processor for image classification tasks using a HuggingFace vision processor.
Source code in src/unitorch/models/processing_utils.py
412 413 | |
classification ¤
classification(image: Union[Image, str]) -> GenericOutputs
Preprocess image into pixel values ready for a vision model.
Source code in src/unitorch/models/processing_utils.py
415 416 417 418 419 420 421 422 | |
ExponentialMovingAverage¤
Bases: Module
Exponential Moving Average (EMA) wrapper for a model's parameters.
The effective decay at step t is decay * (1 - exp(-t / tau)),
which ramps from 0 up to decay so early updates are not over-smoothed.
Source code in src/unitorch/models/modeling_ema.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 | |
from_checkpoint ¤
from_checkpoint(
ckpt_dir: str,
weight_name: Optional[str] = None,
**kwargs
) -> None
Load EMA weights from ckpt_dir.
Source code in src/unitorch/models/modeling_ema.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | |
save_checkpoint ¤
save_checkpoint(
ckpt_dir: str,
weight_name: Optional[str] = None,
**kwargs
) -> None
Save EMA weights to ckpt_dir.
Source code in src/unitorch/models/modeling_ema.py
54 55 56 57 58 59 60 61 62 63 64 | |
forward ¤
forward(*args, **kwargs)
Delegate forward pass to the EMA model.
Source code in src/unitorch/models/modeling_ema.py
66 67 68 | |
step ¤
step(model: Module) -> None
Update EMA parameters with one step from model.
Source code in src/unitorch/models/modeling_ema.py
70 71 72 73 74 75 76 77 78 | |