r""" Set the attention processor to use. Args: processor (`AttnProcessor`): The attention processor to use. _remove_lora (`bool`, *optional*, defaults to `False`): Set to `True` to remove LoRA layers from the model.
(self, processor: "AttnProcessor", _remove_lora: bool = False)
| 369 | self.set_processor(processor) |
| 370 | |
| 371 | def set_processor(self, processor: "AttnProcessor", _remove_lora: bool = False) -> None: |
| 372 | r""" |
| 373 | Set the attention processor to use. |
| 374 | |
| 375 | Args: |
| 376 | processor (`AttnProcessor`): |
| 377 | The attention processor to use. |
| 378 | _remove_lora (`bool`, *optional*, defaults to `False`): |
| 379 | Set to `True` to remove LoRA layers from the model. |
| 380 | """ |
| 381 | if not USE_PEFT_BACKEND and hasattr(self, "processor") and _remove_lora and self.to_q.lora_layer is not None: |
| 382 | deprecate( |
| 383 | "set_processor to offload LoRA", |
| 384 | "0.26.0", |
| 385 | "In detail, removing LoRA layers via calling `set_default_attn_processor` is deprecated. Please make sure to call `pipe.unload_lora_weights()` instead.", |
| 386 | ) |
| 387 | # TODO(Patrick, Sayak) - this can be deprecated once PEFT LoRA integration is complete |
| 388 | # We need to remove all LoRA layers |
| 389 | # Don't forget to remove ALL `_remove_lora` from the codebase |
| 390 | for module in self.modules(): |
| 391 | if hasattr(module, "set_lora_layer"): |
| 392 | module.set_lora_layer(None) |
| 393 | |
| 394 | # if current processor is in `self._modules` and if passed `processor` is not, we need to |
| 395 | # pop `processor` from `self._modules` |
| 396 | if ( |
| 397 | hasattr(self, "processor") |
| 398 | and isinstance(self.processor, torch.nn.Module) |
| 399 | and not isinstance(processor, torch.nn.Module) |
| 400 | ): |
| 401 | logger.info(f"You are removing possibly trained weights of {self.processor} with {processor}") |
| 402 | self._modules.pop("processor") |
| 403 | |
| 404 | self.processor = processor |
| 405 | |
| 406 | def get_processor(self, return_deprecated_lora: bool = False) -> "AttentionProcessor": |
| 407 | r""" |
no test coverage detected