MCPcopy Create free account
hub / github.com/Francis-Rings/MotionFollower / LoRAAttnProcessor2_0

Class LoRAAttnProcessor2_0

src/models/attn_process_diffuser.py:1773–1845  ·  view source on GitHub ↗

r""" Processor for implementing the LoRA attention mechanism using PyTorch 2.0's memory-efficient scaled dot-product attention. Args: hidden_size (`int`): The hidden size of the attention layer. cross_attention_dim (`int`, *optional*): The

Source from the content-addressed store, hash-verified

1771
1772
1773class LoRAAttnProcessor2_0(nn.Module):
1774 r"""
1775 Processor for implementing the LoRA attention mechanism using PyTorch 2.0's memory-efficient scaled dot-product
1776 attention.
1777
1778 Args:
1779 hidden_size (`int`):
1780 The hidden size of the attention layer.
1781 cross_attention_dim (`int`, *optional*):
1782 The number of channels in the `encoder_hidden_states`.
1783 rank (`int`, defaults to 4):
1784 The dimension of the LoRA update matrices.
1785 network_alpha (`int`, *optional*):
1786 Equivalent to `alpha` but it's usage is specific to Kohya (A1111) style LoRAs.
1787 kwargs (`dict`):
1788 Additional keyword arguments to pass to the `LoRALinearLayer` layers.
1789 """
1790
1791 def __init__(
1792 self,
1793 hidden_size: int,
1794 cross_attention_dim: Optional[int] = None,
1795 rank: int = 4,
1796 network_alpha: Optional[int] = None,
1797 **kwargs,
1798 ):
1799 super().__init__()
1800 if not hasattr(F, "scaled_dot_product_attention"):
1801 raise ImportError("AttnProcessor2_0 requires PyTorch 2.0, to use it, please upgrade PyTorch to 2.0.")
1802
1803 self.hidden_size = hidden_size
1804 self.cross_attention_dim = cross_attention_dim
1805 self.rank = rank
1806
1807 q_rank = kwargs.pop("q_rank", None)
1808 q_hidden_size = kwargs.pop("q_hidden_size", None)
1809 q_rank = q_rank if q_rank is not None else rank
1810 q_hidden_size = q_hidden_size if q_hidden_size is not None else hidden_size
1811
1812 v_rank = kwargs.pop("v_rank", None)
1813 v_hidden_size = kwargs.pop("v_hidden_size", None)
1814 v_rank = v_rank if v_rank is not None else rank
1815 v_hidden_size = v_hidden_size if v_hidden_size is not None else hidden_size
1816
1817 out_rank = kwargs.pop("out_rank", None)
1818 out_hidden_size = kwargs.pop("out_hidden_size", None)
1819 out_rank = out_rank if out_rank is not None else rank
1820 out_hidden_size = out_hidden_size if out_hidden_size is not None else hidden_size
1821
1822 self.to_q_lora = LoRALinearLayer(q_hidden_size, q_hidden_size, q_rank, network_alpha)
1823 self.to_k_lora = LoRALinearLayer(cross_attention_dim or hidden_size, hidden_size, rank, network_alpha)
1824 self.to_v_lora = LoRALinearLayer(cross_attention_dim or v_hidden_size, v_hidden_size, v_rank, network_alpha)
1825 self.to_out_lora = LoRALinearLayer(out_hidden_size, out_hidden_size, out_rank, network_alpha)
1826
1827 def __call__(self, attn: Attention, hidden_states: torch.FloatTensor, *args, **kwargs) -> torch.FloatTensor:
1828 self_cls_name = self.__class__.__name__
1829 deprecate(
1830 self_cls_name,

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected