MCPcopy Create free account
hub / github.com/aim-uofa/Framer / XFormersAttnAddedKVProcessor

Class XFormersAttnAddedKVProcessor

models_diffusers/attention_processor.py:1010–1078  ·  view source on GitHub ↗

r""" Processor for implementing memory efficient attention using xFormers. Args: attention_op (`Callable`, *optional*, defaults to `None`): The base [operator](https://facebookresearch.github.io/xformers/components/ops.html#xformers.ops.AttentionOpBase) to

Source from the content-addressed store, hash-verified

1008
1009
1010class XFormersAttnAddedKVProcessor:
1011 r"""
1012 Processor for implementing memory efficient attention using xFormers.
1013
1014 Args:
1015 attention_op (`Callable`, *optional*, defaults to `None`):
1016 The base
1017 [operator](https://facebookresearch.github.io/xformers/components/ops.html#xformers.ops.AttentionOpBase) to
1018 use as the attention operator. It is recommended to set to `None`, and allow xFormers to choose the best
1019 operator.
1020 """
1021
1022 def __init__(self, attention_op: Optional[Callable] = None):
1023 self.attention_op = attention_op
1024
1025 def __call__(
1026 self,
1027 attn: Attention,
1028 hidden_states: torch.FloatTensor,
1029 encoder_hidden_states: Optional[torch.FloatTensor] = None,
1030 attention_mask: Optional[torch.FloatTensor] = None,
1031 ) -> torch.Tensor:
1032 residual = hidden_states
1033 hidden_states = hidden_states.view(hidden_states.shape[0], hidden_states.shape[1], -1).transpose(1, 2)
1034 batch_size, sequence_length, _ = hidden_states.shape
1035
1036 attention_mask = attn.prepare_attention_mask(attention_mask, sequence_length, batch_size)
1037
1038 if encoder_hidden_states is None:
1039 encoder_hidden_states = hidden_states
1040 elif attn.norm_cross:
1041 encoder_hidden_states = attn.norm_encoder_hidden_states(encoder_hidden_states)
1042
1043 hidden_states = attn.group_norm(hidden_states.transpose(1, 2)).transpose(1, 2)
1044
1045 query = attn.to_q(hidden_states)
1046 query = attn.head_to_batch_dim(query)
1047
1048 encoder_hidden_states_key_proj = attn.add_k_proj(encoder_hidden_states)
1049 encoder_hidden_states_value_proj = attn.add_v_proj(encoder_hidden_states)
1050 encoder_hidden_states_key_proj = attn.head_to_batch_dim(encoder_hidden_states_key_proj)
1051 encoder_hidden_states_value_proj = attn.head_to_batch_dim(encoder_hidden_states_value_proj)
1052
1053 if not attn.only_cross_attention:
1054 key = attn.to_k(hidden_states)
1055 value = attn.to_v(hidden_states)
1056 key = attn.head_to_batch_dim(key)
1057 value = attn.head_to_batch_dim(value)
1058 key = torch.cat([encoder_hidden_states_key_proj, key], dim=1)
1059 value = torch.cat([encoder_hidden_states_value_proj, value], dim=1)
1060 else:
1061 key = encoder_hidden_states_key_proj
1062 value = encoder_hidden_states_value_proj
1063
1064 hidden_states = xformers.ops.memory_efficient_attention(
1065 query, key, value, attn_bias=attention_mask, op=self.attention_op, scale=attn.scale
1066 )
1067 hidden_states = hidden_states.to(query.dtype)

Calls

no outgoing calls

Tested by

no test coverage detected