pipeline
ls_mlkit.pipeline
BaseCallback
Source code in src/ls_mlkit/pipeline/callback.py
33 34 35 36 37 38 39 40 41 42 | |
on_event(event, *args, **kwargs)
abstractmethod
On event
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
CallbackEvent
|
the event to trigger |
required |
*args
|
the arguments to pass to the callback |
()
|
|
**kwargs
|
the keyword arguments to pass to the callback |
{}
|
Source code in src/ls_mlkit/pipeline/callback.py
34 35 36 37 38 39 40 41 42 | |
CallbackManager
Source code in src/ls_mlkit/pipeline/callback.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 71 72 73 74 75 76 | |
add_callback(callback)
Add a callback
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
callback
|
BaseCallback
|
the callback to add |
required |
Source code in src/ls_mlkit/pipeline/callback.py
49 50 51 52 53 54 55 56 | |
add_callbacks(callbacks)
Add a list of callbacks
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
callbacks
|
Optional[List[BaseCallback]]
|
the callbacks to add |
required |
Source code in src/ls_mlkit/pipeline/callback.py
58 59 60 61 62 63 64 65 | |
trigger(event, *args, **kwargs)
Trigger all callbacks for a given event
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
CallbackEvent
|
the event to trigger |
required |
*args
|
the arguments to pass to the callback |
()
|
|
**kwargs
|
the keyword arguments to pass to the callback |
{}
|
Source code in src/ls_mlkit/pipeline/callback.py
67 68 69 70 71 72 73 74 75 76 | |
DistributedPipeline
Bases: BasePipeline
Source code in src/ls_mlkit/pipeline/distributed_pipeline.py
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 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 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 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 290 291 292 293 294 295 296 | |
__init__(model, dataset, optimizers, training_config, log_config, logger, collate_fn=None, seed=42, callbacks=None, **kwargs)
Initialize the DistributedPipeline
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
the model to train |
required |
dataset
|
Union[Dataset, Dataset]
|
the dataset to train on |
required |
optimizers
|
Tuple[Optimizer, LambdaLR]
|
the optimizers to use for training |
required |
training_config
|
DistributedTrainingConfig
|
the training configuration |
required |
log_config
|
LogConfig
|
the logging configuration |
required |
logger
|
Logger
|
the logger to use for logging |
required |
collate_fn
|
Optional[Callable]
|
the collate function to use for the dataset. Defaults to None. |
None
|
seed
|
int
|
the seed to use for the random number generator. Defaults to 42. |
42
|
Source code in src/ls_mlkit/pipeline/distributed_pipeline.py
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 151 152 153 154 | |
trigger_callbacks(event, **kwargs)
Trigger all callbacks for a given event
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
CallbackEvent
|
the event to trigger |
required |
**kwargs
|
the keyword arguments to pass to the callback |
{}
|
Source code in src/ls_mlkit/pipeline/distributed_pipeline.py
285 286 287 288 289 290 291 292 293 294 295 296 | |
DistributedTrainingConfig
Bases: TrainingConfig
Source code in src/ls_mlkit/pipeline/distributed_pipeline.py
19 20 21 22 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 53 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 79 80 81 82 83 84 85 86 87 88 | |
__init__(n_epochs=100, batch_size=4, device='cuda', save_strategy='epochs', save_dir=None, save_steps=10, save_epochs=1, save_total_limit=5, num_workers=4, train_shuffle=True, eval_strategy=None, eval_steps=500, eval_epochs=1, grad_clip_strategy='norm', max_grad_norm=1.0, max_grad_value=1.0, gradient_accumulation_steps=1, mixed_precision='fp16', find_unused_parameters=False, **kwargs)
Initialize the DistributedTrainingConfig
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_epochs
|
int
|
the number of epochs. Defaults to 100. |
100
|
batch_size
|
int
|
the batch size. Defaults to 4. |
4
|
device
|
str
|
the device to use for training. Defaults to "cuda". |
'cuda'
|
save_strategy
|
Literal["epochs", "steps", None]
|
the strategy determines whether to save the model and when to save it. Defaults to "epochs". |
'epochs'
|
save_dir
|
str
|
the directory to save the model. Defaults to None. |
None
|
save_steps
|
int
|
the number of steps to save the model. Defaults to 10. |
10
|
save_epochs
|
int
|
the number of epochs to save the model. Defaults to 1. |
1
|
save_total_limit
|
int
|
the maximum number of checkpoints to save. Defaults to 5. |
5
|
num_workers
|
int
|
the number of workers to use for data loading. Defaults to 4. |
4
|
train_shuffle
|
bool
|
whether to shuffle the training data. Defaults to True. |
True
|
eval_strategy
|
Literal["epochs", "steps"]
|
the strategy determines whether to evaluate the model and when to evaluate it. Defaults to None. |
None
|
eval_steps
|
int
|
the number of steps to evaluate the model. Defaults to 500. |
500
|
eval_epochs
|
int
|
the number of epochs to evaluate the model. Defaults to 1. |
1
|
grad_clip_strategy
|
Literal["norm", "value", None]
|
the strategy determines whether to clip the gradient and how to clip it. Defaults to "norm". |
'norm'
|
max_grad_norm
|
float
|
the maximum gradient norm to clip the gradient. Defaults to 1.0. |
1.0
|
max_grad_value
|
float
|
the maximum gradient value to clip the gradient. Defaults to 1.0. |
1.0
|
gradient_accumulation_steps
|
int
|
the number of steps to accumulate gradients before updating the model. Defaults to 1. |
1
|
mixed_precision
|
str
|
the mixed precision to use for training. Defaults to "fp16". |
'fp16'
|
Source code in src/ls_mlkit/pipeline/distributed_pipeline.py
21 22 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 53 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 79 80 81 82 83 84 85 86 87 88 | |
BasePipeline
Source code in src/ls_mlkit/pipeline/pipeline.py
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 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 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 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 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 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 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 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 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 484 485 486 487 488 | |
__init__(model, dataset, optimizers, training_config, log_config, logger, collate_fn=None, callbacks=None, load_checkpoint=True, *args, **kwargs)
Initialize the BasePipeline
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
the model to train |
required |
dataset
|
Union[Dataset, Dataset]
|
the dataset to train on |
required |
optimizers
|
Tuple[Optimizer, LRScheduler]
|
the optimizers to use for training |
required |
training_config
|
TrainingConfig
|
the training configuration |
required |
log_config
|
LogConfig
|
the logging configuration |
required |
logger
|
Logger | None
|
the logger to use for logging |
required |
collate_fn
|
Optional[Callable]
|
the collate function to use for the dataset. Defaults to None. |
None
|
Source code in src/ls_mlkit/pipeline/pipeline.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 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 | |
compute_loss(model, batch)
abstractmethod
Compute the loss
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
the model to train |
required |
batch
|
dict
|
the batch of data |
required |
Returns:
| Type | Description |
|---|---|
Tensor | dict
|
Tensor | dict: loss Tensor or a dictionary containing the loss key |
Source code in src/ls_mlkit/pipeline/pipeline.py
178 179 180 181 182 183 184 185 186 187 188 | |
train()
Train the model
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in src/ls_mlkit/pipeline/pipeline.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |
train_an_epoch()
Train the model for one epoch
Returns:
| Type | Description |
|---|---|
|
None |
Source code in src/ls_mlkit/pipeline/pipeline.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | |
train_a_step(batch)
Train the model for one step
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
Dict[str, Any]
|
the batch of data |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
a dictionary containing the loss key |
Source code in src/ls_mlkit/pipeline/pipeline.py
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 | |
gradient_clip()
Clip the gradient
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in src/ls_mlkit/pipeline/pipeline.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | |
eval()
Evaluate the model
Source code in src/ls_mlkit/pipeline/pipeline.py
309 310 | |
save()
Save the checkpoint
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in src/ls_mlkit/pipeline/pipeline.py
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 407 408 409 410 | |
load()
Load the checkpoint
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in src/ls_mlkit/pipeline/pipeline.py
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 | |
add_callbacks(callbacks)
Add a list of callbacks
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
callbacks
|
List[BaseCallback] | None
|
the callbacks to add |
required |
Source code in src/ls_mlkit/pipeline/pipeline.py
462 463 464 465 466 467 468 | |
trigger_callbacks(event, **kwargs)
Trigger all callbacks for a given event
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
CallbackEvent
|
the event to trigger |
required |
**kwargs
|
the keyword arguments to pass to the callback |
{}
|
Source code in src/ls_mlkit/pipeline/pipeline.py
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 | |
LogConfig
Source code in src/ls_mlkit/pipeline/pipeline.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | |
__init__(log_dir='logs', log_steps=100, log_epochs=1, log_strategy='epochs', *args, **kwargs)
Initialize the LogConfig
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_dir
|
str
|
directory to save the logs. Defaults to "logs". |
'logs'
|
log_steps
|
int
|
log the metrics every n steps. Defaults to 100. |
100
|
log_epochs
|
int
|
log the metrics every n epochs. Defaults to 1. |
1
|
log_strategy
|
Literal["epochs", "steps"]
|
log strategy determines whether to log the metrics and when to log them. Defaults to "epochs". |
'epochs'
|
Source code in src/ls_mlkit/pipeline/pipeline.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | |
TrainingConfig
Source code in src/ls_mlkit/pipeline/pipeline.py
17 18 19 20 21 22 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 53 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 79 80 | |
__init__(n_epochs=100, batch_size=4, device='cuda', save_strategy='epochs', save_dir=None, save_steps=10, save_epochs=1, save_total_limit=5, num_workers=4, train_shuffle=True, eval_strategy=None, eval_steps=500, eval_epochs=1, grad_clip_strategy='norm', max_grad_norm=1.0, max_grad_value=1.0, gradient_accumulation_steps=1, *args, **kwargs)
Initialize the TrainingConfig
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_epochs
|
int
|
the number of epochs |
100
|
batch_size
|
int
|
batch size. Defaults to 4. |
4
|
device
|
str
|
device in training. Defaults to "cuda". |
'cuda'
|
save_strategy
|
Literal["epochs", "steps", None]
|
save strategy determines whether to save the model and when to save it. Defaults to "epochs". |
'epochs'
|
save_dir
|
str
|
directory to save the model. Defaults to None. |
None
|
save_steps
|
int
|
save the model every n steps. Defaults to 10. |
10
|
save_epochs
|
int
|
save the model every n epochs. Defaults to 1. |
1
|
save_total_limit
|
int
|
maximum number of checkpoints to save. Defaults to 5. |
5
|
num_workers
|
int
|
number of workers to use for data loading. Defaults to 4. |
4
|
train_shuffle
|
bool
|
whether to shuffle the training data. Defaults to True. |
True
|
eval_strategy
|
Literal["epochs", "steps"]
|
evaluation strategy determines whether to evaluate the model and when to evaluate it. Defaults to None. |
None
|
eval_steps
|
int
|
evaluate the model every n steps. Defaults to 500. |
500
|
eval_epochs
|
int
|
evaluate the model every n epochs. Defaults to 1. |
1
|
grad_clip_strategy
|
Literal["norm", "value", None]
|
gradient clip strategy determines whether to clip the gradient and how to clip it. Defaults to "norm". |
'norm'
|
max_grad_norm
|
float
|
maximum gradient norm. Defaults to 1.0. |
1.0
|
max_grad_value
|
float
|
maximum gradient value. Defaults to 1.0. |
1.0
|
gradient_accumulation_steps
|
int
|
number of steps to accumulate gradients before updating the model. Defaults to 1. |
1
|
Returns:
| Type | Description |
|---|---|
|
None |
Source code in src/ls_mlkit/pipeline/pipeline.py
18 19 20 21 22 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 53 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 79 80 | |
TrainingState
Source code in src/ls_mlkit/pipeline/pipeline.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | |
__init__(current_epoch=0, current_step_in_epoch=0, current_global_step=0)
Initialize the TrainingState
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current_epoch
|
int
|
the current epoch. Defaults to 0. |
0
|
current_step_in_epoch
|
int
|
the current step in the epoch. Defaults to 0. |
0
|
current_global_step
|
int
|
the current global step. Defaults to 0. |
0
|
Source code in src/ls_mlkit/pipeline/pipeline.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | |