Skip to content

Fix RandGridDistortiond converting entire dict when transform is skipped#8862

Open
AlexanderSanin wants to merge 4 commits into
Project-MONAI:devfrom
AlexanderSanin:fix/rand-grid-distortiond-convert-8604
Open

Fix RandGridDistortiond converting entire dict when transform is skipped#8862
AlexanderSanin wants to merge 4 commits into
Project-MONAI:devfrom
AlexanderSanin:fix/rand-grid-distortiond-convert-8604

Conversation

@AlexanderSanin
Copy link
Copy Markdown
Contributor

Summary

Fixes #8604

When _do_transform is False (transform randomly skipped), RandGridDistortiond called convert_to_tensor on the entire data dictionary. This converted non-image metadata values (integers, strings) into 0-dim tensors, causing AttributeError: 'int' object has no attribute 'numel' during DataLoader collation.

The fix only converts the transform's target keys (via self.key_iterator), consistent with how the transform processes keys in the normal code path.

Test plan

  • Verify RandGridDistortiond with prob=0 no longer crashes in DataLoader with num_workers > 0
  • Verify existing test_rand_grid_distortiond tests pass
  • Verify non-image metadata in the dictionary is preserved as-is when the transform is skipped

Signed-off-by: Oleksandr Sanin alexaaander.sanin@gmail.com

)

Catching BaseException inadvertently suppresses KeyboardInterrupt,
SystemExit, and GeneratorExit, which should nearly always propagate.
All 17 occurrences across monai/ and tests/ are replaced with
Exception, which is the appropriate base class for catchable errors.

Signed-off-by: Oleksandr Sanin <alexaaander.sanin@gmail.com>
…ped (Project-MONAI#8604)

When _do_transform is False, RandGridDistortiond called
convert_to_tensor on the entire data dictionary, which converted
non-image metadata values (integers, strings) into tensors. This
caused AttributeError ('int' object has no attribute 'numel') during
DataLoader collation.

Only convert the transform's target keys instead of the whole dict,
consistent with how the transform handles its keys in the normal
(do_transform=True) path.

Signed-off-by: Oleksandr Sanin <alexaaander.sanin@gmail.com>
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 18, 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: 22a09805-177b-4408-96da-a57e111b6fd3

📥 Commits

Reviewing files that changed from the base of the PR and between 37db0ad and 117d65b.

📒 Files selected for processing (1)
  • monai/transforms/spatial/dictionary.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • monai/transforms/spatial/dictionary.py

📝 Walkthrough

Walkthrough

This PR narrows many broad exception handlers from catching BaseException to catching Exception across module init, TF32 detection, auto3dseg processing, detection metrics, nnUNet runner, inferer, device config, and tests. It also fixes RandGridDistortiond’s skip-augmentation path to convert only selected keys in-place (resolving a DataLoader collation AttributeError).

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning Most changes are exception handler narrowing (BaseException to Exception) across multiple files, unrelated to the core RandGridDistortiond fix described in the PR objective. Remove or justify the exception handler changes in unrelated files (monai/init.py, data_analyzer.py, ensemble_builder.py, etc.); these should be separate PRs or documented if intentional.
Docstring Coverage ⚠️ Warning Docstring coverage is 56.25% 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
Title check ✅ Passed Title accurately describes the main fix: narrowing convert_to_tensor scope in RandGridDistortiond when transform is skipped.
Description check ✅ Passed Description clearly explains the bug, root cause, the fix, and includes a test plan; follows the template structure with summary section.
Linked Issues check ✅ Passed Changes directly address issue #8604: fix converts only transform target keys per self.key_iterator, preserving non-image metadata and preventing AttributeError during collation.

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

…@gmail.com>

I, Oleksandr Yizchak Sanin <alexaaander.sanin@gmail.com>, hereby add my Signed-off-by to this commit: 37db0ad

Signed-off-by: Oleksandr Yizchak Sanin <alexaaander.sanin@gmail.com>
Add explicit dict[Hashable, torch.Tensor] type annotation to the
convert_to_tensor call in the first_key == () branch to satisfy mypy's
no-any-return check.

Signed-off-by: Oleksandr Yizchak Sanin <alexaaander.sanin@gmail.com>
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.

AttributeError in DataLoader when using RandGridDistortiond transform

1 participant