A module which performs QKV attention and splits in a different order.
| 378 | |
| 379 | |
| 380 | class QKVAttention(nn.Module): |
| 381 | """ |
| 382 | A module which performs QKV attention and splits in a different order. |
| 383 | """ |
| 384 | |
| 385 | def __init__(self, n_heads): |
| 386 | super().__init__() |
| 387 | self.n_heads = n_heads |
| 388 | |
| 389 | def forward(self, qkv): |
| 390 | """ |
| 391 | Apply QKV attention. |
| 392 | :param qkv: an [N x (3 * H * C) x T] tensor of Qs, Ks, and Vs. |
| 393 | :return: an [N x (H * C) x T] tensor after attention. |
| 394 | """ |
| 395 | bs, width, length = qkv.shape |
| 396 | assert width % (3 * self.n_heads) == 0 |
| 397 | ch = width // (3 * self.n_heads) |
| 398 | q, k, v = qkv.chunk(3, dim=1) |
| 399 | scale = 1 / math.sqrt(math.sqrt(ch)) |
| 400 | weight = th.einsum( |
| 401 | "bct,bcs->bts", |
| 402 | (q * scale).view(bs * self.n_heads, ch, length), |
| 403 | (k * scale).view(bs * self.n_heads, ch, length), |
| 404 | ) # More stable with f16 than dividing afterwards |
| 405 | weight = th.softmax(weight.float(), dim=-1).type(weight.dtype) |
| 406 | a = th.einsum("bts,bcs->bct", weight, v.reshape(bs * self.n_heads, ch, length)) |
| 407 | return a.reshape(bs, -1, length) |
| 408 | |
| 409 | @staticmethod |
| 410 | def count_flops(model, _x, y): |
| 411 | return count_flops_attn(model, _x, y) |
| 412 | |
| 413 | |
| 414 | class UNetModel(nn.Module): |