|
| 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 | + |
| 22 | +sys.path.append("..") |
| 23 | +from test_examples_utils import ExamplesTestsAccelerate, run_command # noqa: E402 |
| 24 | + |
| 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 TextualInversionSdxl(ExamplesTestsAccelerate): |
| 34 | + def test_textual_inversion_sdxl(self): |
| 35 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 36 | + test_args = f""" |
| 37 | + examples/textual_inversion/textual_inversion_sdxl.py |
| 38 | + --pretrained_model_name_or_path hf-internal-testing/tiny-sdxl-pipe |
| 39 | + --train_data_dir docs/source/en/imgs |
| 40 | + --learnable_property object |
| 41 | + --placeholder_token <cat-toy> |
| 42 | + --initializer_token a |
| 43 | + --save_steps 1 |
| 44 | + --num_vectors 2 |
| 45 | + --resolution 64 |
| 46 | + --train_batch_size 1 |
| 47 | + --gradient_accumulation_steps 1 |
| 48 | + --max_train_steps 2 |
| 49 | + --learning_rate 5.0e-04 |
| 50 | + --scale_lr |
| 51 | + --lr_scheduler constant |
| 52 | + --lr_warmup_steps 0 |
| 53 | + --output_dir {tmpdir} |
| 54 | + """.split() |
| 55 | + |
| 56 | + run_command(self._launch_args + test_args) |
| 57 | + # save_pretrained smoke test |
| 58 | + self.assertTrue(os.path.isfile(os.path.join(tmpdir, "learned_embeds.safetensors"))) |
| 59 | + |
| 60 | + def test_textual_inversion_sdxl_checkpointing(self): |
| 61 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 62 | + test_args = f""" |
| 63 | + examples/textual_inversion/textual_inversion_sdxl.py |
| 64 | + --pretrained_model_name_or_path hf-internal-testing/tiny-sdxl-pipe |
| 65 | + --train_data_dir docs/source/en/imgs |
| 66 | + --learnable_property object |
| 67 | + --placeholder_token <cat-toy> |
| 68 | + --initializer_token a |
| 69 | + --save_steps 1 |
| 70 | + --num_vectors 2 |
| 71 | + --resolution 64 |
| 72 | + --train_batch_size 1 |
| 73 | + --gradient_accumulation_steps 1 |
| 74 | + --max_train_steps 3 |
| 75 | + --learning_rate 5.0e-04 |
| 76 | + --scale_lr |
| 77 | + --lr_scheduler constant |
| 78 | + --lr_warmup_steps 0 |
| 79 | + --output_dir {tmpdir} |
| 80 | + --checkpointing_steps=1 |
| 81 | + --checkpoints_total_limit=2 |
| 82 | + """.split() |
| 83 | + |
| 84 | + run_command(self._launch_args + test_args) |
| 85 | + |
| 86 | + # check checkpoint directories exist |
| 87 | + self.assertEqual( |
| 88 | + {x for x in os.listdir(tmpdir) if "checkpoint" in x}, |
| 89 | + {"checkpoint-2", "checkpoint-3"}, |
| 90 | + ) |
| 91 | + |
| 92 | + def test_textual_inversion_sdxl_checkpointing_checkpoints_total_limit_removes_multiple_checkpoints(self): |
| 93 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 94 | + test_args = f""" |
| 95 | + examples/textual_inversion/textual_inversion_sdxl.py |
| 96 | + --pretrained_model_name_or_path hf-internal-testing/tiny-sdxl-pipe |
| 97 | + --train_data_dir docs/source/en/imgs |
| 98 | + --learnable_property object |
| 99 | + --placeholder_token <cat-toy> |
| 100 | + --initializer_token a |
| 101 | + --save_steps 1 |
| 102 | + --num_vectors 2 |
| 103 | + --resolution 64 |
| 104 | + --train_batch_size 1 |
| 105 | + --gradient_accumulation_steps 1 |
| 106 | + --max_train_steps 2 |
| 107 | + --learning_rate 5.0e-04 |
| 108 | + --scale_lr |
| 109 | + --lr_scheduler constant |
| 110 | + --lr_warmup_steps 0 |
| 111 | + --output_dir {tmpdir} |
| 112 | + --checkpointing_steps=1 |
| 113 | + """.split() |
| 114 | + |
| 115 | + run_command(self._launch_args + test_args) |
| 116 | + |
| 117 | + # check checkpoint directories exist |
| 118 | + self.assertEqual( |
| 119 | + {x for x in os.listdir(tmpdir) if "checkpoint" in x}, |
| 120 | + {"checkpoint-1", "checkpoint-2"}, |
| 121 | + ) |
| 122 | + |
| 123 | + resume_run_args = f""" |
| 124 | + examples/textual_inversion/textual_inversion_sdxl.py |
| 125 | + --pretrained_model_name_or_path hf-internal-testing/tiny-sdxl-pipe |
| 126 | + --train_data_dir docs/source/en/imgs |
| 127 | + --learnable_property object |
| 128 | + --placeholder_token <cat-toy> |
| 129 | + --initializer_token a |
| 130 | + --save_steps 1 |
| 131 | + --num_vectors 2 |
| 132 | + --resolution 64 |
| 133 | + --train_batch_size 1 |
| 134 | + --gradient_accumulation_steps 1 |
| 135 | + --max_train_steps 2 |
| 136 | + --learning_rate 5.0e-04 |
| 137 | + --scale_lr |
| 138 | + --lr_scheduler constant |
| 139 | + --lr_warmup_steps 0 |
| 140 | + --output_dir {tmpdir} |
| 141 | + --checkpointing_steps=1 |
| 142 | + --resume_from_checkpoint=checkpoint-2 |
| 143 | + --checkpoints_total_limit=2 |
| 144 | + """.split() |
| 145 | + |
| 146 | + run_command(self._launch_args + resume_run_args) |
| 147 | + |
| 148 | + # check checkpoint directories exist |
| 149 | + self.assertEqual( |
| 150 | + {x for x in os.listdir(tmpdir) if "checkpoint" in x}, |
| 151 | + {"checkpoint-2", "checkpoint-3"}, |
| 152 | + ) |
0 commit comments