Skip to content

fix: probe CUDA context on correct device in set_task_pid() - #228

Closed
maverick123123 wants to merge 1 commit into
Project-HAMi:mainfrom
maverick123123:fix/set-task-pid-probe-device-mismatch
Closed

fix: probe CUDA context on correct device in set_task_pid()#228
maverick123123 wants to merge 1 commit into
Project-HAMi:mainfrom
maverick123123:fix/set-task-pid-probe-device-mismatch

Conversation

@maverick123123

@maverick123123 maverick123123 commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Problem

set_task_pid() in src/utils.c polls NVML processes on the first physical device where nvml_to_cuda_map(i) >= 0, but the probe context is always created on hardcoded CUDA device 0 via cuDevicePrimaryCtxRetain(&pctx, 0).

When CUDA_VISIBLE_DEVICES does not start with the pod's device 0 (e.g., CVD=1 or CVD=1,0), the polled NVML device and the probe context device are different physical GPUs. getextrapid() never finds the new PID, causing a spurious "host pid is error!" and subsequent fake Device 0 OOM due to incorrect memory accounting.

Fix

Save the cudaDev found in the NVML enumeration loop and use it for both cuDevicePrimaryCtxRetain and cuDevicePrimaryCtxRelease, ensuring the probe context runs on the same device being polled.

Testing

Verified on an 8x RTX 3090 machine with all CVD combinations.

(Closed in favor of #230)

set_task_pid() polls NVML processes on the first device where
nvml_to_cuda_map(i) >= 0, but the probe context was always created
on hardcoded CUDA device 0 via cuDevicePrimaryCtxRetain(&pctx, 0).

When CUDA_VISIBLE_DEVICES does not start with the pod's device 0
(e.g., CVD=1 or CVD=1,0), the polled NVML device and the probe
context device are different physical GPUs. getextrapid() never
finds the new PID, causing a spurious 'host pid is error!' and
subsequent Device 0 OOM due to incorrect memory accounting.

Fix: use the cudaDev found in the NVML enumeration loop for both
cuDevicePrimaryCtxRetain and cuDevicePrimaryCtxRelease, ensuring
the probe context runs on the same device being polled.

Fixes Project-HAMi#225
@maverick123123
maverick123123 force-pushed the fix/set-task-pid-probe-device-mismatch branch from 5785fb8 to 8efff49 Compare July 23, 2026 08:15
@hami-robot

hami-robot Bot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: maverick123123
Once this PR has been reviewed and has the lgtm label, please assign archlitchi for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@hami-robot hami-robot Bot added the size/XS label Jul 23, 2026
@coderabbitai

coderabbitai Bot commented Jul 23, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

set_task_pid() now retains and releases the CUDA primary context on the CUDA device selected during the probe loop instead of always using device 0.

Changes

CUDA probe device alignment

Layer / File(s) Summary
Track and reuse the mapped probe device
src/utils.c
set_task_pid() records the mapped CUDA device selected during probing and uses it for both primary-context retention and release.

Estimated code review effort: 2 (Simple) | ~5 minutes

Suggested reviewers: mesutoezdil

Poem

I’m a rabbit with a CUDA clue,
The probe now follows the device in view.
No more hopping to device zero,
Context paths now match the hero.
Squeak, the mismatch is through!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The change aligns with #225 by using the probed CUDA device for primary context retain/release, removing the device mismatch.
Out of Scope Changes check ✅ Passed No unrelated behavior or broad refactors are evident; the patch is narrowly scoped to the CUDA probe-device fix.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the change: using the mapped CUDA device when probing context in set_task_pid().
✨ 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.

@coderabbitai
coderabbitai Bot requested a review from mesutoezdil July 23, 2026 08:15

@coderabbitai coderabbitai 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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
src/utils.c (2)

135-173: 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Release the retained primary context on every post-retain exit path.

After cuDevicePrimaryCtxRetain(&pctx,probeDev), the polling failures at line 146 and the hostpid == 0 path at line 161 return before cuDevicePrimaryCtxRelease(probeDev), leaving the retained primary-context reference unreleased. Route these exits through cleanup that releases pctx before returning.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/utils.c` around lines 135 - 173, Ensure every exit after
cuDevicePrimaryCtxRetain in the surrounding function releases the retained
primary context. Update the polling failure branch in the
nvmlDeviceGetComputeRunningProcesses loop and the hostpid == 0 branch to route
through cleanup that calls cuDevicePrimaryCtxRelease(probeDev) before returning,
while preserving each branch’s existing return status.

114-135: 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Do not fall back to CUDA device 0 when no mapping succeeds.

probeDev stays 0 if every nvml_to_cuda_map() call fails, so cuDevicePrimaryCtxRetain(&pctx, probeDev) selects CUDA device 0 unintentionally. Start the selector as invalid and release only after a probe device was selected; this avoids per-device accounting changes on a device that was never successfully mapped.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/utils.c` around lines 114 - 135, Update the probe-device selection around
probeDev and cuDevicePrimaryCtxRetain: initialize probeDev to an invalid
sentinel, and only retain the primary context after the loop when a valid
mapping was found. Preserve the existing process-accounting behavior for
successful mappings, while avoiding any fallback to CUDA device 0 when all
nvml_to_cuda_map calls fail.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@src/utils.c`:
- Around line 135-173: Ensure every exit after cuDevicePrimaryCtxRetain in the
surrounding function releases the retained primary context. Update the polling
failure branch in the nvmlDeviceGetComputeRunningProcesses loop and the hostpid
== 0 branch to route through cleanup that calls
cuDevicePrimaryCtxRelease(probeDev) before returning, while preserving each
branch’s existing return status.
- Around line 114-135: Update the probe-device selection around probeDev and
cuDevicePrimaryCtxRetain: initialize probeDev to an invalid sentinel, and only
retain the primary context after the loop when a valid mapping was found.
Preserve the existing process-accounting behavior for successful mappings, while
avoiding any fallback to CUDA device 0 when all nvml_to_cuda_map calls fail.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 473199bf-f244-4971-a9ed-488169fb37c3

📥 Commits

Reviewing files that changed from the base of the PR and between 52f33fc and 8efff49.

📒 Files selected for processing (1)
  • src/utils.c

@hami-robot

hami-robot Bot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Thanks for your pull request. Before we can look at it, you'll need to add a 'DCO signoff' to your commits.

📝 Please follow instructions in the contributing guide to update your commits with the DCO

Full details of the Developer Certificate of Origin can be found at developercertificate.org.

The list of commits missing DCO signoff:

  • 8efff49 fix: probe CUDA context on correct device in set_task_pid()
Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant