Unverified Commit ae060fc4 authored by Sayak Paul's avatar Sayak Paul Committed by GitHub
Browse files

[feat] introduce `unload_lora()`. (#6451)

* introduce unload_lora.

* fix-copies
parent 9d945b2b
......@@ -829,6 +829,17 @@ class UNet2DConditionModel(ModelMixin, ConfigMixin, UNet2DConditionLoadersMixin)
if self.original_attn_processors is not None:
self.set_attn_processor(self.original_attn_processors)
def unload_lora(self):
"""Unloads LoRA weights."""
deprecate(
"unload_lora",
"0.28.0",
"Calling `unload_lora()` is deprecated and will be removed in a future version. Please install `peft` and then call `disable_adapters().",
)
for module in self.modules():
if hasattr(module, "set_lora_layer"):
module.set_lora_layer(None)
def forward(
self,
sample: torch.FloatTensor,
......
......@@ -22,7 +22,7 @@ import torch.utils.checkpoint
from ..configuration_utils import ConfigMixin, register_to_config
from ..loaders import UNet2DConditionLoadersMixin
from ..utils import BaseOutput, logging
from ..utils import BaseOutput, deprecate, logging
from .activations import get_activation
from .attention_processor import (
ADDED_KV_ATTENTION_PROCESSORS,
......@@ -503,6 +503,18 @@ class UNet3DConditionModel(ModelMixin, ConfigMixin, UNet2DConditionLoadersMixin)
if hasattr(upsample_block, k) or getattr(upsample_block, k, None) is not None:
setattr(upsample_block, k, None)
# Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.unload_lora
def unload_lora(self):
"""Unloads LoRA weights."""
deprecate(
"unload_lora",
"0.28.0",
"Calling `unload_lora()` is deprecated and will be removed in a future version. Please install `peft` and then call `disable_adapters().",
)
for module in self.modules():
if hasattr(module, "set_lora_layer"):
module.set_lora_layer(None)
def forward(
self,
sample: torch.FloatTensor,
......
......@@ -1034,6 +1034,17 @@ class UNetFlatConditionModel(ModelMixin, ConfigMixin):
if self.original_attn_processors is not None:
self.set_attn_processor(self.original_attn_processors)
def unload_lora(self):
"""Unloads LoRA weights."""
deprecate(
"unload_lora",
"0.28.0",
"Calling `unload_lora()` is deprecated and will be removed in a future version. Please install `peft` and then call `disable_adapters().",
)
for module in self.modules():
if hasattr(module, "set_lora_layer"):
module.set_lora_layer(None)
def forward(
self,
sample: torch.FloatTensor,
......
......@@ -151,9 +151,7 @@ def create_unet_lora_layers(unet: nn.Module, rank=4, mock_weights=True):
unet_lora_sd = unet_lora_state_dict(unet)
# Unload LoRA.
for module in unet.modules():
if hasattr(module, "set_lora_layer"):
module.set_lora_layer(None)
unet.unload_lora()
return unet_lora_parameters, unet_lora_sd
......@@ -230,9 +228,7 @@ def create_3d_unet_lora_layers(unet: nn.Module, rank=4, mock_weights=True):
unet_lora_sd = unet_lora_state_dict(unet)
# Unload LoRA.
for module in unet.modules():
if hasattr(module, "set_lora_layer"):
module.set_lora_layer(None)
unet.unload_lora()
return unet_lora_sd
......@@ -1545,9 +1541,7 @@ class UNet2DConditionLoRAModelTests(unittest.TestCase):
sample = model(**inputs_dict, cross_attention_kwargs={"scale": 0.0}).sample
# Unload LoRA.
for module in model.modules():
if hasattr(module, "set_lora_layer"):
module.set_lora_layer(None)
model.unload_lora()
with torch.no_grad():
new_sample = model(**inputs_dict).sample
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment