Repository navigation
chore(server): declare Ray usage for built-in servers - #3728
Conversation
yuhezhang-ai
left a comment
There was a problem hiding this comment.
Requesting changes because two built-in servers opt out of Ray while still executing inherited Ray operations: GenRMModel and LegalAgentBenchHarborBridge. Both lose the initialization that passes Gym's configured cluster address to Ray. The five inline comments cover these two runtime regressions and three audit corrections: inherited dependencies, generated templates, and checkout-relative path filtering.
Reviewed the incremental change against parent PR #3622 at 9b993542faa546f985a74bf28e92293175a25950. Codex and Claude Opus 5.5 reviewed head a401b0c1ac27808ac127891a869cafb78ac6b0fd independently; the findings were then checked against the code and focused probes.
Validation: all four tests in tests/unit_tests/test_ray_opt_out.py passed, and Ruff checks/formatting passed for all 213 changed files. Focused probes confirmed that the inherited call paths reach bare ray.init() with the current flags and preserve the configured address when Ray support is restored. Additional probes reproduced the scaffold/audit incompatibility and hidden-parent-directory filtering. These probes stop at the initialization boundary; full GenRM GPU startup and Legal Agent Bench/model rollouts were not run.
a401b0c to
e5e45c7
Compare
e5e45c7 to
2a4a810
Compare
0378ba7 to
81a005e
Compare
|
/ok to test 991ffb2 |
## What changed Add a class-level `ray_enabled: ClassVar[bool | None]` contract for Gym server implementations. `None` preserves the historical Ray-enabled behavior and emits a migration warning; `False` skips the server process's Ray import and connection, while `True` connects as before. Ray is imported lazily so non-Ray server processes do not pay the import cost. Existing server construction, dry-run preparation, Uvicorn manager behavior, readiness handling, and log prefixing remain unchanged. Built-in declarations are intentionally split into dependent PR NVIDIA-NeMo#3728 so this PR stays focused on the shared runtime contract. No separate issue is required; this is part of NVIDIA-NeMo#3669. ## Why Gym historically connected every server process and Uvicorn worker to Ray, including stateless HTTP servers. Each connection imports the Ray runtime and creates process-local client state, so the overhead scales with `num_workers`. The compatibility default protects external components loaded from PyPI installations, forks, or `--search-dir` roots that may implicitly rely on automatic initialization. ## Validation - `PYTHONPATH=/tmp/Gym-ray-split /home/ansubramania/dev/Gym/.venv/bin/python -m pytest tests/unit_tests/test_ray_opt_out.py tests/unit_tests/test_server_utils.py -q` — 80 passed. - Scoped pre-commit over the complete PR diff — passed. ## Rollout evidence N/A for model behavior. This changes only Ray import/connection selection; it does not alter prompts, model requests, server construction, trajectories, verification, or rewards. ## Compatibility - Undeclared external servers remain Ray-enabled and receive a migration warning. - A future release can change `None` to disabled after downstream migration. - The orchestrator's shared Ray cluster is unchanged in this PR. Signed-off-by: Ananth Subramaniam <[email protected]>
yuhezhang-ai
left a comment
There was a problem hiding this comment.
All five earlier findings are addressed. I also checked the rebase onto main and the added HermesSandboxedAgent declaration at 991ffb2; no remaining blocking findings.
Nonblocking migration note: please mention that custom subclasses inherit their built-in parent's Ray setting. Existing subclasses of SimpleAgent that submit Ray tasks now need to explicitly set ray_enabled = True; otherwise they inherit False, skip Gym's configured Ray initialization, and receive no compatibility warning. A short note in the PR's compatibility section or FAQ would make this clear.
Validation at 655ce41: 131 focused tests, declaration/scaffold probes, two real process/HTTP opt-out smoke checks, and scoped lint/format checks passed. The delta to 991ffb2 was inspected; those tests were not rerun after the rebase. No full model rollout or full CI run was performed.
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/36509544679 |
## What does this PR do? Fixes main. #3699 (Aegis v4) merged about an hour after #3728 added the audit that every shipped server declares `ray_enabled`, so `AegisV4SafetyResourcesServer` has no declaration and `tests/unit_tests/test_ray_opt_out.py::test_shipped_server_classes_declare_ray_usage` fails on main and on every merge-queue run. The server makes no Ray calls, so it declares `ray_enabled = False`. ## Validation - `pytest tests/unit_tests/test_ray_opt_out.py -q`: 5 passed (was failing on main at 1e01443) ## Rollouts N/A. Class-level declaration only, no change to requests, verification, or rewards. ## Compatibility and benchmark impact None. The Aegis server stops joining the Ray cluster at startup, which it never used. ## Checklist - [x] I have read the [contributing guidelines](https://github.com/NVIDIA-NeMo/Gym/blob/main/CONTRIBUTING.md). - [x] The change is focused; unrelated "drive-by" edits are tracked as separate issues/PRs. - [x] Tests added or updated and pass locally, or N/A for docs-only / non-code changes (so CI unit/server checks pass when applicable). - [x] Documentation added or updated, or N/A with justification. - [x] Pre-commit checks pass locally (`pre-commit run --all-files`) (so CI lint/format checks pass). - [x] New source files include the required Apache-2.0 SPDX header. - [x] All commits have DCO sign-off (`git commit -s`) (so the DCO check passes). Signed-off-by: Christian Munley <[email protected]>
…tructions into the judge prompt Add ray_enabled = False, required by NVIDIA-NeMo#3728's shipped-server Ray declaration gate. Add source_sentence, the raw unwrapped sentence, so verify() sends it to the judge as source text instead of body.prompt[-1]["content"] -- which included instruction text (e.g. "Translate the following sentence from English to Hindi.") the judge should not see as source content. prompt is unchanged and still goes to the policy model as-is. data/example.jsonl, example_metrics.json, and example_rollouts.jsonl are regenerated for real against a live openai/gpt-oss-120b judge+policy endpoint (SGLang) -- not hand-edited. Per review from @cmunley1 on NVIDIA-NeMo#3348. Signed-off-by: Tanish Kushalkar <[email protected]>
Both servers were cherry-picked from upstream main, where the base server classes declare `ray_enabled` as a ClassVar (#3728). This branch predates #3728, so pydantic treats the bare `ray_enabled = False` class attribute as an unannotated field and the module fails to import. The flag has no meaning here, so remove it. Tests: swemer_oml 28 passed, swe_internal_v1 29 passed. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Sugam Devare <[email protected]>
…mond (NVIDIA-NeMo#4244) ## What changed Declare `ray_enabled = False` on three environment servers that never declared it: `legacy_agent`, `single_agent_turn` and `single_agent_turn_legacy`. `nemo_user_sim` already declared it. The Ray source audit in `tests/unit_tests/test_ray_opt_out.py` now also covers `environment_servers/`, so a new environment server without a declaration fails the audit. GPQA-Diamond only inherited `ray_enabled = False` from the `mcqa` server. It now declares it on its own class, and its entry is removed from the audit's list of inherited declarations. ## Why NVIDIA-NeMo#3728 declared Ray usage for resources, agent and model servers, but not for environment servers. Those servers still take the compatibility default, so each one connects to Ray and logs the migration warning even though none of them use Ray. The GPQA-Diamond declaration supports the stacked follow-up, which decides whether the orchestrator starts Ray by reading each entrypoint's own source. A declaration inherited from another module isn't visible there, so it would count as using Ray. Part of NVIDIA-NeMo#3669. ## Validation - `pytest tests/unit_tests/test_ray_opt_out.py`. With the declarations reverted, the extended audit names each undeclared environment server. - Each changed server's own tests, run from its directory: `environment_servers/legacy_agent`, `environment_servers/single_agent_turn`, `environment_servers/single_agent_turn_legacy` and `resources_servers/gpqa_diamond`. - `pre-commit run` scoped to the changed files. ## Rollout evidence N/A for model behavior. This only declares that these servers don't use Ray. It doesn't change prompts, model requests, trajectories, verification or rewards. The stacked PR includes a `gym env start` smoke run in which the `legacy_agent` environment server starts without connecting to Ray. ## Compatibility These servers stop connecting to Ray and stop logging the undeclared-Ray warning. None of them call Ray. Signed-off-by: Ananth Subramaniam <[email protected]>
…-NeMo#4245) ## 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 NVIDIA-NeMo#3622, NVIDIA-NeMo#3728 and NVIDIA-NeMo#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 NVIDIA-NeMo#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 NVIDIA-NeMo#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 (NVIDIA-NeMo#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. --------- Signed-off-by: Ananth Subramaniam <[email protected]>
What changed
Declare
ray_enabled = TrueorFalseon every built-in Gym server implementation and add source audits that keep declarations synchronized with direct and inherited production Ray dependencies. Ray-backed subclasses such asGenRMModelandLegalAgentBenchHarborBridgeremain enabled even though their Ray calls live in parent implementations.Generated environment scaffolds now emit explicit non-Ray declarations for resources and custom agent servers. The inventory applies exclusions relative to the checkout/component root so it remains valid in hidden worktrees and cache directories.
This PR builds on merged PR #3622, which introduced the class-level contract, compatibility warning, and lazy import. It now targets
maindirectly and contains only the declaration layer.Why
Ray usage is an implementation property, not a deployment/configuration choice. Keeping the declaration on each server class makes the dependency visible to maintainers and avoids duplicating it across YAML variants. The audits catch newly added servers, newly introduced direct Ray imports, and known inherited Ray call paths.
No separate issue is required; this is part of #3669.
Validation
PYTHONPATH=/tmp/Gym-ray-split /home/ansubramania/dev/Gym/.venv/bin/python -m pytest tests/unit_tests/test_ray_opt_out.py tests/unit_tests/test_environment_scaffold.py -q— 53 passed.Rollout evidence
N/A. This PR declares existing server behavior and updates generated templates; it does not change prompts, model requests, trajectories, verification, or rewards.
Compatibility
Ray-backed servers, including inherited consumers, explicitly retain Ray. Non-Ray servers and newly scaffolded components opt out once #3622 is present.