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]])
| 455 | |
| 456 | # Copied from diffusers.models.unets.unet_2d_condition.UNet2DConditionModel.set_attn_processor |
| 457 | def set_attn_processor(self, processor: Union[AttentionProcessor, Dict[str, AttentionProcessor]]): |
| 458 | r""" |
| 459 | Sets the attention processor to use to compute attention. |
| 460 | |
| 461 | Parameters: |
| 462 | processor (`dict` of `AttentionProcessor` or only `AttentionProcessor`): |
| 463 | The instantiated processor class or a dictionary of processor classes that will be set as the processor |
| 464 | for **all** `Attention` layers. |
| 465 | |
| 466 | If `processor` is a dict, the key needs to define the path to the corresponding cross attention |
| 467 | processor. This is strongly recommended when setting trainable attention processors. |
| 468 | |
| 469 | """ |
| 470 | count = len(self.attn_processors.keys()) |
| 471 | |
| 472 | if isinstance(processor, dict) and len(processor) != count: |
| 473 | raise ValueError( |
| 474 | f"A dict of processors was passed, but the number of processors {len(processor)} does not match the" |
| 475 | f" number of attention layers: {count}. Please make sure to pass {count} processor classes." |
| 476 | ) |
| 477 | |
| 478 | def fn_recursive_attn_processor(name: str, module: torch.nn.Module, processor): |
| 479 | if hasattr(module, "set_processor"): |
| 480 | if not isinstance(processor, dict): |
| 481 | module.set_processor(processor) |
| 482 | else: |
| 483 | module.set_processor(processor.pop(f"{name}.processor")) |
| 484 | |
| 485 | for sub_name, child in module.named_children(): |
| 486 | fn_recursive_attn_processor(f"{name}.{sub_name}", child, processor) |
| 487 | |
| 488 | for name, module in self.named_children(): |
| 489 | fn_recursive_attn_processor(name, module, processor) |
| 490 | |
| 491 | def forward( |
| 492 | self, |
nothing calls this directly
no outgoing calls
no test coverage detected