|  | 
|  | 1 | +# coding=utf-8 | 
|  | 2 | +# Copyright 2024 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 gc | 
|  | 17 | +import unittest | 
|  | 18 | + | 
|  | 19 | +import torch | 
|  | 20 | + | 
|  | 21 | +from diffusers import ( | 
|  | 22 | +    FluxTransformer2DModel, | 
|  | 23 | +) | 
|  | 24 | +from diffusers.utils.testing_utils import ( | 
|  | 25 | +    backend_empty_cache, | 
|  | 26 | +    enable_full_determinism, | 
|  | 27 | +    require_torch_accelerator, | 
|  | 28 | +    torch_device, | 
|  | 29 | +) | 
|  | 30 | + | 
|  | 31 | + | 
|  | 32 | +enable_full_determinism() | 
|  | 33 | + | 
|  | 34 | + | 
|  | 35 | +@require_torch_accelerator | 
|  | 36 | +class FluxTransformer2DModelSingleFileTests(unittest.TestCase): | 
|  | 37 | +    model_class = FluxTransformer2DModel | 
|  | 38 | +    ckpt_path = "https://huggingface.co/black-forest-labs/FLUX.1-dev/blob/main/flux1-dev.safetensors" | 
|  | 39 | +    alternate_keys_ckpt_paths = ["https://huggingface.co/Comfy-Org/flux1-dev/blob/main/flux1-dev-fp8.safetensors"] | 
|  | 40 | + | 
|  | 41 | +    repo_id = "black-forest-labs/FLUX.1-dev" | 
|  | 42 | + | 
|  | 43 | +    def setUp(self): | 
|  | 44 | +        super().setUp() | 
|  | 45 | +        gc.collect() | 
|  | 46 | +        backend_empty_cache(torch_device) | 
|  | 47 | + | 
|  | 48 | +    def tearDown(self): | 
|  | 49 | +        super().tearDown() | 
|  | 50 | +        gc.collect() | 
|  | 51 | +        backend_empty_cache(torch_device) | 
|  | 52 | + | 
|  | 53 | +    def test_single_file_components(self): | 
|  | 54 | +        model = self.model_class.from_pretrained(self.repo_id, subfolder="transformer") | 
|  | 55 | +        model_single_file = self.model_class.from_single_file(self.ckpt_path) | 
|  | 56 | + | 
|  | 57 | +        PARAMS_TO_IGNORE = ["torch_dtype", "_name_or_path", "_use_default_values", "_diffusers_version"] | 
|  | 58 | +        for param_name, param_value in model_single_file.config.items(): | 
|  | 59 | +            if param_name in PARAMS_TO_IGNORE: | 
|  | 60 | +                continue | 
|  | 61 | +            assert ( | 
|  | 62 | +                model.config[param_name] == param_value | 
|  | 63 | +            ), f"{param_name} differs between single file loading and pretrained loading" | 
|  | 64 | + | 
|  | 65 | +    def test_checkpoint_loading(self): | 
|  | 66 | +        for ckpt_path in self.alternate_keys_ckpt_paths: | 
|  | 67 | +            torch.cuda.empty_cache() | 
|  | 68 | +            model = self.model_class.from_single_file(ckpt_path) | 
|  | 69 | + | 
|  | 70 | +            del model | 
|  | 71 | +            gc.collect() | 
|  | 72 | +            torch.cuda.empty_cache() | 
0 commit comments