r""" Get the attention processor in use. Args: return_deprecated_lora (`bool`, *optional*, defaults to `False`): Set to `True` to return the deprecated LoRA attention processor. Returns: "AttentionProcessor": The attention pro
(self, return_deprecated_lora: bool = False)
| 393 | self.processor = processor |
| 394 | |
| 395 | def get_processor(self, return_deprecated_lora: bool = False) -> "AttentionProcessor": |
| 396 | r""" |
| 397 | Get the attention processor in use. |
| 398 | |
| 399 | Args: |
| 400 | return_deprecated_lora (`bool`, *optional*, defaults to `False`): |
| 401 | Set to `True` to return the deprecated LoRA attention processor. |
| 402 | |
| 403 | Returns: |
| 404 | "AttentionProcessor": The attention processor in use. |
| 405 | """ |
| 406 | if not return_deprecated_lora: |
| 407 | return self.processor |
| 408 | |
| 409 | # TODO(Sayak, Patrick). The rest of the function is needed to ensure backwards compatible |
| 410 | # serialization format for LoRA Attention Processors. It should be deleted once the integration |
| 411 | # with PEFT is completed. |
| 412 | is_lora_activated = { |
| 413 | name: module.lora_layer is not None |
| 414 | for name, module in self.named_modules() |
| 415 | if hasattr(module, "lora_layer") |
| 416 | } |
| 417 | |
| 418 | # 1. if no layer has a LoRA activated we can return the processor as usual |
| 419 | if not any(is_lora_activated.values()): |
| 420 | return self.processor |
| 421 | |
| 422 | # If doesn't apply LoRA do `add_k_proj` or `add_v_proj` |
| 423 | is_lora_activated.pop("add_k_proj", None) |
| 424 | is_lora_activated.pop("add_v_proj", None) |
| 425 | # 2. else it is not posssible that only some layers have LoRA activated |
| 426 | if not all(is_lora_activated.values()): |
| 427 | raise ValueError( |
| 428 | f"Make sure that either all layers or no layers have LoRA activated, but have {is_lora_activated}" |
| 429 | ) |
| 430 | |
| 431 | # 3. And we need to merge the current LoRA layers into the corresponding LoRA attention processor |
| 432 | non_lora_processor_cls_name = self.processor.__class__.__name__ |
| 433 | lora_processor_cls = getattr(import_module(__name__), "LoRA" + non_lora_processor_cls_name) |
| 434 | |
| 435 | hidden_size = self.inner_dim |
| 436 | |
| 437 | # now create a LoRA attention processor from the LoRA layers |
| 438 | if lora_processor_cls in [LoRAAttnProcessor, LoRAAttnProcessor2_0, LoRAXFormersAttnProcessor]: |
| 439 | kwargs = { |
| 440 | "cross_attention_dim": self.cross_attention_dim, |
| 441 | "rank": self.to_q.lora_layer.rank, |
| 442 | "network_alpha": self.to_q.lora_layer.network_alpha, |
| 443 | "q_rank": self.to_q.lora_layer.rank, |
| 444 | "q_hidden_size": self.to_q.lora_layer.out_features, |
| 445 | "k_rank": self.to_k.lora_layer.rank, |
| 446 | "k_hidden_size": self.to_k.lora_layer.out_features, |
| 447 | "v_rank": self.to_v.lora_layer.rank, |
| 448 | "v_hidden_size": self.to_v.lora_layer.out_features, |
| 449 | "out_rank": self.to_out[0].lora_layer.rank, |
| 450 | "out_hidden_size": self.to_out[0].lora_layer.out_features, |
| 451 | } |
| 452 |
nothing calls this directly
no outgoing calls
no test coverage detected