[Bugfix] Reject negative --device-ids indices instead of selecting the wrong device by vineethsaivs · Pull Request #49053 · vllm-project/vllm · GitHub
Skip to content

[Bugfix] Reject negative --device-ids indices instead of selecting the wrong device - #49053

Merged
hmellor merged 4 commits into
vllm-project:mainfrom
vineethsaivs:fix/device-ids-negative-index
Oct 8, 2026
Merged

hmellor merged 4 commits into
vllm-project:mainfrom
vineethsaivs:fix/device-ids-negative-index

Conversation

@vineethsaivs

Copy link
Copy Markdown
Contributor

Purpose

EngineArgs._resolve_device_ids composes --device-ids with CUDA_VISIBLE_DEVICES: when CVD is set, each --device-ids value is treated as an index into the visible set. The range check only guarded the upper bound:

for i in int_ids:
    if i >= len(cvd_ids):
        raise ValueError(f"--device-ids index {i} is out of range ...")
return [cvd_ids[i] for i in int_ids]

A negative index (for example --device-ids -1) makes i >= len(cvd_ids) evaluate to False, so no error is raised and cvd_ids[i] silently negative-indexes into the list, resolving to a valid-but-wrong physical device. With CUDA_VISIBLE_DEVICES=4,5 and --device-ids -1, the method returns physical device [5] instead of rejecting the input.

This completes the bounds check to not 0 <= i < len(cvd_ids) so an out-of-range negative index raises the same "out of range" ValueError as a too-large one, matching the intent of the existing test_device_ids_with_cvd_out_of_range test.

This changes only input validation (silent mis-selection becomes a clear error); it does not affect model output, accuracy, or serving numerics, so no model-evaluation results apply.

Not a duplicate: the only open PRs touching this area, #46132 and #41776, add MIG/UUID parsing for CUDA_VISIBLE_DEVICES; neither changes the _resolve_device_ids index bounds check. No open issue tracks this.

Test Plan

Added tests/engine/test_arg_utils.py::TestDeviceIds::test_device_ids_with_cvd_negative_index, mirroring the existing test_device_ids_with_cvd_out_of_range:

pytest tests/engine/test_arg_utils.py::TestDeviceIds -q

Test Result

_resolve_device_ids does not load a model, so I validated the exact logic on CPU by executing the method against a stubbed platform (a full CUDA build was not available on my machine); the added test runs in CI:

  • Before: EngineArgs(model="m", device_ids=[-1]) with CUDA_VISIBLE_DEVICES=4,5 returned [5] (no error raised).
  • After: it raises ValueError: --device-ids index -1 is out of range for CUDA_VISIBLE_DEVICES=4,5 (2 devices visible).
  • Unchanged for valid input: device_ids=[0, 1] still resolves to [4, 5].

AI assistance disclosure: AI assistance was used to help identify and prepare this change, per the repository's contribution policy. The fix is a single bounds-check completion plus a regression test, and I have reviewed every changed line.

@vineethsaivs
vineethsaivs requested a review from hmellor as a code owner July 18, 2026 19:58

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@github-actions

Copy link
Copy Markdown

@mergify mergify Bot added the bug Something isn't working label Jul 18, 2026
…e wrong device

When CUDA_VISIBLE_DEVICES is set, --device-ids values are treated as
indices into the visible set. The range check only guarded the upper
bound (i >= len(cvd_ids)), so a negative index such as -1 passed
validation and cvd_ids[i] silently negative-indexed into the list,
resolving to a valid-but-wrong physical device instead of erroring.

Complete the bounds check to 0 <= i < len(cvd_ids) so an out-of-range
negative index raises the same 'out of range' ValueError as a too-large
one. Add a regression test alongside the existing out-of-range test.

Signed-off-by: Vineeth Sai <vineethsai4444@gmail.com>
@vineethsaivs
vineethsaivs force-pushed the fix/device-ids-negative-index branch from 294e81c to 34aff41 Compare July 18, 2026 20:06
@vineethsaivs

Copy link
Copy Markdown
Contributor Author

Gentle ping @hmellor (CODEOWNER for vllm/engine/arg_utils.py).

CI is currently sitting on pre-run-check: "PR must have the 'verified', 'ready', or 'ready-run-all-tests' label ... or the author must have at least 4 merged PRs (found 0)". As a first-time contributor I cannot add the label myself, so the test suite has never actually run on this PR. Could you add ready (or take a look) when you get a chance?

Recap: --device-ids indices are used to index into the device list without a bounds check, so a negative index silently wraps to a device from the other end of the list instead of being rejected. +'ve out-of-range values already error, this makes the negative side behave the same. Change is small and comes with a regression test in tests/engine/test_arg_utils.py.

Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
@hmellor
hmellor enabled auto-merge (squash) October 6, 2026 14:14
@github-actions github-actions Bot added the ready ONLY add when PR is ready to merge/full CI is needed label Oct 6, 2026
@hmellor

hmellor commented Oct 6, 2026

Copy link
Copy Markdown
Member

/ci run

@github-actions

github-actions Bot commented Oct 6, 2026

Copy link
Copy Markdown

❌ This PR is 1 commit behind upstream main. Your branch must contain every commit currently on upstream main. No new CI build was started. Merge or rebase onto the latest main, then rerun /ci run. To test this branch at your own risk, use /ci run --allow-stale.

@hmellor

hmellor commented Oct 6, 2026

Copy link
Copy Markdown
Member

/ci run --allow-stale

@hmellor

hmellor commented Oct 8, 2026

Copy link
Copy Markdown
Member

/ci run --allow-stale

@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown

✅ Triggered Buildkite CI #93570 for commit 6ce4a5fd2b25.

⚠️ This PR is 26 commits behind upstream main. Running CI at your own risk because --allow-stale was requested; outdated CI configuration may cause failures. Before merging, merge or rebase onto the latest main, then rerun /ci run on the latest PR commit.

@hmellor

hmellor commented Oct 8, 2026

Copy link
Copy Markdown
Member

/ci retry

@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown

@hmellor
hmellor merged commit 194da61 into vllm-project:main Oct 8, 2026
141 of 142 checks passed
michael-gpu pushed a commit to practical-inference/vllm-ampere-extended that referenced this pull request Oct 20, 2026
…e wrong device (vllm-project#49053)

Signed-off-by: Vineeth Sai <vineethsai4444@gmail.com>
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
Co-authored-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working ready ONLY add when PR is ready to merge/full CI is needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants