|
| 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 DreamBoothLoRAHiDreamImage(ExamplesTestsAccelerate): |
| 36 | + instance_data_dir = "docs/source/en/imgs" |
| 37 | + pretrained_model_name_or_path = "hf-internal-testing/tiny-hidream-i1-pipe" |
| 38 | + text_encoder_4_path = "hf-internal-testing/tiny-random-LlamaForCausalLM" |
| 39 | + tokenizer_4_path = "hf-internal-testing/tiny-random-LlamaForCausalLM" |
| 40 | + script_path = "examples/dreambooth/train_dreambooth_lora_hidream.py" |
| 41 | + transformer_layer_type = "double_stream_blocks.0.block.attn1.to_k" |
| 42 | + |
| 43 | + def test_dreambooth_lora_hidream(self): |
| 44 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 45 | + test_args = f""" |
| 46 | + {self.script_path} |
| 47 | + --pretrained_model_name_or_path {self.pretrained_model_name_or_path} |
| 48 | + --pretrained_text_encoder_4_name_or_path {self.text_encoder_4_path} |
| 49 | + --pretrained_tokenizer_4_name_or_path {self.tokenizer_4_path} |
| 50 | + --instance_data_dir {self.instance_data_dir} |
| 51 | + --resolution 32 |
| 52 | + --train_batch_size 1 |
| 53 | + --gradient_accumulation_steps 1 |
| 54 | + --max_train_steps 2 |
| 55 | + --learning_rate 5.0e-04 |
| 56 | + --scale_lr |
| 57 | + --lr_scheduler constant |
| 58 | + --lr_warmup_steps 0 |
| 59 | + --output_dir {tmpdir} |
| 60 | + --max_sequence_length 16 |
| 61 | + """.split() |
| 62 | + |
| 63 | + test_args.extend(["--instance_prompt", ""]) |
| 64 | + run_command(self._launch_args + test_args) |
| 65 | + # save_pretrained smoke test |
| 66 | + self.assertTrue(os.path.isfile(os.path.join(tmpdir, "pytorch_lora_weights.safetensors"))) |
| 67 | + |
| 68 | + # make sure the state_dict has the correct naming in the parameters. |
| 69 | + lora_state_dict = safetensors.torch.load_file(os.path.join(tmpdir, "pytorch_lora_weights.safetensors")) |
| 70 | + is_lora = all("lora" in k for k in lora_state_dict.keys()) |
| 71 | + self.assertTrue(is_lora) |
| 72 | + |
| 73 | + # when not training the text encoder, all the parameters in the state dict should start |
| 74 | + # with `"transformer"` in their names. |
| 75 | + starts_with_transformer = all(key.startswith("transformer") for key in lora_state_dict.keys()) |
| 76 | + self.assertTrue(starts_with_transformer) |
| 77 | + |
| 78 | + def test_dreambooth_lora_latent_caching(self): |
| 79 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 80 | + test_args = f""" |
| 81 | + {self.script_path} |
| 82 | + --pretrained_model_name_or_path {self.pretrained_model_name_or_path} |
| 83 | + --pretrained_text_encoder_4_name_or_path {self.text_encoder_4_path} |
| 84 | + --pretrained_tokenizer_4_name_or_path {self.tokenizer_4_path} |
| 85 | + --instance_data_dir {self.instance_data_dir} |
| 86 | + --resolution 32 |
| 87 | + --train_batch_size 1 |
| 88 | + --gradient_accumulation_steps 1 |
| 89 | + --max_train_steps 2 |
| 90 | + --cache_latents |
| 91 | + --learning_rate 5.0e-04 |
| 92 | + --scale_lr |
| 93 | + --lr_scheduler constant |
| 94 | + --lr_warmup_steps 0 |
| 95 | + --output_dir {tmpdir} |
| 96 | + --max_sequence_length 16 |
| 97 | + """.split() |
| 98 | + |
| 99 | + test_args.extend(["--instance_prompt", ""]) |
| 100 | + run_command(self._launch_args + test_args) |
| 101 | + # save_pretrained smoke test |
| 102 | + self.assertTrue(os.path.isfile(os.path.join(tmpdir, "pytorch_lora_weights.safetensors"))) |
| 103 | + |
| 104 | + # make sure the state_dict has the correct naming in the parameters. |
| 105 | + lora_state_dict = safetensors.torch.load_file(os.path.join(tmpdir, "pytorch_lora_weights.safetensors")) |
| 106 | + is_lora = all("lora" in k for k in lora_state_dict.keys()) |
| 107 | + self.assertTrue(is_lora) |
| 108 | + |
| 109 | + # when not training the text encoder, all the parameters in the state dict should start |
| 110 | + # with `"transformer"` in their names. |
| 111 | + starts_with_transformer = all(key.startswith("transformer") for key in lora_state_dict.keys()) |
| 112 | + self.assertTrue(starts_with_transformer) |
| 113 | + |
| 114 | + def test_dreambooth_lora_layers(self): |
| 115 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 116 | + test_args = f""" |
| 117 | + {self.script_path} |
| 118 | + --pretrained_model_name_or_path {self.pretrained_model_name_or_path} |
| 119 | + --pretrained_text_encoder_4_name_or_path {self.text_encoder_4_path} |
| 120 | + --pretrained_tokenizer_4_name_or_path {self.tokenizer_4_path} |
| 121 | + --instance_data_dir {self.instance_data_dir} |
| 122 | + --resolution 32 |
| 123 | + --train_batch_size 1 |
| 124 | + --gradient_accumulation_steps 1 |
| 125 | + --max_train_steps 2 |
| 126 | + --cache_latents |
| 127 | + --learning_rate 5.0e-04 |
| 128 | + --scale_lr |
| 129 | + --lora_layers {self.transformer_layer_type} |
| 130 | + --lr_scheduler constant |
| 131 | + --lr_warmup_steps 0 |
| 132 | + --output_dir {tmpdir} |
| 133 | + --max_sequence_length 16 |
| 134 | + """.split() |
| 135 | + |
| 136 | + test_args.extend(["--instance_prompt", ""]) |
| 137 | + run_command(self._launch_args + test_args) |
| 138 | + # save_pretrained smoke test |
| 139 | + self.assertTrue(os.path.isfile(os.path.join(tmpdir, "pytorch_lora_weights.safetensors"))) |
| 140 | + |
| 141 | + # make sure the state_dict has the correct naming in the parameters. |
| 142 | + lora_state_dict = safetensors.torch.load_file(os.path.join(tmpdir, "pytorch_lora_weights.safetensors")) |
| 143 | + is_lora = all("lora" in k for k in lora_state_dict.keys()) |
| 144 | + self.assertTrue(is_lora) |
| 145 | + |
| 146 | + # when not training the text encoder, all the parameters in the state dict should start |
| 147 | + # with `"transformer"` in their names. In this test, we only params of |
| 148 | + # `self.transformer_layer_type` should be in the state dict. |
| 149 | + starts_with_transformer = all(self.transformer_layer_type in key for key in lora_state_dict) |
| 150 | + self.assertTrue(starts_with_transformer) |
| 151 | + |
| 152 | + def test_dreambooth_lora_hidream_checkpointing_checkpoints_total_limit(self): |
| 153 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 154 | + test_args = f""" |
| 155 | + {self.script_path} |
| 156 | + --pretrained_model_name_or_path={self.pretrained_model_name_or_path} |
| 157 | + --pretrained_text_encoder_4_name_or_path {self.text_encoder_4_path} |
| 158 | + --pretrained_tokenizer_4_name_or_path {self.tokenizer_4_path} |
| 159 | + --instance_data_dir={self.instance_data_dir} |
| 160 | + --output_dir={tmpdir} |
| 161 | + --resolution=32 |
| 162 | + --train_batch_size=1 |
| 163 | + --gradient_accumulation_steps=1 |
| 164 | + --max_train_steps=6 |
| 165 | + --checkpoints_total_limit=2 |
| 166 | + --checkpointing_steps=2 |
| 167 | + --max_sequence_length 16 |
| 168 | + """.split() |
| 169 | + |
| 170 | + test_args.extend(["--instance_prompt", ""]) |
| 171 | + run_command(self._launch_args + test_args) |
| 172 | + |
| 173 | + self.assertEqual( |
| 174 | + {x for x in os.listdir(tmpdir) if "checkpoint" in x}, |
| 175 | + {"checkpoint-4", "checkpoint-6"}, |
| 176 | + ) |
| 177 | + |
| 178 | + def test_dreambooth_lora_hidream_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self): |
| 179 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 180 | + test_args = f""" |
| 181 | + {self.script_path} |
| 182 | + --pretrained_model_name_or_path={self.pretrained_model_name_or_path} |
| 183 | + --pretrained_text_encoder_4_name_or_path {self.text_encoder_4_path} |
| 184 | + --pretrained_tokenizer_4_name_or_path {self.tokenizer_4_path} |
| 185 | + --instance_data_dir={self.instance_data_dir} |
| 186 | + --output_dir={tmpdir} |
| 187 | + --resolution=32 |
| 188 | + --train_batch_size=1 |
| 189 | + --gradient_accumulation_steps=1 |
| 190 | + --max_train_steps=4 |
| 191 | + --checkpointing_steps=2 |
| 192 | + --max_sequence_length 16 |
| 193 | + """.split() |
| 194 | + |
| 195 | + test_args.extend(["--instance_prompt", ""]) |
| 196 | + run_command(self._launch_args + test_args) |
| 197 | + |
| 198 | + self.assertEqual({x for x in os.listdir(tmpdir) if "checkpoint" in x}, {"checkpoint-2", "checkpoint-4"}) |
| 199 | + |
| 200 | + resume_run_args = f""" |
| 201 | + {self.script_path} |
| 202 | + --pretrained_model_name_or_path={self.pretrained_model_name_or_path} |
| 203 | + --pretrained_text_encoder_4_name_or_path {self.text_encoder_4_path} |
| 204 | + --pretrained_tokenizer_4_name_or_path {self.tokenizer_4_path} |
| 205 | + --instance_data_dir={self.instance_data_dir} |
| 206 | + --output_dir={tmpdir} |
| 207 | + --resolution=32 |
| 208 | + --train_batch_size=1 |
| 209 | + --gradient_accumulation_steps=1 |
| 210 | + --max_train_steps=8 |
| 211 | + --checkpointing_steps=2 |
| 212 | + --resume_from_checkpoint=checkpoint-4 |
| 213 | + --checkpoints_total_limit=2 |
| 214 | + --max_sequence_length 16 |
| 215 | + """.split() |
| 216 | + |
| 217 | + resume_run_args.extend(["--instance_prompt", ""]) |
| 218 | + run_command(self._launch_args + resume_run_args) |
| 219 | + |
| 220 | + self.assertEqual({x for x in os.listdir(tmpdir) if "checkpoint" in x}, {"checkpoint-6", "checkpoint-8"}) |
0 commit comments