Skip to content

[Tune] setup_mlflow utility calls deprecated ray.train.get_context() function #58600

@dyld-w

Description

@dyld-w

What happened + What you expected to happen

The problem occurred when following Ray's Running an MLFlow Example. The tune_with_callback implementation worked perfectly, but the setup_mlflow approach resulted in:

DeprecationWarning: `ray.train.get_context` is deprecated when running in a function passed to Ray Tune. Please use `ray.tune.get_context` instead. See this issue for more context: https://github.com/ray-project/ray/issues/49454

Sure enough, the ray.air.integrations.mlflow.setup_mlflow() source code at line 150 calls ray.train.get_context() instead of ray.tune.get_context().

Versions / Dependencies

Ray: 2.51.1
Python: 3.12.11
OS: macOS Tahoe 26.1

Reproduction script

To reproduce, follow Ray's Running an MLFlow Example, specifically the setup_mlflow utility portion:

import os
import tempfile
import time

import mlflow

from ray import tune
from ray.air.integrations.mlflow import setup_mlflow

def evaluation_fn(step, width, height):
    return (0.1 + width * step / 100) ** (-1) + height * 0.1

def train_function_mlflow(config):
    tracking_uri = config.pop("tracking_uri", None)
    setup_mlflow(
        config,
        experiment_name="setup_mlflow_example",
        tracking_uri=tracking_uri,
    )

    # Hyperparameters
    width, height = config["width"], config["height"]

    for step in range(config.get("steps", 100)):
        # Iterative training function - can be any arbitrary training procedure
        intermediate_score = evaluation_fn(step, width, height)
        # Log the metrics to mlflow
        mlflow.log_metrics(dict(mean_loss=intermediate_score), step=step)
        # Feed the score back to Tune.
        tune.report({"iterations": step, "mean_loss": intermediate_score})
        time.sleep(0.1)

def tune_with_setup(mlflow_tracking_uri, finish_fast=False):
    # Set the experiment, or create a new one if does not exist yet.
    mlflow.set_tracking_uri(mlflow_tracking_uri)
    mlflow.set_experiment(experiment_name="setup_mlflow_example")

    tuner = tune.Tuner(
        train_function_mlflow,
        tune_config=tune.TuneConfig(num_samples=5),
        run_config=tune.RunConfig(
            name="mlflow",
        ),
        param_space={
            "width": tune.randint(10, 100),
            "height": tune.randint(0, 100),
            "steps": 5 if finish_fast else 100,
            "tracking_uri": mlflow.get_tracking_uri(),
        },
    )
    results = tuner.fit()

smoke_test = True

if smoke_test:
    mlflow_tracking_uri = os.path.join(tempfile.gettempdir(), "mlruns")
else:
    mlflow_tracking_uri = "<MLFLOW_TRACKING_URI>"

tune_with_setup(mlflow_tracking_uri, finish_fast=smoke_test)
if not smoke_test:
    df = mlflow.search_runs(
        [mlflow.get_experiment_by_name("setup_mlflow_example").experiment_id]
    )
    print(df)

Issue Severity

Low: It annoys or frustrates me.

Metadata

Metadata

Assignees

Labels

bugSomething that is supposed to be working; but isn'tcommunity-backlogtech-debtThe issue that's due to tech debttriageNeeds triage (eg: priority, bug/not-bug, and owning component)tuneTune-related issues

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions