-
Notifications
You must be signed in to change notification settings - Fork 130
refactor wan lora loader #691
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
Conversation
Summary of ChangesHello @gushiqiao, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request focuses on refactoring the LoRA (Low-Rank Adaptation) loading and application mechanism. The core logic for applying LoRA weights has been extracted from the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
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.
Code Review
This pull request refactors the LoRA loading mechanism for WAN models by extracting the logic into a reusable LoRALoader class. This is a positive change for code structure and maintainability. My review focuses on two main points: a potential bug in the application of LoRA scaling factors which could lead to incorrect model behavior, and the removal of functionality for unloading LoRAs, which represents a feature regression. Addressing these points will ensure the refactoring is both correct and functionally complete.
| self.lora_loader.apply_lora( | ||
| weight_dict=weight_dict, | ||
| lora_weights=lora_weights, | ||
| alpha=alpha, | ||
| strength=alpha, | ||
| ) |
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.
The call to self.lora_loader.apply_lora passes the alpha value to both the alpha and strength parameters. Based on the implementation of LoRALoader, this will result in the LoRA weights being scaled by alpha twice (e.g., proportional to alpha^2 for diff-based LoRAs), which is likely not the intended behavior and differs from the original implementation.
The strength parameter seems to be intended for an additional, separate strength multiplier, and it defaults to 1.0. To match the expected behavior of applying the LoRA strength once, you should probably omit the strength parameter in this call.
self.lora_loader.apply_lora(
weight_dict=weight_dict,
lora_weights=lora_weights,
alpha=alpha,
)| self.model._apply_weights(weight_dict) | ||
|
|
||
| logger.info(f"Applied LoRA: {lora_name} with alpha={alpha}") | ||
| del lora_weights | ||
| return True | ||
|
|
||
| @torch.no_grad() | ||
| def _apply_lora_weights(self, weight_dict, lora_weights, alpha): | ||
| lora_pairs = {} | ||
| lora_diffs = {} | ||
|
|
||
| def try_lora_pair(key, prefix, suffix_a, suffix_b, target_suffix): | ||
| if key.endswith(suffix_a): | ||
| base_name = key[len(prefix) :].replace(suffix_a, target_suffix) | ||
| pair_key = key.replace(suffix_a, suffix_b) | ||
| if pair_key in lora_weights: | ||
| lora_pairs[base_name] = (key, pair_key) | ||
|
|
||
| def try_lora_diff(key, prefix, suffix, target_suffix): | ||
| if key.endswith(suffix): | ||
| base_name = key[len(prefix) :].replace(suffix, target_suffix) | ||
| lora_diffs[base_name] = key | ||
|
|
||
| prefixs = [ | ||
| "", # empty prefix | ||
| "diffusion_model.", | ||
| ] | ||
| for prefix in prefixs: | ||
| for key in lora_weights.keys(): | ||
| if not key.startswith(prefix): | ||
| continue | ||
|
|
||
| try_lora_pair(key, prefix, "lora_A.weight", "lora_B.weight", "weight") | ||
| try_lora_pair(key, prefix, "lora_down.weight", "lora_up.weight", "weight") | ||
| try_lora_diff(key, prefix, "diff", "weight") | ||
| try_lora_diff(key, prefix, "diff_b", "bias") | ||
| try_lora_diff(key, prefix, "diff_m", "modulation") | ||
|
|
||
| applied_count = 0 | ||
| for name, param in weight_dict.items(): | ||
| if name in lora_pairs: | ||
| if name not in self.override_dict: | ||
| self.override_dict[name] = param.clone().cpu() | ||
| name_lora_A, name_lora_B = lora_pairs[name] | ||
| lora_A = lora_weights[name_lora_A].to(param.device, param.dtype) | ||
| lora_B = lora_weights[name_lora_B].to(param.device, param.dtype) | ||
| if param.shape == (lora_B.shape[0], lora_A.shape[1]): | ||
| param += torch.matmul(lora_B, lora_A) * alpha | ||
| applied_count += 1 | ||
| elif name in lora_diffs: | ||
| if name not in self.override_dict: | ||
| self.override_dict[name] = param.clone().cpu() | ||
|
|
||
| name_diff = lora_diffs[name] | ||
| lora_diff = lora_weights[name_diff].to(param.device, param.dtype) | ||
| if param.shape == lora_diff.shape: | ||
| param += lora_diff * alpha | ||
| applied_count += 1 | ||
|
|
||
| logger.info(f"Applied {applied_count} LoRA weight adjustments") | ||
| if applied_count == 0: | ||
| logger.info( | ||
| "Warning: No LoRA weights were applied. Expected naming conventions: 'diffusion_model.<layer_name>.lora_A.weight' and 'diffusion_model.<layer_name>.lora_B.weight'. Please verify the LoRA weight file." | ||
| ) | ||
|
|
||
| @torch.no_grad() | ||
| def remove_lora(self): | ||
| logger.info(f"Removing LoRA ...") | ||
|
|
||
| restored_count = 0 | ||
| for k, v in self.override_dict.items(): | ||
| self.model.original_weight_dict[k] = v.to(self.model.device) | ||
| restored_count += 1 | ||
|
|
||
| logger.info(f"LoRA removed, restored {restored_count} weights") | ||
|
|
||
| self.model._apply_weights(self.model.original_weight_dict) | ||
|
|
||
| torch.cuda.empty_cache() | ||
| gc.collect() | ||
|
|
||
| self.lora_metadata = {} | ||
| self.override_dict = {} | ||
|
|
||
| def list_loaded_loras(self): | ||
| return list(self.lora_metadata.keys()) |
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.
This refactoring removes the remove_lora and list_loaded_loras methods. The removal of remove_lora is a significant functionality regression, as it's no longer possible to unload a LoRA after it has been applied.
The underlying mechanism that supported this feature, which involved storing original weights in self.override_dict, has also been removed from the LoRA application logic. As a result, the self.override_dict attribute in WanLoraWrapper is now unused.
If the ability to remove LoRAs is a required feature, this functionality needs to be reinstated. If it's intentionally being removed, the now-dead code (self.override_dict) should also be removed from the __init__ method for clarity.
No description provided.