Skip to content

feat(cli): start Ray only when a configured server may use it - #4245

Merged
ananthsub merged 2 commits into
mainfrom
ananthsub/orchestrator-skip-ray
Oct 8, 2026
Merged

ananthsub merged 2 commits into
mainfrom
ananthsub/orchestrator-skip-ray

Conversation

@ananthsub

@ananthsub ananthsub commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

What changed

RunHelper.start() now starts or joins Ray only when a configured server may use it. Previously RunHelper._start called initialize_ray() unconditionally. That started a local Ray cluster, or connected to ray_head_node_address, even when every configured server declared ray_enabled = False.

This applies to every path that launches servers through RunHelper:

  • gym env start, including dry runs (++dry_run=true)
  • gym eval run without --no-serve
  • programs that import and call RunHelper themselves

How it works:

  • The check: a new entrypoint_may_use_ray() in nemo_gym/server_utils.py reads each server entrypoint's source without importing it, because each server runs in its own venv. It finds the classes the entrypoint calls run_webserver() on and reads their ray_enabled declarations.
  • When it skips Ray: only when every such class literally declares ray_enabled = False.
  • When it still starts Ray: when a declaration is missing, inherited, computed, or on a class imported from another module, or when the entrypoint can't be read. This matches the runtime default for undeclared servers.
  • Logging: when Ray starts, Gym prints which servers caused it. It prints nothing when Ray is skipped.
  • Refactor: the config walk moves into _configured_servers(), so the Ray decision and the launch loop use the same server list.

Why

A local Ray cluster costs host memory, an object store in /dev/shm, background processes, and several seconds of startup. Starting one here took about 9 seconds. Configs whose servers all opted out of Ray in #3622, #3728 and #4244 still paid that cost on every launch. That hurts most in memory-limited containers and in image builds that use a dry run to prebuild server venvs, which then had to stop the leftover cluster with ray stop.

The check itself takes about 2 ms per server. Across all built-in entrypoints the median is 1.8 ms and the slowest is 40 ms.

This is step 7 of #3669.

Guidance

For server authors: declare ray_enabled on the class your entrypoint calls run_webserver() on, even when a parent class already declares it. The check reads only that class's own source, so an inherited False still starts Ray. To see why Ray started for a config, look for the log line naming the servers that may use it.

For programs that embed Gym:

  • Gym starts Ray itself: when your process hasn't initialized Ray before calling RunHelper.start(), a config whose servers all declare ray_enabled = False now runs without any Ray cluster.
  • Your process already runs Ray: when Ray is initialized before RunHelper.start(), for example inside a Ray actor, nothing changes. initialize_ray() already returned immediately in that case, and Gym never started a cluster of its own. Servers that use Ray still join the cluster named by ray_head_node_address, and servers that declare ray_enabled = False don't connect, as since feat(server): allow servers to opt out of Ray #3622.

For image builds: a dry run of a config whose servers all declare ray_enabled = False no longer starts Ray, so a following ray stop is unnecessary. It's harmless to keep as a guard for configs that include servers that use Ray.

