|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2022 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 | + |
| 17 | +import logging |
| 18 | +import os |
| 19 | +import shutil |
| 20 | +import subprocess |
| 21 | +import sys |
| 22 | +import tempfile |
| 23 | +import unittest |
| 24 | +from typing import List |
| 25 | + |
| 26 | +from accelerate.utils import write_basic_config |
| 27 | +from diffusers.testing_utils import slow |
| 28 | + |
| 29 | + |
| 30 | +logging.basicConfig(level=logging.DEBUG) |
| 31 | + |
| 32 | +logger = logging.getLogger() |
| 33 | + |
| 34 | + |
| 35 | +# These utils relate to ensuring the right error message is received when running scripts |
| 36 | +class SubprocessCallException(Exception): |
| 37 | + pass |
| 38 | + |
| 39 | + |
| 40 | +def run_command(command: List[str], return_stdout=False): |
| 41 | + """ |
| 42 | + Runs `command` with `subprocess.check_output` and will potentially return the `stdout`. Will also properly capture |
| 43 | + if an error occured while running `command` |
| 44 | + """ |
| 45 | + try: |
| 46 | + output = subprocess.check_output(command, stderr=subprocess.STDOUT) |
| 47 | + if return_stdout: |
| 48 | + if hasattr(output, "decode"): |
| 49 | + output = output.decode("utf-8") |
| 50 | + return output |
| 51 | + except subprocess.CalledProcessError as e: |
| 52 | + raise SubprocessCallException( |
| 53 | + f"Command `{' '.join(command)}` failed with the following error:\n\n{e.output.decode()}" |
| 54 | + ) from e |
| 55 | + |
| 56 | + |
| 57 | +stream_handler = logging.StreamHandler(sys.stdout) |
| 58 | +logger.addHandler(stream_handler) |
| 59 | + |
| 60 | + |
| 61 | +class ExamplesTestsAccelerate(unittest.TestCase): |
| 62 | + @classmethod |
| 63 | + def setUpClass(cls): |
| 64 | + super().setUpClass() |
| 65 | + cls._tmpdir = tempfile.mkdtemp() |
| 66 | + cls.configPath = os.path.join(cls._tmpdir, "default_config.yml") |
| 67 | + |
| 68 | + write_basic_config(save_location=cls.configPath) |
| 69 | + cls._launch_args = ["accelerate", "launch", "--config_file", cls.configPath] |
| 70 | + |
| 71 | + @classmethod |
| 72 | + def tearDownClass(cls): |
| 73 | + super().tearDownClass() |
| 74 | + shutil.rmtree(cls._tmpdir) |
| 75 | + |
| 76 | + @slow |
| 77 | + def test_train_unconditional(self): |
| 78 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 79 | + test_args = f""" |
| 80 | + examples/unconditional_image_generation/train_unconditional.py |
| 81 | + --dataset_name huggan/few-shot-aurora |
| 82 | + --resolution 64 |
| 83 | + --output_dir {tmpdir} |
| 84 | + --train_batch_size 4 |
| 85 | + --num_epochs 1 |
| 86 | + --gradient_accumulation_steps 1 |
| 87 | + --learning_rate 1e-3 |
| 88 | + --lr_warmup_steps 5 |
| 89 | + --mixed_precision fp16 |
| 90 | + """.split() |
| 91 | + |
| 92 | + run_command(self._launch_args + test_args, return_stdout=True) |
| 93 | + # save_pretrained smoke test |
| 94 | + self.assertTrue(os.path.isfile(os.path.join(tmpdir, "unet", "diffusion_pytorch_model.bin"))) |
| 95 | + self.assertTrue(os.path.isfile(os.path.join(tmpdir, "scheduler", "scheduler_config.json"))) |
| 96 | + # logging test |
| 97 | + self.assertTrue(len(os.listdir(os.path.join(tmpdir, "logs", "train_unconditional"))) > 0) |
| 98 | + |
| 99 | + @slow |
| 100 | + def test_textual_inversion(self): |
| 101 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 102 | + test_args = f""" |
| 103 | + examples/textual_inversion/textual_inversion.py |
| 104 | + --pretrained_model_name_or_path CompVis/stable-diffusion-v1-4 |
| 105 | + --use_auth_token |
| 106 | + --train_data_dir docs/source/imgs |
| 107 | + --learnable_property object |
| 108 | + --placeholder_token <cat-toy> |
| 109 | + --initializer_token toy |
| 110 | + --resolution 64 |
| 111 | + --train_batch_size 1 |
| 112 | + --gradient_accumulation_steps 2 |
| 113 | + --max_train_steps 10 |
| 114 | + --learning_rate 5.0e-04 |
| 115 | + --scale_lr |
| 116 | + --lr_scheduler constant |
| 117 | + --lr_warmup_steps 0 |
| 118 | + --output_dir {tmpdir} |
| 119 | + --mixed_precision fp16 |
| 120 | + """.split() |
| 121 | + |
| 122 | + run_command(self._launch_args + test_args) |
| 123 | + # save_pretrained smoke test |
| 124 | + self.assertTrue(os.path.isfile(os.path.join(tmpdir, "learned_embeds.bin"))) |
0 commit comments