MCPcopy Create free account
hub / github.com/TencentARC/BrushNet / get_processor

Method get_processor

src/diffusers/models/attention_processor.py:400–488  ·  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

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

Calls 3

load_state_dictMethod · 0.80
popMethod · 0.45
state_dictMethod · 0.45

Tested by

no test coverage detected