Skip to content

Commit 3a418ae

Browse files
change licesning with meta. Tensor is torch.Tensor. remove __future__.
1 parent 1edaadb commit 3a418ae

File tree

12 files changed

+75
-95
lines changed

12 files changed

+75
-95
lines changed

src/anomalib/models/components/dinov2/dinov2_loader.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
downstream anomaly detection tasks.
2828
"""
2929

30-
from __future__ import annotations
31-
3230
import logging
3331
from pathlib import Path
3432
from typing import ClassVar

src/anomalib/models/components/dinov2/layers/attention.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Copyright (C) 2025 Intel Corporation
22
# SPDX-License-Identifier: Apache-2.0
3+
# Copyright (C) 2025 Meta Platforms, Inc. and affiliates.
4+
# SPDX-License-Identifier: Apache-2.0
35

46
"""Attention layers for DINOv2 Vision Transformers.
57
@@ -11,12 +13,10 @@
1113
blocks for feature extraction and masked modeling.
1214
"""
1315

14-
from __future__ import annotations
15-
1616
import logging
1717

1818
import torch
19-
from torch import Tensor, nn
19+
from torch import nn
2020
from torch.nn import functional as F # noqa: N812
2121

2222
logger = logging.getLogger(__name__)
@@ -82,15 +82,15 @@ def init_weights(
8282
if self.proj.bias is not None:
8383
nn.init.zeros_(self.proj.bias)
8484

85-
def forward(self, x: Tensor, is_causal: bool = False) -> Tensor:
85+
def forward(self, x: torch.Tensor, is_causal: bool = False) -> torch.Tensor:
8686
"""Apply multi-head self-attention.
8787
8888
Args:
8989
x: Input sequence of shape ``(B, N, C)``.
9090
is_causal: If True, applies causal masking.
9191
9292
Returns:
93-
Tensor of shape ``(B, N, C)`` containing attended features.
93+
torch.Tensor of shape ``(B, N, C)`` containing attended features.
9494
"""
9595
b, n, c = x.shape
9696
qkv = self.qkv(x).reshape(b, n, 3, self.num_heads, c // self.num_heads)
@@ -120,7 +120,7 @@ class MemEffAttention(Attention):
120120
this implementation uses the scaled dot product from torch.
121121
"""
122122

