Prevent ray from setting OMP_NUM_THREADS?

How severe does this issue affect your experience of using Ray?

  • Medium: It contributes to significant difficulty to complete my task, but I can work around it.

At some point between 1.8.0 and 1.11.0, importing ray seems to set OMP_NUM_THREADS to 1.

For example, when I use ray 1.9.2 the code below runs on a new VM without error.

import os
assert os.getenv("OMP_NUM_THREADS") is None
import ray
assert os.getenv("OMP_NUM_THREADS") == "1"

Is there an easy way to disable this behavior in a non-interactive shell? It’s broken a lot of our stuff.

When I wrap a function with ray.remote, the issue gets fixed if I decorate with something like

import os
n = os.cpu_count()
ray.remote(
    num_cpus=n,
    runtime_env={
        "env_vars": {
            "OMP_NUM_THREADS": str(n), "NUMEXPR_MAX_THREADS": str(n)
        }
    }
)

Edit: Also the following seems to work for tune: [tune] set omp_num_threads = num_cpus per trial · Issue #10128 · ray-project/ray · GitHub.

Edit: Added change request here: [Core] OMP_NUM_THREADS change · Issue #24012 · ray-project/ray · GitHub

You can also just set OMP NUM THREADS inside the remote function itself whenever you need it to be some value.