Skip to content

Conversation

@shubham-61969
Copy link

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_nd and prefers
the 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

  • Non-breaking change (fix or new feature that would not break existing functionality).
  • New tests added to cover the changes.

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>
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 24, 2025

📝 Walkthrough

Walkthrough

pad_nd now prefers the PyTorch padding path for modes {"constant", "reflect", "edge", "replicate", "wrap", "circular"} irrespective of input dtype. It constructs a trimmed call_kwargs (removing "value" for non-constant modes) and attempts the PyTorch backend; if PyTorch raises NotImplementedError or certain ValueError/TypeError/RuntimeError messages indicating unsupported combinations, it falls back to NumPy padding using call_kwargs. A new test module adds unit tests covering backend selection, dtype preservation across many dtypes and modes, and that supplying value with non-constant modes raises ValueError.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed Title clearly describes the main enhancement: adding support for additional dtypes in pad_nd.
Description check ✅ Passed Description is complete with required sections (issue reference, detailed explanation, types of changes) and covers the core changes.
Linked Issues check ✅ Passed Changes directly address issue #7842 by relaxing dtype restrictions and enabling PyTorch backend support for bool and integer dtypes.
Out of Scope Changes check ✅ Passed All changes are scoped to pad_nd dtype support: functional improvements and corresponding unit tests for backend selection and dtype preservation.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

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.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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 checks isinstance(err, NotImplementedError). NotImplementedError would propagate uncaught, breaking the fallback mechanism. The test at test_pad_falls_back_to_np_if_pt_raises expects 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

📥 Commits

Reviewing files that changed from the base of the PR and between 15fd428 and df7711d.

📒 Files selected for processing (2)
  • monai/transforms/croppad/functional.py
  • tests/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.py
  • tests/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>
Copy link
Contributor

@coderabbitai coderabbitai bot left a 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_pad to raise NotImplementedError and verifying _np_pad is 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 omitting value=0 for non-constant modes.

Line 66 passes value=0 for 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

📥 Commits

Reviewing files that changed from the base of the PR and between df7711d and ad9f60a.

📒 Files selected for processing (2)
  • monai/transforms/croppad/functional.py
  • tests/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.py
  • tests/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 stripping value in non-constant modes.

Copying kwargs and removing "value" when mode != "constant" prevents passing unsupported arguments to reflect/replicate/etc. modes.

shubham-61969 and others added 2 commits December 27, 2025 19:49
Signed-off-by: Shubham Chandravanshi <shubham.chandravanshi378@gmail.com>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (4)
tests/transforms/croppad/test_pad_nd_dtypes.py (4)

15-20: Import ordering could follow PEP8 more strictly.

Group imports: standard library, third-party, local. Consider:

 from __future__ import annotations
+
 from unittest.mock import Mock, patch
+
 import pytest
 import torch
+
 import monai.transforms.croppad.functional as F
 from monai.transforms.croppad.functional import pad_nd

22-31: Consider verifying output shape and values.

Test confirms backend selection and dtype preservation but doesn't check padding correctness. Add assertions:

assert out.shape == (1, 6, 8)
assert out[0, 1, 2].item() == True  # verify padded region if needed

33-45: Expand fallback testing to cover other exception types.

The implementation also catches ValueError, TypeError, and RuntimeError with message matching. Test these paths:

🔎 Additional test cases
@pytest.mark.parametrize("error_type,message", [
    (ValueError, "not supported"),
    (TypeError, "unexpected keyword argument"),
    (RuntimeError, "not implemented"),
])
def test_pad_falls_back_on_other_errors(error_type, message):
    """Test fallback when PyTorch raises ValueError/TypeError/RuntimeError."""
    img = torch.ones((1, 4, 4), dtype=torch.bool)
    to_pad = [(0, 0), (1, 1), (2, 2)]
    with (
        patch.object(F, "_pt_pad", new=Mock(side_effect=error_type(message))),
        patch.object(F, "_np_pad", wraps=F._np_pad) as mock_np,
    ):
        out = pad_nd(img, to_pad, mode="constant", value=0)
    assert mock_np.called
    assert out.dtype == img.dtype

