Skip to content

Commit 986e48c

Browse files
committed
--unsafe-fixes
1 parent 8a7eeed commit 986e48c

32 files changed

+89
-106
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,11 @@ repos:
3030
rev: v0.7.0
3131
hooks:
3232
- id: ruff
33-
args: ["--fix"]
33+
args: ["--fix"
3434
exclude: |
3535
(?x)(
3636
^versioneer.py|
37-
^monai/_version.py|
38-
^monai/networks/| # avoid typing rewrites
39-
^monai/apps/detection/utils/anchor_utils.py| # avoid typing rewrites
40-
^tests/test_compute_panoptic_quality.py # avoid typing rewrites
37+
^monai/_version.py
4138
)
4239
4340
- repo: https://github.com/asottile/yesqa

monai/apps/detection/utils/anchor_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
from __future__ import annotations
4141

42-
from typing import List, Sequence
42+
from collections.abc import Sequence
4343

4444
import torch
4545
from torch import Tensor, nn
@@ -106,7 +106,7 @@ class AnchorGenerator(nn.Module):
106106
anchor_generator = AnchorGenerator(sizes, aspect_ratios)
107107
"""
108108

109-
__annotations__ = {"cell_anchors": List[torch.Tensor]}
109+
__annotations__ = {"cell_anchors": list[torch.Tensor]}
110110

111111
def __init__(
112112
self,
@@ -364,7 +364,7 @@ class AnchorGeneratorWithAnchorShape(AnchorGenerator):
364364
anchor_generator = AnchorGeneratorWithAnchorShape(feature_map_scales, base_anchor_shapes)
365365
"""
366366

367-
__annotations__ = {"cell_anchors": List[torch.Tensor]}
367+
__annotations__ = {"cell_anchors": list[torch.Tensor]}
368368

369369
def __init__(
370370
self,

monai/networks/blocks/attention_utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
from __future__ import annotations
1111

12-
from typing import Tuple
1312

1413
import torch
1514
import torch.nn.functional as F
@@ -50,7 +49,7 @@ def get_rel_pos(q_size: int, k_size: int, rel_pos: torch.Tensor) -> torch.Tensor
5049

5150

5251
def add_decomposed_rel_pos(
53-
attn: torch.Tensor, q: torch.Tensor, rel_pos_lst: nn.ParameterList, q_size: Tuple, k_size: Tuple
52+
attn: torch.Tensor, q: torch.Tensor, rel_pos_lst: nn.ParameterList, q_size: tuple, k_size: tuple
5453
) -> torch.Tensor:
5554
r"""
5655
Calculate decomposed Relative Positional Embeddings from mvitv2 implementation:

monai/networks/blocks/crossattention.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
from __future__ import annotations
1313

14-
from typing import Optional, Tuple
1514

1615
import torch
1716
import torch.nn as nn
@@ -41,9 +40,9 @@ def __init__(
4140
save_attn: bool = False,
4241
causal: bool = False,
4342
sequence_length: int | None = None,
44-
rel_pos_embedding: Optional[str] = None,
45-
input_size: Optional[Tuple] = None,
46-
attention_dtype: Optional[torch.dtype] = None,
43+
rel_pos_embedding: str | None = None,
44+
input_size: tuple | None = None,
45+
attention_dtype: torch.dtype | None = None,
4746
use_flash_attention: bool = False,
4847
) -> None:
4948
"""
@@ -134,7 +133,7 @@ def __init__(
134133
)
135134
self.input_size = input_size
136135

137-
def forward(self, x: torch.Tensor, context: Optional[torch.Tensor] = None):
136+
def forward(self, x: torch.Tensor, context: torch.Tensor | None = None):
138137
"""
139138
Args:
140139
x (torch.Tensor): input tensor. B x (s_dim_1 * ... * s_dim_n) x C

monai/networks/blocks/denseblock.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from __future__ import annotations
1313

14-
from typing import Sequence
14+
from collections.abc import Sequence
1515

1616
import torch
1717
import torch.nn as nn

monai/networks/blocks/mlp.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
from __future__ import annotations
1313

14-
from typing import Union
1514

1615
import torch.nn as nn
1716

@@ -56,8 +55,8 @@ def __init__(
5655
self.linear2 = nn.Linear(mlp_dim, hidden_size)
5756
self.fn = get_act_layer(act)
5857
# Use Union[nn.Dropout, nn.Identity] for type annotations
59-
self.drop1: Union[nn.Dropout, nn.Identity]
60-
self.drop2: Union[nn.Dropout, nn.Identity]
58+
self.drop1: nn.Dropout | nn.Identity
59+
self.drop2: nn.Dropout | nn.Identity
6160

6261
dropout_opt = look_up_option(dropout_mode, SUPPORTED_DROPOUT_MODE)
6362
if dropout_opt == "vit":

monai/networks/blocks/patchembedding.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from __future__ import annotations
1313

1414
from collections.abc import Sequence
15-
from typing import Optional
1615

1716
import numpy as np
1817
import torch
@@ -54,7 +53,7 @@ def __init__(
5453
pos_embed_type: str = "learnable",
5554
dropout_rate: float = 0.0,
5655
spatial_dims: int = 3,
57-
pos_embed_kwargs: Optional[dict] = None,
56+
pos_embed_kwargs: dict | None = None,
5857
) -> None:
5958
"""
6059
Args:

monai/networks/blocks/pos_embed_utils.py

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

1414
import collections.abc
1515
from itertools import repeat
16-
from typing import List, Union
1716

1817
import torch
1918
import torch.nn as nn
@@ -33,7 +32,7 @@ def parse(x):
3332

3433

3534
def build_fourier_position_embedding(
36-
grid_size: Union[int, List[int]], embed_dim: int, spatial_dims: int = 3, scales: Union[float, List[float]] = 1.0
35+
grid_size: int | list[int], embed_dim: int, spatial_dims: int = 3, scales: float | list[float] = 1.0
3736
) -> torch.nn.Parameter:
3837
"""
3938
Builds a (Anistropic) Fourier feature position embedding based on the given grid size, embed dimension,
@@ -86,7 +85,7 @@ def build_fourier_position_embedding(
8685

8786

8887
def build_sincos_position_embedding(
89-
grid_size: Union[int, List[int]], embed_dim: int, spatial_dims: int = 3, temperature: float = 10000.0
88+
grid_size: int | list[int], embed_dim: int, spatial_dims: int = 3, temperature: float = 10000.0
9089
) -> torch.nn.Parameter:
9190
"""
9291
Builds a sin-cos position embedding based on the given grid size, embed dimension, spatial dimensions, and temperature.

monai/networks/blocks/rel_pos_embedding.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from __future__ import annotations
1111

12-
from typing import Iterable, Tuple
12+
from collections.abc import Iterable
1313

1414
import torch
1515
from torch import nn
@@ -19,7 +19,7 @@
1919

2020

2121
class DecomposedRelativePosEmbedding(nn.Module):
22-
def __init__(self, s_input_dims: Tuple[int, int] | Tuple[int, int, int], c_dim: int, num_heads: int) -> None:
22+
def __init__(self, s_input_dims: tuple[int, int] | tuple[int, int, int], c_dim: int, num_heads: int) -> None:
2323
"""
2424
Args:
2525
s_input_dims (Tuple): input spatial dimension. (H, W) or (H, W, D)

monai/networks/blocks/selfattention.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
from __future__ import annotations
1313

14-
from typing import Optional, Tuple, Union
1514

1615
import torch
1716
import torch.nn as nn
@@ -41,7 +40,7 @@ def __init__(
4140
causal: bool = False,
4241
sequence_length: int | None = None,
4342
rel_pos_embedding: str | None = None,
44-
input_size: Tuple | None = None,
43+
input_size: tuple | None = None,
4544
attention_dtype: torch.dtype | None = None,
4645
include_fc: bool = True,
4746
use_combined_linear: bool = True,
@@ -101,16 +100,16 @@ def __init__(
101100

102101
self.num_heads = num_heads
103102
self.hidden_input_size = hidden_input_size if hidden_input_size else hidden_size
104-
self.out_proj: Union[nn.Linear, nn.Identity]
103+
self.out_proj: nn.Linear | nn.Identity
105104
if include_fc:
106105
self.out_proj = nn.Linear(self.inner_dim, self.hidden_input_size)
107106
else:
108107
self.out_proj = nn.Identity()
109108

110-
self.qkv: Union[nn.Linear, nn.Identity]
111-
self.to_q: Union[nn.Linear, nn.Identity]
112-
self.to_k: Union[nn.Linear, nn.Identity]
113-
self.to_v: Union[nn.Linear, nn.Identity]
109+
self.qkv: nn.Linear | nn.Identity
110+
self.to_q: nn.Linear | nn.Identity
111+
self.to_k: nn.Linear | nn.Identity
112+
self.to_v: nn.Linear | nn.Identity
114113

115114
if use_combined_linear:
116115
self.qkv = nn.Linear(self.hidden_input_size, self.inner_dim * 3, bias=qkv_bias)
@@ -153,7 +152,7 @@ def __init__(
153152
)
154153
self.input_size = input_size
155154

156-
def forward(self, x, attn_mask: Optional[torch.Tensor] = None):
155+
def forward(self, x, attn_mask: torch.Tensor | None = None):
157156
"""
158157
Args:
159158
x (torch.Tensor): input tensor. B x (s_dim_1 * ... * s_dim_n) x C

0 commit comments

Comments
 (0)