Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion monai/networks/blocks/crossattention.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

from __future__ import annotations

from typing import Optional

import torch
import torch.nn as nn

Expand Down Expand Up @@ -139,7 +141,7 @@ def __init__(
)
self.input_size = input_size

def forward(self, x: torch.Tensor, context: torch.Tensor | None = None):
def forward(self, x: torch.Tensor, context: Optional[torch.Tensor] = None): # noqa: UP045
"""
Args:
x (torch.Tensor): input tensor. B x (s_dim_1 * ... * s_dim_n) x C
Expand Down
4 changes: 3 additions & 1 deletion monai/networks/blocks/selfattention.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

from __future__ import annotations

from typing import Optional

import torch
import torch.nn as nn
import torch.nn.functional as F
Expand Down Expand Up @@ -158,7 +160,7 @@ def __init__(
)
self.input_size = input_size

def forward(self, x, attn_mask: torch.Tensor | None = None):
def forward(self, x, attn_mask: Optional[torch.Tensor] = None): # noqa: UP045
"""
Args:
x (torch.Tensor): input tensor. B x (s_dim_1 * ... * s_dim_n) x C
Expand Down
12 changes: 11 additions & 1 deletion monai/networks/blocks/transformerblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

from __future__ import annotations

from typing import Optional

import torch
import torch.nn as nn

Expand All @@ -23,6 +25,11 @@ class TransformerBlock(nn.Module):
An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale <https://arxiv.org/abs/2010.11929>"
"""

# Treat ``with_cross_attention`` as a TorchScript constant so the cross-attention branch in
# ``forward`` is pruned when it is False. Otherwise scripting tries to compile the
# ``self.cross_attn(..., context=context)`` call against ``nn.Identity`` and fails.
__constants__ = ["with_cross_attention"]

def __init__(
self,
hidden_size: int,
Expand Down Expand Up @@ -102,7 +109,10 @@ def __init__(
self.cross_attn = nn.Identity()

def forward(
self, x: torch.Tensor, context: torch.Tensor | None = None, attn_mask: torch.Tensor | None = None
self,
x: torch.Tensor,
context: Optional[torch.Tensor] = None, # noqa: UP045
attn_mask: Optional[torch.Tensor] = None, # noqa: UP045
) -> torch.Tensor:
x = x + self.attn(self.norm1(x), attn_mask=attn_mask)
if self.with_cross_attention:
Expand Down
Loading