-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Fix _merged_adapters bookkeeping desync on partial unfuse_lora(components=...) #14215
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
Open
ErenAta16
wants to merge
1
commit into
huggingface:main
Choose a base branch
from
ErenAta16:fix/unfuse-lora-partial-components-bookkeeping
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+66
−3
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1711,6 +1711,45 @@ def test_simple_inference_with_text_lora_denoiser_fused_multi( | |
| pipe.unfuse_lora(components=self.pipeline_class._lora_loadable_modules) | ||
| self.assertTrue(pipe.num_fused_loras == 0, f"{pipe.num_fused_loras=}, {pipe.fused_loras=}") | ||
|
|
||
| def test_fuse_unfuse_partial_components_keeps_merged_adapter_bookkeeping(self): | ||
| """ | ||
| `_merged_adapters` (backing `num_fused_loras`/`fused_loras`) is shared across the whole pipeline, | ||
| while actual merge state is tracked per component by PEFT. Fusing an adapter into every loadable | ||
| component and then unfusing only *some* of them should not report the adapter as fully unfused, | ||
| it's still merged into the untouched component(s). | ||
| """ | ||
| if "text_encoder" not in self.pipeline_class._lora_loadable_modules: | ||
| return | ||
|
Comment on lines
+1721
to
+1722
Member
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. We should use |
||
|
|
||
| components, text_lora_config, denoiser_lora_config = self.get_dummy_components() | ||
| pipe = self.pipeline_class(**components) | ||
| pipe = pipe.to(torch_device) | ||
| pipe.set_progress_bar_config(disable=None) | ||
|
|
||
| pipe.text_encoder.add_adapter(text_lora_config, "adapter-1") | ||
| self.assertTrue(check_if_lora_correctly_set(pipe.text_encoder), "Lora not correctly set in text encoder") | ||
|
|
||
| denoiser = pipe.transformer if self.unet_kwargs is None else pipe.unet | ||
| denoiser_component_name = "unet" if self.unet_kwargs is not None else "transformer" | ||
| denoiser.add_adapter(denoiser_lora_config, "adapter-1") | ||
| self.assertTrue(check_if_lora_correctly_set(denoiser), "Lora not correctly set in denoiser.") | ||
|
|
||
| pipe.fuse_lora(components=["text_encoder", denoiser_component_name], adapter_names=["adapter-1"]) | ||
| self.assertTrue(pipe.num_fused_loras == 1, f"{pipe.num_fused_loras=}, {pipe.fused_loras=}") | ||
|
|
||
| # Only unfuse the text encoder; the denoiser still has "adapter-1" merged into its base weights. | ||
| pipe.unfuse_lora(components=["text_encoder"]) | ||
| self.assertTrue( | ||
| pipe.num_fused_loras == 1, | ||
| f"adapter-1 is still merged into {denoiser_component_name}, but num_fused_loras dropped to " | ||
| f"{pipe.num_fused_loras=}, {pipe.fused_loras=}", | ||
| ) | ||
| self.assertIn("adapter-1", pipe.fused_loras) | ||
|
|
||
| # Now unfuse the remaining component; bookkeeping should correctly drop to 0. | ||
| pipe.unfuse_lora(components=[denoiser_component_name]) | ||
| self.assertTrue(pipe.num_fused_loras == 0, f"{pipe.num_fused_loras=}, {pipe.fused_loras=}") | ||
|
|
||
| def test_lora_scale_kwargs_match_fusion(self, expected_atol: float = 1e-3, expected_rtol: float = 1e-3): | ||
| attention_kwargs_name = determine_attention_kwargs_name(self.pipeline_class) | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Prefer the existing skip convention over a bare
return. As written, this test reports as passed for pipelines that don't have atext_encoderloadable module, which hides the fact that it never exercised the scenario. The rest of this file gates on thesupports_text_encoder_lorasflag withpytest.skip(...)(e.g. lines 385, 521), which is also more accurate — a pipeline can have atext_encodermodule but not support text-encoder LoRAs.