MLflow
The MLflow applications enable users to track their Machine Learning experiments in HPC environments. The tools provide a convenient way to launch the MLflow web interface directly on the cluster, while storing experiment metadata and artifacts on the cluster filesystem.
There are two MLflow applications available through Foldspace.
Start the tool
You can start the MLflow UI on the desired cluster, for example, on the serial partition (default), with a single task (default) for up to 30 minutes (default), using the following commands:
foldspace --user <your username> --cluster <cluster name> --tool mlflow
foldspace --user <your username> --cluster leonardo --tool mlflow-aif --account <project account>
After the command is executed, a Slurm job is submitted and MLflow starts on the allocated node. The user, password and the URL necessary to access the the MLflow UI will be displayed on the terminal as soon as the service is started.
Note
The MLflow UI started through Foldspace is primarily intended to provide access to the MLflow web interface. Training jobs running on compute nodes log experiment metadata directly into the Backend Store database, without communicating with the UI server.
Backend Store
The Backend Store is a database where MLflow stores metadata about experiments and runs (e.g. ID, model ID, trace ID, tags, start and end times, parameters and metrics). Foldspace MLflow applications use an SQLite Backend Store, which is created and configured automatically if needed when the MLflow server is started in the following default locations:
$HOME/mlflow.db
/leonardo_work/<project account>/mlflow.db
Where <project account> is the project account you specify when starting the tool through the option --account.
Warning
The mlflow-aif Backend Store is shared among all project collaborators.
All users can read and write experiment metadata, and changes may affect
other collaborators. Moreover, since the Backend Store relies on SQLite, workloads
with many concurrent writers may experience lock contention and reduced
performance.
Note
The Foldspace MLflow applications are configured to show in the UI only the Backend Store DB in the default locations. If you need to use a different location, we suggest you to create a symlink to the desired location.
Backend Store DB Versions
When an MLflow application starts and finds a preexisting Backend Store DB, it will check whether the version of the existing DB is compatible with the current MLflow application. If it is compatible, it will be used as the Backend Store. Otherwise, the application will create a backup of the current DB and then will attempt an upgrade to a compatible version. If the upgrade from the current DB to a compatible version is not supported, the application will raise an error.
Warning
If you use MLflow servers besides Foldspace MLflow applications to create or access the Backend Store DB, make sure the MLflow version is aligned with the version used by the Foldspace MLflow applications. Otherwise, using Foldspace MLflow applications may lead to compatibility issues with the existing Backend Store DB.
Artifact Store
The Artifact Store is a location where MLflow stores the artifacts (e.g., model files, plots, datasets) produced during machine learning experiments. The default Artifact Store location is:
$HOME/mlruns
/leonardo_work/<project account>/mlruns
Where
<project account> is the project account you specify when starting the tool through the option --account.
Warning
The mlflow-aif Backend Store is shared among all project collaborators.
All the users that belong to the project account can write and read artifacts in this path,
and changes may affect other collaborators.
Alternative artifact locations are supported through MLFLOW_ARTIFACT_ROOT during experiment creation.
The selected path is stored in the experiment metadata and used by the Foldspace MLflow interface to access artifacts.
For experiments tracked through mlflow-aif, users should ensure that the selected location is accessible to all collaborators and,
when artifacts need to be shared, located in a project-shared workspace with appropriate permissions.
Logging Experiments
Machine learning metrics and parameters can be logged directly from training scripts without starting the Foldspace MLflow UI. The Foldspace MLflow UI can be started whenever needed to display the experiments, runs, metrics, and artifacts stored in the Backend Store.
Warning
To avoid compatibility issues, ensure that the MLflow version installed in your training environment (venv or container) matches the version used by the Foldspace application.
Implementation Example
Training jobs are not automatically connected to the Backend Store created by the Foldspace MLflow applications.
The variable MLFLOW_TRACKING_URI must be set to the desired Backend Store created.
Only runs logged to the Backend Store used by the Foldspace MLflow application will be visible in the Foldspace MLflow UI.
You can use a .env file to manage the configuration:
# .env file for MLflow client configuration
MLFLOW_TRACKING_URI="sqlite:////$HOME/mlflow.db"
MLFLOW_TRACKING_USERNAME="<USERNAME>"
MLFLOW_ARTIFACT_ROOT="<PATH_TO_ARTIFACT_STORE>"
# .env file for MLflow client configuration
MLFLOW_TRACKING_URI="sqlite:////leonardo_work/<project account>/mlflow.db"
MLFLOW_TRACKING_USERNAME="<USERNAME>"
MLFLOW_ARTIFACT_ROOT="/leonardo_work/<project account>/mlruns"
Warning
We recommend setting MLFLOW_TRACKING_USERNAME to the same username
assigned by Foldspace when starting the MLflow tool, as displayed in the
terminal output. This ensures that runs logged from your training scripts
are attributed to the same user shown in the MLflow UI, avoiding confusion
about the origin of experiments produced by the same user.
Then, you can use the MLflow client in your Python code to log experiments and access tracking data stored in the configured Backend Store and Artifact Store.
import os
import mlflow
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Configure MLflow
mlflow.set_tracking_uri(os.getenv("MLFLOW_TRACKING_URI"))
MLFLOW_EXPERIMENT_NAME = "<EXPERIMENT_NAME>"
# Create or retrieve the experiment
mlflow.set_experiment(MLFLOW_EXPERIMENT_NAME)
with mlflow.start_run():
mlflow.log_param("learning_rate", 0.001)
mlflow.log_metric("accuracy", 0.95)
# Log an artifact
mlflow.log_artifact("model_summary.txt")
Note
If you want an experiment to use a specific Artifact Store location (for
example, the shared Artifact Store created by mlflow-aif), the artifact
location must be specified when the experiment is created. MLflow does not
allow changing the artifact location through mlflow.set_experiment().
A common pattern is to first check whether the experiment already exists and create it with the desired artifact location only when needed:
MLFLOW_EXPERIMENT_NAME = "<EXPERIMENT_NAME>"
experiment = mlflow.get_experiment_by_name(MLFLOW_EXPERIMENT_NAME)
if experiment is not None:
experiment_id = experiment.experiment_id
else:
experiment_id = mlflow.create_experiment(
MLFLOW_EXPERIMENT_NAME,
artifact_location=os.getenv("MLFLOW_ARTIFACT_ROOT")
)
mlflow.set_experiment(MLFLOW_EXPERIMENT_NAME)