(self, x: torch.Tensor, attn_bias=None)
| 23 | assert torch.__version__ >= '2.0', "SDPA requires PyTorch 2.0 or later" |
| 24 | class _AttentionWrapper(module.__class__): |
| 25 | def forward(self, x: torch.Tensor, attn_bias=None) -> torch.Tensor: |
| 26 | B, N, C = x.shape |
| 27 | qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4) # (3, B, H, N, C // H) |
| 28 | |
| 29 | q, k, v = torch.unbind(qkv, 0) # (B, H, N, C // H) |
| 30 | |
| 31 | x = F.scaled_dot_product_attention(q, k, v, attn_bias) |
| 32 | x = x.permute(0, 2, 1, 3).reshape(B, N, C) |
| 33 | |
| 34 | x = self.proj(x) |
| 35 | x = self.proj_drop(x) |
| 36 | return x |
| 37 | module.__class__ = _AttentionWrapper |
| 38 | return module |
| 39 |
nothing calls this directly
no outgoing calls
no test coverage detected