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

Class LoRAXFormersAttnProcessor

src/models/attn_process_diffuser.py:1848–1924  ·  view source on GitHub ↗

r""" Processor for implementing the LoRA attention mechanism with memory efficient attention using xFormers. Args: hidden_size (`int`, *optional*): The hidden size of the attention layer. cross_attention_dim (`int`, *optional*): The number of c

Source from the content-addressed store, hash-verified

1846
1847
1848class LoRAXFormersAttnProcessor(nn.Module):
1849 r"""
1850 Processor for implementing the LoRA attention mechanism with memory efficient attention using xFormers.
1851
1852 Args:
1853 hidden_size (`int`, *optional*):
1854 The hidden size of the attention layer.
1855 cross_attention_dim (`int`, *optional*):
1856 The number of channels in the `encoder_hidden_states`.
1857 rank (`int`, defaults to 4):
1858 The dimension of the LoRA update matrices.
1859 attention_op (`Callable`, *optional*, defaults to `None`):
1860 The base
1861 [operator](https://facebookresearch.github.io/xformers/components/ops.html#xformers.ops.AttentionOpBase) to
1862 use as the attention operator. It is recommended to set to `None`, and allow xFormers to choose the best
1863 operator.
1864 network_alpha (`int`, *optional*):
1865 Equivalent to `alpha` but it's usage is specific to Kohya (A1111) style LoRAs.
1866 kwargs (`dict`):
1867 Additional keyword arguments to pass to the `LoRALinearLayer` layers.
1868 """
1869
1870 def __init__(
1871 self,
1872 hidden_size: int,
1873 cross_attention_dim: int,
1874 rank: int = 4,
1875 attention_op: Optional[Callable] = None,
1876 network_alpha: Optional[int] = None,
1877 **kwargs,
1878 ):
1879 super().__init__()
1880
1881 self.hidden_size = hidden_size
1882 self.cross_attention_dim = cross_attention_dim
1883 self.rank = rank
1884 self.attention_op = attention_op
1885
1886 q_rank = kwargs.pop("q_rank", None)
1887 q_hidden_size = kwargs.pop("q_hidden_size", None)
1888 q_rank = q_rank if q_rank is not None else rank
1889 q_hidden_size = q_hidden_size if q_hidden_size is not None else hidden_size
1890
1891 v_rank = kwargs.pop("v_rank", None)
1892 v_hidden_size = kwargs.pop("v_hidden_size", None)
1893 v_rank = v_rank if v_rank is not None else rank
1894 v_hidden_size = v_hidden_size if v_hidden_size is not None else hidden_size
1895
1896 out_rank = kwargs.pop("out_rank", None)
1897 out_hidden_size = kwargs.pop("out_hidden_size", None)
1898 out_rank = out_rank if out_rank is not None else rank
1899 out_hidden_size = out_hidden_size if out_hidden_size is not None else hidden_size
1900
1901 self.to_q_lora = LoRALinearLayer(q_hidden_size, q_hidden_size, q_rank, network_alpha)
1902 self.to_k_lora = LoRALinearLayer(cross_attention_dim or hidden_size, hidden_size, rank, network_alpha)
1903 self.to_v_lora = LoRALinearLayer(cross_attention_dim or v_hidden_size, v_hidden_size, v_rank, network_alpha)
1904 self.to_out_lora = LoRALinearLayer(out_hidden_size, out_hidden_size, out_rank, network_alpha)
1905

Calls

no outgoing calls

Tested by

no test coverage detected