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

Method get_processor

src/models/attention_processor.py:406–494  ·  view source on GitHub ↗

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 processor i

(self, return_deprecated_lora: bool = False)

Source from the content-addressed store, hash-verified

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

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected