Path: blob/main/tests/repo_utils/test_check_dummies.py
1441 views
# Copyright 2023 The HuggingFace Team. All rights reserved.1#2# Licensed under the Apache License, Version 2.0 (the "License");3# you may not use this file except in compliance with the License.4# You may obtain a copy of the License at5#6# http://www.apache.org/licenses/LICENSE-2.07#8# Unless required by applicable law or agreed to in writing, software9# distributed under the License is distributed on an "AS IS" BASIS,10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11# See the License for the specific language governing permissions and12# limitations under the License.1314import os15import sys16import unittest171819git_repo_path = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))20sys.path.append(os.path.join(git_repo_path, "utils"))2122import check_dummies # noqa: E40223from check_dummies import create_dummy_files, create_dummy_object, find_backend, read_init # noqa: E402242526# Align TRANSFORMERS_PATH in check_dummies with the current path27check_dummies.PATH_TO_DIFFUSERS = os.path.join(git_repo_path, "src", "diffusers")282930class CheckDummiesTester(unittest.TestCase):31def test_find_backend(self):32simple_backend = find_backend(" if not is_torch_available():")33self.assertEqual(simple_backend, "torch")3435# backend_with_underscore = find_backend(" if not is_tensorflow_text_available():")36# self.assertEqual(backend_with_underscore, "tensorflow_text")3738double_backend = find_backend(" if not (is_torch_available() and is_transformers_available()):")39self.assertEqual(double_backend, "torch_and_transformers")4041# double_backend_with_underscore = find_backend(42# " if not (is_sentencepiece_available() and is_tensorflow_text_available()):"43# )44# self.assertEqual(double_backend_with_underscore, "sentencepiece_and_tensorflow_text")4546triple_backend = find_backend(47" if not (is_torch_available() and is_transformers_available() and is_onnx_available()):"48)49self.assertEqual(triple_backend, "torch_and_transformers_and_onnx")5051def test_read_init(self):52objects = read_init()53# We don't assert on the exact list of keys to allow for smooth grow of backend-specific objects54self.assertIn("torch", objects)55self.assertIn("torch_and_transformers", objects)56self.assertIn("flax_and_transformers", objects)57self.assertIn("torch_and_transformers_and_onnx", objects)5859# Likewise, we can't assert on the exact content of a key60self.assertIn("UNet2DModel", objects["torch"])61self.assertIn("FlaxUNet2DConditionModel", objects["flax"])62self.assertIn("StableDiffusionPipeline", objects["torch_and_transformers"])63self.assertIn("FlaxStableDiffusionPipeline", objects["flax_and_transformers"])64self.assertIn("LMSDiscreteScheduler", objects["torch_and_scipy"])65self.assertIn("OnnxStableDiffusionPipeline", objects["torch_and_transformers_and_onnx"])6667def test_create_dummy_object(self):68dummy_constant = create_dummy_object("CONSTANT", "'torch'")69self.assertEqual(dummy_constant, "\nCONSTANT = None\n")7071dummy_function = create_dummy_object("function", "'torch'")72self.assertEqual(73dummy_function, "\ndef function(*args, **kwargs):\n requires_backends(function, 'torch')\n"74)7576expected_dummy_class = """77class FakeClass(metaclass=DummyObject):78_backends = 'torch'7980def __init__(self, *args, **kwargs):81requires_backends(self, 'torch')8283@classmethod84def from_config(cls, *args, **kwargs):85requires_backends(cls, 'torch')8687@classmethod88def from_pretrained(cls, *args, **kwargs):89requires_backends(cls, 'torch')90"""91dummy_class = create_dummy_object("FakeClass", "'torch'")92self.assertEqual(dummy_class, expected_dummy_class)9394def test_create_dummy_files(self):95expected_dummy_pytorch_file = """# This file is autogenerated by the command `make fix-copies`, do not edit.96from ..utils import DummyObject, requires_backends979899CONSTANT = None100101102def function(*args, **kwargs):103requires_backends(function, ["torch"])104105106class FakeClass(metaclass=DummyObject):107_backends = ["torch"]108109def __init__(self, *args, **kwargs):110requires_backends(self, ["torch"])111112@classmethod113def from_config(cls, *args, **kwargs):114requires_backends(cls, ["torch"])115116@classmethod117def from_pretrained(cls, *args, **kwargs):118requires_backends(cls, ["torch"])119"""120dummy_files = create_dummy_files({"torch": ["CONSTANT", "function", "FakeClass"]})121self.assertEqual(dummy_files["torch"], expected_dummy_pytorch_file)122123124