Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
shivamshrirao
GitHub Repository: shivamshrirao/diffusers
Path: blob/main/tests/repo_utils/test_check_dummies.py
1441 views
1
# Copyright 2023 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 # noqa: E402
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
from ..utils import DummyObject, requires_backends
98
99
100
CONSTANT = None
101
102
103
def function(*args, **kwargs):
104
requires_backends(function, ["torch"])
105
106
107
class FakeClass(metaclass=DummyObject):
108
_backends = ["torch"]
109
110
def __init__(self, *args, **kwargs):
111
requires_backends(self, ["torch"])
112
113
@classmethod
114
def from_config(cls, *args, **kwargs):
115
requires_backends(cls, ["torch"])
116
117
@classmethod
118
def from_pretrained(cls, *args, **kwargs):
119
requires_backends(cls, ["torch"])
120
"""
121
dummy_files = create_dummy_files({"torch": ["CONSTANT", "function", "FakeClass"]})
122
self.assertEqual(dummy_files["torch"], expected_dummy_pytorch_file)
123
124