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

Method set_attn_processor

src/diffusers/models/brushnet.py:558–590  ·  view source on GitHub ↗

r""" Sets the attention processor to use to compute attention. Parameters: processor (`dict` of `AttentionProcessor` or only `AttentionProcessor`): The instantiated processor class or a dictionary of processor classes that will be set as the processor

(self, processor: Union[AttentionProcessor, Dict[str, AttentionProcessor]])

Source from the content-addressed store, hash-verified

556
557 # Copied from diffusers.models.unets.unet_2d_condition.UNet2DConditionModel.set_attn_processor
558 def set_attn_processor(self, processor: Union[AttentionProcessor, Dict[str, AttentionProcessor]]):
559 r"""
560 Sets the attention processor to use to compute attention.
561
562 Parameters:
563 processor (`dict` of `AttentionProcessor` or only `AttentionProcessor`):
564 The instantiated processor class or a dictionary of processor classes that will be set as the processor
565 for **all** `Attention` layers.
566
567 If `processor` is a dict, the key needs to define the path to the corresponding cross attention
568 processor. This is strongly recommended when setting trainable attention processors.
569
570 """
571 count = len(self.attn_processors.keys())
572
573 if isinstance(processor, dict) and len(processor) != count:
574 raise ValueError(
575 f"A dict of processors was passed, but the number of processors {len(processor)} does not match the"
576 f" number of attention layers: {count}. Please make sure to pass {count} processor classes."
577 )
578
579 def fn_recursive_attn_processor(name: str, module: torch.nn.Module, processor):
580 if hasattr(module, "set_processor"):
581 if not isinstance(processor, dict):
582 module.set_processor(processor)
583 else:
584 module.set_processor(processor.pop(f"{name}.processor"))
585
586 for sub_name, child in module.named_children():
587 fn_recursive_attn_processor(f"{name}.{sub_name}", child, processor)
588
589 for name, module in self.named_children():
590 fn_recursive_attn_processor(name, module, processor)
591
592 # Copied from diffusers.models.unets.unet_2d_condition.UNet2DConditionModel.set_default_attn_processor
593 def set_default_attn_processor(self):

Calls

no outgoing calls