59-70: LGTM. Consider expanding mode coverage.

Correctly handles value kwarg for constant mode only. For more comprehensive testing, add modes like "edge", "wrap", "circular".

📜 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

📥 Commits

Reviewing files that changed from the base of the PR and between ad9f60a and 7aa63e7.

📒 Files selected for processing (2)
  • monai/transforms/croppad/functional.py
  • tests/transforms/croppad/test_pad_nd_dtypes.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • monai/transforms/croppad/functional.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:

  • tests/transforms/croppad/test_pad_nd_dtypes.py
🧬 Code graph analysis (1)
tests/transforms/croppad/test_pad_nd_dtypes.py (1)
monai/transforms/croppad/functional.py (3)
  • pad_nd (73-119)
  • _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: min-dep-pytorch (2.8.0)
  • GitHub Check: min-dep-py3 (3.11)
  • GitHub Check: min-dep-pytorch (2.6.0)
  • GitHub Check: min-dep-os (ubuntu-latest)
  • 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-py3 (3.9)
  • GitHub Check: min-dep-pytorch (2.7.1)
  • GitHub Check: min-dep-py3 (3.10)
  • GitHub Check: min-dep-os (macOS-latest)
  • GitHub Check: quick-py3 (windows-latest)
  • GitHub Check: quick-py3 (ubuntu-latest)
  • GitHub Check: quick-py3 (macOS-latest)
  • GitHub Check: packaging
  • GitHub Check: flake8-py3 (codeformat)
  • GitHub Check: build-docs
  • GitHub Check: flake8-py3 (pytype)
  • GitHub Check: flake8-py3 (mypy)
🔇 Additional comments (1)
tests/transforms/croppad/test_pad_nd_dtypes.py (1)

47-57: LGTM.

Good dtype coverage. Shape and dtype assertions are appropriate for this test.

Copy link
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.

Hi @shubham-61969 thanks for the contribution. I think the change itself is fine with a few comments, the tests do need to be reformulated with unittest in particular. I think the previous implementation with its limitations was a result of older PyTorch versions so it's good to get this fix in. Please have a look again and then we can rereview.

Comment on lines 103 to 104
if mode != "constant":
call_kwargs.pop("value", None)
Copy link
Member

Choose a reason for hiding this comment

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

If this condition isn't present, the effect of providing a value argument and not use "constant" mode is to raise an exception in the pad routine used, I think this is intended behaviour. Here if the value argument is removed this silently allows unintended arguments to be ignored, it's better to raise an exception instead.

@@ -0,0 +1,70 @@
# Copyright (c) MONAI Consortium
Copy link
Member

Choose a reason for hiding this comment

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

In MONAI we use unittest and parameterized packages for tests and not pytest, specifically we use test classes and methods for unit tests rather than functions. Please reformulate these tests to use these packages according the style of other tests that are present here. It might make sense to add your tests to an existing file rather than a new one, have a look at existing files to see if it does make sense that way.

shubham-61969 and others added 5 commits January 21, 2026 23:04
- Raise an explicit error when �alue is provided with non-constant modes.
- Rewrite tests using unittest + parameterized to match MONAI style.

Signed-off-by: Shubham Chandravanshi <shubham.chandravanshi378@gmail.com>
Signed-off-by: Shubham Chandravanshi <shubham.chandravanshi378@gmail.com>
Signed-off-by: Shubham Chandravanshi <shubham.chandravanshi378@gmail.com>
@shubham-61969
Copy link
Author

Hi @ericspod , thanks for the feedback.

I’ve updated the implementation to explicitly raise an error when value is provided with modes other than "constant", so the original contract is preserved. I also reworked the tests to use unittest + parameterized and aligned them with MONAI’s existing test style.

All GitHub CI checks are now passing.

Could you please take another look? If there are any further changes you’d like, I’m happy to address them otherwise I believe this should be ready to merge.

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.

Support more dtypes in pad_nd

2 participants