lit_mlflow.callback
source module lit_mlflow.callback
Classes
-
MlFlowAutoCallback — Initialize callback state and supported MLflow logger types.
source class MlFlowAutoCallback(verbose: bool = True, patch_device_monitor: bool = True, serialization_format: Literal['pickle', 'pt2'] = 'pickle', input_example_fn: Callable[[Any], Tensor | tuple[Tensor, ...] | list[Tensor]] | None = None)
Bases : Callback
Initialize callback state and supported MLflow logger types.
Parameters
-
verbose : bool — Stored verbosity setting; it does not currently change behavior.
-
patch_device_monitor : bool — Whether to patch device statistics logging during setup.
-
serialization_format : Literal['pickle', 'pt2'] — MLflow model serialization format.
-
input_example_fn : Callable[[Any], Tensor | tuple[Tensor, ...] | list[Tensor]] | None — Select the model's positional tensor inputs from a training batch for PT2 export.
Attributes
-
state_key : str — Identifier for the state of the callback.
-
client : MlflowClient | None — Get the selected logger's MLflow client, if a logger is available.
Methods
-
setup — Disable MLflow autologging, configure logging, and mark the run as running.
-
teardown — Mark the run finished and terminate it when the fitting stage ends.
-
on_fit_start — Do nothing when fitting begins.
-
on_fit_end — Do nothing when fitting ends.
-
on_sanity_check_start — Do nothing when the validation sanity check starts.
-
on_sanity_check_end — Do nothing when the validation sanity check ends.
-
on_train_batch_start — Capture the first model input when PT2 serialization is enabled.
-
on_train_batch_end — Do nothing when a training batch ends.
-
on_train_epoch_start — Do nothing when a training epoch begins.
-
on_train_epoch_end — Log callback metrics as floats using the current epoch as the step.
-
on_validation_epoch_start — Do nothing when a validation epoch begins.
-
on_validation_epoch_end — Do nothing when a validation epoch ends.
-
on_test_epoch_start — Do nothing when a test epoch begins.
-
on_test_epoch_end — Do nothing when a test epoch ends.
-
on_predict_epoch_start — Do nothing when a prediction epoch begins.
-
on_predict_epoch_end — Do nothing when a prediction epoch ends.
-
on_validation_batch_start — Do nothing when a validation batch begins.
-
on_validation_batch_end — Do nothing when a validation batch ends.
-
on_test_batch_start — Do nothing when a test batch begins.
-
on_test_batch_end — Do nothing when a test batch ends.
-
on_predict_batch_start — Do nothing when a prediction batch begins.
-
on_predict_batch_end — Do nothing when a prediction batch ends.
-
on_train_start — Tag the run for training and log epochs, optimizer defaults, and early stopping settings.
-
on_train_end — Log early stopping results, the model summary, and the serialized model.
-
on_validation_start — Set the run's mode tag to validating when a logger is available.
-
on_validation_end — Do nothing when validation ends.
-
on_test_start — Set the run's mode tag to testing on global rank zero.
-
on_test_end — Do nothing when testing ends.
-
on_predict_start — Do nothing when prediction begins.
-
on_predict_end — Do nothing when prediction ends.
-
on_exception — Mark the MLflow run as failed when trainer execution raises an exception.
source property MlFlowAutoCallback.client: MlflowClient | None
Get the selected logger's MLflow client, if a logger is available.
Returns
-
MlflowClient | None — The MLflow client, or None when no supported logger is selected.
source method MlFlowAutoCallback.setup(trainer: pl.Trainer, pl_module: pl.LightningModule, stage: str) → None
Disable MLflow autologging, configure logging, and mark the run as running.
Setup also logs cluster and training dataset metadata and optionally patches device statistics monitors.
Parameters
-
trainer : pl.Trainer — Trainer providing loggers, callbacks, and data.
-
pl_module : pl.LightningModule — Model passed to dataset metadata logging.
-
stage : str — Lightning stage invoking setup; unused.
source method MlFlowAutoCallback.teardown(trainer: pl.Trainer, pl_module: pl.LightningModule, stage: str) → None
Mark the run finished and terminate it when the fitting stage ends.
Parameters
-
trainer : pl.Trainer — Trainer whose global rank is checked.
-
pl_module : pl.LightningModule — Model passed by the callback hook; unused.
-
stage : str — Lightning stage that has ended.
source method MlFlowAutoCallback.on_fit_start(trainer: pl.Trainer, pl_module: pl.LightningModule) → None
Do nothing when fitting begins.
Parameters
-
trainer : pl.Trainer — Trainer invoking the hook.
-
pl_module : pl.LightningModule — Model being fitted.
source method MlFlowAutoCallback.on_fit_end(trainer: pl.Trainer, pl_module: pl.LightningModule) → None
Do nothing when fitting ends.
Parameters
-
trainer : pl.Trainer — Trainer invoking the hook.
-
pl_module : pl.LightningModule — Model being fitted.
source method MlFlowAutoCallback.on_sanity_check_start(trainer: pl.Trainer, pl_module: pl.LightningModule) → None
Do nothing when the validation sanity check starts.
Parameters
-
trainer : pl.Trainer — Trainer invoking the hook.
-
pl_module : pl.LightningModule — Model being validated.
source method MlFlowAutoCallback.on_sanity_check_end(trainer: pl.Trainer, pl_module: pl.LightningModule) → None
Do nothing when the validation sanity check ends.
Parameters
-
trainer : pl.Trainer — Trainer invoking the hook.
-
pl_module : pl.LightningModule — Model being validated.
source method MlFlowAutoCallback.on_train_batch_start(trainer: pl.Trainer, pl_module: pl.LightningModule, batch: Any, batch_idx: int) → None
Capture the first model input when PT2 serialization is enabled.
Parameters
-
trainer : pl.Trainer — Trainer invoking the hook.
-
pl_module : pl.LightningModule — Model being trained.
-
batch : Any — Current training batch.
-
batch_idx : int — Index of the batch.
Raises
-
ValueError
source method MlFlowAutoCallback.on_train_batch_end(trainer: pl.Trainer, pl_module: pl.LightningModule, outputs: STEP_OUTPUT, batch: Any, batch_idx: int) → None
Do nothing when a training batch ends.
Parameters
-
trainer : pl.Trainer — Trainer invoking the hook.
-
pl_module : pl.LightningModule — Model being trained.
-
outputs : STEP_OUTPUT — Output from the training step.
-
batch : Any — Current training batch.
-
batch_idx : int — Index of the batch.
Note
When present, outputs["loss"] is normalized by accumulate_grad_batches.
source method MlFlowAutoCallback.on_train_epoch_start(trainer: pl.Trainer, pl_module: pl.LightningModule) → None
Do nothing when a training epoch begins.
Parameters
-
trainer : pl.Trainer — Trainer invoking the hook.
-
pl_module : pl.LightningModule — Model being trained.
source method MlFlowAutoCallback.on_train_epoch_end(trainer: pl.Trainer, pl_module: pl.LightningModule) → None
Log callback metrics as floats using the current epoch as the step.
Parameters
-
trainer : pl.Trainer — Trainer providing the callback metrics.
-
pl_module : pl.LightningModule — Model providing the current epoch.
source method MlFlowAutoCallback.on_validation_epoch_start(trainer: pl.Trainer, pl_module: pl.LightningModule) → None
Do nothing when a validation epoch begins.
Parameters
-
trainer : pl.Trainer — Trainer invoking the hook.
-
pl_module : pl.LightningModule — Model being validated.
source method MlFlowAutoCallback.on_validation_epoch_end(trainer: pl.Trainer, pl_module: pl.LightningModule) → None
Do nothing when a validation epoch ends.
Parameters
-
trainer : pl.Trainer — Trainer invoking the hook.
-
pl_module : pl.LightningModule — Model being validated.
source method MlFlowAutoCallback.on_test_epoch_start(trainer: pl.Trainer, pl_module: pl.LightningModule) → None
Do nothing when a test epoch begins.
Parameters
-
trainer : pl.Trainer — Trainer invoking the hook.
-
pl_module : pl.LightningModule — Model being tested.
source method MlFlowAutoCallback.on_test_epoch_end(trainer: pl.Trainer, pl_module: pl.LightningModule) → None
Do nothing when a test epoch ends.
Parameters
-
trainer : pl.Trainer — Trainer invoking the hook.
-
pl_module : pl.LightningModule — Model being tested.
source method MlFlowAutoCallback.on_predict_epoch_start(trainer: pl.Trainer, pl_module: pl.LightningModule) → None
Do nothing when a prediction epoch begins.
Parameters
-
trainer : pl.Trainer — Trainer invoking the hook.
-
pl_module : pl.LightningModule — Model making predictions.
source method MlFlowAutoCallback.on_predict_epoch_end(trainer: pl.Trainer, pl_module: pl.LightningModule) → None
Do nothing when a prediction epoch ends.
Parameters
-
trainer : pl.Trainer — Trainer invoking the hook.
-
pl_module : pl.LightningModule — Model making predictions.
source method MlFlowAutoCallback.on_validation_batch_start(trainer: pl.Trainer, pl_module: pl.LightningModule, batch: Any, batch_idx: int, dataloader_idx: int = 0) → None
Do nothing when a validation batch begins.
Parameters
-
trainer : pl.Trainer — Trainer invoking the hook.
-
pl_module : pl.LightningModule — Model being validated.
-
batch : Any — Current validation batch.
-
batch_idx : int — Index of the batch.
-
dataloader_idx : int — Index of the validation dataloader.
source method MlFlowAutoCallback.on_validation_batch_end(trainer: pl.Trainer, pl_module: pl.LightningModule, outputs: STEP_OUTPUT, batch: Any, batch_idx: int, dataloader_idx: int = 0) → None
Do nothing when a validation batch ends.
Parameters
-
trainer : pl.Trainer — Trainer invoking the hook.
-
pl_module : pl.LightningModule — Model being validated.
-
outputs : STEP_OUTPUT — Output from the validation step.
-
batch : Any — Current validation batch.
-
batch_idx : int — Index of the batch.
-
dataloader_idx : int — Index of the validation dataloader.
source method MlFlowAutoCallback.on_test_batch_start(trainer: pl.Trainer, pl_module: pl.LightningModule, batch: Any, batch_idx: int, dataloader_idx: int = 0) → None
Do nothing when a test batch begins.
Parameters
-
trainer : pl.Trainer — Trainer invoking the hook.
-
pl_module : pl.LightningModule — Model being tested.
-
batch : Any — Current test batch.
-
batch_idx : int — Index of the batch.
-
dataloader_idx : int — Index of the test dataloader.
source method MlFlowAutoCallback.on_test_batch_end(trainer: pl.Trainer, pl_module: pl.LightningModule, outputs: STEP_OUTPUT, batch: Any, batch_idx: int, dataloader_idx: int = 0) → None
Do nothing when a test batch ends.
Parameters
-
trainer : pl.Trainer — Trainer invoking the hook.
-
pl_module : pl.LightningModule — Model being tested.
-
outputs : STEP_OUTPUT — Output from the test step.
-
batch : Any — Current test batch.
-
batch_idx : int — Index of the batch.
-
dataloader_idx : int — Index of the test dataloader.
source method MlFlowAutoCallback.on_predict_batch_start(trainer: pl.Trainer, pl_module: pl.LightningModule, batch: Any, batch_idx: int, dataloader_idx: int = 0) → None
Do nothing when a prediction batch begins.
Parameters
-
trainer : pl.Trainer — Trainer invoking the hook.
-
pl_module : pl.LightningModule — Model making predictions.
-
batch : Any — Current prediction batch.
-
batch_idx : int — Index of the batch.
-
dataloader_idx : int — Index of the prediction dataloader.
source method MlFlowAutoCallback.on_predict_batch_end(trainer: pl.Trainer, pl_module: pl.LightningModule, outputs: Any, batch: Any, batch_idx: int, dataloader_idx: int = 0) → None
Do nothing when a prediction batch ends.
Parameters
-
trainer : pl.Trainer — Trainer invoking the hook.
-
pl_module : pl.LightningModule — Model making predictions.
-
outputs : Any — Output from the prediction step.
-
batch : Any — Current prediction batch.
-
batch_idx : int — Index of the batch.
-
dataloader_idx : int — Index of the prediction dataloader.
source method MlFlowAutoCallback.on_train_start(trainer: pl.Trainer, pl_module: pl.LightningModule) → None
Tag the run for training and log epochs, optimizer defaults, and early stopping settings.
Parameters
-
trainer : pl.Trainer — Trainer providing configuration, optimizers, and callbacks.
-
pl_module : pl.LightningModule — Model passed by the callback hook; unused.
source method MlFlowAutoCallback.on_train_end(trainer: pl.Trainer, pl_module: pl.LightningModule) → None
Log early stopping results, the model summary, and the serialized model.
Parameters
-
trainer : pl.Trainer — Trainer providing callbacks and the global rank.
-
pl_module : pl.LightningModule — Model to summarize and serialize.
source method MlFlowAutoCallback.on_validation_start(trainer: pl.Trainer, pl_module: pl.LightningModule) → None
Set the run's mode tag to validating when a logger is available.
Parameters
-
trainer : pl.Trainer — Trainer invoking the hook; unused.
-
pl_module : pl.LightningModule — Model being validated; unused.
source method MlFlowAutoCallback.on_validation_end(trainer: pl.Trainer, pl_module: pl.LightningModule) → None
Do nothing when validation ends.
Parameters
-
trainer : pl.Trainer — Trainer invoking the hook.
-
pl_module : pl.LightningModule — Model being validated.
source method MlFlowAutoCallback.on_test_start(trainer: pl.Trainer, pl_module: pl.LightningModule) → None
Set the run's mode tag to testing on global rank zero.
Parameters
-
trainer : pl.Trainer — Trainer whose global rank is checked.
-
pl_module : pl.LightningModule — Model being tested; unused.
source method MlFlowAutoCallback.on_test_end(trainer: pl.Trainer, pl_module: pl.LightningModule) → None
Do nothing when testing ends.
Parameters
-
trainer : pl.Trainer — Trainer invoking the hook.
-
pl_module : pl.LightningModule — Model being tested.
source method MlFlowAutoCallback.on_predict_start(trainer: pl.Trainer, pl_module: pl.LightningModule) → None
Do nothing when prediction begins.
Parameters
-
trainer : pl.Trainer — Trainer invoking the hook.
-
pl_module : pl.LightningModule — Model making predictions.
source method MlFlowAutoCallback.on_predict_end(trainer: pl.Trainer, pl_module: pl.LightningModule) → None
Do nothing when prediction ends.
Parameters
-
trainer : pl.Trainer — Trainer invoking the hook.
-
pl_module : pl.LightningModule — Model making predictions.
source method MlFlowAutoCallback.on_exception(trainer: pl.Trainer, pl_module: pl.LightningModule, exception: BaseException) → None
Mark the MLflow run as failed when trainer execution raises an exception.
Parameters
-
trainer : pl.Trainer — Trainer whose global rank is checked.
-
pl_module : pl.LightningModule — Model passed by the callback hook; unused.
-
exception : BaseException — Exception that interrupted execution; unused.