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: AttentionProcessor | dict[str, AttentionProcessor])
| 62 | return processors |
| 63 | |
| 64 | def set_attn_processor(self, processor: AttentionProcessor | dict[str, AttentionProcessor]): |
| 65 | r""" |
| 66 | Sets the attention processor to use to compute attention. |
| 67 | |
| 68 | Parameters: |
| 69 | processor (`dict` of `AttentionProcessor` or only `AttentionProcessor`): |
| 70 | The instantiated processor class or a dictionary of processor classes that will be set as the processor |
| 71 | for **all** `Attention` layers. |
| 72 | |
| 73 | If `processor` is a dict, the key needs to define the path to the corresponding cross attention |
| 74 | processor. This is strongly recommended when setting trainable attention processors. |
| 75 | |
| 76 | """ |
| 77 | count = len(self.attn_processors.keys()) |
| 78 | |
| 79 | if isinstance(processor, dict) and len(processor) != count: |
| 80 | raise ValueError( |
| 81 | f"A dict of processors was passed, but the number of processors {len(processor)} does not match the" |
| 82 | f" number of attention layers: {count}. Please make sure to pass {count} processor classes." |
| 83 | ) |
| 84 | |
| 85 | def fn_recursive_attn_processor(name: str, module: torch.nn.Module, processor): |
| 86 | if hasattr(module, "set_processor"): |
| 87 | if not isinstance(processor, dict): |
| 88 | module.set_processor(processor) |
| 89 | else: |
| 90 | module.set_processor(processor.pop(f"{name}.processor")) |
| 91 | |
| 92 | for sub_name, child in module.named_children(): |
| 93 | fn_recursive_attn_processor(f"{name}.{sub_name}", child, processor) |
| 94 | |
| 95 | for name, module in self.named_children(): |
| 96 | fn_recursive_attn_processor(name, module, processor) |
| 97 | |
| 98 | def fuse_qkv_projections(self): |
| 99 | """ |
no outgoing calls