|
| 1 | +# Copyright 2022 The HuggingFace Team. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | +import sys |
| 17 | +import unittest |
| 18 | + |
| 19 | + |
| 20 | +git_repo_path = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) |
| 21 | +sys.path.append(os.path.join(git_repo_path, "utils")) |
| 22 | + |
| 23 | +import check_dummies |
| 24 | +from check_dummies import create_dummy_files, create_dummy_object, find_backend, read_init # noqa: E402 |
| 25 | + |
| 26 | + |
| 27 | +# Align TRANSFORMERS_PATH in check_dummies with the current path |
| 28 | +check_dummies.PATH_TO_DIFFUSERS = os.path.join(git_repo_path, "src", "diffusers") |
| 29 | + |
| 30 | + |
| 31 | +class CheckDummiesTester(unittest.TestCase): |
| 32 | + def test_find_backend(self): |
| 33 | + simple_backend = find_backend(" if not is_torch_available():") |
| 34 | + self.assertEqual(simple_backend, "torch") |
| 35 | + |
| 36 | + # backend_with_underscore = find_backend(" if not is_tensorflow_text_available():") |
| 37 | + # self.assertEqual(backend_with_underscore, "tensorflow_text") |
| 38 | + |
| 39 | + double_backend = find_backend(" if not (is_torch_available() and is_transformers_available()):") |
| 40 | + self.assertEqual(double_backend, "torch_and_transformers") |
| 41 | + |
| 42 | + # double_backend_with_underscore = find_backend( |
| 43 | + # " if not (is_sentencepiece_available() and is_tensorflow_text_available()):" |
| 44 | + # ) |
| 45 | + # self.assertEqual(double_backend_with_underscore, "sentencepiece_and_tensorflow_text") |
| 46 | + |
| 47 | + triple_backend = find_backend( |
| 48 | + " if not (is_torch_available() and is_transformers_available() and is_onnx_available()):" |
| 49 | + ) |
| 50 | + self.assertEqual(triple_backend, "torch_and_transformers_and_onnx") |
| 51 | + |
| 52 | + def test_read_init(self): |
| 53 | + objects = read_init() |
| 54 | + # We don't assert on the exact list of keys to allow for smooth grow of backend-specific objects |
| 55 | + self.assertIn("torch", objects) |
| 56 | + self.assertIn("torch_and_transformers", objects) |
| 57 | + self.assertIn("flax_and_transformers", objects) |
| 58 | + self.assertIn("torch_and_transformers_and_onnx", objects) |
| 59 | + |
| 60 | + # Likewise, we can't assert on the exact content of a key |
| 61 | + self.assertIn("UNet2DModel", objects["torch"]) |
| 62 | + self.assertIn("FlaxUNet2DConditionModel", objects["flax"]) |
| 63 | + self.assertIn("StableDiffusionPipeline", objects["torch_and_transformers"]) |
| 64 | + self.assertIn("FlaxStableDiffusionPipeline", objects["flax_and_transformers"]) |
| 65 | + self.assertIn("LMSDiscreteScheduler", objects["torch_and_scipy"]) |
| 66 | + self.assertIn("OnnxStableDiffusionPipeline", objects["torch_and_transformers_and_onnx"]) |
| 67 | + |
| 68 | + def test_create_dummy_object(self): |
| 69 | + dummy_constant = create_dummy_object("CONSTANT", "'torch'") |
| 70 | + self.assertEqual(dummy_constant, "\nCONSTANT = None\n") |
| 71 | + |
| 72 | + dummy_function = create_dummy_object("function", "'torch'") |
| 73 | + self.assertEqual( |
| 74 | + dummy_function, "\ndef function(*args, **kwargs):\n requires_backends(function, 'torch')\n" |
| 75 | + ) |
| 76 | + |
| 77 | + expected_dummy_class = """ |
| 78 | +class FakeClass(metaclass=DummyObject): |
| 79 | + _backends = 'torch' |
| 80 | +
|
| 81 | + def __init__(self, *args, **kwargs): |
| 82 | + requires_backends(self, 'torch') |
| 83 | +
|
| 84 | + @classmethod |
| 85 | + def from_config(cls, *args, **kwargs): |
| 86 | + requires_backends(cls, 'torch') |
| 87 | +
|
| 88 | + @classmethod |
| 89 | + def from_pretrained(cls, *args, **kwargs): |
| 90 | + requires_backends(cls, 'torch') |
| 91 | +""" |
| 92 | + dummy_class = create_dummy_object("FakeClass", "'torch'") |
| 93 | + self.assertEqual(dummy_class, expected_dummy_class) |
| 94 | + |
| 95 | + def test_create_dummy_files(self): |
| 96 | + expected_dummy_pytorch_file = """# This file is autogenerated by the command `make fix-copies`, do not edit. |
| 97 | +# flake8: noqa |
| 98 | +
|
| 99 | +from ..utils import DummyObject, requires_backends |
| 100 | +
|
| 101 | +
|
| 102 | +CONSTANT = None |
| 103 | +
|
| 104 | +
|
| 105 | +def function(*args, **kwargs): |
| 106 | + requires_backends(function, ["torch"]) |
| 107 | +
|
| 108 | +
|
| 109 | +class FakeClass(metaclass=DummyObject): |
| 110 | + _backends = ["torch"] |
| 111 | +
|
| 112 | + def __init__(self, *args, **kwargs): |
| 113 | + requires_backends(self, ["torch"]) |
| 114 | +
|
| 115 | + @classmethod |
| 116 | + def from_config(cls, *args, **kwargs): |
| 117 | + requires_backends(cls, ["torch"]) |
| 118 | +
|
| 119 | + @classmethod |
| 120 | + def from_pretrained(cls, *args, **kwargs): |
| 121 | + requires_backends(cls, ["torch"]) |
| 122 | +""" |
| 123 | + dummy_files = create_dummy_files({"torch": ["CONSTANT", "function", "FakeClass"]}) |
| 124 | + self.assertEqual(dummy_files["torch"], expected_dummy_pytorch_file) |
0 commit comments