123-
def forward(self, x: Tensor, attn_bias: Tensor | None = None) -> Tensor:
123+
def forward(self, x: torch.Tensor, attn_bias: torch.Tensor | None = None) -> torch.Tensor:
124124
"""Compute memory-efficient attention using PyTorch's scaled dot product attention.
125125
126126
Args:

src/anomalib/models/components/dinov2/layers/block.py

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,27 @@
11
# Copyright (C) 2025 Intel Corporation
22
# SPDX-License-Identifier: Apache-2.0
3+
# Copyright (C) 2025 Meta Platforms, Inc. and affiliates.
4+
# SPDX-License-Identifier: Apache-2.0
5+
36

47
"""Transformer blocks used in DINOv2 Vision Transformers.
58
69
This module implements:
710
- Standard transformer blocks with attention and MLP (`Block`)
811
- Causal attention blocks (`CausalAttentionBlock`)
9-
10-
The implementation is adapted from the original DINO and timm Vision
11-
Transformer code:
12-
13-
- https://github.com/facebookresearch/dino/blob/master/vision_transformer.py
14-
- https://github.com/rwightman/pytorch-image-models/tree/master/timm/layers/patch_embed.py
1512
"""
1613

17-
from __future__ import annotations
18-
1914
import logging
20-
from typing import TYPE_CHECKING
15+
from collections.abc import Callable
2116

2217
import torch
23-
from torch import Tensor, nn
18+
from torch import nn
2419

2520
from .attention import Attention
2621
from .drop_path import DropPath
2722
from .layer_scale import LayerScale
2823
from .mlp import Mlp
2924

30-
if TYPE_CHECKING:
31-
from collections.abc import Callable
32-
3325
logger = logging.getLogger("dinov2")
3426

3527

@@ -66,7 +58,7 @@ def __init__(
6658
ffn_bias: bool = True,
6759
drop: float = 0.0,
6860
attn_drop: float = 0.0,
69-
init_values: float | Tensor | None = None,
61+
init_values: float | torch.Tensor | None = None,
7062
drop_path: float = 0.0,
7163
act_layer: Callable[..., nn.Module] = nn.GELU,
7264
norm_layer: Callable[..., nn.Module] = nn.LayerNorm,
@@ -101,13 +93,13 @@ def __init__(
10193

10294
self.sample_drop_ratio: float = drop_path
10395

104-
def forward(self, x: Tensor) -> Tensor:
96+
def forward(self, x: torch.Tensor) -> torch.Tensor:
10597
"""Apply attention and MLP residual blocks with optional stochastic depth."""
10698

107-
def attn_residual_func(inp: Tensor) -> Tensor:
99+
def attn_residual_func(inp: torch.Tensor) -> torch.Tensor:
108100
return self.ls1(self.attn(self.norm1(inp)))
109101

110-
def ffn_residual_func(inp: Tensor) -> Tensor:
102+
def ffn_residual_func(inp: torch.Tensor) -> torch.Tensor:
111103
return self.ls2(self.mlp(self.norm2(inp)))
112104

113105
if self.training and self.sample_drop_ratio > 0.1:
@@ -200,17 +192,17 @@ def init_weights(
200192
nn.init.normal_(self.feed_forward.fc2.weight, std=init_proj_std)
201193
self.ffn_norm.reset_parameters()
202194

203-
def forward(self, x: Tensor) -> Tensor:
195+
def forward(self, x: torch.Tensor) -> torch.Tensor:
204196
"""Apply causal attention followed by a feed-forward block."""
205197
x_attn = x + self.ls1(self.attention(self.attention_norm(x), self.is_causal))
206198
return x_attn + self.ls2(self.feed_forward(self.ffn_norm(x_attn)))
207199

208200

209201
def drop_add_residual_stochastic_depth(
210-
x: Tensor,
211-
residual_func: Callable[[Tensor], Tensor],
202+
x: torch.Tensor,
203+
residual_func: Callable[[torch.Tensor], torch.Tensor],
212204
sample_drop_ratio: float = 0.0,
213-
) -> Tensor:
205+
) -> torch.Tensor:
214206
"""Apply stochastic depth to a residual branch on a subset of samples.
215207
216208
Args:
@@ -219,7 +211,7 @@ def drop_add_residual_stochastic_depth(
219211
sample_drop_ratio: Fraction of samples to drop for residual computation.
220212
221213
Returns:
222-
Tensor with residual added to a subset of samples.
214+
torch.Tensor with residual added to a subset of samples.
223215
"""
224216
b, _, _ = x.shape
225217
sample_subset_size = max(int(b * (1 - sample_drop_ratio)), 1)

src/anomalib/models/components/dinov2/layers/dino_head.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
# Copyright (C) 2025 Intel Corporation
22
# SPDX-License-Identifier: Apache-2.0
3+
# Copyright (C) 2025 Meta Platforms, Inc. and affiliates.
4+
# SPDX-License-Identifier: Apache-2.0
5+
36

47
"""DINO projection head module.
58
69
Reference:
710
https://github.com/facebookresearch/dinov2/blob/main/dinov2/layers/dino_head.py
811
"""
912

10-
from __future__ import annotations
11-
1213
import torch
13-
from torch import Tensor, nn
14+
from torch import nn
1415
from torch.nn.init import trunc_normal_
1516
from torch.nn.utils import weight_norm
1617

@@ -68,7 +69,7 @@ def _init_weights(self, module: nn.Module) -> None: # noqa: PLR6301
6869
if module.bias is not None:
6970
nn.init.constant_(module.bias, 0)
7071

71-
def forward(self, x: Tensor) -> Tensor:
72+
def forward(self, x: torch.Tensor) -> torch.Tensor:
7273
"""Run the DINO projection head forward pass."""
7374
x = self.mlp(x)
7475

src/anomalib/models/components/dinov2/layers/drop_path.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Copyright (C) 2025 Intel Corporation
22
# SPDX-License-Identifier: Apache-2.0
3+
# Copyright (C) 2025 Meta Platforms, Inc. and affiliates.
4+
# SPDX-License-Identifier: Apache-2.0
35

46
"""Stochastic depth drop-path implementation used in DINOv2.
57
@@ -8,12 +10,11 @@
810
drops entire residual branches during training to improve model robustness.
911
"""
1012

11-
from __future__ import annotations
12-
13-
from torch import Tensor, nn
13+
import torch
14+
from torch import nn
1415

1516

16-
def drop_path(x: Tensor, drop_prob: float = 0.0, training: bool = False) -> Tensor:
17+
def drop_path(x: torch.torch.Tensor, drop_prob: float = 0.0, training: bool = False) -> torch.Tensor:
1718
"""Apply stochastic depth to an input tensor.
1819
1920
Args:
@@ -22,7 +23,7 @@ def drop_path(x: Tensor, drop_prob: float = 0.0, training: bool = False) -> Tens
2223
training: Whether the module is in training mode.
2324
2425
Returns:
25-
Tensor with dropped paths applied during training, or the original
26+
torch.Tensor with dropped paths applied during training, or the original
2627
tensor during evaluation.
2728
2829
Notes:
@@ -57,6 +58,6 @@ def __init__(self, drop_prob: float | None = None) -> None:
5758
super().__init__()
5859
self.drop_prob = drop_prob if drop_prob is not None else 0.0
5960

60-
def forward(self, x: Tensor) -> Tensor:
61+
def forward(self, x: torch.Tensor) -> torch.Tensor:
6162
"""Forward pass applying stochastic depth."""
6263
return drop_path(x, drop_prob=self.drop_prob, training=self.training)

src/anomalib/models/components/dinov2/layers/layer_scale.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Copyright (C) 2025 Intel Corporation
22
# SPDX-License-Identifier: Apache-2.0
3+
# Copyright (C) 2025 Meta Platforms, Inc. and affiliates.
4+
# SPDX-License-Identifier: Apache-2.0
35

46
"""LayerScale module used in DINOv2.
57
@@ -8,10 +10,8 @@
810
Vision Transformers with residual connections.
911
"""
1012

11-
from __future__ import annotations
12-
1313
import torch
14-
from torch import Tensor, nn
14+
from torch import nn
1515

1616

1717
class LayerScale(nn.Module):
@@ -33,7 +33,7 @@ class LayerScale(nn.Module):
3333
def __init__(
3434
self,
3535
dim: int,
36-
init_values: float | Tensor = 1e-5,
36+
init_values: float | torch.Tensor = 1e-5,
3737
inplace: bool = False,
3838
device: torch.device | None = None,
3939
dtype: torch.dtype | None = None,
@@ -48,6 +48,6 @@ def reset_parameters(self) -> None:
4848
"""Reset scale parameters to their initialization values."""
4949
nn.init.constant_(self.gamma, self.init_values)
5050

51-
def forward(self, x: Tensor) -> Tensor:
51+
def forward(self, x: torch.Tensor) -> torch.Tensor:
5252
"""Apply channel-wise scaling to the input tensor."""
5353
return x.mul_(self.gamma) if self.inplace else x * self.gamma

src/anomalib/models/components/dinov2/layers/mlp.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Copyright (C) 2025 Intel Corporation
22
# SPDX-License-Identifier: Apache-2.0
3+
# Copyright (C) 2025 Meta Platforms, Inc. and affiliates.
4+
# SPDX-License-Identifier: Apache-2.0
35

46
"""Feed-forward MLP block used in DINOv2 Vision Transformers.
57
@@ -8,14 +10,10 @@
810
inside each transformer block.
911
"""
1012

11-
from __future__ import annotations
12-
13-
from typing import TYPE_CHECKING
14-
15-
from torch import Tensor, nn
13+
from collections.abc import Callable
1614

17-
if TYPE_CHECKING:
18-
from collections.abc import Callable
15+
import torch
16+
from torch import nn
1917

2018

2119
class Mlp(nn.Module):
@@ -51,7 +49,7 @@ def __init__(
5149
self.fc2 = nn.Linear(hidden_features, out_features, bias=bias)
5250
self.drop = nn.Dropout(drop)
5351

54-
def forward(self, x: Tensor) -> Tensor:
52+
def forward(self, x: torch.Tensor) -> torch.Tensor:
5553
"""Apply the two-layer feed-forward transformation."""
5654
x = self.fc1(x)
5755
x = self.act(x)

src/anomalib/models/components/dinov2/layers/patch_embed.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Copyright (C) 2025 Intel Corporation
22
# SPDX-License-Identifier: Apache-2.0
3+
# Copyright (C) 2025 Meta Platforms, Inc. and affiliates.
4+
# SPDX-License-Identifier: Apache-2.0
35

46
"""Patch embedding module for DINOv2 Vision Transformers.
57
@@ -8,14 +10,10 @@
810
optional output reshaping, and optional normalization.
911
"""
1012

11-
from __future__ import annotations
12-
13-
from typing import TYPE_CHECKING
14-
15-
from torch import Tensor, nn
13+
from collections.abc import Callable
1614

17-
if TYPE_CHECKING:
18-
from collections.abc import Callable
15+
import torch
16+
from torch import nn
1917

2018

2119
def make_2tuple(x: int | tuple[int, int]) -> tuple[int, int]:
@@ -79,7 +77,7 @@ def __init__(
7977
self.proj = nn.Conv2d(in_chans, embed_dim, kernel_size=patch_hw, stride=patch_hw)
8078
self.norm = norm_layer(embed_dim) if norm_layer is not None else nn.Identity()
8179

82-
def forward(self, x: Tensor) -> Tensor:
80+
def forward(self, x: torch.Tensor) -> torch.Tensor:
8381
"""Embed the input image into patch tokens."""
8482
_, _, h, w = x.shape
8583
patch_h, patch_w = self.patch_size

src/anomalib/models/components/dinov2/layers/swiglu_ffn.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Copyright (C) 2025 Intel Corporation
22
# SPDX-License-Identifier: Apache-2.0
3+
# Copyright (C) 2025 Meta Platforms, Inc. and affiliates.
4+
# SPDX-License-Identifier: Apache-2.0
35

46
"""SwiGLU-based feed-forward layers used in DINOv2.
57
@@ -12,15 +14,11 @@
1214
These layers are used as transformer FFN blocks in DINOv2 models.
1315
"""
1416

15-
from __future__ import annotations
16-
17-
from typing import TYPE_CHECKING
17+
from collections.abc import Callable
1818

19+
import torch
1920
import torch.nn.functional as F # noqa: N812
20-
from torch import Tensor, nn
21-
22-
if TYPE_CHECKING:
23-
from collections.abc import Callable
21+
from torch import nn
2422

2523

2624
class SwiGLUFFN(nn.Module):
@@ -55,7 +53,7 @@ def __init__(
5553
self.w12 = nn.Linear(in_features, 2 * hidden_features, bias=bias)
5654
self.w3 = nn.Linear(hidden_features, out_features, bias=bias)
5755

58-
def forward(self, x: Tensor) -> Tensor:
56+
def forward(self, x: torch.Tensor) -> torch.Tensor:
5957
"""Apply the SwiGLU feed-forward transformation."""
6058
x12 = self.w12(x)
6159
x1, x2 = x12.chunk(2, dim=-1)
@@ -138,7 +136,7 @@ def __init__(
138136
self.w2 = nn.Linear(in_features, hidden_aligned, bias=bias, device=device)
139137
self.w3 = nn.Linear(hidden_aligned, out_features, bias=bias, device=device)
140138

141-
def forward(self, x: Tensor) -> Tensor:
139+
def forward(self, x: torch.Tensor) -> torch.Tensor:
142140
"""Apply aligned SwiGLU feed-forward transformation."""
143141
x1 = self.w1(x)
144142
x2 = self.w2(x)

0 commit comments

Comments
 (0)