fix(networks): replace Tensor | None with Optional[Tensor] for TorchScript compatibility#8879
Open
AlexanderSanin wants to merge 2 commits into
Open
Conversation
…r] for TorchScript compatibility The `|` union type syntax (e.g. `torch.Tensor | None`) was introduced in Python 3.10. While `from __future__ import annotations` defers evaluation at runtime, TorchScript's annotation parser does not support this syntax and fails when scripting models that contain these forward method signatures. Replace `torch.Tensor | None` with `Optional[torch.Tensor]` in the `forward` methods of: - `monai/networks/blocks/crossattention.py` (CrossAttentionBlock) - `monai/networks/blocks/selfattention.py` (SABlock) - `monai/networks/blocks/transformerblock.py` (TransformerBlock) These three blocks are used in the ViT/UNETR scripting path, causing `RuntimeError: Can't redefine method: forward on class` when `torch.jit.script()` is called on a UNETR model. Closes Project-MONAI#7939 Signed-off-by: Oleksandr Sanin <alexaaander.sanin@gmail.com>
Contributor
Author
|
Hey @holgerroth @wyli @ericspod. Could you, please, have a look at this? |
for more information, see https://pre-commit.ci
Contributor
✨ 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #7939
The
|union type syntax (e.g.torch.Tensor | None) was introduced in Python 3.10. Whilefrom __future__ import annotationsdefers annotation evaluation at runtime, TorchScript's annotation parser does not support this syntax and fails when scripting models that include these forward method signatures, producing:This PR replaces
torch.Tensor | NonewithOptional[torch.Tensor](fromtyping) in theforwardmethods of the three blocks that form the ViT/UNETR scripting path:monai/networks/blocks/crossattention.py—CrossAttentionBlock.forwardmonai/networks/blocks/selfattention.py—SABlock.forwardmonai/networks/blocks/transformerblock.py—TransformerBlock.forwardTest plan
torch.jit.script(CrossAttentionBlock(...))succeeds after fixtorch.jit.script(SABlock(...))succeeds after fixtorch.jit.script(TransformerBlock(...))succeeds after fixpython -m pytest tests/networks/nets/test_unetr.py -k test_scriptpython -m pytest tests/networks/blocks/test_crossattention.pypython -m pytest tests/networks/blocks/test_selfattention.py