unitorch.models¤
CheckpointMixin¤
from_checkpoint ¤
from_checkpoint(
ckpt_dir: str,
weight_name: Optional[str] = None,
**kwargs
)
Load model weights from a checkpoint.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ckpt_dir |
str
|
Directory path of the checkpoint. |
required |
weight_name |
str
|
Name of the weight file. |
None
|
Returns:
Type | Description |
---|---|
None |
Source code in src/unitorch/models/__init__.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
|
from_pretrained ¤
from_pretrained(
weight_path: Union[str, List[str]] = None,
state_dict: Union[Dict, List[Dict]] = None,
replace_keys: Optional[Dict] = dict(),
prefix_keys: Optional[Dict] = dict(),
)
Load pretrained weights into the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
weight_path |
str or List[str]
|
Path(s) to the pretrained weight file(s). |
None
|
state_dict |
Dict or List[Dict]
|
Pretrained state_dict(s) to load weights from. |
None
|
replace_keys |
Dict
|
Dictionary mapping keys in the pretrained state_dict to the model's keys. |
dict()
|
prefix_keys |
Dict
|
Dictionary prefix keys in the pretrained state_dict to the model's keys. |
dict()
|
Returns:
Type | Description |
---|---|
None |
Source code in src/unitorch/models/__init__.py
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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
|
save_checkpoint ¤
save_checkpoint(
ckpt_dir: str,
weight_name: Optional[str] = None,
**kwargs
)
Save the model's current state as a checkpoint.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ckpt_dir |
str
|
Directory path to save the checkpoint. |
required |
weight_name |
str
|
Name of the weight file. |
None
|
Returns:
Type | Description |
---|---|
None |
Source code in src/unitorch/models/__init__.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
|
GenericModel¤
Bases: Module
, CheckpointMixin
Source code in src/unitorch/models/__init__.py
154 155 156 |
|
device
property
¤
device
Returns the device of the model's parameters.
Returns:
Type | Description |
---|---|
torch.device: The device of the model's parameters. |
dtype
property
¤
dtype: dtype
Returns the data type of the model's parameters.
Returns:
Type | Description |
---|---|
dtype
|
torch.dtype: The data type of the model's parameters. |
init_weights ¤
init_weights()
Initialize the weights of the model.
Source code in src/unitorch/models/__init__.py
175 176 177 178 179 |
|
HfTextGenerationProcessor¤
Processor for text generation tasks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tokenizer |
PreTrainedTokenizer
|
The tokenizer to use for text generation. |
required |
max_seq_length |
int
|
Maximum sequence length. Defaults to 128. |
128
|
max_gen_seq_length |
int
|
Maximum generated sequence length. Defaults to 48. |
48
|
Source code in src/unitorch/models/processing_utils.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
detokenize ¤
detokenize(
sequences: Tensor,
skip_special_tokens: Optional[bool] = True,
)
Detokenize the sequences.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sequences |
Tensor
|
The sequences to detokenize. |
required |
skip_special_tokens |
bool
|
Whether to skip special tokens. Defaults to True. |
True
|
Returns:
Name | Type | Description |
---|---|---|
list |
The detokenized sequences. |
Source code in src/unitorch/models/processing_utils.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
|
generation ¤
generation(
text: str,
text_pair: str,
max_seq_length: Optional[int] = None,
max_gen_seq_length: Optional[int] = None,
)
Generate inputs, labels, and tokens for text generation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str
|
The input text. |
required |
text_pair |
str
|
The paired text. |
required |
max_seq_length |
int
|
Maximum sequence length. Defaults to None. |
None
|
max_gen_seq_length |
int
|
Maximum generated sequence length. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
GenericOutputs |
The generated input tokens, attention masks, label tokens, and attention masks. |
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 135 136 137 138 139 140 141 142 143 |
|
generation_inputs ¤
generation_inputs(
text: str, max_seq_length: Optional[int] = None
)
Generate inputs for text generation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str
|
The input text. |
required |
max_seq_length |
int
|
Maximum sequence length. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
GenericOutputs |
The generated input tokens and attention mask. |
Source code in src/unitorch/models/processing_utils.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
|
generation_labels ¤
generation_labels(
text: str, max_gen_seq_length: Optional[int] = None
)
Generate labels for text generation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str
|
The input text. |
required |
max_gen_seq_length |
int
|
Maximum generated sequence length. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
GenericOutputs |
The generated label tokens and attention mask. |
Source code in src/unitorch/models/processing_utils.py
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 |
|
HfTextClassificationProcessor¤
Processor for text classification tasks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tokenizer |
PreTrainedTokenizer
|
The tokenizer to use for text classification. |
required |
max_seq_length |
int
|
Maximum sequence length. Defaults to 128. |
128
|
source_type_id |
int
|
Source type ID. Defaults to 0. |
0
|
target_type_id |
int
|
Target type ID. Defaults to 1. |
1
|
position_start_id |
int
|
Start position ID. Defaults to 0. |
0
|
Source code in src/unitorch/models/processing_utils.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
|
classification ¤
classification(
text: str,
text_pair: Optional[str] = None,
max_seq_length: Optional[int] = None,
)
Generate inputs for text classification.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str
|
The input text. |
required |
text_pair |
str
|
The paired text. Defaults to None. |
None
|
max_seq_length |
int
|
Maximum sequence length. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
GenericOutputs |
The generated input tokens, token type IDs, attention mask, and position IDs. |
Source code in src/unitorch/models/processing_utils.py
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
|
HfImageClassificationProcessor¤
Processor for image classification tasks.
Initialize the HfImageClassificationProcessor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vision_processor |
BaseImageProcessor
|
The vision processor object used for image transformations. |
required |
Source code in src/unitorch/models/processing_utils.py
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
|
classification ¤
classification(image: Union[Image, str])
Perform image classification on the given image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
Image
|
The input image. |
required |
Returns:
Name | Type | Description |
---|---|---|
GenericOutputs |
The output of the image classification, including pixel values. |
Source code in src/unitorch/models/processing_utils.py
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 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 |
|
ExponentialMovingAverage¤
Bases: Module
Exponential Moving Average (EMA) for model parameters.
Initializes the ExponentialMovingAverage.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Module
|
The model to apply EMA to. |
required |
decay |
float
|
Decay rate for the EMA. Defaults to 0.9999. |
0.9999
|
tau |
int
|
Time constant for the EMA. Defaults to 2000. |
2000
|
num_steps |
int
|
Number of steps taken for the EMA. Defaults to 0. |
0
|
Source code in src/unitorch/models/modeling_ema.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
forward ¤
forward(*args, **kwargs)
Forward pass through the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args |
Variable length argument list. |
()
|
|
**kwargs |
Arbitrary keyword arguments. |
{}
|
Returns:
Type | Description |
---|---|
The output of the model. |
Source code in src/unitorch/models/modeling_ema.py
95 96 97 98 99 100 101 102 103 104 105 106 |
|
from_checkpoint ¤
from_checkpoint(
ckpt_dir: str,
weight_name: Optional[str] = None,
**kwargs
)
Load model weights from a checkpoint.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ckpt_dir |
str
|
Directory path of the checkpoint. |
required |
weight_name |
str
|
Name of the weight file. |
None
|
Returns:
Type | Description |
---|---|
None |
Source code in src/unitorch/models/modeling_ema.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
|
save_checkpoint ¤
save_checkpoint(
ckpt_dir: str,
weight_name: Optional[str] = None,
**kwargs
)
Save the model's current state as a checkpoint.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ckpt_dir |
str
|
Directory path to save the checkpoint. |
required |
weight_name |
str
|
Name of the weight file. |
None
|
Returns:
Type | Description |
---|---|
None |
Source code in src/unitorch/models/modeling_ema.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
|
step ¤
step(model)
Performs a step of EMA.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Module
|
The model to update the EMA with. |
required |
Source code in src/unitorch/models/modeling_ema.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
|