|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2025 HuggingFace Inc. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import unittest |
| 17 | + |
| 18 | +import torch |
| 19 | + |
| 20 | +from diffusers.loaders.lora_conversion_utils import _convert_non_diffusers_flux2_lora_to_diffusers |
| 21 | +from diffusers.modular_pipelines.flux2.encoders import Flux2KleinTextEncoderStep |
| 22 | + |
| 23 | + |
| 24 | +class Flux2LoraConversionTests(unittest.TestCase): |
| 25 | + def test_convert_non_diffusers_flux2_lora_maps_guidance_embedder(self): |
| 26 | + state_dict = { |
| 27 | + "diffusion_model.img_in.lora_A.weight": torch.randn(2, 2), |
| 28 | + "diffusion_model.img_in.lora_B.weight": torch.randn(2, 2), |
| 29 | + "diffusion_model.txt_in.lora_A.weight": torch.randn(2, 2), |
| 30 | + "diffusion_model.txt_in.lora_B.weight": torch.randn(2, 2), |
| 31 | + "diffusion_model.time_in.in_layer.lora_A.weight": torch.randn(2, 2), |
| 32 | + "diffusion_model.time_in.in_layer.lora_B.weight": torch.randn(2, 2), |
| 33 | + "diffusion_model.time_in.out_layer.lora_A.weight": torch.randn(2, 2), |
| 34 | + "diffusion_model.time_in.out_layer.lora_B.weight": torch.randn(2, 2), |
| 35 | + "diffusion_model.guidance_in.in_layer.lora_A.weight": torch.randn(2, 2), |
| 36 | + "diffusion_model.guidance_in.in_layer.lora_B.weight": torch.randn(2, 2), |
| 37 | + "diffusion_model.guidance_in.out_layer.lora_A.weight": torch.randn(2, 2), |
| 38 | + "diffusion_model.guidance_in.out_layer.lora_B.weight": torch.randn(2, 2), |
| 39 | + } |
| 40 | + |
| 41 | + converted_state_dict = _convert_non_diffusers_flux2_lora_to_diffusers(state_dict) |
| 42 | + |
| 43 | + expected_keys = { |
| 44 | + "transformer.x_embedder.lora_A.weight", |
| 45 | + "transformer.x_embedder.lora_B.weight", |
| 46 | + "transformer.context_embedder.lora_A.weight", |
| 47 | + "transformer.context_embedder.lora_B.weight", |
| 48 | + "transformer.time_guidance_embed.timestep_embedder.linear_1.lora_A.weight", |
| 49 | + "transformer.time_guidance_embed.timestep_embedder.linear_1.lora_B.weight", |
| 50 | + "transformer.time_guidance_embed.timestep_embedder.linear_2.lora_A.weight", |
| 51 | + "transformer.time_guidance_embed.timestep_embedder.linear_2.lora_B.weight", |
| 52 | + "transformer.time_guidance_embed.guidance_embedder.linear_1.lora_A.weight", |
| 53 | + "transformer.time_guidance_embed.guidance_embedder.linear_1.lora_B.weight", |
| 54 | + "transformer.time_guidance_embed.guidance_embedder.linear_2.lora_A.weight", |
| 55 | + "transformer.time_guidance_embed.guidance_embedder.linear_2.lora_B.weight", |
| 56 | + } |
| 57 | + |
| 58 | + self.assertEqual(set(converted_state_dict.keys()), expected_keys) |
| 59 | + |
| 60 | + def test_flux2_text_subpipeline_rejects_transformer_lora_loading(self): |
| 61 | + text_pipe = Flux2KleinTextEncoderStep().init_pipeline() |
| 62 | + |
| 63 | + with self.assertRaisesRegex(ValueError, "defines a `transformer` component"): |
| 64 | + text_pipe.load_lora_weights({}) |
0 commit comments