lit_mlflow
source package lit_mlflow
Classes
-
MlFlowAutoCallback — Initialize callback state and supported MLflow logger types.
-
DbxMLFlowLogger — Initialize an MLflow logger and configure Databricks integration when available.
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.
source class DbxMLFlowLogger(experiment_name: str = 'lightning_logs', run_name: str | None = None, tracking_uri: str | None = mlflow.get_tracking_uri(), tags: dict[str, Any] | None = None, save_dir: str | None = './mlruns', log_model: Literal[True, False, 'all'] = False, prefix: str = '', artifact_location: str | None = None, run_id: str | None = None)
Bases : MLFlowLogger
Initialize an MLflow logger and configure Databricks integration when available.
In Databricks, suppress Py4J gateway logs and propagate notebook credentials before initializing the MLflow client. Outside Databricks, emit a warning.
Parameters
-
experiment_name : str — Name of the MLflow experiment.
-
run_name : str | None — Optional name for the MLflow run.
-
tracking_uri : str | None — URI of the MLflow tracking server.
-
tags : dict[str, Any] | None — Tags to associate with the run.
-
save_dir : str | None — Local directory used when no tracking URI is provided.
-
log_model : Literal[True, False, 'all'] — Whether and when to log model checkpoints.
-
prefix : str — Prefix for logged metric keys.
-
artifact_location : str | None — Location for the experiment's artifacts.
-
run_id : str | None — ID of an existing run to reuse.
Attributes
-
name : Optional[str] — Get the experiment id.
-
version : Optional[str] — Get the run id.
-
root_dir : Optional[str] — Return the root directory where all versions of an experiment get saved, or
Noneif the logger does not save data locally. -
log_dir : Optional[str] — Return directory the current version of the experiment gets saved, or
Noneif the logger does not save data locally. -
group_separator : str — Return the default separator used by the logger to group the data into subfolders.
-
save_dir : Optional[str] — The root file directory in which MLflow experiments are saved.
-
experiment : MlflowClient — Get the MLflow client, using the notebook ID as experiment ID when available.
-
run_id : str | None — Get the logger's run ID, preferring the active MLflow run.
-
experiment_id : Optional[str] — Create the experiment if it does not exist to get the experiment id.
source property DbxMLFlowLogger.experiment: MlflowClient
Get the MLflow client, using the notebook ID as experiment ID when available.
Returns
-
MlflowClient — The MLflow client provided by the parent logger.
source property DbxMLFlowLogger.run_id: str | None
Get the logger's run ID, preferring the active MLflow run.
Returns
-
str | None — The active run ID, the logger's existing run ID, or None.