MCPcopy Index your code
hub / github.com/VisionXLab/OF-Diff / QKVAttention

Class QKVAttention

ldm/modules/diffusionmodules/openaimodel.py:380–411  ·  view source on GitHub ↗

A module which performs QKV attention and splits in a different order.

Source from the content-addressed store, hash-verified

378
379
380class 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
414class UNetModel(nn.Module):

Callers 2

__init__Method · 0.85
__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected