Basic transformer unit combining MDTA and GDFN with skip connections. Unlike standard transformers that use LayerNorm, this block uses Instance Norm for better adaptation to image restoration tasks. Args: spatial_dims: Number of spatial dimensions (2D or 3D) dim: Number
| 22 | |
| 23 | |
| 24 | class MDTATransformerBlock(nn.Module): |
| 25 | """Basic transformer unit combining MDTA and GDFN with skip connections. |
| 26 | Unlike standard transformers that use LayerNorm, this block uses Instance Norm |
| 27 | for better adaptation to image restoration tasks. |
| 28 | |
| 29 | Args: |
| 30 | spatial_dims: Number of spatial dimensions (2D or 3D) |
| 31 | dim: Number of input channels |
| 32 | num_heads: Number of attention heads |
| 33 | ffn_expansion_factor: Expansion factor for feed-forward network |
| 34 | bias: Whether to use bias in attention layers |
| 35 | layer_norm_use_bias: Whether to use bias in layer normalization. Defaults to False. |
| 36 | flash_attention: Whether to use flash attention optimization. Defaults to False. |
| 37 | """ |
| 38 | |
| 39 | def __init__( |
| 40 | self, |
| 41 | spatial_dims: int, |
| 42 | dim: int, |
| 43 | num_heads: int, |
| 44 | ffn_expansion_factor: float, |
| 45 | bias: bool, |
| 46 | layer_norm_use_bias: bool = False, |
| 47 | flash_attention: bool = False, |
| 48 | ): |
| 49 | super().__init__() |
| 50 | self.norm1 = Norm[Norm.INSTANCE, spatial_dims](dim, affine=layer_norm_use_bias) |
| 51 | self.attn = CABlock(spatial_dims, dim, num_heads, bias, flash_attention) |
| 52 | self.norm2 = Norm[Norm.INSTANCE, spatial_dims](dim, affine=layer_norm_use_bias) |
| 53 | self.ffn = FeedForward(spatial_dims, dim, ffn_expansion_factor, bias) |
| 54 | |
| 55 | def forward(self, x: torch.Tensor) -> torch.Tensor: |
| 56 | x = x + self.attn(self.norm1(x)) |
| 57 | x = x + self.ffn(self.norm2(x)) |
| 58 | return x |
| 59 | |
| 60 | |
| 61 | class OverlapPatchEmbed(Convolution): |
no outgoing calls
searching dependent graphs…