|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2023 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 | +import safetensors |
| 22 | + |
| 23 | + |
| 24 | +sys.path.append("..") |
| 25 | +from test_examples_utils import ExamplesTestsAccelerate, run_command # noqa: E402 |
| 26 | + |
| 27 | + |
| 28 | +logging.basicConfig(level=logging.DEBUG) |
| 29 | + |
| 30 | +logger = logging.getLogger() |
| 31 | +stream_handler = logging.StreamHandler(sys.stdout) |
| 32 | +logger.addHandler(stream_handler) |
| 33 | + |
| 34 | + |
| 35 | +class TextToImageLCM(ExamplesTestsAccelerate): |
| 36 | + def test_text_to_image_lcm_lora_sdxl(self): |
| 37 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 38 | + test_args = f""" |
| 39 | + examples/consistency_distillation/train_lcm_distill_lora_sdxl.py |
| 40 | + --pretrained_teacher_model hf-internal-testing/tiny-stable-diffusion-xl-pipe |
| 41 | + --dataset_name hf-internal-testing/dummy_image_text_data |
| 42 | + --resolution 64 |
| 43 | + --lora_rank 4 |
| 44 | + --train_batch_size 1 |
| 45 | + --gradient_accumulation_steps 1 |
| 46 | + --max_train_steps 2 |
| 47 | + --learning_rate 5.0e-04 |
| 48 | + --scale_lr |
| 49 | + --lr_scheduler constant |
| 50 | + --lr_warmup_steps 0 |
| 51 | + --output_dir {tmpdir} |
| 52 | + """.split() |
| 53 | + |
| 54 | + run_command(self._launch_args + test_args) |
| 55 | + # save_pretrained smoke test |
| 56 | + self.assertTrue(os.path.isfile(os.path.join(tmpdir, "pytorch_lora_weights.safetensors"))) |
| 57 | + |
| 58 | + # make sure the state_dict has the correct naming in the parameters. |
| 59 | + lora_state_dict = safetensors.torch.load_file(os.path.join(tmpdir, "pytorch_lora_weights.safetensors")) |
| 60 | + is_lora = all("lora" in k for k in lora_state_dict.keys()) |
| 61 | + self.assertTrue(is_lora) |
| 62 | + |
| 63 | + def test_text_to_image_lcm_lora_sdxl_checkpointing(self): |
| 64 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 65 | + test_args = f""" |
| 66 | + examples/consistency_distillation/train_lcm_distill_lora_sdxl.py |
| 67 | + --pretrained_teacher_model hf-internal-testing/tiny-stable-diffusion-xl-pipe |
| 68 | + --dataset_name hf-internal-testing/dummy_image_text_data |
| 69 | + --resolution 64 |
| 70 | + --lora_rank 4 |
| 71 | + --train_batch_size 1 |
| 72 | + --gradient_accumulation_steps 1 |
| 73 | + --max_train_steps 7 |
| 74 | + --checkpointing_steps 2 |
| 75 | + --learning_rate 5.0e-04 |
| 76 | + --scale_lr |
| 77 | + --lr_scheduler constant |
| 78 | + --lr_warmup_steps 0 |
| 79 | + --output_dir {tmpdir} |
| 80 | + """.split() |
| 81 | + |
| 82 | + run_command(self._launch_args + test_args) |
| 83 | + |
| 84 | + self.assertEqual( |
| 85 | + {x for x in os.listdir(tmpdir) if "checkpoint" in x}, |
| 86 | + {"checkpoint-2", "checkpoint-4", "checkpoint-6"}, |
| 87 | + ) |
| 88 | + |
| 89 | + test_args = f""" |
| 90 | + examples/consistency_distillation/train_lcm_distill_lora_sdxl.py |
| 91 | + --pretrained_teacher_model hf-internal-testing/tiny-stable-diffusion-xl-pipe |
| 92 | + --dataset_name hf-internal-testing/dummy_image_text_data |
| 93 | + --resolution 64 |
| 94 | + --lora_rank 4 |
| 95 | + --train_batch_size 1 |
| 96 | + --gradient_accumulation_steps 1 |
| 97 | + --max_train_steps 9 |
| 98 | + --checkpointing_steps 2 |
| 99 | + --resume_from_checkpoint latest |
| 100 | + --learning_rate 5.0e-04 |
| 101 | + --scale_lr |
| 102 | + --lr_scheduler constant |
| 103 | + --lr_warmup_steps 0 |
| 104 | + --output_dir {tmpdir} |
| 105 | + """.split() |
| 106 | + |
| 107 | + run_command(self._launch_args + test_args) |
| 108 | + |
| 109 | + self.assertEqual( |
| 110 | + {x for x in os.listdir(tmpdir) if "checkpoint" in x}, |
| 111 | + {"checkpoint-2", "checkpoint-4", "checkpoint-6", "checkpoint-8"}, |
| 112 | + ) |
0 commit comments