MLflow Integration#
Custom MLflow model flavor for Darts forecasting models. Supports saving, loading,
and logging any Darts ForecastingModel (statistical, ML-based, and PyTorch-based)
to MLflow, as well as automatic logging via autolog().
See the MLflow quickstart for an end-to-end walkthrough.
Here’s a quick start example
import os
import tempfile
import mlflow
import darts.metrics as metrics
from darts.datasets import AirPassengersDataset
from darts.models import LinearRegressionModel
from darts.utils.mlflow import autolog
# dummy temporary directory for local MLflow tracking;
# use a permanent location for real use cases
tmpdir = tempfile.mkdtemp()
mlflow_db = os.path.join(tmpdir, "mlflow.db")
artifact_root = os.path.join(tmpdir, "mlruns")
mlflow.set_tracking_uri(f"sqlite:///{mlflow_db}")
# SQLite stores run metadata; artifacts default to ./mlruns unless we set a location
mlflow.set_experiment(
experiment_id=mlflow.create_experiment(
"darts-quickstart",
artifact_location=artifact_root,
)
)
# load series and create train and val splits
series = AirPassengersDataset().load()
train, val = series[:-36], series[-36:]
# two models to compare with different lookback windows
model_lags, names = [12, 24], ["one-year", "two-years"]
horizon = 12
# activate autologging and try out the models
autolog()
for lags, name in zip(model_lags, names):
with mlflow.start_run(run_name=name) as run:
model = LinearRegressionModel(lags=lags)
model.fit(train) # autolog logs params and covariate metadata
# log standalone metrics from manual predictions
pred = model.predict(n=horizon)
metrics.mae(val, pred) # time-aggregated logged as scaler metric
metrics.ae(val, pred) # time-dependent logged as stepped metric
# log backtest metrics from historical forecasts over val set
bt = model.backtest(
series=series,
start=val.start_time(),
retrain=False,
forecast_horizon=horizon,
reduction=None, # `None` logs as stepped metric, scalar otherwise
metric=[metrics.mae, metrics.rmse],
)
autolog(disable=True)
# you can launch the MLflow UI with the command below from your terminal;
# then open the returned address for example in a browser (similar to http://localhost:5000)
print(f"mlflow ui --backend-store-uri {mlflow.get_tracking_uri()}")
When autolog() is enabled, the following functionalities emit detailed logs when inside
an active MLflow run (e.g. within with mlflow.start_run():):
Calling
ForecastingModel.fit():Logs model creation parameters (
model.model_params), both as MLflow params and as amodel_params.jsonartifact.Logs target series info and covariate usage information (past, future, and static covariates) as a
series_info.jsonartifact.Stores the trained model artifact when
log_models=True(default:False), and setsdarts.model_is_pretrainedtoTrue.Logs per-epoch training and validation metrics for PyTorch-based models.
Calling
ForecastingModel.historical_forecasts(retrain=True):Logs the same model creation parameters and
series_info.jsonasfit()(overwriting any priorfit()artifacts in the same run).Stores an untrained model template (via
untrained_model()) whenlog_models=Trueand no model was logged yet in the run. Sets the run tag and model metadatadarts.model_is_pretrainedtoFalseso downstream code knows the artifact must be refit before inference. If a model was already logged (e.g. viafit()or manuallog_model()), the existing artifact is kept unchanged.
Calling any Darts metric:
Logs the result of that metric call as an MLflow metric. More information in the notes below.
Calling
ForecastingModel.backtest():Logs all evaluation metrics under
backtest_*keys. More information in the notes below.
Important
Cross-Model Run Comparability: Metric values are only comparable
across runs when the evaluation settings match. Use the same evaluation
time frame, forecast horizon, and evaluation start date for every
backtest() / metric call you intend to compare.
Note
Metric Naming Convention: Logged metric keys follow the pattern
{metric_name}{component}{quantile_or_label} (e.g.,
"mae_target0_q0_500"), where each part is included only when the
corresponding axis is present:
metric_name- the metric function name, or thenamemetric keyword argument when provided (e.g.,"mae").component- the component name: e.g.,"_target0"whencomponent_reduction=None.quantile_or_label:the quantile label: e.g.,
"_q0.500"for quantile metrics with keyword argumentq=[0.5]the quantile interval label: e.g.,
"_qi0.800"for quantile interval metrics with keyword argumentq_interval=[(0.1, 0.9)](80% interval between quantiles 0.1 and 0.9).the class label: e.g.,
"_label1"for classification metrics with keyword argumentlabelswhenlabel_reduction=None.
Backtest metrics are prefixed by "backtest_".
Note
Metric Logging and Display: Darts offers many ways of evaluating forecasting models. To offer the highest value, we adapt what is logged to MLflow based on the use case scenario.
Metric type:
Time-aggregated metrics (e.g.
mae()): Logged as scaler values.Per-time step metrics (e.g.
ae()wheretime_reduction=None): Logged as stepped metrics (one value per step in the forecast horizon).
Single or Multi-series:
Single series: Logged as explained above.
Multiple (a list of) series: When the metric’s
series_reduction=None, the logged metric is aggregated over all series using autolog’sagg_func. The detailed per-series metrics / backtest metrics are logged under a singlemetrics_per_series.jsontable run artifact.
Standalone or backtest metric:
Standalone metric: Logged as explained above.
Backtest metric: If backtest’s
reductionis other thanNone, the windowed forecast metrics are aggregated and logged as explained above. IfNone, metrics are logged as stepped MLflow metrics.Time-aggregated metrics: each step represents a specifc forecast (window) metric. Steps represent the historical forecast windows (0, 1, …, n_windows - 1).
Per-time step metrics: each step represents a step in the forecast horizon aggregated over all windows. Steps represent the steps in the forecast horizon (0, 1, …, horizon - 1).
Multi-series alignment (
series_alignautolog option):"end"(default): shorter series skip early steps/windows so their last points overlap the tail of longer series."start": shorter series contribute at early steps/windows; longer series have fewer contributing series at tail steps.
Backtest aggregate (
log_backtest_aggregateautolog option):When enabled, logs an additional scalar per backtest metric key under
backtest_agg_{metric_key}(e.g.backtest_agg_mae), computed asagg_funcover all logged steps for that key.
When components are preserved (component_reduction=None), all
series scored together must have the same number of components; names
are taken from the first series.
- darts.utils.mlflow.autolog(log_models=False, log_params=True, log_metrics=True, log_torch_metrics=True, agg_func=<function nanmean>, series_align='end', log_backtest_aggregate=False, disable=False, silent=False)[source]#
Enable (or disable) automatic MLflow logging for Darts.
For a detailed overview of logged params, metrics, and artifacts, see the detailed documentation.
- Parameters:
log_models (
bool) – IfTrue, log a model artifact when callingfit()(trained model) orhistorical_forecasts(retrain=True)(untrained template viauntrained_model()). Defaults toFalse. If a model was already logged in the run (including via manuallog_model()), autolog does not add another artifact.log_params (
bool) – IfTrue(default), log model creation parameters when callingfit()orhistorical_forecasts().log_metrics (
bool) – IfTrue(default), log the result of any Darts metric call made inside an active MLflow run, including standalone metric calls or via backtest.log_torch_metrics (
bool) – IfTrue(default), enablemlflow.pytorch.autolog(log_models=False)around PyTorch-based model training to automatically log per-epoch training and validation metrics. Only effective for PyTorch-based models.agg_func (
Callable) – Function used to aggregate a metric’s per-series values into the single value logged for a list of series (e.g.np.nanmean, the default, ornp.median). Called asagg_func(values)on a list of floats. Also used whenlog_backtest_aggregate=Trueto collapse all logged steps of a backtest metric into a single scalar.series_align (
Literal['start','end']) – How to align shorter series when multiple series differ in window or time length."end"(default) aligns from the end so shorter series skip early steps/windows;"start"aligns from the start so shorter series contribute at early steps/windows.log_backtest_aggregate (
bool) – IfTrue, log an additional scalar per backtest metric key underbacktest_agg_{metric_key}(e.g.backtest_agg_mae), computed asagg_funcover all logged steps for that key. Defaults toFalse.disable (
bool) – IfTrue, restore the originalfit()methods and stop autologging.silent (
bool) – IfTrue(defaultFalse), suppress all event logging and warnings from MLflow during autologging.
- Return type:
None
- darts.utils.mlflow.get_default_conda_env()[source]#
Return a default conda environment dict for a Darts model.
- Returns:
A conda environment specification dictionary.
- Return type:
dict
- darts.utils.mlflow.get_default_pip_requirements()[source]#
Return the default pip requirements for logging a Darts model.
- Returns:
A list of pip requirement strings.
- Return type:
list[str]
- darts.utils.mlflow.load_model(model_uri, dst_path=None, **kwargs)[source]#
Load a Darts model from an MLflow model URI.
- Parameters:
model_uri (
str) – An MLflow model URI, e.g."runs:/<run_id>/model","models:/<name>/<version>", or a localfile:///...path.dst_path (
str|None) – Optional local path for downloading remote artifacts.**kwargs – Additional keyword arguments forwarded to the model’s
load()method (e.g.map_locationfor a TorchForecastingModel).
- Returns:
The loaded Darts forecasting model.
- Return type:
ForecastingModel
- darts.utils.mlflow.log_model(model, **kwargs)[source]#
Log a Darts model to the current MLflow run, using the Darts MLflow flavor.
This is a thin wrapper around
mlflow.models.Model.log()that supplies the Darts flavor for saving/loading; every other argument is forwarded as-is. See the MLflow documentation for the full list of accepted parameters (e.g.name,registered_model_name,conda_env,pip_requirements,metadata,tags, …).- Parameters:
model (
ForecastingModel) – A fitted DartsForecastingModelinstance.**kwargs – Forwarded to
mlflow.models.Model.log(). Usenameto set the run-relative artifact path.artifact_pathparameter is deprecated by MLflow and not exposed here.
- Returns:
MLflow ModelInfo object containing model_uri, run_id, artifact_path, model_id, timestamps, and other metadata about the logged model.
- Return type:
ModelInfo
Notes
signatureandinput_exampleare currently not supported, as they are used to support serving and input validation in the MLflow pyfunc flavor, which is not implemented for Darts models.
- darts.utils.mlflow.save_model(model, path, conda_env=None, code_paths=None, mlflow_model=None, signature=None, input_example=None, pip_requirements=None, extra_pip_requirements=None, metadata=None)[source]#
Save a Darts forecasting model in MLflow format.
Produces an MLflow model directory at
pathcontaining:The serialized Darts model (delegated to the model’s own
save()method).An
MLmodelYAML file with flavor metadata.conda.yamlandrequirements.txtenvironment files.
- Parameters:
model (
ForecastingModel) – A fitted DartsForecastingModelinstance.path (
str) – Local filesystem path where the model directory will be created.conda_env (
dict|str|None) – A conda environment specification (dict or path to aconda.yaml). IfNone, a default environment is generated.code_paths (
list[str] |None) – A list of local filesystem paths to Python file dependencies (or directories containing file dependencies). These files are prepended to the system path when the model is loaded.mlflow_model (
Model|None) – Optional MLflow Model object to use for saving. When provided (typically byModel.log()), this model instance is used instead of creating a new one.signature (
ModelSignature|None) – Unsupported, see notes. Anmlflow.models.ModelSignatureinstance describing model input/output. Usemlflow.models.infer_signature()to automatically generate from example inputs.input_example (
Any|None) – Unsupported, see notes. An example input for the model (used by MLflow UI).pip_requirements (
list[str] |None) – A list of pip requirement strings. Overridesconda_envpip section when provided.extra_pip_requirements (
list[str] |None) – A list of additional pip requirement strings to add to the model’s environment, in addition to the default requirements.metadata (
dict[str,Any] |None) – Optional dictionary of custom metadata to store in theMLmodelfile.
- Return type:
None
Notes
signatureandinput_exampleparams are currently not supported, as they are used to support serving and input validation in the MLflow pyfunc flavor, which is not implemented for Darts models. They are accepted as params for simplifying potential future extensibility, and to keep in line with MLflow API conventions.