(self, x)
| 98 | self.norm = LayerNorm(dim, eps=1e-6, data_format="channels_last") |
| 99 | |
| 100 | def forward(self, x): |
| 101 | B, H, W, C = x.size() |
| 102 | x = self.norm(x) |
| 103 | |
| 104 | q = self.q(x) |
| 105 | x = self.l(x).permute(0, 3, 1, 2) |
| 106 | x = self.act(x) |
| 107 | |
| 108 | a = self.conv(x) |
| 109 | a = a.permute(0, 2, 3, 1) |
| 110 | a = self.a(a) |
| 111 | |
| 112 | if self.window != 0: |
| 113 | b = self.conv_att(x) |
| 114 | b = b.permute(0, 2, 3, 1) |
| 115 | kv = self.kv(b) |
| 116 | kv = kv.reshape(B, H * W, 2, self.num_head, C // self.num_head).permute(2, 0, 3, 1, 4) |
| 117 | k, v = kv.unbind(0) |
| 118 | # t = self.t(a).reshape(B, H*W, self.num_head, -1).permute(0, 2, 1, 3).softmax(dim=-1) |
| 119 | |
| 120 | m = self.m.reshape(1, -1, self.num_head, C // self.num_head).permute(0, 2, 1, 3).expand(B, -1, -1, -1) |
| 121 | attn = (m * (C // self.num_head) ** -0.5) @ k.transpose(-2, -1) |
| 122 | attn = attn.softmax(dim=-1) |
| 123 | attn = ( |
| 124 | (attn @ v) |
| 125 | .reshape(B, self.num_head, self.window, self.window, C // self.num_head) |
| 126 | .permute(0, 1, 4, 2, 3) |
| 127 | .reshape(B, C, self.window, self.window) |
| 128 | ) |
| 129 | attn = F.interpolate(attn, (H, W), mode="bilinear", align_corners=False).permute(0, 2, 3, 1) |
| 130 | # attn = (t @ attn).permute(0, 2, 1, 3).reshape(B, H, W, C) |
| 131 | |
| 132 | x = q * a |
| 133 | |
| 134 | if self.window != 0: |
| 135 | x = torch.cat([x, attn], dim=3) |
| 136 | x = self.proj(x) |
| 137 | |
| 138 | return x |
| 139 | |
| 140 | |
| 141 | class MemoryModulev2(nn.Module): |
nothing calls this directly
no outgoing calls
no test coverage detected