Skip to content

fix: Fix crashes seen with nsys profiling on vLLM workers when TP > 1 - #2156

Merged
terrykong merged 6 commits into
NVIDIA-NeMo:mainfrom
snivertynv:vllm_nsys_crash_fix
Jun 17, 2026
Merged

terrykong merged 6 commits into
NVIDIA-NeMo:mainfrom
snivertynv:vllm_nsys_crash_fix

Conversation

@snivertynv

@snivertynv snivertynv commented Mar 25, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do ?

This PR fixes a couple of issues with nsys profiling in Nemo RL.

  • Fix crashes seen with nsys profiling on vLLM workers when TP > 1.
  • Modify default nsys arguments used to reduce profiling overhead.

Issues

List issues that this PR closes (syntax):

Usage

  • You can potentially add a usage example below
# Add a code snippet demonstrating how to use this

Before your PR is "Ready for review"

Pre checks:

  • Make sure you read and followed Contributor guidelines
  • Did you write any new necessary tests?
  • Did you run the unit tests and functional tests locally? Visit our Testing Guide for how to run tests
  • Did you add or update any necessary documentation? Visit our Document Development Guide for how to write, build and test the docs.

Additional Information

Problem: When tensor_parallel_size > 1, nsight profiling was applied to both the outer VllmGenerationWorker (via @ray.remote decorator) and the internal vLLM TP workers (via ray_workers_use_nsight=True). Running both under nsys caused Ray's compiled DAG to hang with a RayChannelTimeoutError.

Fix: Make nsight application TP-aware so only one set of workers is profiled.

Files changed:

  • nemo_rl/models/generation/vllm/vllm_worker.py
    Removed nsight from the @ray.remote decorator on VllmGenerationWorker
    Extended configure_worker to return a 4th element (runtime_env) with TP-aware logic:
    a. TP=1 (not is_part_of_parallel_workers): returns nsight config so the outer worker is profiled directly (engine runs in-process)
    b. TP>1 (is_part_of_parallel_workers): returns empty runtime_env — outer worker is NOT profiled
    Updated the log message in _create_engine_from_config to clearly explain that only internal TP workers are profiled when TP > 1

  • nemo_rl/distributed/worker_groups.py
    Extended RayWorkerBuilder.create_worker to handle an optional 4th return value (runtime_env overrides) from configure_worker, backward compatible with existing 3-element returns

  • nemo_rl/models/generation/vllm/vllm_worker_async.py
    Removed nsight from the @ray.remote decorator on VllmAsyncGenerationWorker
    Removed unused get_nsight_config_if_pattern_matches import

  • tests/unit/models/generation/test_vllm_generation.py
    Updated configure_worker_fixed_seed to unpack and forward the new 4th return value

  • docs/nsys-profiling.md
    Updated profile output documentation to explain TP=1 vs TP>1 behavior

Summary by CodeRabbit

  • Documentation

    • Updated profiling guidance for distributed vLLM model parallelism scenarios, including refined nsight configuration and capture strategies.
  • New Features

    • Improved GPU profiling with deferred capture capability for distributed workers, enabling profiling start via API calls.
    • Enhanced runtime environment configuration for better control over profiling in distributed execution.

@snivertynv
snivertynv requested review from a team as code owners March 25, 2026 15:28
@copy-pr-bot

copy-pr-bot Bot commented Mar 25, 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.

@github-actions github-actions Bot added the Documentation Improvements or additions to documentation label Mar 25, 2026
@coderabbitai

coderabbitai Bot commented Mar 25, 2026 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

This pull request refactors vLLM's nsight GPU profiling configuration in distributed Ray settings. The changes shift nsight configuration from decorator-time injection to being returned by configure_worker, introduce a monkey-patching mechanism for vLLM's internal nsight setup, and implement runtime environment override handling in the Ray worker initialization pipeline. Profiling now targets internal TP workers via ray_workers_use_nsight with deferred capture using cudaProfilerApi.

Changes

Cohort / File(s) Summary
Profiling Configuration
nemo_rl/distributed/worker_group_utils.py
Updated nsight target list from "cuda,cudnn,cublas,nvtx" to "cuda,nvtx" and added "s": "none" setting to the returned configuration.
Worker Group Management
nemo_rl/distributed/worker_groups.py
Enhanced configure_worker handling in RayWorkerBuilder.IsolatedWorkerInitializer.create_worker to accept optional 4th return value (runtime_env_overrides) and merge it into Ray actor options["runtime_env"].
vLLM Worker Integration
nemo_rl/models/generation/vllm/vllm_worker.py, nemo_rl/models/generation/vllm/vllm_worker_async.py
Changed configure_worker return signature to include runtime_env dictionary as 4th tuple element; added _patch_vllm_nsight_config() static method that monkey-patches vLLM's ray worker nsight configuration; removed inline runtime_env from @ray.remote decorators.
Test Updates
tests/unit/models/generation/test_vllm_generation.py
Updated configure_worker_fixed_seed test patch to pass through and return the additional runtime_env value from configure_worker.
Documentation
docs/nsys-profiling.md
Updated profiling output guidance to reflect new approach using ray_workers_use_nsight with monkey-patched configuration and deferred capture via cudaProfilerApi.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 69.23% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Test Results For Major Changes ⚠️ Warning PR claims 'reduces default nsys profiling overhead' but provides no performance benchmarks, measurements, or before-and-after data to support this assertion. Add performance benchmarks with specific configuration context and measurement methodology, or clarify that overhead reduction is a design intent rather than measured improvement.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: fixing crashes with nsys profiling on vLLM workers when tensor parallelism is greater than 1.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (2)
tests/unit/models/generation/test_vllm_generation.py (1)

