"git@developer.sourcefind.cn:renzhc/diffusers_dcu.git" did not exist on "ff8f58086b83414f785a0d1e9b1064331cd14ca4"
Unverified Commit 852dc76d authored by Batuhan Taskaya's avatar Batuhan Taskaya Committed by GitHub
Browse files

Support higher dimension LoRAs (#4625)



* Support higher dimension LoRAs

* add: tests

* fix: assertion values.

---------
Co-authored-by: default avatarSayak Paul <spsayakpaul@gmail.com>
parent 064f1508
...@@ -22,9 +22,6 @@ class LoRALinearLayer(nn.Module): ...@@ -22,9 +22,6 @@ class LoRALinearLayer(nn.Module):
def __init__(self, in_features, out_features, rank=4, network_alpha=None, device=None, dtype=None): def __init__(self, in_features, out_features, rank=4, network_alpha=None, device=None, dtype=None):
super().__init__() super().__init__()
if rank > min(in_features, out_features):
raise ValueError(f"LoRA rank {rank} must be less or equal than {min(in_features, out_features)}")
self.down = nn.Linear(in_features, rank, bias=False, device=device, dtype=dtype) self.down = nn.Linear(in_features, rank, bias=False, device=device, dtype=dtype)
self.up = nn.Linear(rank, out_features, bias=False, device=device, dtype=dtype) self.up = nn.Linear(rank, out_features, bias=False, device=device, dtype=dtype)
# This value has the same meaning as the `--network_alpha` option in the kohya-ss trainer script. # This value has the same meaning as the `--network_alpha` option in the kohya-ss trainer script.
...@@ -54,9 +51,6 @@ class LoRAConv2dLayer(nn.Module): ...@@ -54,9 +51,6 @@ class LoRAConv2dLayer(nn.Module):
): ):
super().__init__() super().__init__()
if rank > min(in_features, out_features):
raise ValueError(f"LoRA rank {rank} must be less or equal than {min(in_features, out_features)}")
self.down = nn.Conv2d(in_features, rank, kernel_size=kernel_size, stride=stride, padding=padding, bias=False) self.down = nn.Conv2d(in_features, rank, kernel_size=kernel_size, stride=stride, padding=padding, bias=False)
# according to the official kohya_ss trainer kernel_size are always fixed for the up layer # according to the official kohya_ss trainer kernel_size are always fixed for the up layer
# # see: https://github.com/bmaltais/kohya_ss/blob/2accb1305979ba62f5077a23aabac23b4c37e935/networks/lora_diffusers.py#L129 # # see: https://github.com/bmaltais/kohya_ss/blob/2accb1305979ba62f5077a23aabac23b4c37e935/networks/lora_diffusers.py#L129
......
...@@ -779,6 +779,25 @@ class LoraIntegrationTests(unittest.TestCase): ...@@ -779,6 +779,25 @@ class LoraIntegrationTests(unittest.TestCase):
self.assertTrue(np.allclose(images, expected, atol=1e-4)) self.assertTrue(np.allclose(images, expected, atol=1e-4))
def test_kohya_sd_v15_with_higher_dimensions(self):
generator = torch.Generator().manual_seed(0)
pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", safety_checker=None).to(
torch_device
)
lora_model_id = "hf-internal-testing/urushisato-lora"
lora_filename = "urushisato_v15.safetensors"
pipe.load_lora_weights(lora_model_id, weight_name=lora_filename)
images = pipe(
"masterpiece, best quality, mountain", output_type="np", generator=generator, num_inference_steps=2
).images
images = images[0, -3:, -3:, -1].flatten()
expected = np.array([0.7165, 0.6616, 0.5833, 0.7504, 0.6718, 0.587, 0.6871, 0.6361, 0.5694])
self.assertTrue(np.allclose(images, expected, atol=1e-4))
def test_vanilla_funetuning(self): def test_vanilla_funetuning(self):
generator = torch.Generator().manual_seed(0) generator = torch.Generator().manual_seed(0)
......
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