r""" Processor for performing scaled dot-product attention (enabled by default if you're using PyTorch 2.0), with extra learnable key and value matrices for the text encoder.
| 937 | |
| 938 | |
| 939 | class AttnAddedKVProcessor2_0: |
| 940 | r""" |
| 941 | Processor for performing scaled dot-product attention (enabled by default if you're using PyTorch 2.0), with extra |
| 942 | learnable key and value matrices for the text encoder. |
| 943 | """ |
| 944 | |
| 945 | def __init__(self): |
| 946 | if not hasattr(F, "scaled_dot_product_attention"): |
| 947 | raise ImportError( |
| 948 | "AttnAddedKVProcessor2_0 requires PyTorch 2.0, to use it, please upgrade PyTorch to 2.0." |
| 949 | ) |
| 950 | |
| 951 | def __call__( |
| 952 | self, |
| 953 | attn: Attention, |
| 954 | hidden_states: torch.FloatTensor, |
| 955 | encoder_hidden_states: Optional[torch.FloatTensor] = None, |
| 956 | attention_mask: Optional[torch.FloatTensor] = None, |
| 957 | scale: float = 1.0, |
| 958 | ) -> torch.Tensor: |
| 959 | residual = hidden_states |
| 960 | |
| 961 | args = () if USE_PEFT_BACKEND else (scale,) |
| 962 | |
| 963 | hidden_states = hidden_states.view(hidden_states.shape[0], hidden_states.shape[1], -1).transpose(1, 2) |
| 964 | batch_size, sequence_length, _ = hidden_states.shape |
| 965 | |
| 966 | attention_mask = attn.prepare_attention_mask(attention_mask, sequence_length, batch_size, out_dim=4) |
| 967 | |
| 968 | if encoder_hidden_states is None: |
| 969 | encoder_hidden_states = hidden_states |
| 970 | elif attn.norm_cross: |
| 971 | encoder_hidden_states = attn.norm_encoder_hidden_states(encoder_hidden_states) |
| 972 | |
| 973 | hidden_states = attn.group_norm(hidden_states.transpose(1, 2)).transpose(1, 2) |
| 974 | |
| 975 | query = attn.to_q(hidden_states, *args) |
| 976 | query = attn.head_to_batch_dim(query, out_dim=4) |
| 977 | |
| 978 | encoder_hidden_states_key_proj = attn.add_k_proj(encoder_hidden_states) |
| 979 | encoder_hidden_states_value_proj = attn.add_v_proj(encoder_hidden_states) |
| 980 | encoder_hidden_states_key_proj = attn.head_to_batch_dim(encoder_hidden_states_key_proj, out_dim=4) |
| 981 | encoder_hidden_states_value_proj = attn.head_to_batch_dim(encoder_hidden_states_value_proj, out_dim=4) |
| 982 | |
| 983 | if not attn.only_cross_attention: |
| 984 | key = attn.to_k(hidden_states, *args) |
| 985 | value = attn.to_v(hidden_states, *args) |
| 986 | key = attn.head_to_batch_dim(key, out_dim=4) |
| 987 | value = attn.head_to_batch_dim(value, out_dim=4) |
| 988 | key = torch.cat([encoder_hidden_states_key_proj, key], dim=2) |
| 989 | value = torch.cat([encoder_hidden_states_value_proj, value], dim=2) |
| 990 | else: |
| 991 | key = encoder_hidden_states_key_proj |
| 992 | value = encoder_hidden_states_value_proj |
| 993 | |
| 994 | # the output of sdp = (batch, num_heads, seq_len, head_dim) |
| 995 | # TODO: add support for attn.scale when we move to Torch 2.1 |
| 996 | hidden_states = F.scaled_dot_product_attention( |
nothing calls this directly
no outgoing calls
no test coverage detected