r""" Processor for implementing the LoRA attention mechanism with extra learnable key and value matrices for the text encoder. Args: hidden_size (`int`, *optional*): The hidden size of the attention layer. cross_attention_dim (`int`, *optional*, defaul
| 1925 | |
| 1926 | |
| 1927 | class LoRAAttnAddedKVProcessor(nn.Module): |
| 1928 | r""" |
| 1929 | Processor for implementing the LoRA attention mechanism with extra learnable key and value matrices for the text |
| 1930 | encoder. |
| 1931 | |
| 1932 | Args: |
| 1933 | hidden_size (`int`, *optional*): |
| 1934 | The hidden size of the attention layer. |
| 1935 | cross_attention_dim (`int`, *optional*, defaults to `None`): |
| 1936 | The number of channels in the `encoder_hidden_states`. |
| 1937 | rank (`int`, defaults to 4): |
| 1938 | The dimension of the LoRA update matrices. |
| 1939 | network_alpha (`int`, *optional*): |
| 1940 | Equivalent to `alpha` but it's usage is specific to Kohya (A1111) style LoRAs. |
| 1941 | kwargs (`dict`): |
| 1942 | Additional keyword arguments to pass to the `LoRALinearLayer` layers. |
| 1943 | """ |
| 1944 | |
| 1945 | def __init__( |
| 1946 | self, |
| 1947 | hidden_size: int, |
| 1948 | cross_attention_dim: Optional[int] = None, |
| 1949 | rank: int = 4, |
| 1950 | network_alpha: Optional[int] = None, |
| 1951 | ): |
| 1952 | super().__init__() |
| 1953 | |
| 1954 | self.hidden_size = hidden_size |
| 1955 | self.cross_attention_dim = cross_attention_dim |
| 1956 | self.rank = rank |
| 1957 | |
| 1958 | self.to_q_lora = LoRALinearLayer(hidden_size, hidden_size, rank, network_alpha) |
| 1959 | self.add_k_proj_lora = LoRALinearLayer(cross_attention_dim or hidden_size, hidden_size, rank, network_alpha) |
| 1960 | self.add_v_proj_lora = LoRALinearLayer(cross_attention_dim or hidden_size, hidden_size, rank, network_alpha) |
| 1961 | self.to_k_lora = LoRALinearLayer(hidden_size, hidden_size, rank, network_alpha) |
| 1962 | self.to_v_lora = LoRALinearLayer(hidden_size, hidden_size, rank, network_alpha) |
| 1963 | self.to_out_lora = LoRALinearLayer(hidden_size, hidden_size, rank, network_alpha) |
| 1964 | |
| 1965 | def __call__(self, attn: Attention, hidden_states: torch.FloatTensor, *args, **kwargs) -> torch.FloatTensor: |
| 1966 | self_cls_name = self.__class__.__name__ |
| 1967 | deprecate( |
| 1968 | self_cls_name, |
| 1969 | "0.26.0", |
| 1970 | ( |
| 1971 | f"Make sure use {self_cls_name[4:]} instead by setting" |
| 1972 | "LoRA layers to `self.{to_q,to_k,to_v,add_k_proj,add_v_proj,to_out[0]}.lora_layer` respectively. This will be done automatically when using" |
| 1973 | " `LoraLoaderMixin.load_lora_weights`" |
| 1974 | ), |
| 1975 | ) |
| 1976 | attn.to_q.lora_layer = self.to_q_lora.to(hidden_states.device) |
| 1977 | attn.to_k.lora_layer = self.to_k_lora.to(hidden_states.device) |
| 1978 | attn.to_v.lora_layer = self.to_v_lora.to(hidden_states.device) |
| 1979 | attn.to_out[0].lora_layer = self.to_out_lora.to(hidden_states.device) |
| 1980 | |
| 1981 | attn._modules.pop("processor") |
| 1982 | attn.processor = AttnAddedKVProcessor() |
| 1983 | return attn.processor(attn, hidden_states, *args, **kwargs) |
| 1984 |
nothing calls this directly
no outgoing calls
no test coverage detected