Add NaViT: Native Resolution Vision Transformer with Patch n' Pack - #9011
Add NaViT: Native Resolution Vision Transformer with Patch n' Pack#9011vikashg wants to merge 10 commits into
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughAdds the NaViT native-resolution vision transformer for variable-resolution 2D and 3D inputs. The implementation supports patch extraction, token dropout, sequence grouping, packed attention masks, factorized positional embeddings, transformer blocks, attention pooling, and classification. NaViT is exported from Estimated code review effort: 4 (Complex) | ~60 minutes 🚥 Pre-merge checks | ✅ 5✅ 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.
Actionable comments posted: 4
🧹 Nitpick comments (1)
monai/networks/nets/navit.py (1)
427-446: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win
forward()docstring omits theRaisessection.
forward()raisesValueErrorfor wrong ndim (line 475), wrong channel count (line 480), and non-divisible spatial dims (line 484), but none of this is documented. As per path instructions, docstrings should describe raised exceptions in the appropriate Google-style section.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@monai/networks/nets/navit.py` around lines 427 - 446, The forward() docstring in NaViT should document its ValueError cases. Add a Google-style Raises section describing ValueError for invalid tensor dimensionality, mismatched channel count, or spatial dimensions not divisible by patch_size.Source: Path instructions
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@monai/networks/nets/navit.py`:
- Around line 344-348: Validate each dimension of image_size in the constructor
before building pos_embed_axes, requiring exact divisibility by patch_size and
raising a clear error otherwise. Use image_size_t and preserve the existing
positional-embedding table sizing only for valid dimensions.
- Around line 314-321: Update the constructor validation near the
hidden_size/num_heads divisibility check to explicitly reject non-positive
num_heads with ValueError before evaluating hidden_size % num_heads. Preserve
the existing ValueError for non-divisible positive values and the documented
validation behavior.
- Around line 492-497: Gate the token-dropout block in the NaViT forward path on
both self.calc_token_dropout being set and self.training, so dropout is applied
only during training. Keep the existing dropout fraction, index selection, and
sequence/position filtering unchanged when training is active.
- Around line 206-218: Update the forward method to preserve the post-attention
residual in x, pass only its normalized value to self.mlp, and add the MLP
output back to the unnormalized residual. Keep the attention residual and
existing return shape unchanged.
---
Nitpick comments:
In `@monai/networks/nets/navit.py`:
- Around line 427-446: The forward() docstring in NaViT should document its
ValueError cases. Add a Google-style Raises section describing ValueError for
invalid tensor dimensionality, mismatched channel count, or spatial dimensions
not divisible by patch_size.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: 91dc08cb-fbc3-4e40-8d78-11336fac95b2
📒 Files selected for processing (6)
CHANGELOG.mddocs/source/networks.rstdocs/source/whatsnew_1_5_2.mdmonai/networks/nets/__init__.pymonai/networks/nets/navit.pytests/networks/nets/test_navit.py
Adds NaViT (monai.networks.nets.NaViT), a Vision Transformer that removes the fixed-resolution constraint of standard ViT by packing multiple variable-size images into a single sequence per batch element. Key features: - Patch n' Pack: multiple images concatenated into one sequence per group, with a per-image attention mask preventing cross-image attention - Factorised positional embeddings: separate learnable tables per spatial axis, allowing generalisation to unseen resolutions - Token dropout: configurable fraction of patch tokens dropped during training (float or callable) - Attention pooling: learned query attends over each image's tokens to produce a fixed-size per-image representation - QK normalisation: RMS normalisation on queries and keys (ViT-22B style) - 2D and 3D support: works for (C, H, W) and (C, H, W, D) inputs Changes: - monai/networks/nets/navit.py: new NaViT implementation - monai/networks/nets/__init__.py: export NaViT - tests/networks/nets/test_navit.py: 24 unit tests covering shape, variable resolutions, token dropout, auto-grouping, gradient flow, ill arguments, and forward validation - docs/source/networks.rst: autoclass entry - docs/source/whatsnew_1_5_2.md: feature description - CHANGELOG.md: entry under Unreleased Signed-off-by: Vikash Gupta <write2vikash@gmail.com>
for more information, see https://pre-commit.ci
Signed-off-by: Vikash Gupta <write2vikash@gmail.com>
NaViT depends on einops (optional dependency), so test_navit must be excluded from the minimal CI runner per CONTRIBUTING.md guidelines. Signed-off-by: Vikash Gupta <write2vikash@gmail.com>
…ling - Remove NaViT section from whatsnew_1_5_2.md: 1.5.2 was a security-only patch release; NaViT belongs in the upcoming release under [Unreleased] - CHANGELOG [Unreleased]: factorised -> factorized, normalisation -> normalization - navit.py docstrings and comments: normalisation -> normalization, generalisation -> generalization, factorised -> factorized (MONAI uses American English per CONTRIBUTING.md) Signed-off-by: Vikash Gupta <write2vikash@gmail.com>
Remediation for bot commit aa7a76a ([pre-commit.ci] auto fixes from pre-commit.com hooks) which removed an unused SABlock import from navit.py. That commit was made by pre-commit-ci[bot] and could not carry a Signed-off-by line. Signed-off-by: Vikash Gupta <write2vikash@gmail.com>
- Fix residual stream bug: use x = self.mlp(self.norm(x)) + x (pre-norm) - Gate token dropout with self.training for deterministic eval - Add num_heads <= 0 ValueError guard - Validate image_size divisibility by patch_size in constructor - Add Raises section to forward() docstring - Refactor tests: DEFAULT_KWARGS, consolidated forward-validation tests Signed-off-by: Vikash Gupta <write2vikash@gmail.com>
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@tests/networks/nets/test_navit.py`:
- Around line 168-170: Complete the docstring for test_forward_validation by
documenting the image_shape parameter and the ValueError assertion condition,
following the repository’s Google-style format; do not alter the test behavior.
- Around line 184-204: Strengthen test_token_dropout_callable and
test_token_dropout_disabled_in_eval by using a recording callable, asserting it
is invoked in training and not invoked in evaluation. Add a training-mode
fixed-float token-dropout test with identical input under different RNG seeds
and assert the outputs differ, preserving the existing shape and eval
determinism checks.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: 8301cac5-e1d5-4442-8c55-002f545792fe
📒 Files selected for processing (2)
monai/networks/nets/navit.pytests/networks/nets/test_navit.py
🚧 Files skipped from review as they are similar to previous changes (1)
- monai/networks/nets/navit.py
Signed-off-by: Vikash Gupta <write2vikash@gmail.com>
…ence - test_token_dropout_callable_invoked_during_training: asserts callable IS invoked during training via a recording wrapper - test_token_dropout_callable_not_invoked_during_eval: asserts callable is NOT invoked during eval mode - test_token_dropout_produces_different_outputs_in_training: same input with different RNG seeds produces different outputs (proves dropout runs) - test_token_dropout_disabled_in_eval: preserved existing determinism check Signed-off-by: Vikash Gupta <write2vikash@gmail.com>
Adds NaViT (monai.networks.nets.NaViT), a Vision Transformer that removes the fixed-resolution constraint of standard ViT by packing multiple variable-size images into a single sequence per batch element.
Key features:
Changes:
Fixes # .
Description
A few sentences describing the changes proposed in this pull request.
Types of changes
./runtests.sh -f -u --net --coverage../runtests.sh --quick --unittests --disttests.make htmlcommand in thedocs/folder.