(
self,
dim: int,
num_heads: int,
mlp_ratio: float = 4.0,
qkv_bias: bool = False,
proj_bias: bool = True,
ffn_bias: bool = True,
drop: float = 0.0,
attn_drop: float = 0.0,
init_values=None,
drop_path: float = 0.0,
act_layer: Callable[..., nn.Module] = nn.GELU,
norm_layer: Callable[..., nn.Module] = nn.LayerNorm,
attn_class: Callable[..., nn.Module] = Attention,
ffn_layer: Callable[..., nn.Module] = Mlp,
)
| 48 | |
| 49 | class Block(nn.Module): |
| 50 | def __init__( |
| 51 | self, |
| 52 | dim: int, |
| 53 | num_heads: int, |
| 54 | mlp_ratio: float = 4.0, |
| 55 | qkv_bias: bool = False, |
| 56 | proj_bias: bool = True, |
| 57 | ffn_bias: bool = True, |
| 58 | drop: float = 0.0, |
| 59 | attn_drop: float = 0.0, |
| 60 | init_values=None, |
| 61 | drop_path: float = 0.0, |
| 62 | act_layer: Callable[..., nn.Module] = nn.GELU, |
| 63 | norm_layer: Callable[..., nn.Module] = nn.LayerNorm, |
| 64 | attn_class: Callable[..., nn.Module] = Attention, |
| 65 | ffn_layer: Callable[..., nn.Module] = Mlp, |
| 66 | ) -> None: |
| 67 | super().__init__() |
| 68 | # print(f"biases: qkv: {qkv_bias}, proj: {proj_bias}, ffn: {ffn_bias}") |
| 69 | self.norm1 = norm_layer(dim) |
| 70 | self.attn = attn_class( |
| 71 | dim, |
| 72 | num_heads=num_heads, |
| 73 | qkv_bias=qkv_bias, |
| 74 | proj_bias=proj_bias, |
| 75 | attn_drop=attn_drop, |
| 76 | proj_drop=drop, |
| 77 | ) |
| 78 | self.ls1 = LayerScale(dim, init_values=init_values) if init_values else nn.Identity() |
| 79 | self.drop_path1 = DropPath(drop_path) if drop_path > 0.0 else nn.Identity() |
| 80 | |
| 81 | self.norm2 = norm_layer(dim) |
| 82 | mlp_hidden_dim = int(dim * mlp_ratio) |
| 83 | self.mlp = ffn_layer( |
| 84 | in_features=dim, |
| 85 | hidden_features=mlp_hidden_dim, |
| 86 | act_layer=act_layer, |
| 87 | drop=drop, |
| 88 | bias=ffn_bias, |
| 89 | ) |
| 90 | self.ls2 = LayerScale(dim, init_values=init_values) if init_values else nn.Identity() |
| 91 | self.drop_path2 = DropPath(drop_path) if drop_path > 0.0 else nn.Identity() |
| 92 | |
| 93 | self.sample_drop_ratio = drop_path |
| 94 | |
| 95 | def forward(self, x: Tensor) -> Tensor: |
| 96 | def attn_residual_func(x: Tensor) -> Tensor: |
no test coverage detected