-
Notifications
You must be signed in to change notification settings - Fork 6.2k
[Wan 2.2 LoRA] add support for 2nd transformer lora loading + wan 2.2 lightx2v lora #12074
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1829,6 +1829,18 @@ def _convert_non_diffusers_wan_lora_to_diffusers(state_dict): | |||||
k.startswith("time_projection") and k.endswith(".weight") for k in original_state_dict | ||||||
) | ||||||
|
||||||
def get_alpha_scales(down_weight, alpha_key): | ||||||
rank = down_weight.shape[0] | ||||||
alpha = original_state_dict.pop(alpha_key).item() | ||||||
scale = alpha / rank # LoRA is scaled by 'alpha / rank' in forward pass, so we need to scale it back here | ||||||
scale_down = scale | ||||||
scale_up = 1.0 | ||||||
while scale_down * 2 < scale_up: | ||||||
scale_down *= 2 | ||||||
scale_up /= 2 | ||||||
return scale_down, scale_up | ||||||
|
||||||
|
||||||
for key in list(original_state_dict.keys()): | ||||||
if key.endswith((".diff", ".diff_b")) and "norm" in key: | ||||||
# NOTE: we don't support this because norm layer diff keys are just zeroed values. We can support it | ||||||
|
@@ -1848,15 +1860,25 @@ def _convert_non_diffusers_wan_lora_to_diffusers(state_dict): | |||||
for i in range(min_block, max_block + 1): | ||||||
# Self-attention | ||||||
for o, c in zip(["q", "k", "v", "o"], ["to_q", "to_k", "to_v", "to_out.0"]): | ||||||
original_key = f"blocks.{i}.self_attn.{o}.{lora_down_key}.weight" | ||||||
converted_key = f"blocks.{i}.attn1.{c}.lora_A.weight" | ||||||
if original_key in original_state_dict: | ||||||
converted_state_dict[converted_key] = original_state_dict.pop(original_key) | ||||||
has_alpha = f"blocks.{i}.self_attn.{o}.alpha" in original_state_dict | ||||||
original_key_A = f"blocks.{i}.self_attn.{o}.{lora_down_key}.weight" | ||||||
converted_key_A = f"blocks.{i}.attn1.{c}.lora_A.weight" | ||||||
|
||||||
original_key = f"blocks.{i}.self_attn.{o}.{lora_up_key}.weight" | ||||||
converted_key = f"blocks.{i}.attn1.{c}.lora_B.weight" | ||||||
if original_key in original_state_dict: | ||||||
converted_state_dict[converted_key] = original_state_dict.pop(original_key) | ||||||
original_key_B = f"blocks.{i}.self_attn.{o}.{lora_up_key}.weight" | ||||||
converted_key_B = f"blocks.{i}.attn1.{c}.lora_B.weight" | ||||||
|
||||||
if has_alpha: | ||||||
down_weight = original_state_dict.pop(original_key_A) | ||||||
up_weight = original_state_dict.pop(original_key_B) | ||||||
Comment on lines
+1870
to
+1872
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does the popping have to be conditioned on I think we can just check if |
||||||
scale_down, scale_up = get_alpha_scales(down_weight, f"blocks.{i}.self_attn.{o}.alpha") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
converted_state_dict[converted_key_A] = down_weight * scale_down | ||||||
converted_state_dict[converted_key_B] = up_weight * scale_up | ||||||
|
||||||
else: | ||||||
if original_key_A in original_state_dict: | ||||||
converted_state_dict[converted_key_A] = original_state_dict.pop(original_key_A) | ||||||
if original_key_B in original_state_dict: | ||||||
converted_state_dict[converted_key_B] = original_state_dict.pop(original_key_B) | ||||||
|
||||||
original_key = f"blocks.{i}.self_attn.{o}.diff_b" | ||||||
converted_key = f"blocks.{i}.attn1.{c}.lora_B.bias" | ||||||
|
@@ -1865,15 +1887,25 @@ def _convert_non_diffusers_wan_lora_to_diffusers(state_dict): | |||||
|
||||||
# Cross-attention | ||||||
for o, c in zip(["q", "k", "v", "o"], ["to_q", "to_k", "to_v", "to_out.0"]): | ||||||
original_key = f"blocks.{i}.cross_attn.{o}.{lora_down_key}.weight" | ||||||
converted_key = f"blocks.{i}.attn2.{c}.lora_A.weight" | ||||||
if original_key in original_state_dict: | ||||||
converted_state_dict[converted_key] = original_state_dict.pop(original_key) | ||||||
has_alpha = f"blocks.{i}.cross_attn.{o}.alpha" in original_state_dict | ||||||
original_key_A = f"blocks.{i}.cross_attn.{o}.{lora_down_key}.weight" | ||||||
converted_key_A = f"blocks.{i}.attn2.{c}.lora_A.weight" | ||||||
|
||||||
original_key_B = f"blocks.{i}.cross_attn.{o}.{lora_up_key}.weight" | ||||||
converted_key_B = f"blocks.{i}.attn2.{c}.lora_B.weight" | ||||||
|
||||||
if has_alpha: | ||||||
down_weight = original_state_dict.pop(original_key_A) | ||||||
up_weight = original_state_dict.pop(original_key_B) | ||||||
scale_down, scale_up = get_alpha_scales(down_weight, f"blocks.{i}.cross_attn.{o}.alpha") | ||||||
converted_state_dict[converted_key_A] = down_weight * scale_down | ||||||
converted_state_dict[converted_key_B] = up_weight * scale_up | ||||||
else: | ||||||
if original_key_A in original_state_dict: | ||||||
converted_state_dict[converted_key_A] = original_state_dict.pop(original_key_A) | ||||||
Comment on lines
+1890
to
+1905
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. |
||||||
|
||||||
original_key = f"blocks.{i}.cross_attn.{o}.{lora_up_key}.weight" | ||||||
converted_key = f"blocks.{i}.attn2.{c}.lora_B.weight" | ||||||
if original_key in original_state_dict: | ||||||
converted_state_dict[converted_key] = original_state_dict.pop(original_key) | ||||||
if original_key_B in original_state_dict: | ||||||
converted_state_dict[converted_key_B] = original_state_dict.pop(original_key_B) | ||||||
|
||||||
original_key = f"blocks.{i}.cross_attn.{o}.diff_b" | ||||||
converted_key = f"blocks.{i}.attn2.{c}.lora_B.bias" | ||||||
|
@@ -1882,15 +1914,25 @@ def _convert_non_diffusers_wan_lora_to_diffusers(state_dict): | |||||
|
||||||
if is_i2v_lora: | ||||||
for o, c in zip(["k_img", "v_img"], ["add_k_proj", "add_v_proj"]): | ||||||
original_key = f"blocks.{i}.cross_attn.{o}.{lora_down_key}.weight" | ||||||
converted_key = f"blocks.{i}.attn2.{c}.lora_A.weight" | ||||||
if original_key in original_state_dict: | ||||||
converted_state_dict[converted_key] = original_state_dict.pop(original_key) | ||||||
|
||||||
original_key = f"blocks.{i}.cross_attn.{o}.{lora_up_key}.weight" | ||||||
converted_key = f"blocks.{i}.attn2.{c}.lora_B.weight" | ||||||
if original_key in original_state_dict: | ||||||
converted_state_dict[converted_key] = original_state_dict.pop(original_key) | ||||||
has_alpha = f"blocks.{i}.cross_attn.{o}.alpha" in original_state_dict | ||||||
original_key_A = f"blocks.{i}.cross_attn.{o}.{lora_down_key}.weight" | ||||||
converted_key_A = f"blocks.{i}.attn2.{c}.lora_A.weight" | ||||||
|
||||||
original_key_B = f"blocks.{i}.cross_attn.{o}.{lora_up_key}.weight" | ||||||
converted_key_B = f"blocks.{i}.attn2.{c}.lora_B.weight" | ||||||
|
||||||
if has_alpha: | ||||||
down_weight = original_state_dict.pop(original_key_A) | ||||||
up_weight = original_state_dict.pop(original_key_B) | ||||||
scale_down, scale_up = get_alpha_scales(down_weight, f"blocks.{i}.cross_attn.{o}.alpha") | ||||||
converted_state_dict[converted_key_A] = down_weight * scale_down | ||||||
converted_state_dict[converted_key_B] = up_weight * scale_up | ||||||
else: | ||||||
if original_key_A in original_state_dict: | ||||||
converted_state_dict[converted_key_A] = original_state_dict.pop(original_key_A) | ||||||
|
||||||
if original_key_B in original_state_dict: | ||||||
converted_state_dict[converted_key_B] = original_state_dict.pop(original_key_B) | ||||||
|
||||||
original_key = f"blocks.{i}.cross_attn.{o}.diff_b" | ||||||
converted_key = f"blocks.{i}.attn2.{c}.lora_B.bias" | ||||||
|
@@ -1899,15 +1941,25 @@ def _convert_non_diffusers_wan_lora_to_diffusers(state_dict): | |||||
|
||||||
# FFN | ||||||
for o, c in zip(["ffn.0", "ffn.2"], ["net.0.proj", "net.2"]): | ||||||
original_key = f"blocks.{i}.{o}.{lora_down_key}.weight" | ||||||
converted_key = f"blocks.{i}.ffn.{c}.lora_A.weight" | ||||||
if original_key in original_state_dict: | ||||||
converted_state_dict[converted_key] = original_state_dict.pop(original_key) | ||||||
has_alpha = f"blocks.{i}.{o}.alpha" in original_state_dict | ||||||
original_key_A = f"blocks.{i}.{o}.{lora_down_key}.weight" | ||||||
converted_key_A = f"blocks.{i}.ffn.{c}.lora_A.weight" | ||||||
|
||||||
original_key_B = f"blocks.{i}.{o}.{lora_up_key}.weight" | ||||||
converted_key_B = f"blocks.{i}.ffn.{c}.lora_B.weight" | ||||||
|
||||||
if has_alpha: | ||||||
down_weight = original_state_dict.pop(original_key_A) | ||||||
up_weight = original_state_dict.pop(original_key_B) | ||||||
scale_down, scale_up = get_alpha_scales(down_weight, f"blocks.{i}.{o}.alpha") | ||||||
converted_state_dict[converted_key_A] = down_weight * scale_down | ||||||
converted_state_dict[converted_key_B] = up_weight * scale_up | ||||||
else: | ||||||
if original_key_A in original_state_dict: | ||||||
converted_state_dict[converted_key_A] = original_state_dict.pop(original_key_A) | ||||||
|
||||||
original_key = f"blocks.{i}.{o}.{lora_up_key}.weight" | ||||||
converted_key = f"blocks.{i}.ffn.{c}.lora_B.weight" | ||||||
if original_key in original_state_dict: | ||||||
converted_state_dict[converted_key] = original_state_dict.pop(original_key) | ||||||
if original_key_B in original_state_dict: | ||||||
converted_state_dict[converted_key_B] = original_state_dict.pop(original_key_B) | ||||||
|
||||||
original_key = f"blocks.{i}.{o}.diff_b" | ||||||
converted_key = f"blocks.{i}.ffn.{c}.lora_B.bias" | ||||||
|
@@ -2072,4 +2124,4 @@ def _convert_non_diffusers_ltxv_lora_to_diffusers(state_dict, non_diffusers_pref | |||||
raise ValueError("Invalid LoRA state dict for LTX-Video.") | ||||||
converted_state_dict = {k.removeprefix(f"{non_diffusers_prefix}."): v for k, v in state_dict.items()} | ||||||
converted_state_dict = {f"transformer.{k}": v for k, v in converted_state_dict.items()} | ||||||
return converted_state_dict | ||||||
return converted_state_dict |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5064,7 +5064,7 @@ class WanLoraLoaderMixin(LoraBaseMixin): | |
Load LoRA layers into [`WanTransformer3DModel`]. Specific to [`WanPipeline`] and `[WanImageToVideoPipeline`]. | ||
""" | ||
|
||
_lora_loadable_modules = ["transformer"] | ||
_lora_loadable_modules = ["transformer", "transformer_2"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to note that this loader is shared amongst Wan 2.1 and 2.2 as the pipelines are also one and the same. For Wan 2.1, we won't have any |
||
transformer_name = TRANSFORMER_NAME | ||
|
||
@classmethod | ||
|
@@ -5269,15 +5269,28 @@ def load_lora_weights( | |
if not is_correct_format: | ||
raise ValueError("Invalid LoRA checkpoint.") | ||
|
||
self.load_lora_into_transformer( | ||
state_dict, | ||
transformer=getattr(self, self.transformer_name) if not hasattr(self, "transformer") else self.transformer, | ||
adapter_name=adapter_name, | ||
metadata=metadata, | ||
_pipeline=self, | ||
low_cpu_mem_usage=low_cpu_mem_usage, | ||
hotswap=hotswap, | ||
) | ||
load_into_transformer_2 = kwargs.pop("load_into_transformer_2", False) | ||
if load_into_transformer_2: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should raise in case |
||
self.load_lora_into_transformer( | ||
state_dict, | ||
transformer=self.transformer_2, | ||
adapter_name=adapter_name, | ||
metadata=metadata, | ||
_pipeline=self, | ||
low_cpu_mem_usage=low_cpu_mem_usage, | ||
hotswap=hotswap, | ||
) | ||
else: | ||
self.load_lora_into_transformer( | ||
state_dict, | ||
transformer=getattr(self, self.transformer_name) if not hasattr(self, | ||
"transformer") else self.transformer, | ||
adapter_name=adapter_name, | ||
metadata=metadata, | ||
_pipeline=self, | ||
low_cpu_mem_usage=low_cpu_mem_usage, | ||
hotswap=hotswap, | ||
) | ||
Comment on lines
+5283
to
+5293
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why put it under |
||
|
||
@classmethod | ||
# Copied from diffusers.loaders.lora_pipeline.SD3LoraLoaderMixin.load_lora_into_transformer with SD3Transformer2DModel->WanTransformer3DModel | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.