MCPcopy Create free account
hub / github.com/MatrixTeam-AI/RAIN / CustomDiffusionAttnProcessor

Class CustomDiffusionAttnProcessor

src/models/attention_processor.py:763–864  ·  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

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