Index
optimus_dl.recipe.train.mixins.execution
¶
TrainingContextMixin
¶
Mixin for setting up the training context (precision, scaling, devices).
Responsible for initializing PyTorch's AMP (Automatic Mixed Precision) and GradScaler based on the optimization configuration. This ensures consistent precision settings across the training loop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
optimization_config
|
OptimizationConfig
|
Configuration containing AMP settings. |
required |
Source code in optimus_dl/recipe/train/mixins/execution/context_mixin.py
setup_training_context(device)
¶
Initialize AMP context and Gradient Scaler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
device
|
The target compute device. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A dictionary containing: |
dict[str, Any]
|
|
dict[str, Any]
|
|
dict[str, Any]
|
|
dict[str, Any]
|
|
Source code in optimus_dl/recipe/train/mixins/execution/context_mixin.py
TrainingInterruptionMixin
¶
Mixin for gracefully handling training interruptions.
Provides a mechanism to catch KeyboardInterrupt (Ctrl+C) and trigger a
safe shutdown sequence, which typically involves saving a final checkpoint
to ensure progress is not lost.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
save_freq
|
int
|
Frequency of regular checkpoints. If 0, saving is disabled. |
0
|
output_path
|
str | None
|
Path where checkpoints are saved. |
None
|
checkpoint_callback
|
Callable[..., None] | None
|
Callable to execute for saving the checkpoint. |
None
|
Source code in optimus_dl/recipe/train/mixins/execution/interruption_mixin.py
handle_training_interruption(iteration, collective, **kwargs)
¶
Handle interruption by saving a final checkpoint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iteration
|
int
|
The current training iteration count. |
required |
collective
|
Collective | None
|
The distributed collective instance. |
required |
**kwargs
|
Any
|
Additional arguments to pass to the checkpoint callback. |
{}
|
Source code in optimus_dl/recipe/train/mixins/execution/interruption_mixin.py
TrainingIterationMixin
¶
Mixin for executing a complete training step with gradient accumulation.
Encapsulates the core training logic: 1. Forward Pass: Runs the model and criterion, measuring time. 2. Backward Pass: Scales gradients and backpropagates, handling loss parallelism if applicable. 3. Optimization: Unscales gradients, clips norms, and steps the optimizer. 4. Logging: Records detailed performance metrics (forward/backward times, grad norms, etc.).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
optimization_config
|
OptimizationConfig
|
Configuration for optimization (accumulation steps, clipping). |
required |
log_freq
|
int
|
Frequency of metric logging. |
1
|
Source code in optimus_dl/recipe/train/mixins/execution/iteration_mixin.py
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 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 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 | |
accumulation_context(model, is_last_microbatch)
¶
Get the appropriate context manager for gradient accumulation.
For FSDP/DDP models, this handles synchronization (e.g., disabling all-reduce during accumulation steps).
Source code in optimus_dl/recipe/train/mixins/execution/iteration_mixin.py
execute_backward_pass(loss, scaler)
¶
Run the backward pass with gradient scaling.
Handles loss_parallel context if the loss is a DTensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loss
|
Tensor
|
The computed loss tensor. |
required |
scaler
|
Any
|
The gradient scaler. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Execution time in milliseconds. |
Source code in optimus_dl/recipe/train/mixins/execution/iteration_mixin.py
execute_forward_pass(model, criterion, batch, amp_ctx, requested_protocols=None)
¶
Run the forward pass inside an AMP context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
BaseModel
|
The model to run. |
required |
criterion
|
BaseCriterion
|
The loss function. |
required |
batch
|
Any
|
The input data. |
required |
amp_ctx
|
Any
|
The autocast context manager. |
required |
requested_protocols
|
set[str] | None
|
Protocols requested by the metrics system. |
None
|
Returns:
| Type | Description |
|---|---|
ForwardPassResult
|
ForwardPassResult with the computed loss, exposed protocols, and execution time. |
Source code in optimus_dl/recipe/train/mixins/execution/iteration_mixin.py
execute_optimizer_step(optimizer, model, scaler, clip_grad_norm=None)
¶
Perform the optimization step.
Includes gradient unscaling, optional gradient clipping, and the optimizer step itself. Updates the scaler state afterwards.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
optimizer
|
Optimizer
|
The optimizer. |
required |
model
|
BaseModel
|
The model (needed for clipping gradients). |
required |
scaler
|
Any
|
The gradient scaler. |
required |
clip_grad_norm
|
float | None
|
Maximum norm for gradient clipping. |
None
|
Returns:
| Type | Description |
|---|---|
OptimizerStepResult
|
OptimizerStepResult with execution time and the computed gradient norm. |
Source code in optimus_dl/recipe/train/mixins/execution/iteration_mixin.py
log_batch_metrics(elapsed_batch_get, elapsed_forward, elapsed_backward, acc_steps)
¶
Log timing metrics for data loading and forward/backward passes.
Source code in optimus_dl/recipe/train/mixins/execution/iteration_mixin.py
log_memory_usage()
¶
Log GPU memory usage statistics.
Source code in optimus_dl/recipe/train/mixins/execution/iteration_mixin.py
log_optimizer_metrics(elapsed_optimizer, grad_norm, lr_scheduler, optimizer)
¶
Log optimizer performance, gradient norms, and learning rates.
Source code in optimus_dl/recipe/train/mixins/execution/iteration_mixin.py
run_training_iteration(model, optimizer, criterion, train_data_iter, training_context, lr_scheduler=None, metric_engine=None)
¶
Execute one full training iteration, including gradient accumulation.
This is the main driver for a training step. It loops acc_steps times
to accumulate gradients before performing a single optimizer update.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
BaseModel
|
The model to train. |
required |
optimizer
|
Optimizer
|
The optimizer. |
required |
criterion
|
BaseCriterion
|
The loss function. |
required |
train_data_iter
|
Iterator
|
Iterator yielding training batches. |
required |
training_context
|
dict[str, Any]
|
Dict with scaler, amp_ctx, etc. |
required |
lr_scheduler
|
Any | None
|
Optional learning rate scheduler. |
None
|
metric_engine
|
Any | None
|
Optional MetricEngine for training metrics. |
None
|
Source code in optimus_dl/recipe/train/mixins/execution/iteration_mixin.py
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 | |
Modules and Sub-packages¶
context_mixin: Training context mixin for AMP and gradient scaler setup.interruption_mixin: Training interruption mixin for handling errors and keyboard interrupts.iteration_mixin: Training iteration mixin for orchestrating complete training iterations.