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]])
| 541 | |
| 542 | # Copied from diffusers.models.unets.unet_2d_condition.UNet2DConditionModel.set_attn_processor |
| 543 | def set_attn_processor(self, processor: Union[AttentionProcessor, Dict[str, AttentionProcessor]]): |
| 544 | r""" |
| 545 | Sets the attention processor to use to compute attention. |
| 546 | |
| 547 | Parameters: |
| 548 | processor (`dict` of `AttentionProcessor` or only `AttentionProcessor`): |
| 549 | The instantiated processor class or a dictionary of processor classes that will be set as the processor |
| 550 | for **all** `Attention` layers. |
| 551 | |
| 552 | If `processor` is a dict, the key needs to define the path to the corresponding cross attention |
| 553 | processor. This is strongly recommended when setting trainable attention processors. |
| 554 | |
| 555 | """ |
| 556 | count = len(self.attn_processors.keys()) |
| 557 | |
| 558 | if isinstance(processor, dict) and len(processor) != count: |
| 559 | raise ValueError( |
| 560 | f"A dict of processors was passed, but the number of processors {len(processor)} does not match the" |
| 561 | f" number of attention layers: {count}. Please make sure to pass {count} processor classes." |
| 562 | ) |
| 563 | |
| 564 | def fn_recursive_attn_processor(name: str, module: torch.nn.Module, processor): |
| 565 | if hasattr(module, "set_processor"): |
| 566 | if not isinstance(processor, dict): |
| 567 | module.set_processor(processor) |
| 568 | else: |
| 569 | module.set_processor(processor.pop(f"{name}.processor")) |
| 570 | |
| 571 | for sub_name, child in module.named_children(): |
| 572 | fn_recursive_attn_processor(f"{name}.{sub_name}", child, processor) |
| 573 | |
| 574 | for name, module in self.named_children(): |
| 575 | fn_recursive_attn_processor(name, module, processor) |
| 576 | |
| 577 | # Copied from diffusers.models.unets.unet_2d_condition.UNet2DConditionModel.set_default_attn_processor |
| 578 | def set_default_attn_processor(self): |
no outgoing calls
no test coverage detected