-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Fix LongCat LoRA load/unload and add regression test #12867
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
surajyadav-research
wants to merge
1
commit into
huggingface:main
Choose a base branch
from
surajyadav-research:longcat-lora
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.
+111
−4
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
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 |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| # Copyright 2025 The HuggingFace Team. | ||
|
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. This is not needed. Please try to consult the existing testing structure for pipeline-level LoRA testing c.f. https://github.com/huggingface/diffusers/tree/main/tests/lora/ |
||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import unittest | ||
|
|
||
| import numpy as np | ||
| import torch | ||
|
|
||
| from diffusers import LongCatImagePipeline | ||
|
|
||
| from ...testing_utils import enable_full_determinism, require_accelerate, require_torch_gpu, slow | ||
|
|
||
|
|
||
| enable_full_determinism() | ||
|
|
||
|
|
||
| def _pil_to_np01(img): | ||
| """PIL -> float32 in [0, 1], shape (H, W, 3).""" | ||
| arr = np.asarray(img).astype(np.float32) / 255.0 | ||
| if arr.ndim == 3 and arr.shape[-1] > 3: | ||
| arr = arr[..., :3] | ||
| return arr | ||
|
|
||
|
|
||
| class LongCatImagePipelineLoRATests(unittest.TestCase): | ||
| @slow | ||
| @require_torch_gpu | ||
| @require_accelerate | ||
| def test_lora_load_changes_output_and_unload_restores(self): | ||
| """ | ||
| 1) Generate baseline image | ||
| 2) Load LoRA -> output should change | ||
| 3) Unload LoRA -> output should return close to baseline | ||
| """ | ||
| model_id = "meituan-longcat/LongCat-Image" | ||
| lora_repo = "lrzjason/LongCatEmojiTest" | ||
| weight_name = "longcat_image-9-450.safetensors" | ||
| adapter_name = "emoji" | ||
|
|
||
| pipe = LongCatImagePipeline.from_pretrained(model_id, torch_dtype=torch.bfloat16) | ||
| pipe.enable_model_cpu_offload() | ||
| pipe.set_progress_bar_config(disable=True) | ||
|
|
||
| prompt = "a 3d anime character, cute emoji style, studio lighting" | ||
|
|
||
| common_kwargs = { | ||
| "height": 768, | ||
| "width": 1344, | ||
| "guidance_scale": 4.0, | ||
| "num_inference_steps": 8, | ||
| "num_images_per_prompt": 1, | ||
| "output_type": "pil", | ||
| } | ||
|
|
||
| # 1) Baseline (no LoRA) | ||
| g0 = torch.Generator(device="cpu").manual_seed(123) | ||
| base_img = pipe(prompt, generator=g0, **common_kwargs).images[0] | ||
|
|
||
| # 2) Load LoRA | ||
| pipe.load_lora_weights( | ||
| lora_repo, | ||
| weight_name=weight_name, | ||
| adapter_name=adapter_name, | ||
| ) | ||
|
|
||
| g1 = torch.Generator(device="cpu").manual_seed(123) | ||
| lora_img = pipe(prompt, generator=g1, **common_kwargs).images[0] | ||
|
|
||
| # 3) Unload LoRA | ||
| pipe.unload_lora_weights() | ||
|
|
||
| g2 = torch.Generator(device="cpu").manual_seed(123) | ||
| after_img = pipe(prompt, generator=g2, **common_kwargs).images[0] | ||
|
|
||
| base = _pil_to_np01(base_img) | ||
| lora = _pil_to_np01(lora_img) | ||
| after = _pil_to_np01(after_img) | ||
|
|
||
| diff_lora = float(np.mean(np.abs(base - lora))) | ||
| diff_after = float(np.mean(np.abs(base - after))) | ||
|
|
||
| self.assertGreater( | ||
| diff_lora, | ||
| 1e-4, | ||
| msg=f"LoRA didn't change output enough (mean|base-lora|={diff_lora}).", | ||
| ) | ||
|
|
||
| # After unload, output should be substantially closer to base than the LoRA output. | ||
| self.assertLess( | ||
| diff_after, | ||
| diff_lora * 0.5, | ||
| msg=( | ||
| "Unloading LoRA didn't restore base behavior enough " | ||
| f"(mean|base-after|={diff_after}, mean|base-lora|={diff_lora})." | ||
| ), | ||
| ) | ||
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.
This seems quite incorrect to me.
Flux has two LoRA loadable modules:
diffusers/src/diffusers/loaders/lora_pipeline.py
Line 1493 in f6b6a71
For LongCat, it uses a different text encoder (Flux uses two text encoder, let along) and rest of its components also seems to be different from Flux:
diffusers/src/diffusers/pipelines/longcat_image/pipeline_longcat_image.py
Lines 214 to 222 in f6b6a71
So, could you please explain how using the
FluxLoraLoaderMixinis appropriate here?Instead, I suggest we write a dedicated LoRA loader mixin class for LongCat --
LongCatLoraLoaderMixin. You can refer todiffusers/src/diffusers/loaders/lora_pipeline.py
Line 4775 in f6b6a71
as an example.