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)
| 142 | |
| 143 | # Copied from diffusers.models.unets.unet_2d_condition.UNet2DConditionModel.set_attn_processor |
| 144 | def set_attn_processor(self, processor): |
| 145 | r""" |
| 146 | Sets the attention processor to use to compute attention. |
| 147 | |
| 148 | Parameters: |
| 149 | processor (`dict` of `AttentionProcessor` or only `AttentionProcessor`): |
| 150 | The instantiated processor class or a dictionary of processor classes that will be set as the processor |
| 151 | for **all** `Attention` layers. |
| 152 | |
| 153 | If `processor` is a dict, the key needs to define the path to the corresponding cross attention |
| 154 | processor. This is strongly recommended when setting trainable attention processors. |
| 155 | |
| 156 | """ |
| 157 | count = len(self.attn_processors.keys()) |
| 158 | |
| 159 | if isinstance(processor, dict) and len(processor) != count: |
| 160 | raise ValueError( |
| 161 | f"A dict of processors was passed, but the number of processors {len(processor)} does not match the" |
| 162 | f" number of attention layers: {count}. Please make sure to pass {count} processor classes." |
| 163 | ) |
| 164 | |
| 165 | def fn_recursive_attn_processor(name: str, module: torch.nn.Module, processor): |
| 166 | if hasattr(module, "set_processor"): |
| 167 | if not isinstance(processor, dict): |
| 168 | module.set_processor(processor) |
| 169 | else: |
| 170 | module.set_processor(processor.pop(f"{name}.processor")) |
| 171 | |
| 172 | for sub_name, child in module.named_children(): |
| 173 | fn_recursive_attn_processor(f"{name}.{sub_name}", child, processor) |
| 174 | |
| 175 | for name, module in self.named_children(): |
| 176 | fn_recursive_attn_processor(name, module, processor) |
| 177 | |
| 178 | def _set_gradient_checkpointing(self, module, value=False): |
| 179 | if hasattr(module, "gradient_checkpointing"): |
nothing calls this directly
no outgoing calls
no test coverage detected