Validation

  • pytest tests/unit_tests/test_cli.py tests/unit_tests/test_server_utils.py tests/unit_tests/test_ray_opt_out.py tests/unit_tests/test_cli_endpoint_readiness.py tests/unit_tests/h2_ping_sidecar, rerun after rebasing onto the HTTP/2 ping sidecar change (feat(sidecar): optionally run an HTTP/2 PING proxy from gym env start for endpoints with idle timeouts #4079).
  • New tests:
    • the source check, covering declared, undeclared, inherited, imported, multiple-class, missing and unparseable entrypoints;
    • a test that every built-in entrypoint's result matches the audited declarations;
    • a RunHelper test that Ray is skipped for False and started for True or undeclared. The skip case fails on the previous code.
  • pre-commit run scoped to the changed files.

Rollout evidence

Smoke run:

gym env start "+config_paths=[resources_servers/example_single_tool_call/configs/example_single_tool_call.yaml,responses_api_models/openai_model/configs/openai_model.yaml]"
  • Gym did not start or join a Ray cluster.
  • gym env status reported all four servers healthy: the resources server, the simple agent, the legacy agent environment server and the OpenAI model server.
  • No new raylet or gcs_server processes appeared.
  • No server logged the undeclared-Ray warning.

No model requests were sent. This change only decides whether Ray starts, so it can't affect prompts, trajectories, verification or rewards.

Compatibility

  • Configs with any Ray-backed or undeclared server: unchanged. Ray starts or connects exactly as before.
  • Configs whose servers all declare ray_enabled = False: Gym doesn't start a local Ray cluster. If ray_head_node_address is set, the gym process no longer connects to it, since none of the servers use Ray. A process that already initialized Ray before calling RunHelper is unaffected.
  • A subclass that only inherits ray_enabled = False from another module: still starts Ray until it declares the value on its own class.

@copy-pr-bot

copy-pr-bot Bot commented Oct 8, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

Base automatically changed from ananthsub/declare-ray-env-servers to main October 8, 2026 17:32
@ananthsub
ananthsub marked this pull request as ready for review October 8, 2026 17:35
@nemo-automation-bot
nemo-automation-bot Bot requested review from a team October 8, 2026 17:35
@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

@copy-pr-bot

copy-pr-bot Bot commented Oct 8, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

hemildesai
hemildesai previously approved these changes Oct 8, 2026
The orchestrator called initialize_ray() unconditionally, so every
`gym env start` and end-to-end rollout collection started a local Ray
cluster (or joined the configured one), even when every configured
server declares ray_enabled = False and never connects to it.

Before spawning servers, read each entrypoint's source and find the
classes it calls run_webserver() on. Ray is skipped only when every such
class literally declares ray_enabled = False. Undeclared, inherited,
imported, or unreadable declarations count as using Ray, which matches
the runtime default for undeclared servers. The config walk moves into
_configured_servers() so the Ray decision and the launch loop share one
server list.

Part of #3669.

Signed-off-by: Ananth Subramaniam <[email protected]>
@ananthsub
ananthsub force-pushed the ananthsub/orchestrator-skip-ray branch from b755c37 to 403a4a7 Compare October 8, 2026 18:35
@ananthsub

Copy link
Copy Markdown
Contributor Author

/ok to test 403a4a7

@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/37827662047

Merged via the queue into main with commit c50775d Oct 8, 2026
48 of 51 checks passed
@ananthsub
ananthsub deleted the ananthsub/orchestrator-skip-ray branch October 8, 2026 19:06
ehosseiniasl pushed a commit to ehosseiniasl/Gym that referenced this pull request Oct 9, 2026
## What changed

Gym now works when Ray isn't installed. Ray stays a core dependency in
this PR; a follow-up moves it into the new `ray` extra.

- **Importing Gym:** `global_config.py` no longer raises at import when
Ray is missing. The parent Ray version is `None`, and `gym --version`
reports Ray as `not installed`.
- **Pinning Ray in server venvs:** the parent's exact Ray version is now
passed to every server venv install as a uv constraint (`-c <(printf
...)`), stored under a new `head_server_constraints` config key, instead
of as a `ray[default]==<version>` requirement in `head_server_deps`.
- Where a server installs Ray, it still gets exactly the parent's
version.
- The pin no longer forces Ray into venvs whose servers don't depend on
it, which matters once Ray leaves the core dependencies.
  - When the parent has no Ray, there's no constraint.
- **Servers that leave `ray_enabled` undeclared:**
- They join Ray only when Ray is importable in their venv and the run
has a cluster address to join.
  - Otherwise they run without Ray and log a warning saying so.
- Previously, such a server with no address called `ray.init()` and
started a private cluster, in every process and Uvicorn worker.
- **A Gym-launched server that needs Ray but finds no cluster address**
now raises an error naming the class, instead of starting a private
cluster. This can only happen when the supervisor decided no server
needs Ray, for example because Ray isn't installed where `gym` runs.
- **Supervisor decision:**
  - Servers that declare `ray_enabled = True` always need Ray.
- Servers whose declaration can't be read from source (undeclared,
inherited, imported) count only when Ray is installed where `gym` runs.
- If a server declares `True` and Ray isn't installed there, `gym env
start` stops before launching anything, with a one-line error naming the
servers and the install command.
- `entrypoint_may_use_ray()` becomes `entrypoint_ray_enabled()`, which
returns `True`, `False`, or `None` for "can't tell".
- **New `ray` extra:** `nemo-gym[ray]` (also included in `all`)
duplicates the core Ray and `jinja2` requirements for now. Error
messages can point to it, and server manifests can request it before Ray
leaves the core dependencies. The `uv.lock` change is metadata only; no
package versions moved.
- **Docs:** the FAQ's Ray section and the system design page describe
when Ray starts, how undeclared servers behave, and the `ray` extra. The
FAQ also still described the pre-NVIDIA-NeMo#4245 behavior.

## Why

This is the second step of making Ray optional (NVIDIA-NeMo#3669). Before the core
dependency can move into an extra, every path that assumed Ray was
installed has to cope without it:
- the import-time version lookup;
- the per-venv pin that installed Ray everywhere;
- `gym --version`;
- undeclared servers starting private clusters.

## Validation

- `pytest tests/unit_tests/test_global_config.py
tests/unit_tests/test_cli_setup_command.py
tests/unit_tests/test_ray_opt_out.py tests/unit_tests/test_cli.py
tests/unit_tests/test_server_utils.py tests/unit_tests/test_cli_main.py
tests/unit_tests/test_cli_endpoint_readiness.py
tests/unit_tests/h2_ping_sidecar`
- **Full `tests/unit_tests/` run:** the same set of tests fails on this
branch and on its base commit. All of them come from optional extras
missing in the local venv (`sandbox`, `wandb`, `mlflow`).
- **New and updated tests:**
- the config parses without Ray, and the Ray pin is a constraint rather
than a requirement;
- install commands carry the constraint on every path, including both
installs on the PyPI `pyproject.toml` path;
- undeclared servers join only when Ray is importable and a cluster is
configured, and otherwise run without Ray and warn;
- a Gym-launched Ray server without a cluster fails, while a standalone
one may still start its own;
- the supervisor decision across declarations and Ray availability,
including the early failure;
  - `gym --version` with Ray missing;
- the source declaration reader returns `None` for undeclared,
inherited, imported, unreadable and mixed entrypoints.
- **Real `uv` check, offline against hand-built wheels:** a constraint
passed through process substitution didn't install the constrained
package when nothing requested it. A conflicting constraint made the
install fail as unsatisfiable.
- `fern check` passes.
- `pre-commit run` scoped to the changed files.

## Rollout evidence

**Smoke run without Ray.** I made a venv with this branch installed and
then ran `uv pip uninstall ray`.
- `import nemo_gym.global_config, nemo_gym.server_utils,
nemo_gym.cli.env` succeeded, with `ray_version = None`.
- `gym --version --json` reported `"ray": "not installed"`.
- `gym env start` with `example_single_tool_call` and `openai_model`
reached `All 4 / 4 servers ready`: the resources server, the simple
agent, the legacy agent environment server and the model server. No Ray
processes were started.
- The same venv with the `code_gen` config, which declares `ray_enabled
= True`, exited before building any venv:

  ```
Error: These servers use Ray, but Ray isn't installed where `gym` runs:
code_gen. Install with:
    pip install nemo-gym[ray]
  ```

No model requests were sent. This changes dependency handling and
whether Ray starts, not prompts, trajectories, verification or rewards.

## Compatibility

- **Installs with Ray (everyone today):** servers that declare
`ray_enabled` behave exactly as before. Server venvs still get the
parent's exact Ray version.
- **Undeclared servers started outside Gym with no
`ray_head_node_address`:** they no longer start a private Ray cluster at
startup. If such a server calls Ray anyway, Ray initializes lazily on
first use.
- **Undeclared servers launched by Gym:** unchanged when Ray is
installed where `gym` runs, because Gym starts Ray for them and they
join it.
- **`head_server_deps` no longer contains Ray.** The Ray pin lives in
the new reserved `head_server_constraints` key.

Signed-off-by: Ananth Subramaniam <[email protected]>
@linear-code linear-code Bot mentioned this pull request Nov 12, 2026
8 of 26 tasks

This branch was previously deployed

1 inactive deployment
public — 403a4a7f Deployed Oct 8, 2026 by copy-pr-bot[bot] via release / finalize / notify #4767
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants