x: (b h w c) rel_pos: mask: (n l l)
(self, x: torch.Tensor, rel_pos, chunkwise_recurrent=False, incremental_state=None)
| 233 | nn.init.constant_(self.out_proj.bias, 0.0) |
| 234 | |
| 235 | def forward(self, x: torch.Tensor, rel_pos, chunkwise_recurrent=False, incremental_state=None): |
| 236 | ''' |
| 237 | x: (b h w c) |
| 238 | rel_pos: mask: (n l l) |
| 239 | ''' |
| 240 | bsz, h, w, _ = x.size() |
| 241 | mask = rel_pos |
| 242 | |
| 243 | assert h * w == mask.size(1) |
| 244 | |
| 245 | q = self.q_proj(x) |
| 246 | k = self.k_proj(x) |
| 247 | v = self.v_proj(x) |
| 248 | lepe = self.lepe(v) |
| 249 | |
| 250 | k *= self.scaling |
| 251 | qr = q.view(bsz, h, w, self.num_heads, -1).permute(0, 3, 1, 2, 4) # (b n h w d1) |
| 252 | kr = k.view(bsz, h, w, self.num_heads, -1).permute(0, 3, 1, 2, 4) # (b n h w d1) |
| 253 | |
| 254 | qr = qr.flatten(2, 3) # (b n l d1) |
| 255 | kr = kr.flatten(2, 3) # (b n l d1) |
| 256 | vr = v.reshape(bsz, h, w, self.num_heads, -1).permute(0, 3, 1, 2, 4) # (b n h w d2) |
| 257 | vr = vr.flatten(2, 3) # (b n l d2) |
| 258 | qk_mat = qr @ kr.transpose(-1, -2) # (b n l l) |
| 259 | qk_mat = qk_mat + mask # (b n l l) |
| 260 | qk_mat = torch.softmax(qk_mat, -1) # (b n l l) |
| 261 | output = torch.matmul(qk_mat, vr) # (b n l d2) |
| 262 | output = output.transpose(1, 2).reshape(bsz, h, w, -1) # (b h w n*d2) |
| 263 | output = output + lepe |
| 264 | output = self.out_proj(output) |
| 265 | return output |
| 266 | |
| 267 | |
| 268 | class FeedForwardNetwork(nn.Module): |
nothing calls this directly
no outgoing calls
no test coverage detected