617-623: Add an active unit test for the 4-tuple configure_worker() contract.

This change only updates a skipped test, so the new (..., runtime_env) return path can still regress without being exercised. A small non-GPU unit test around RayWorkerBuilder.IsolatedWorkerInitializer.create_worker() with a dummy worker returning four values would lock this behavior in.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/unit/models/generation/test_vllm_generation.py` around lines 617 - 623,
Add an active unit test that verifies the 4-tuple return contract of
configure_worker() by calling
RayWorkerBuilder.IsolatedWorkerInitializer.create_worker() with a dummy/no-GPU
worker and asserting it returns (resources, env_vars, init_kwargs, runtime_env);
modify or replace the existing skipped test that defines
configure_worker_fixed_seed to instead invoke create_worker() (or the code path
that calls configure_worker()) with a stub worker that returns four values and
assert init_kwargs contains the fixed seed and that runtime_env is present,
ensuring the test fails if the runtime_env return path regresses.
nemo_rl/distributed/worker_groups.py (1)

203-208: Merge runtime_env_overrides recursively.

Lines 205-208 do a top-level assignment, so an override like {"env_vars": {...}} would replace the env vars assembled on Lines 195-201. Since the 4th configure_worker() value is now a generic runtime_env override, it should go through the same recursive merge path as the rest of options.

♻️ Suggested change
-                if runtime_env_overrides:
-                    if "runtime_env" not in options:
-                        options["runtime_env"] = {}
-                    for k, v in runtime_env_overrides.items():
-                        options["runtime_env"][k] = v  # type: ignore
+                if runtime_env_overrides:
+                    options = recursive_merge_options(
+                        options,
+                        {"runtime_env": runtime_env_overrides},
+                    )
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@nemo_rl/distributed/worker_groups.py` around lines 203 - 208, The current
code assigns runtime_env_overrides at the top level which replaces nested maps
(e.g., env_vars) instead of merging; change the block handling
runtime_env_overrides so it recursively/deep-merges into options["runtime_env"]
(create options["runtime_env"] = {} if missing) by iterating
runtime_env_overrides.items() and for each key, if
options["runtime_env"].get(key) and both values are dicts then merge their
entries recursively, otherwise assign the override value; update the logic near
where configure_worker() passes the 4th runtime_env override to use this
deep-merge behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@nemo_rl/models/generation/vllm/vllm_worker.py`:
- Around line 433-439: The TP-worker nsight config being patched in
_patch_vllm_nsight_config() currently uses the heavier target list ("t":
"cuda,cudnn,cublas,nvtx"); update that patched nsight config to match
get_nsight_config_if_pattern_matches()'s low-overhead default ("t": "cuda,nvtx")
so the internal TP>1 workers receive the reduced tracing targets; locate the
nsight dict constructed or injected in _patch_vllm_nsight_config() and replace
the target string accordingly, keeping the existing
capture-range=cudaProfilerApi override and preserving
vllm_kwargs["ray_workers_use_nsight"] behavior.

---

Nitpick comments:
In `@nemo_rl/distributed/worker_groups.py`:
- Around line 203-208: The current code assigns runtime_env_overrides at the top
level which replaces nested maps (e.g., env_vars) instead of merging; change the
block handling runtime_env_overrides so it recursively/deep-merges into
options["runtime_env"] (create options["runtime_env"] = {} if missing) by
iterating runtime_env_overrides.items() and for each key, if
options["runtime_env"].get(key) and both values are dicts then merge their
entries recursively, otherwise assign the override value; update the logic near
where configure_worker() passes the 4th runtime_env override to use this
deep-merge behavior.

In `@tests/unit/models/generation/test_vllm_generation.py`:
- Around line 617-623: Add an active unit test that verifies the 4-tuple return
contract of configure_worker() by calling
RayWorkerBuilder.IsolatedWorkerInitializer.create_worker() with a dummy/no-GPU
worker and asserting it returns (resources, env_vars, init_kwargs, runtime_env);
modify or replace the existing skipped test that defines
configure_worker_fixed_seed to instead invoke create_worker() (or the code path
that calls configure_worker()) with a stub worker that returns four values and
assert init_kwargs contains the fixed seed and that runtime_env is present,
ensuring the test fails if the runtime_env return path regresses.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 91b692e4-8fb2-4d29-8412-a1e2f42d6e38

📥 Commits

Reviewing files that changed from the base of the PR and between b0da493 and f6d8446.

📒 Files selected for processing (6)
  • docs/nsys-profiling.md
  • nemo_rl/distributed/worker_group_utils.py
  • nemo_rl/distributed/worker_groups.py
  • nemo_rl/models/generation/vllm/vllm_worker.py
  • nemo_rl/models/generation/vllm/vllm_worker_async.py
  • tests/unit/models/generation/test_vllm_generation.py

@guyueh1
guyueh1 requested a review from terrykong March 25, 2026 15:41
@guyueh1 guyueh1 changed the title Fix crashes seen with nsys profiling on vLLM workers when TP > 1 fix: Fix crashes seen with nsys profiling on vLLM workers when TP > 1 Mar 25, 2026
Comment thread nemo_rl/distributed/worker_groups.py Resolved
Comment thread nemo_rl/distributed/worker_groups.py Resolved
@guyueh1
guyueh1 requested a review from terrykong April 3, 2026 15:15
guyueh1
guyueh1 previously approved these changes Apr 3, 2026

@guyueh1 guyueh1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for contributing the fix

@guyueh1

guyueh1 commented Apr 7, 2026

Copy link
Copy Markdown
Contributor

/ok to test bb683b5

@snivertynv
snivertynv force-pushed the vllm_nsys_crash_fix branch from bb683b5 to 4f0428a Compare April 13, 2026 13:20
@guyueh1

guyueh1 commented Apr 14, 2026

Copy link
Copy Markdown
Contributor

/ok to test 4f0428a

@guyueh1

guyueh1 commented Apr 14, 2026

Copy link
Copy Markdown
Contributor

i've tried the fix locally and it works so i'll approve

@terrykong
terrykong enabled auto-merge (squash) May 1, 2026 06:51
terrykong
terrykong previously approved these changes May 1, 2026
@terrykong terrykong added the CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) label May 1, 2026
@terrykong

Copy link
Copy Markdown
Collaborator

/ok to test 4f0428a

@terrykong

Copy link
Copy Markdown
Collaborator

/ok to test 29936a4

@terrykong

Copy link
Copy Markdown
Collaborator

Hi @snivertynv , can you resolve the L0 failures? i think the sphinx build can be resolved by rebasing on main (which is also required b/c of the merge conflicts)

@snivertynv
snivertynv force-pushed the vllm_nsys_crash_fix branch from 29936a4 to 21ac81b Compare May 5, 2026 09:57
@snivertynv
snivertynv requested review from a team as code owners May 5, 2026 09:57
snivertynv and others added 4 commits June 17, 2026 02:33
…ify default nsys arguments used to reduce profiling overhead.

Signed-off-by: Sriharsha Niverty <[email protected]>
…e for consistency. Also avoid clobbering nested keys like env_vars with a deep merge

Signed-off-by: Sriharsha Niverty <[email protected]>
configure_worker now returns (resources, env_vars, init_kwargs, runtime_env)
but the port assignment tests still unpacked 3 values.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Signed-off-by: Sriharsha Niverty <[email protected]>
The PR changed default nsight options to reduce TP profiling overhead:
- "t" changed from "cuda,cudnn,cublas,nvtx" to "cuda,nvtx"
- Added "s": "none" (no CPU sampling)

Update test_get_nsight_config_if_pattern_matches to match.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Signed-off-by: Sriharsha Niverty <[email protected]>
@snivertynv
snivertynv force-pushed the vllm_nsys_crash_fix branch from d76cc39 to e664626 Compare June 17, 2026 09:34
@snivertynv

Copy link
Copy Markdown
Contributor Author

/ok to test e664626

… 4-tuples

- Fix two more test_worker_groups.py assertions that still checked for
  "cuda,cudnn,cublas,nvtx" (now "cuda,nvtx" since the PR reduces overhead)
- Update MegatronPolicyWorker.configure_worker and MegatronValueWorker.configure_worker
  to return 4-tuples (resources, env_vars, init_kwargs, runtime_env) matching
  the updated RayWorkerBuilder that expects 4 values from configure_worker

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Signed-off-by: Sriharsha Niverty <[email protected]>
@snivertynv
snivertynv requested a review from a team as a code owner June 17, 2026 11:48
@snivertynv

Copy link
Copy Markdown
Contributor Author

/ok to test 6e360cc

The PR adds "s": "none" as a new default nsight profiling option.
The exact-key-set assertion in test_get_nsight_config_extra_options
was not updated to include "s", causing L0_Unit_Tests_Distributed
to fail.

Signed-off-by: Sriharsha Niverty <[email protected]>
@snivertynv

Copy link
Copy Markdown
Contributor Author

/ok to test 21e6cf8

@guyueh1 guyueh1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed LGTM

This branch was previously deployed

3 inactive (1 outdated) deployments
public — 21e6cf82 Deployed Jun 17, 2026 by copy-pr-bot[bot] via release / finalize / publish-docs #1213
test — 21e6cf82 Deployed Jun 17, 2026 by copy-pr-bot[bot] via Wait in test approval queue #12475
nemo-ci — 86f39e62 Deployed May 5, 2026 by copy-pr-bot[bot] via Docs_Tests #11166
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) Documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Run fails when nsys profiling is enabled for vLLM workers

3 participants