-
Notifications
You must be signed in to change notification settings - Fork 1.4k
ENH: support additional dtypes in pad_nd #8672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
ENH: support additional dtypes in pad_nd #8672
Conversation
Prefer the PyTorch padding backend when supported and safely fall back to NumPy on error. Add unit tests to validate backend selection and ensure output dtype is preserved. Signed-off-by: Shubham Chandravanshi <shubham.chandravanshi378@gmail.com>
📝 WalkthroughWalkthroughThe pad_nd padding implementation was changed to always try the PyTorch padding path for modes {"constant","reflect","edge","replicate","wrap","circular"} (with a fallback to NumPy on error) instead of gating on input dtype. call_kwargs are built from kwargs (dropping "value" when mode != "constant") and passed to the padding call. NotImplementedError from PyTorch triggers a NumPy fallback. ValueError/TypeError/RuntimeError messages matching specific keywords also trigger the NumPy fallback. A new test module verifies backend selection and dtype preservation across multiple dtypes and modes. Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Pre-merge checks and finishing touches✅ Passed checks (5 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 4
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
monai/transforms/croppad/functional.py (1)
99-110: Critical: NotImplementedError not caught by except clause.Line 103 catches
(ValueError, TypeError, RuntimeError)but line 104 checksisinstance(err, NotImplementedError). NotImplementedError would propagate uncaught, breaking the fallback mechanism. The test attest_pad_falls_back_to_np_if_pt_raisesexpects this fallback but would fail in real execution.🔎 Proposed fix
- except (ValueError, TypeError, RuntimeError) as err: + except (ValueError, TypeError, RuntimeError, NotImplementedError) as err: if isinstance(err, NotImplementedError) or any( k in str(err) for k in ("supported", "unexpected keyword", "implemented", "value") ):
🧹 Nitpick comments (1)
tests/transforms/croppad/test_pad_nd_dtypes.py (1)
49-58: Consider testing additional modes.Current tests only cover "constant" mode. The updated code supports {"reflect", "edge", "replicate", "wrap", "circular"} via PyTorch. Testing dtype preservation across these modes would strengthen coverage.
Optional enhancement
@pytest.mark.parametrize( "mode", ["constant", "reflect", "replicate"] ) @pytest.mark.parametrize( "dtype", [torch.bool, torch.int8, torch.float32] ) def test_pad_modes_with_dtypes(mode, dtype): """Test that pad_nd handles various modes and dtypes correctly.""" img = torch.ones((1, 4, 4), dtype=dtype) to_pad = [(0, 0), (1, 1), (2, 2)] out = pad_nd(img, to_pad, mode=mode, value=0) assert out.shape == (1, 6, 8) assert out.dtype == img.dtype
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Cache: Disabled due to data retention organization setting
Knowledge base: Disabled due to Reviews -> Disable Knowledge Base setting
📒 Files selected for processing (2)
monai/transforms/croppad/functional.pytests/transforms/croppad/test_pad_nd_dtypes.py
🧰 Additional context used
📓 Path-based instructions (1)
**/*.py
⚙️ CodeRabbit configuration file
Review the Python code for quality and correctness. Ensure variable names adhere to PEP8 style guides, are sensible and informative in regards to their function, though permitting simple names for loop and comprehension variables. Ensure routine names are meaningful in regards to their function and use verbs, adjectives, and nouns in a semantically appropriate way. Docstrings should be present for all definition which describe each variable, return value, and raised exception in the appropriate section of the Google-style of docstrings. Examine code for logical error or inconsistencies, and suggest what may be changed to addressed these. Suggest any enhancements for code improving efficiency, maintainability, comprehensibility, and correctness. Ensure new or modified definitions will be covered by existing or new unit tests.
Files:
monai/transforms/croppad/functional.pytests/transforms/croppad/test_pad_nd_dtypes.py
🧬 Code graph analysis (2)
monai/transforms/croppad/functional.py (1)
monai/transforms/utils_pytorch_numpy_unification.py (1)
mode(426-438)
tests/transforms/croppad/test_pad_nd_dtypes.py (1)
monai/transforms/croppad/functional.py (3)
pad_nd(73-110)_pt_pad(59-70)_np_pad(45-56)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (19)
- GitHub Check: flake8-py3 (pytype)
- GitHub Check: quick-py3 (ubuntu-latest)
- GitHub Check: flake8-py3 (mypy)
- GitHub Check: min-dep-pytorch (2.7.1)
- GitHub Check: quick-py3 (windows-latest)
- GitHub Check: quick-py3 (macOS-latest)
- GitHub Check: min-dep-pytorch (2.5.1)
- GitHub Check: flake8-py3 (codeformat)
- GitHub Check: min-dep-pytorch (2.6.0)
- GitHub Check: build-docs
- GitHub Check: min-dep-py3 (3.10)
- GitHub Check: min-dep-os (ubuntu-latest)
- GitHub Check: min-dep-pytorch (2.8.0)
- GitHub Check: min-dep-os (windows-latest)
- GitHub Check: min-dep-os (macOS-latest)
- GitHub Check: min-dep-py3 (3.12)
- GitHub Check: min-dep-py3 (3.11)
- GitHub Check: packaging
- GitHub Check: min-dep-py3 (3.9)
Strip value for non-constant modes for both PyTorch and NumPy backends and ensure reliable fallback behavior. Update tests to cover multiple padding modes and dtype preservation. Signed-off-by: Shubham Chandravanshi <shubham.chandravanshi378@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
♻️ Duplicate comments (4)
tests/transforms/croppad/test_pad_nd_dtypes.py (4)
1-20: Module docstring and imports look good.Past review comments about missing module docstring have been addressed.
22-31: Test correctly validates PyTorch backend usage and dtype preservation.Past docstring feedback addressed.
33-45: Fallback test is well-structured.Mocking
_pt_padto raiseNotImplementedErrorand verifying_np_padis called covers the new fallback path. Past docstring feedback addressed.
47-57: Good dtype coverage.Tests bool, int8/16/32/64, uint8, float32. Past docstring feedback addressed.
🧹 Nitpick comments (2)
monai/transforms/croppad/functional.py (1)
99-112: Minor formatting: double spaces before**call_kwargs.Lines 107 and 112 have two spaces before
**call_kwargs.🔎 Fix spacing
except NotImplementedError: - return _np_pad(img, pad_width=to_pad, mode=mode, **call_kwargs) + return _np_pad(img, pad_width=to_pad, mode=mode, **call_kwargs) except (ValueError, TypeError, RuntimeError) as err: if any( k in str(err) for k in ("supported", "unexpected keyword", "implemented", "value") ): - return _np_pad(img, pad_width=to_pad, mode=mode, **call_kwargs) + return _np_pad(img, pad_width=to_pad, mode=mode, **call_kwargs)tests/transforms/croppad/test_pad_nd_dtypes.py (1)
59-69: Consider omittingvalue=0for non-constant modes.Line 66 passes
value=0for all modes, but "reflect" and "replicate" don't use this parameter. While the implementation strips it, omitting it in tests makes the intent clearer.🔎 Suggested change
- out = pad_nd(img, to_pad, mode=mode, value=0) + kwargs = {"value": 0} if mode == "constant" else {} + out = pad_nd(img, to_pad, mode=mode, **kwargs)
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Cache: Disabled due to data retention organization setting
Knowledge base: Disabled due to Reviews -> Disable Knowledge Base setting
📒 Files selected for processing (2)
monai/transforms/croppad/functional.pytests/transforms/croppad/test_pad_nd_dtypes.py
🧰 Additional context used
📓 Path-based instructions (1)
**/*.py
⚙️ CodeRabbit configuration file
Review the Python code for quality and correctness. Ensure variable names adhere to PEP8 style guides, are sensible and informative in regards to their function, though permitting simple names for loop and comprehension variables. Ensure routine names are meaningful in regards to their function and use verbs, adjectives, and nouns in a semantically appropriate way. Docstrings should be present for all definition which describe each variable, return value, and raised exception in the appropriate section of the Google-style of docstrings. Examine code for logical error or inconsistencies, and suggest what may be changed to addressed these. Suggest any enhancements for code improving efficiency, maintainability, comprehensibility, and correctness. Ensure new or modified definitions will be covered by existing or new unit tests.
Files:
monai/transforms/croppad/functional.pytests/transforms/croppad/test_pad_nd_dtypes.py
🧬 Code graph analysis (1)
monai/transforms/croppad/functional.py (1)
monai/transforms/utils_pytorch_numpy_unification.py (1)
mode(426-438)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (19)
- GitHub Check: flake8-py3 (codeformat)
- GitHub Check: flake8-py3 (pytype)
- GitHub Check: quick-py3 (windows-latest)
- GitHub Check: quick-py3 (ubuntu-latest)
- GitHub Check: quick-py3 (macOS-latest)
- GitHub Check: flake8-py3 (mypy)
- GitHub Check: packaging
- GitHub Check: build-docs
- GitHub Check: min-dep-py3 (3.10)
- GitHub Check: min-dep-py3 (3.11)
- GitHub Check: min-dep-pytorch (2.8.0)
- GitHub Check: min-dep-py3 (3.9)
- GitHub Check: min-dep-py3 (3.12)
- GitHub Check: min-dep-pytorch (2.5.1)
- GitHub Check: min-dep-os (windows-latest)
- GitHub Check: min-dep-os (ubuntu-latest)
- GitHub Check: min-dep-pytorch (2.7.1)
- GitHub Check: min-dep-pytorch (2.6.0)
- GitHub Check: min-dep-os (macOS-latest)
🔇 Additional comments (1)
monai/transforms/croppad/functional.py (1)
102-105: Logic looks correct for strippingvaluein non-constant modes.Copying kwargs and removing "value" when mode != "constant" prevents passing unsupported arguments to reflect/replicate/etc. modes.
| except NotImplementedError: | ||
| return _np_pad(img, pad_width=to_pad, mode=mode, **call_kwargs) | ||
| except (ValueError, TypeError, RuntimeError) as err: | ||
| if isinstance(err, NotImplementedError) or any( | ||
| if any( | ||
| k in str(err) for k in ("supported", "unexpected keyword", "implemented", "value") | ||
| ): | ||
| return _np_pad(img, pad_width=to_pad, mode=mode, **kwargs) | ||
| return _np_pad(img, pad_width=to_pad, mode=mode, **call_kwargs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
# First, let's look at the file structure and read the relevant section
cd /repo
wc -l monai/transforms/croppad/functional.pyRepository: Project-MONAI/MONAI
Length of output: 178
🏁 Script executed:
# Read the code around lines 106-112 with more context
head -130 monai/transforms/croppad/functional.py | tail -35Repository: Project-MONAI/MONAI
Length of output: 1953
🏁 Script executed:
# Search for the function that contains this error handling
grep -n "def.*pad" monai/transforms/croppad/functional.py | head -20Repository: Project-MONAI/MONAI
Length of output: 507
🌐 Web query:
PyTorch torch.nn.functional.pad exception types error handling documentation
💡 Result:
Short answer: PyTorch's torch.nn.functional.pad doesn't have a dedicated "exceptions" section in the docs — it follows normal Python/C++ argument-checking and backend checks, so you should expect primarily TypeError and ValueError from bad arguments (and occasional RuntimeError from lower-level/back-end kernels). See examples and sources below.
Common cases and what raises them
- TypeError: wrong argument types (e.g., TorchScript expects a List[int] for pad, not a tuple; wrong type for value). [3]
- ValueError: invalid pad shape/length (e.g., pad length not compatible with input dims or torchvision explicitly checks and raises for unsupported lengths). Also raised when a mode (reflect/replicate) requires constraints (padding must be <= input dim etc.). See docs on pad/modes. [1][2]
- RuntimeError (or backend errors): lower-level kernel or device-specific errors (e.g., CUDA nondeterminism warnings/errors or kernel failures) may surface as RuntimeError from the op at runtime. [1]
References
- torch.nn.functional.pad docs (describes pad format and mode constraints). [1]
- PyTorch GitHub issue showing ValueError from torchvision and differences in pad-length checking. [2]
- PyTorch forum thread showing a TorchScript TypeError when pad is not a List[int]. [3]
If you want, I can: (a) show example inputs that trigger each exception and how to catch them (try/except), or (b) search the exact source-code checks for the PyTorch version you use — tell me your PyTorch version.
Error-handling approach is reasonable but fragile.
Matching substrings like "supported", "unexpected keyword", "implemented", "value" in exception messages works but is brittle—error text can change across PyTorch versions. The approach tries to distinguish fallback cases (unsupported modes/dtypes) from genuine errors, but PyTorch doesn't provide dedicated exception types for unsupported operations.
Document why substring matching is necessary here, or investigate whether PyTorch's error hierarchy or error codes could provide more stable detection. If documentation is chosen, explain the fallback detection strategy clearly for future maintainers.
🤖 Prompt for AI Agents
In monai/transforms/croppad/functional.py around lines 106 to 112, the current
exception handling falls back to numpy based on fragile substring checks in the
PyTorch error message; either replace brittle string matching with a more stable
detection (e.g., detect specific PyTorch exception classes or inspect error
attributes/errno where available) or, if no stable API exists, add an inline
comment and unit-test-backed docstring that explains why substring matching is
necessary, lists the exact substrings being matched, and notes the PyTorch
versions observed—update the code to centralize the matching logic into a small
helper function with tests and a clear explanatory comment so future maintainers
can safely modify or replace it.
Prefer the PyTorch padding backend when supported and safely fall back
to NumPy on error. Add unit tests to validate backend selection and
ensure output dtype is preserved.
Fixes #7842
Description
This pull request relaxes dtype restrictions in
pad_ndand prefersthe PyTorch padding backend when supported, with a safe fallback to
NumPy on error. This enables support for additional dtypes (e.g. bool)
that are already handled correctly by recent PyTorch versions.
Unit tests are added to validate backend selection and ensure dtype
preservation.
Types of changes