| 
 | 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 logging  | 
 | 17 | +import os  | 
 | 18 | +import sys  | 
 | 19 | +import tempfile  | 
 | 20 | + | 
 | 21 | +sys.path.append("..")  | 
 | 22 | +from examples.diffusers.test_examples_utils import ExamplesTests, run_command  # noqa: E402  | 
 | 23 | + | 
 | 24 | +ExamplesTests._launch_args = ["python"]  | 
 | 25 | + | 
 | 26 | +logging.basicConfig(level=logging.DEBUG)  | 
 | 27 | + | 
 | 28 | +logger = logging.getLogger()  | 
 | 29 | +stream_handler = logging.StreamHandler(sys.stdout)  | 
 | 30 | +logger.addHandler(stream_handler)  | 
 | 31 | + | 
 | 32 | + | 
 | 33 | +class ControlNet(ExamplesTests):  | 
 | 34 | +    def test_controlnet_checkpointing_checkpoints_total_limit(self):  | 
 | 35 | +        with tempfile.TemporaryDirectory() as tmpdir:  | 
 | 36 | +            test_args = f"""  | 
 | 37 | +            examples/diffusers/controlnet/train_controlnet.py  | 
 | 38 | +            --pretrained_model_name_or_path=hf-internal-testing/tiny-stable-diffusion-pipe  | 
 | 39 | +            --revision refs/pr/4  | 
 | 40 | +            --dataset_name=hf-internal-testing/fill10  | 
 | 41 | +            --output_dir={tmpdir}  | 
 | 42 | +            --resolution=64  | 
 | 43 | +            --train_batch_size=1  | 
 | 44 | +            --gradient_accumulation_steps=1  | 
 | 45 | +            --max_train_steps=6  | 
 | 46 | +            --checkpoints_total_limit=2  | 
 | 47 | +            --checkpointing_steps=2  | 
 | 48 | +            --controlnet_model_name_or_path=hf-internal-testing/tiny-controlnet  | 
 | 49 | +            """.split()  | 
 | 50 | + | 
 | 51 | +            run_command(self._launch_args + test_args)  | 
 | 52 | + | 
 | 53 | +            self.assertEqual(  | 
 | 54 | +                {x for x in os.listdir(tmpdir) if "checkpoint" in x},  | 
 | 55 | +                {"checkpoint-4", "checkpoint-6"},  | 
 | 56 | +            )  | 
 | 57 | + | 
 | 58 | +    def test_controlnet_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self):  | 
 | 59 | +        with tempfile.TemporaryDirectory() as tmpdir:  | 
 | 60 | +            test_args = f"""  | 
 | 61 | +            examples/diffusers/controlnet/train_controlnet.py  | 
 | 62 | +            --pretrained_model_name_or_path=hf-internal-testing/tiny-stable-diffusion-pipe  | 
 | 63 | +            --revision refs/pr/4  | 
 | 64 | +            --dataset_name=hf-internal-testing/fill10  | 
 | 65 | +            --output_dir={tmpdir}  | 
 | 66 | +            --resolution=64  | 
 | 67 | +            --train_batch_size=1  | 
 | 68 | +            --gradient_accumulation_steps=1  | 
 | 69 | +            --controlnet_model_name_or_path=hf-internal-testing/tiny-controlnet  | 
 | 70 | +            --max_train_steps=6  | 
 | 71 | +            --checkpointing_steps=2  | 
 | 72 | +            """.split()  | 
 | 73 | + | 
 | 74 | +            run_command(self._launch_args + test_args)  | 
 | 75 | + | 
 | 76 | +            self.assertEqual(  | 
 | 77 | +                {x for x in os.listdir(tmpdir) if "checkpoint" in x},  | 
 | 78 | +                {"checkpoint-2", "checkpoint-4", "checkpoint-6"},  | 
 | 79 | +            )  | 
 | 80 | + | 
 | 81 | +            resume_run_args = f"""  | 
 | 82 | +            examples/diffusers/controlnet/train_controlnet.py  | 
 | 83 | +            --pretrained_model_name_or_path=hf-internal-testing/tiny-stable-diffusion-pipe  | 
 | 84 | +            --revision refs/pr/4  | 
 | 85 | +            --dataset_name=hf-internal-testing/fill10  | 
 | 86 | +            --output_dir={tmpdir}  | 
 | 87 | +            --resolution=64  | 
 | 88 | +            --train_batch_size=1  | 
 | 89 | +            --gradient_accumulation_steps=1  | 
 | 90 | +            --controlnet_model_name_or_path=hf-internal-testing/tiny-controlnet  | 
 | 91 | +            --max_train_steps=8  | 
 | 92 | +            --checkpointing_steps=2  | 
 | 93 | +            --resume_from_checkpoint=checkpoint-6  | 
 | 94 | +            --checkpoints_total_limit=2  | 
 | 95 | +            """.split()  | 
 | 96 | + | 
 | 97 | +            run_command(self._launch_args + resume_run_args)  | 
 | 98 | + | 
 | 99 | +            self.assertEqual({x for x in os.listdir(tmpdir) if "checkpoint" in x}, {"checkpoint-6", "checkpoint-8"})  | 
 | 100 | + | 
 | 101 | + | 
 | 102 | +class ControlNetSDXL(ExamplesTests):  | 
 | 103 | +    def test_controlnet_sdxl(self):  | 
 | 104 | +        with tempfile.TemporaryDirectory() as tmpdir:  | 
 | 105 | +            test_args = f"""  | 
 | 106 | +            examples/diffusers/controlnet/train_controlnet_sdxl.py  | 
 | 107 | +            --pretrained_model_name_or_path=hf-internal-testing/tiny-stable-diffusion-xl-pipe  | 
 | 108 | +            --revision refs/pr/2  | 
 | 109 | +            --dataset_name=hf-internal-testing/fill10  | 
 | 110 | +            --output_dir={tmpdir}  | 
 | 111 | +            --resolution=64  | 
 | 112 | +            --train_batch_size=1  | 
 | 113 | +            --gradient_accumulation_steps=1  | 
 | 114 | +            --controlnet_model_name_or_path=hf-internal-testing/tiny-controlnet-sdxl  | 
 | 115 | +            --max_train_steps=4  | 
 | 116 | +            --checkpointing_steps=2  | 
 | 117 | +            """.split()  | 
 | 118 | + | 
 | 119 | +            run_command(self._launch_args + test_args)  | 
 | 120 | + | 
 | 121 | +            self.assertTrue(os.path.isfile(os.path.join(tmpdir, "diffusion_pytorch_model.safetensors")))  | 
 | 122 | + | 
 | 123 | + | 
 | 124 | +class ControlNetflux(ExamplesTests):  | 
 | 125 | +    def test_controlnet_flux(self):  | 
 | 126 | +        with tempfile.TemporaryDirectory() as tmpdir:  | 
 | 127 | +            test_args = f"""  | 
 | 128 | +            examples/diffusers/controlnet/train_controlnet_flux.py  | 
 | 129 | +            --pretrained_model_name_or_path=hf-internal-testing/tiny-flux-pipe  | 
 | 130 | +            --output_dir={tmpdir}  | 
 | 131 | +            --dataset_name=hf-internal-testing/fill10  | 
 | 132 | +            --conditioning_image_column=conditioning_image  | 
 | 133 | +            --image_column=image  | 
 | 134 | +            --caption_column=text  | 
 | 135 | +            --resolution=64  | 
 | 136 | +            --train_batch_size=1  | 
 | 137 | +            --gradient_accumulation_steps=1  | 
 | 138 | +            --max_train_steps=4  | 
 | 139 | +            --checkpointing_steps=2  | 
 | 140 | +            --num_double_layers=1  | 
 | 141 | +            --num_single_layers=1  | 
 | 142 | +            """.split()  | 
 | 143 | + | 
 | 144 | +            run_command(self._launch_args + test_args)  | 
 | 145 | + | 
 | 146 | +            self.assertTrue(os.path.isfile(os.path.join(tmpdir, "diffusion_pytorch_model.safetensors")))  | 
0 commit comments