Skip to content

Blog#

Executing experiments with Ray

Ray is an open-source unified framework for scaling AI and Python applications. You can use Ray as an execution backend to run MLtraq experiments using its native joblib integration.

The example shows how to set up a local Ray cluster, running an experiment with four workers. The 1000 runs of the experiment are allocated to the 4 workers, 250 each. The contents of the parameter "execution.backend_params" are passed as arguments to ray.init(...).

How can I install Ray?

You can install Ray with:

pip install "ray[default]"

Executing experiments with Ray

from ray import get_runtime_context

from mltraq import Run, create_experiment, options


def get_worker_id(run: Run):
    run.fields.worker_id = get_runtime_context().get_worker_id()


with options().ctx(
    {
        "execution.backend": "ray",
        "execution.backend_params": {
            "logging_level": "FATAL",
            "include_dashboard": False,
            "num_cpus": 4,
        },
    }
):
    e = create_experiment().add_runs(i=range(1000)).execute(get_worker_id)

print(e.runs.df().worker_id.value_counts())
Output
worker_id
61439935e93ff5b95f95bb3feb7ad2e9dd05fce4e69f722a759596e6    251
c065d0d5dc741d7a5bec91685548c828aca20c90a54997d5426d3483    251
1016d0f1fa2d064f2e4dfbdff3e334ad804e288bb55b7748c183098d    251
dc9411c3e4953ff43c3058ba4f66033274fc73e3b4076bdb8e56f52b    247
Name: count, dtype: int64