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

Class CustomDiffusionAttnProcessor

models_diffusers/attention_processor.py:769–870  ·  view source on GitHub ↗

r""" Processor for implementing attention for the Custom Diffusion method. Args: train_kv (`bool`, defaults to `True`): Whether to newly train the key and value matrices corresponding to the text features. train_q_out (`bool`, defaults to `True`): Whe

Source from the content-addressed store, hash-verified

767
768
769class CustomDiffusionAttnProcessor(nn.Module):
770 r"""
771 Processor for implementing attention for the Custom Diffusion method.
772
773 Args:
774 train_kv (`bool`, defaults to `True`):
775 Whether to newly train the key and value matrices corresponding to the text features.
776 train_q_out (`bool`, defaults to `True`):
777 Whether to newly train query matrices corresponding to the latent image features.
778 hidden_size (`int`, *optional*, defaults to `None`):
779 The hidden size of the attention layer.
780 cross_attention_dim (`int`, *optional*, defaults to `None`):
781 The number of channels in the `encoder_hidden_states`.
782 out_bias (`bool`, defaults to `True`):
783 Whether to include the bias parameter in `train_q_out`.
784 dropout (`float`, *optional*, defaults to 0.0):
785 The dropout probability to use.
786 """
787
788 def __init__(
789 self,
790 train_kv: bool = True,
791 train_q_out: bool = True,
792 hidden_size: Optional[int] = None,
793 cross_attention_dim: Optional[int] = None,
794 out_bias: bool = True,
795 dropout: float = 0.0,
796 ):
797 super().__init__()
798 self.train_kv = train_kv
799 self.train_q_out = train_q_out
800
801 self.hidden_size = hidden_size
802 self.cross_attention_dim = cross_attention_dim
803
804 # `_custom_diffusion` id for easy serialization and loading.
805 if self.train_kv:
806 self.to_k_custom_diffusion = nn.Linear(cross_attention_dim or hidden_size, hidden_size, bias=False)
807 self.to_v_custom_diffusion = nn.Linear(cross_attention_dim or hidden_size, hidden_size, bias=False)
808 if self.train_q_out:
809 self.to_q_custom_diffusion = nn.Linear(hidden_size, hidden_size, bias=False)
810 self.to_out_custom_diffusion = nn.ModuleList([])
811 self.to_out_custom_diffusion.append(nn.Linear(hidden_size, hidden_size, bias=out_bias))
812 self.to_out_custom_diffusion.append(nn.Dropout(dropout))
813
814 def __call__(
815 self,
816 attn: Attention,
817 hidden_states: torch.FloatTensor,
818 encoder_hidden_states: Optional[torch.FloatTensor] = None,
819 attention_mask: Optional[torch.FloatTensor] = None,
820 ) -> torch.Tensor:
821 batch_size, sequence_length, _ = hidden_states.shape
822 attention_mask = attn.prepare_attention_mask(attention_mask, sequence_length, batch_size)
823 if self.train_q_out:
824 query = self.to_q_custom_diffusion(hidden_states).to(attn.to_q.weight.dtype)
825 else:
826 query = attn.to_q(hidden_states.to(attn.to_q.weight.dtype))

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected