Skip to content

Track and reduce Gym's dependency on Ray #3669

Open
10 of 26 issues completed
Open

Description

@ananthsub

Context

Gym historically starts or joins a Ray cluster from the CLI orchestrator and connects every server process and Uvicorn worker, including stateless servers that never call Ray. This adds per-process runtime state, memory, background work, and cluster connections that scale with num_workers. It is especially relevant when Gym is co-located with large-scale RL training and host-memory headroom is limited.

External components loaded from PyPI installations through --search-dir, NEMO_GYM_EXTRA_ROOTS, private forks, or separate environment repositories may rely on the current implicit initialization. The migration therefore needs a compatibility window.

Migration path

  1. Add an implementation-level ray_enabled: ClassVar[bool | None] declaration.
  2. During the compatibility phase, treat None as enabled and emit an actionable warning.
  3. Mark every shipped server explicitly True or False.
  4. Stop non-Ray server processes and Uvicorn workers from importing or connecting to Ray.
  5. Migrate workloads to better-fitting execution boundaries where possible.
  6. After downstream servers have had time to migrate, interpret None as disabled so Ray becomes opt-in.
  7. Use lightweight pre-spawn dependency metadata to avoid starting the orchestrator's Ray cluster when no configured server requires it.
  8. Reassess whether Ray should remain a required core dependency or become optional.

Status (2026-11-19)

Steps 1–8 of the migration path are done on main:

Related fix: with num_workers > 1, each Uvicorn worker built its server twice before #5681. The extra build ran before the worker's health check started, so a slow build could get the worker killed and restarted.

Docs: #6065 fixed the pages that still said Gym always installs and starts Ray. The Architecture page now explains why Gym runs separate servers and how a server moves heavy work off its own processes: sandboxes first, then model servers, then Ray. The Ray guide is its own page, Scale CPU-Heavy Work with Ray, with an upgrade note for 0.7.0 users. AGENTS.md and the add-benchmark skill tell coding agents to declare ray_enabled = True, request nemo-gym[ray], and use the Sandbox API for isolated execution.

Measurements

Each comparison runs current main against main with the old behavior restored: Ray started for every run, every server process and Uvicorn worker connected, and Ray installed in every server environment. Everything else is identical. Numbers are medians of three interleaved runs.

Evaluation, MMLU-Pro and IFEval with 100 tasks each against a hosted OpenAI-compatible endpoint, default server settings:

  • Time until all servers are ready: 12.9 s with Ray, 6.3 s without. Almost all of the difference is starting the Ray cluster.
  • Memory once running (PSS): 2.8 GB with Ray, 1.1 GB without. Ray's own 31 background processes hold about 1.5 GB for the whole run.
  • Gym processes connected to Ray: all with the old behavior, none today. Scores are unchanged.
  • Each server environment is about 200 MB smaller without Ray (816 MB against 616 MB for a simple server).

Training, a production-scale RL configuration with 35 environments and 112 Gym servers, including a policy model server with 16 Uvicorn workers. This is a CPU-only simulation of how NeMo RL's single-controller path starts Gym inside a Ray actor on an existing cluster, using the Gym version NeMo RL currently pins plus the external_app.py entrypoints above:

  • Gym processes connected to the training job's Ray cluster: 132 with the old behavior, 1 with these changes (code_gen, which still used Ray at that Gym version).
  • Memory on Gym's node: 23.5 GB with the old behavior, 18.5 GB with these changes.
  • Time until Gym is ready: 58 s against 49 s. Individual runs varied by about 10 s because 112 servers start at once on one machine.

An earlier single-server measurement on macOS found the same pattern: about 4 s of added startup and 28 extra processes.

Servers that still depend on Ray

Each server has a follow-up issue that records how it uses Ray and the proposed replacement. Each box is checked once the server no longer declares ray_enabled = True on main, or documents why it keeps Ray. On main (2026-11-18), 16 server implementations declare ray_enabled = True.

Candidates for the Sandbox API

These primarily use Ray to distribute harness execution, code execution, compilation, tests, or other disposable task environments. The Sandbox API provides a clearer lifecycle and stronger isolation; provider-side pooling and concurrency controls can replace the outer Ray task layer.

Resources servers:

Agent servers:

Some of these already use sandbox-backed execution internally. The remaining work is to determine whether Ray is still needed around that sandbox lifecycle or whether async Gym orchestration and provider capacity controls are sufficient.

Candidates for persistent endpoints

These use Ray to keep heavyweight models resident, place them on GPUs, and route repeated scoring or inference calls. They are better candidates for independently deployed persistent services with stable HTTP APIs, batching, readiness, and model-version metadata.

Not yet categorized

No longer use Ray

Cases where Ray may remain appropriate

Ray can remain an explicit dependency for trusted cluster-local workloads that genuinely need long-lived mutable actors, placement groups or custom resources, shared-object transfer, gang scheduling, or low-latency distributed coordination. Each remaining use should document why a sandbox, bounded local concurrency, or persistent endpoint is insufficient.

Acceptance criteria

Related work

Sub-issues

Sub-issues

10 of 26 Issues completed
Loading 26 sub-issues

Activity

  1. added
    feature
    area:core
    on Sep 23, 2026
  2. 19 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Platformarea:corefeature

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions