.. _mlflow_tool_card: 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. .. image:: ../img/foldspace_mlflow_screen_example.png :alt: Example of Foldspace Mlflow interface :width: 700px :align: center | There are two MLflow applications available through Foldspace. .. dropdown:: mlflow :animate: fade-in-slide-down :color: primary :open: ``mlflow`` allows users to track Machine Learning experiments of their own. This application is not envisioned for sharing MLflow experiments with other users. .. note:: Available in all clusters. Pitagora users may want to always specify the ``--partition`` option, since the Mlflow tools run in a container, and containers are not allowed on the Pitagora serial partition. .. dropdown:: mlflow-aif :animate: fade-in-slide-down :color: primary :open: ``mlflow-aif`` is an application devised for the AI community and allows users to track Machine Learning experiments on shared (project specific) cluster workspaces, such as WORK directories. The tracking information is stored in the shared workspace, allowing multiple users to collaborate. .. note:: Available only in Leonardo. 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: .. tab-set:: .. tab-item:: mlflow .. code-block:: bash foldspace --user --cluster --tool mlflow .. tab-item:: mlflow-aif .. code-block:: bash foldspace --user --cluster leonardo --tool mlflow-aif --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: .. tab-set:: .. tab-item:: mlflow ``$HOME/mlflow.db`` .. tab-item:: mlflow-aif ``/leonardo_work//mlflow.db`` Where ```` 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: .. tab-set:: .. tab-item:: mlflow ``$HOME/mlruns`` .. tab-item:: mlflow-aif ``/leonardo_work//mlruns`` Where ```` 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: .. tab-set:: .. tab-item:: mlflow .. code-block:: bash # .env file for MLflow client configuration MLFLOW_TRACKING_URI="sqlite:////$HOME/mlflow.db" MLFLOW_TRACKING_USERNAME="" MLFLOW_ARTIFACT_ROOT="" .. tab-item:: mlflow-aif .. code-block:: bash # .env file for MLflow client configuration MLFLOW_TRACKING_URI="sqlite:////leonardo_work//mlflow.db" MLFLOW_TRACKING_USERNAME="" MLFLOW_ARTIFACT_ROOT="/leonardo_work//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. .. code-block:: python 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 = "" # 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: .. code-block:: python MLFLOW_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)