|
| 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 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 DreamBoothLoRASDXLWithEDM(ExamplesTestsAccelerate): |
| 36 | + def test_dreambooth_lora_sdxl_with_edm(self): |
| 37 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 38 | + test_args = f""" |
| 39 | + examples/dreambooth/train_dreambooth_lora_sdxl.py |
| 40 | + --pretrained_model_name_or_path hf-internal-testing/tiny-stable-diffusion-xl-pipe |
| 41 | + --do_edm_style_training |
| 42 | + --instance_data_dir docs/source/en/imgs |
| 43 | + --instance_prompt photo |
| 44 | + --resolution 64 |
| 45 | + --train_batch_size 1 |
| 46 | + --gradient_accumulation_steps 1 |
| 47 | + --max_train_steps 2 |
| 48 | + --learning_rate 5.0e-04 |
| 49 | + --scale_lr |
| 50 | + --lr_scheduler constant |
| 51 | + --lr_warmup_steps 0 |
| 52 | + --output_dir {tmpdir} |
| 53 | + """.split() |
| 54 | + |
| 55 | + run_command(self._launch_args + test_args) |
| 56 | + # save_pretrained smoke test |
| 57 | + self.assertTrue(os.path.isfile(os.path.join(tmpdir, "pytorch_lora_weights.safetensors"))) |
| 58 | + |
| 59 | + # make sure the state_dict has the correct naming in the parameters. |
| 60 | + lora_state_dict = safetensors.torch.load_file(os.path.join(tmpdir, "pytorch_lora_weights.safetensors")) |
| 61 | + is_lora = all("lora" in k for k in lora_state_dict.keys()) |
| 62 | + self.assertTrue(is_lora) |
| 63 | + |
| 64 | + # when not training the text encoder, all the parameters in the state dict should start |
| 65 | + # with `"unet"` in their names. |
| 66 | + starts_with_unet = all(key.startswith("unet") for key in lora_state_dict.keys()) |
| 67 | + self.assertTrue(starts_with_unet) |
| 68 | + |
| 69 | + def test_dreambooth_lora_playground(self): |
| 70 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 71 | + test_args = f""" |
| 72 | + examples/dreambooth/train_dreambooth_lora_sdxl.py |
| 73 | + --pretrained_model_name_or_path hf-internal-testing/tiny-playground-v2-5-pipe |
| 74 | + --instance_data_dir docs/source/en/imgs |
| 75 | + --instance_prompt photo |
| 76 | + --resolution 64 |
| 77 | + --train_batch_size 1 |
| 78 | + --gradient_accumulation_steps 1 |
| 79 | + --max_train_steps 2 |
| 80 | + --learning_rate 5.0e-04 |
| 81 | + --scale_lr |
| 82 | + --lr_scheduler constant |
| 83 | + --lr_warmup_steps 0 |
| 84 | + --output_dir {tmpdir} |
| 85 | + """.split() |
| 86 | + |
| 87 | + run_command(self._launch_args + test_args) |
| 88 | + # save_pretrained smoke test |
| 89 | + self.assertTrue(os.path.isfile(os.path.join(tmpdir, "pytorch_lora_weights.safetensors"))) |
| 90 | + |
| 91 | + # make sure the state_dict has the correct naming in the parameters. |
| 92 | + lora_state_dict = safetensors.torch.load_file(os.path.join(tmpdir, "pytorch_lora_weights.safetensors")) |
| 93 | + is_lora = all("lora" in k for k in lora_state_dict.keys()) |
| 94 | + self.assertTrue(is_lora) |
| 95 | + |
| 96 | + # when not training the text encoder, all the parameters in the state dict should start |
| 97 | + # with `"unet"` in their names. |
| 98 | + starts_with_unet = all(key.startswith("unet") for key in lora_state_dict.keys()) |
| 99 | + self.assertTrue(starts_with_unet) |
0 commit comments