Skip to content

Conversation

@Kheil-Z
Copy link

@Kheil-Z Kheil-Z commented Nov 12, 2025

Fixes #8540 .

Description

This PR updates the docstring of monai.networks.blocks.Warp to clarify that mode='nearest' results in zero gradients almost everywhere, effectively blocking gradient flow in registration training.

Changes:

Added a note to the Warp docstring about the gradient behavior of mode='nearest'.

Added a reference to the semi-supervised registration tutorial:
https://github.com/Project-MONAI/tutorials/blob/main/3d_registration/learn2reg_oasis_unpaired_brain_mr.ipynb

Next Steps (future PR):

Extend VoxelMorph.forward to handle optional label and keypoint arguments for consistent warping behavior.

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
Contributor

coderabbitai bot commented Nov 12, 2025

Walkthrough

This PR adds an informational note to the Warp class constructor docstring in monai/networks/blocks/warp.py. The note states that using mode="nearest" yields non-differentiable warping (near-zero gradients), recommends bilinear/trilinear interpolation for learning-based registration, and includes a reference/example notebook link. No code logic or control flow is modified.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~2 minutes

  • Pure docstring addition in one file; no behavioral changes.

Pre-merge checks and finishing touches

✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed Title directly matches the main change: adding a differentiability warning for Warp with mode='nearest' to documentation.
Description check ✅ Passed Description covers all required template sections: fixes issue reference, clear description of changes, types marked appropriately, and inline docstrings/documentation updated.
Linked Issues check ✅ Passed PR fulfills #8540 requirements: adds clear warning about non-differentiability of mode='nearest' in Warp docstring and provides learning resource reference.
Out of Scope Changes check ✅ Passed Changes are confined to docstring updates in Warp.init; no code logic changes. Future VoxelMorph work noted as out of scope for this PR.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent 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 7adf291 and 5f5936e.

📒 Files selected for processing (1)
  • monai/networks/blocks/warp.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • monai/networks/blocks/warp.py
⏰ 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: packaging
  • GitHub Check: build-docs
  • GitHub Check: quick-py3 (macOS-latest)
  • GitHub Check: flake8-py3 (mypy)
  • GitHub Check: quick-py3 (windows-latest)
  • GitHub Check: quick-py3 (ubuntu-latest)
  • GitHub Check: flake8-py3 (pytype)
  • GitHub Check: flake8-py3 (codeformat)
  • 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-os (ubuntu-latest)
  • GitHub Check: min-dep-pytorch (2.8.0)
  • GitHub Check: min-dep-pytorch (2.7.1)
  • GitHub Check: min-dep-py3 (3.10)
  • GitHub Check: min-dep-py3 (3.9)
  • GitHub Check: min-dep-pytorch (2.6.0)
  • GitHub Check: min-dep-py3 (3.11)
  • GitHub Check: min-dep-pytorch (2.5.1)

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: 0

🧹 Nitpick comments (1)
monai/networks/blocks/warp.py (1)

56-61: Fix minor formatting inconsistencies in the documentation note.

The content is accurate and addresses the issue well, but two formatting tweaks are needed for consistency with the existing docstring style:

  1. Line 56: Remove the space after = in mode= (should be mode="nearest" not mode= "nearest")
  2. Line 58: Use backticks for code literals ("bilinear", "trilinear") instead of straight quotes, consistent with the rest of the docstring

Apply this diff to fix the formatting:

-        Note that using mode= ``"nearest"`` makes the warping operation effectively non-differentiable:
+        Note that using mode=``"nearest"`` makes the warping operation effectively non-differentiable:
         gradients are zero almost everywhere, which can block gradient flow during training.
-        For learning-based registration, use "bilinear" (2D) or "trilinear" (3D) interpolation instead.
+        For learning-based registration, use ``"bilinear"`` (2D) or ``"trilinear"`` (3D) interpolation instead.
📜 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 a8a7e57 and 7adf291.

📒 Files selected for processing (1)
  • monai/networks/blocks/warp.py (1 hunks)
🧰 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/networks/blocks/warp.py

Kheil-Z and others added 3 commits November 12, 2025 13:31
I, Kheil-Z <ziadishappy@gmail.com>, hereby add my Signed-off-by to this commit: 7adf291
I, Kheil-Z <ziadishappy@gmail.com>, hereby add my Signed-off-by to this commit: 5f5936e

Signed-off-by: Kheil-Z <ziadishappy@gmail.com>
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 @Kheil-Z thanks for the contribution! I think we're good to merge if @KumoLiu could trigger blossom later.

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.

Add warning to documentation that warp in nearest neighbor mode is non differentiable

2 participants