Skip to content

8800 potentially wrong device using cuda variable status in monaiauto3dseganalyzerpy#8801

Merged
ericspod merged 10 commits intoProject-MONAI:devfrom
garciadias:8800-potentially-wrong-device-using_cuda-variable-status-in-monaiauto3dseganalyzerpy
Apr 2, 2026
Merged

8800 potentially wrong device using cuda variable status in monaiauto3dseganalyzerpy#8801
ericspod merged 10 commits intoProject-MONAI:devfrom
garciadias:8800-potentially-wrong-device-using_cuda-variable-status-in-monaiauto3dseganalyzerpy

Conversation

@garciadias
Copy link
Copy Markdown
Contributor

Fixes #8800.

Description

Fix bug introduced by me on @benediktjohannes' PR#8708

Fixed a device synchronization bug in LabelStats.__call__() where the using_cuda was being ignored. When image and label tensors were on different devices, the code would:

  1. Set using_cuda to True if one is on GPU
  2. Ignore using_cuda and move tensors to CPU if there was a mismatch

Now using_cuda is calculated, and when devices don't match, both tensors are moved to CUDA (when either is on CUDA) or CPU otherwise.

Replaced # type: ignore comments with proper cast() for type safety.

Types of changes

  • Non-breaking change (fix or new feature that would not break existing functionality).
  • Breaking change (fix or new feature that would cause existing functionality to change).
  • New tests added to cover the changes.
  • Integration tests passed locally by running ./runtests.sh -f -u --net --coverage.
  • Quick tests passed locally by running ./runtests.sh --quick --unittests --disttests.
  • In-line docstrings updated.
  • Documentation updated, tested make html command in the docs/ folder.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 2, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: cd73f3a7-e2ea-4f84-9fde-d8124717b1b4

📥 Commits

Reviewing files that changed from the base of the PR and between 98ab779 and 7eb50bc.

📒 Files selected for processing (2)
  • monai/auto3dseg/analyzer.py
  • tests/apps/test_auto3dseg.py
✅ Files skipped from review due to trivial changes (1)
  • tests/apps/test_auto3dseg.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • monai/auto3dseg/analyzer.py

📝 Walkthrough

Walkthrough

Adjusted device-handling in LabelStats.__call__ so device negotiation uses an explicit using_cuda check and, when image_tensor and label_tensor differ, moves both tensors to a CUDA device if either is on CUDA (preferring the image tensor's CUDA device). When neither tensor uses CUDA, the previous behavior of moving the label to the image device is preserved. Added cast(MetaTensor, ...) typing around transferred tensors and gated the existing CUDA empty-cache behavior behind the using_cuda flag. Test change: added an inline comment in test_label_stats_mixed_device_analyzer.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Title check ⚠️ Warning Title is confusing with poor formatting and lacks clarity; it reads like concatenated variable/file names rather than a clear change summary. Revise to: 'Fix device synchronization bug in LabelStats when tensors on different devices' or similar clear statement of the actual fix.
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Description check ✅ Passed Description clearly explains the bug, the fix, and includes references to linked issue #8800 and prior PR #8708; follows template structure adequately.
Linked Issues check ✅ Passed Changes directly address issue #8800: using_cuda is now computed first, and device-mismatch handling moves both tensors to CUDA or CPU consistently, eliminating the state mismatch.
Out of Scope Changes check ✅ Passed All changes are in-scope: device-mismatch fix in LabelStats.call(), type safety improvements via cast(), and a test comment addition are all aligned with issue #8800 objectives.

✏️ 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.

I, R. Garcia-Dias <rafaelagd@gmail.com>, hereby add my Signed-off-by to this commit: ba56a6d

Signed-off-by: R. Garcia-Dias <rafaelagd@gmail.com>
Copy link
Copy Markdown
Contributor

@benediktjohannes benediktjohannes left a comment

Choose a reason for hiding this comment

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

Overall LGTM, just one question

@benediktjohannes
Copy link
Copy Markdown
Contributor

And thanks for addressing this!

@benediktjohannes
Copy link
Copy Markdown
Contributor

And we need to address lint please

Copy link
Copy Markdown
Contributor

@benediktjohannes benediktjohannes left a comment

Choose a reason for hiding this comment

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

Approving this (only Lint fix left, but in General LGTM, thanks)!

@benediktjohannes
Copy link
Copy Markdown
Contributor

I don‘t have permissions to approve / merge, so could you please do this @ericspod? Thanks!

Copy link
Copy Markdown
Member

@ericspod ericspod left a comment

Choose a reason for hiding this comment

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

I think this looks good now, you can fix your DCO issue with a remedial commit or I just mark it passed.

@benediktjohannes
Copy link
Copy Markdown
Contributor

I think this looks good now, you can fix your DCO issue with a remedial commit or I just mark it passed.

Thanks!

@ericspod ericspod enabled auto-merge (squash) April 2, 2026 16:57
@ericspod ericspod merged commit d3d0209 into Project-MONAI:dev Apr 2, 2026
26 checks passed
@benediktjohannes
Copy link
Copy Markdown
Contributor

Great work, keep it up! 👍

@benediktjohannes
Copy link
Copy Markdown
Contributor

And thank you for helping out so quick, very nice! I've worked for several other projects which took way longer for those things, so very nice that you directly addressed this, thank you!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Potentially wrong device using_cuda variable status in monai/auto3dseg/analyzer.py

3 participants