(self, x, cond)
| 524 | self.ffn_drop_path = DropPath(drop_prob=drop_path_prob) |
| 525 | |
| 526 | def forward(self, x, cond): |
| 527 | x = self.prenorm_x(x) |
| 528 | # cond = self.prenorm_cond(cond) |
| 529 | |
| 530 | q = self.q(x) |
| 531 | k, v = self.kv(cond).chunk(2, dim=1) |
| 532 | |
| 533 | q, k, v = map(lambda in_qkv: F.normalize(in_qkv, dim=1), (q, k, v)) |
| 534 | |
| 535 | q = q.softmax(dim=-2) |
| 536 | k = k.softmax(dim=-1) |
| 537 | |
| 538 | # convert to freq space |
| 539 | q = torch.fft.rfft2(q, dim=(-2, -1), norm="ortho") # b, c, h, w/2+1 |
| 540 | k = torch.fft.rfft2(k, dim=(-2, -1), norm="ortho") # b, c, h, w/2+1 |
| 541 | v = torch.fft.rfft2(v, dim=(-2, -1), norm="ortho") # b, c, h, w/2+1 |
| 542 | |
| 543 | b, c, xf, yf = q.shape |
| 544 | |
| 545 | q, k, v = map( |
| 546 | lambda in_x: rearrange( |
| 547 | in_x, "b (h c) xf yf -> b h c (xf yf)", h=self.nheads |
| 548 | ), |
| 549 | (q, k, v), |
| 550 | ) |
| 551 | q = q * self.scale |
| 552 | # c x c attn map |
| 553 | context = torch.einsum("b h d n, b h e n -> b h d e", k, v) |
| 554 | # h w fused feature map |
| 555 | out = torch.einsum("b h d e, b h d n -> b h e n", context, q) |
| 556 | out = rearrange( |
| 557 | out, "n h c (xf yf) -> n (h c) xf yf", xf=xf, yf=yf, h=self.nheads |
| 558 | ) |
| 559 | |
| 560 | # convert to rgb space |
| 561 | out = torch.fft.irfft2(out, dim=(-2, -1), norm="ortho") |
| 562 | |
| 563 | attn_out = self.attn_out(out) + self.attn_res(x) |
| 564 | |
| 565 | # ffn |
| 566 | ffn_out = self.ffn_drop_path(self.ffn(attn_out)) + attn_out |
| 567 | return ffn_out |
| 568 | |
| 569 | |
| 570 | class WrappedCondInj(nn.Module): |
nothing calls this directly
no outgoing calls
no test coverage detected