CoolFace
Modelpublic

OpenGVLab/InternViT-300M-448px

sourceHugging Facemitupdated 2y agoView on Hugging Face
62likes3.4kdownloads
flash_attention.py77 linesDownload Raw Back to root
1# https://github.com/Dao-AILab/flash-attention/blob/v0.2.8/flash_attn/flash_attention.py2import torch3import torch.nn as nn4from einops import rearrange5 6try:  # v17    from flash_attn.flash_attn_interface import \8        flash_attn_unpadded_qkvpacked_func9except:  # v210    from flash_attn.flash_attn_interface import flash_attn_varlen_qkvpacked_func as flash_attn_unpadded_qkvpacked_func11 12from flash_attn.bert_padding import pad_input, unpad_input13 14 15class FlashAttention(nn.Module):16    """Implement the scaled dot product attention with softmax.17    Arguments18    ---------19        softmax_scale: The temperature to use for the softmax attention.20                      (default: 1/sqrt(d_keys) where d_keys is computed at21                      runtime)22        attention_dropout: The dropout rate to apply to the attention23                           (default: 0.0)24    """25 26    def __init__(self, softmax_scale=None, attention_dropout=0.0, device=None, dtype=None):27        super().__init__()28        self.softmax_scale = softmax_scale29        self.dropout_p = attention_dropout30 31    def forward(self, qkv, key_padding_mask=None, causal=False, cu_seqlens=None,32                max_s=None, need_weights=False):33        """Implements the multihead softmax attention.34        Arguments35        ---------36            qkv: The tensor containing the query, key, and value. (B, S, 3, H, D) if key_padding_mask is None37                if unpadded: (nnz, 3, h, d)38            key_padding_mask: a bool tensor of shape (B, S)39        """40        assert not need_weights41        assert qkv.dtype in [torch.float16, torch.bfloat16]42        assert qkv.is_cuda43 44        if cu_seqlens is None:45            batch_size = qkv.shape[0]46            seqlen = qkv.shape[1]47            if key_padding_mask is None:48                qkv = rearrange(qkv, 'b s ... -> (b s) ...')49                max_s = seqlen50                cu_seqlens = torch.arange(0, (batch_size + 1) * seqlen, step=seqlen, dtype=torch.int32,51                                          device=qkv.device)52                output = flash_attn_unpadded_qkvpacked_func(53                    qkv, cu_seqlens, max_s, self.dropout_p if self.training else 0.0,54                    softmax_scale=self.softmax_scale, causal=causal55                )56                output = rearrange(output, '(b s) ... -> b s ...', b=batch_size)57            else:58                nheads = qkv.shape[-2]59                x = rearrange(qkv, 'b s three h d -> b s (three h d)')60                x_unpad, indices, cu_seqlens, max_s = unpad_input(x, key_padding_mask)61                x_unpad = rearrange(x_unpad, 'nnz (three h d) -> nnz three h d', three=3, h=nheads)62                output_unpad = flash_attn_unpadded_qkvpacked_func(63                    x_unpad, cu_seqlens, max_s, self.dropout_p if self.training else 0.0,64                    softmax_scale=self.softmax_scale, causal=causal65                )66                output = rearrange(pad_input(rearrange(output_unpad, 'nnz h d -> nnz (h d)'),67                                             indices, batch_size, seqlen),68                                   'b s (h d) -> b s h d', h=nheads)69        else:70            assert max_s is not None71            output = flash_attn_unpadded_qkvpacked_func(72                qkv, cu_seqlens, max_s, self.dropout_p if self.training else 0.0,73                softmax_scale=self.softmax_scale, causal=causal74            )75 76        return output, None77