Skip to content

Commit eb55abe

Browse files
fix(networks): replace Tensor | None union syntax with Optional[Tensor] 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 #7939 Signed-off-by: Oleksandr Sanin <alexaaander.sanin@gmail.com>
1 parent 0a8d945 commit eb55abe

3 files changed

Lines changed: 6 additions & 3 deletions

File tree

monai/networks/blocks/crossattention.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import torch
1515
import torch.nn as nn
16+
from typing import Optional
1617

1718
from monai.networks.layers.utils import get_rel_pos_embedding_layer
1819
from monai.utils import optional_import
@@ -139,7 +140,7 @@ def __init__(
139140
)
140141
self.input_size = input_size
141142

142-
def forward(self, x: torch.Tensor, context: torch.Tensor | None = None):
143+
def forward(self, x: torch.Tensor, context: Optional[torch.Tensor] = None):
143144
"""
144145
Args:
145146
x (torch.Tensor): input tensor. B x (s_dim_1 * ... * s_dim_n) x C

monai/networks/blocks/selfattention.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import torch
1515
import torch.nn as nn
1616
import torch.nn.functional as F
17+
from typing import Optional
1718

1819
from monai.networks.layers.utils import get_rel_pos_embedding_layer
1920
from monai.utils import optional_import
@@ -158,7 +159,7 @@ def __init__(
158159
)
159160
self.input_size = input_size
160161

161-
def forward(self, x, attn_mask: torch.Tensor | None = None):
162+
def forward(self, x, attn_mask: Optional[torch.Tensor] = None):
162163
"""
163164
Args:
164165
x (torch.Tensor): input tensor. B x (s_dim_1 * ... * s_dim_n) x C

monai/networks/blocks/transformerblock.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import torch
1515
import torch.nn as nn
16+
from typing import Optional
1617

1718
from monai.networks.blocks import CrossAttentionBlock, MLPBlock, SABlock
1819

@@ -89,7 +90,7 @@ def __init__(
8990
)
9091

9192
def forward(
92-
self, x: torch.Tensor, context: torch.Tensor | None = None, attn_mask: torch.Tensor | None = None
93+
self, x: torch.Tensor, context: Optional[torch.Tensor] = None, attn_mask: Optional[torch.Tensor] = None
9394
) -> torch.Tensor:
9495
x = x + self.attn(self.norm1(x), attn_mask=attn_mask)
9596
if self.with_cross_attention:

0 commit comments

Comments
 (0)