CoolFace
Apppublic

chendl/compositional_test

sourceHugging Faceupdated 3y agoView on Hugging Face
1likes
test_check_dummies.py127 linesDownload Raw Back to repo_utils
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 at6#7#     http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# 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 and13# limitations under the License.14 15import os16import sys17import unittest18 19 20git_repo_path = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))21sys.path.append(os.path.join(git_repo_path, "utils"))22 23import check_dummies  # noqa: E40224from check_dummies import create_dummy_files, create_dummy_object, find_backend, read_init  # noqa: E40225 26 27# Align TRANSFORMERS_PATH in check_dummies with the current path28check_dummies.PATH_TO_TRANSFORMERS = os.path.join(git_repo_path, "src", "transformers")29 30DUMMY_CONSTANT = """31{0} = None32"""33 34DUMMY_CLASS = """35class {0}(metaclass=DummyObject):36    _backends = {1}37 38    def __init__(self, *args, **kwargs):39        requires_backends(self, {1})40"""41 42 43DUMMY_FUNCTION = """44def {0}(*args, **kwargs):45    requires_backends({0}, {1})46"""47 48 49class CheckDummiesTester(unittest.TestCase):50    def test_find_backend(self):51        no_backend = find_backend('    _import_structure["models.albert"].append("AlbertTokenizerFast")')52        self.assertIsNone(no_backend)53 54        simple_backend = find_backend("    if not is_tokenizers_available():")55        self.assertEqual(simple_backend, "tokenizers")56 57        backend_with_underscore = find_backend("    if not is_tensorflow_text_available():")58        self.assertEqual(backend_with_underscore, "tensorflow_text")59 60        double_backend = find_backend("    if not (is_sentencepiece_available() and is_tokenizers_available()):")61        self.assertEqual(double_backend, "sentencepiece_and_tokenizers")62 63        double_backend_with_underscore = find_backend(64            "    if not (is_sentencepiece_available() and is_tensorflow_text_available()):"65        )66        self.assertEqual(double_backend_with_underscore, "sentencepiece_and_tensorflow_text")67 68        triple_backend = find_backend(69            "    if not (is_sentencepiece_available() and is_tokenizers_available() and is_vision_available()):"70        )71        self.assertEqual(triple_backend, "sentencepiece_and_tokenizers_and_vision")72 73    def test_read_init(self):74        objects = read_init()75        # We don't assert on the exact list of keys to allow for smooth grow of backend-specific objects76        self.assertIn("torch", objects)77        self.assertIn("tensorflow_text", objects)78        self.assertIn("sentencepiece_and_tokenizers", objects)79 80        # Likewise, we can't assert on the exact content of a key81        self.assertIn("BertModel", objects["torch"])82        self.assertIn("TFBertModel", objects["tf"])83        self.assertIn("FlaxBertModel", objects["flax"])84        self.assertIn("BertModel", objects["torch"])85        self.assertIn("TFBertTokenizer", objects["tensorflow_text"])86        self.assertIn("convert_slow_tokenizer", objects["sentencepiece_and_tokenizers"])87 88    def test_create_dummy_object(self):89        dummy_constant = create_dummy_object("CONSTANT", "'torch'")90        self.assertEqual(dummy_constant, "\nCONSTANT = None\n")91 92        dummy_function = create_dummy_object("function", "'torch'")93        self.assertEqual(94            dummy_function, "\ndef function(*args, **kwargs):\n    requires_backends(function, 'torch')\n"95        )96 97        expected_dummy_class = """98class FakeClass(metaclass=DummyObject):99    _backends = 'torch'100 101    def __init__(self, *args, **kwargs):102        requires_backends(self, 'torch')103"""104        dummy_class = create_dummy_object("FakeClass", "'torch'")105        self.assertEqual(dummy_class, expected_dummy_class)106 107    def test_create_dummy_files(self):108        expected_dummy_pytorch_file = """# This file is autogenerated by the command `make fix-copies`, do not edit.109from ..utils import DummyObject, requires_backends110 111 112CONSTANT = None113 114 115def function(*args, **kwargs):116    requires_backends(function, ["torch"])117 118 119class FakeClass(metaclass=DummyObject):120    _backends = ["torch"]121 122    def __init__(self, *args, **kwargs):123        requires_backends(self, ["torch"])124"""125        dummy_files = create_dummy_files({"torch": ["CONSTANT", "function", "FakeClass"]})126        self.assertEqual(dummy_files["torch"], expected_dummy_pytorch_file)127