check_dummies.py 6.38 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# coding=utf-8
# Copyright 2020 The HuggingFace Inc. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
import os
import re


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_dummies.py
PATH_TO_TRANSFORMERS = "src/transformers"

_re_single_line_import = re.compile(r"\s+from\s+\S*\s+import\s+([^\(\s].*)\n")
26
_re_test_backend = re.compile(r"^\s+if\s+is\_([a-z]*)\_available\(\):\s*$")
27
28


29
BACKENDS = ["torch", "tf", "flax", "sentencepiece", "tokenizers", "vision"]
30

31

32
33
DUMMY_CONSTANT = """
{0} = None
34
35
"""

36
DUMMY_PRETRAINED_CLASS = """
37
38
class {0}:
    def __init__(self, *args, **kwargs):
39
        requires_{1}(self)
40
41
42

    @classmethod
    def from_pretrained(self, *args, **kwargs):
43
        requires_{1}(self)
44
45
"""

46
DUMMY_CLASS = """
47
48
class {0}:
    def __init__(self, *args, **kwargs):
49
        requires_{1}(self)
50
51
"""

52
DUMMY_FUNCTION = """
53
def {0}(*args, **kwargs):
54
    requires_{1}({0})
55
56
57
"""


58
def read_init():
Julien Chaumond's avatar
Julien Chaumond committed
59
    """ Read the init and extracts PyTorch, TensorFlow, SentencePiece and Tokenizers objects. """
60
    with open(os.path.join(PATH_TO_TRANSFORMERS, "__init__.py"), "r", encoding="utf-8", newline="\n") as f:
61
62
        lines = f.readlines()

63
    # Get to the point we do the actual imports for type checking
64
    line_index = 0
65
    while not lines[line_index].startswith("if TYPE_CHECKING"):
66
67
        line_index += 1

68
69
70
    backend_specific_objects = {}
    # Go through the end of the file
    while line_index < len(lines):
71
        # If the line is an if is_backend_available, we grab all objects associated.
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
        if _re_test_backend.search(lines[line_index]) is not None:
            backend = _re_test_backend.search(lines[line_index]).groups()[0]
            line_index += 1

            # Ignore if backend isn't tracked for dummies.
            if backend not in BACKENDS:
                continue

            objects = []
            # Until we unindent, add backend objects to the list
            while len(lines[line_index]) <= 1 or lines[line_index].startswith(" " * 8):
                line = lines[line_index]
                single_line_import_search = _re_single_line_import.search(line)
                if single_line_import_search is not None:
                    objects.extend(single_line_import_search.groups()[0].split(", "))
                elif line.startswith(" " * 12):
                    objects.append(line[12:-2])
                line_index += 1

            backend_specific_objects[backend] = objects
        else:
            line_index += 1
94

95
    return backend_specific_objects
96
97


98
def create_dummy_object(name, backend_name):
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
    """ Create the code for the dummy object corresponding to `name`."""
    _pretrained = [
        "Config" "ForCausalLM",
        "ForConditionalGeneration",
        "ForMaskedLM",
        "ForMultipleChoice",
        "ForQuestionAnswering",
        "ForSequenceClassification",
        "ForTokenClassification",
        "Model",
        "Tokenizer",
    ]
    if name.isupper():
        return DUMMY_CONSTANT.format(name)
    elif name.islower():
114
        return DUMMY_FUNCTION.format(name, backend_name)
115
116
117
118
119
120
121
    else:
        is_pretrained = False
        for part in _pretrained:
            if part in name:
                is_pretrained = True
                break
        if is_pretrained:
122
            return DUMMY_PRETRAINED_CLASS.format(name, backend_name)
123
        else:
124
            return DUMMY_CLASS.format(name, backend_name)
125
126
127
128


def create_dummy_files():
    """ Create the content of the dummy files. """
129
130
131
132
    backend_specific_objects = read_init()
    # For special correspondence backend to module name as used in the function requires_modulename
    module_names = {"torch": "pytorch"}
    dummy_files = {}
133

134
135
136
137
138
139
    for backend, objects in backend_specific_objects.items():
        backend_name = module_names.get(backend, backend)
        dummy_file = "# This file is autogenerated by the command `make fix-copies`, do not edit.\n"
        dummy_file += f"from ..file_utils import requires_{backend_name}\n\n"
        dummy_file += "\n".join([create_dummy_object(o, backend_name) for o in objects])
        dummy_files[backend] = dummy_file
140

141
    return dummy_files
142
143
144
145


def check_dummies(overwrite=False):
    """ Check if the dummy files are up to date and maybe `overwrite` with the right content. """
146
147
148
149
150
    dummy_files = create_dummy_files()
    # For special correspondence backend to shortcut as used in utils/dummy_xxx_objects.py
    short_names = {"torch": "pt"}

    # Locate actual dummy modules and read their content.
151
    path = os.path.join(PATH_TO_TRANSFORMERS, "utils")
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
    dummy_file_paths = {
        backend: os.path.join(path, f"dummy_{short_names.get(backend, backend)}_objects.py")
        for backend in dummy_files.keys()
    }

    actual_dummies = {}
    for backend, file_path in dummy_file_paths.items():
        with open(file_path, "r", encoding="utf-8", newline="\n") as f:
            actual_dummies[backend] = f.read()

    for backend in dummy_files.keys():
        if dummy_files[backend] != actual_dummies[backend]:
            if overwrite:
                print(
                    f"Updating transformers.utils.dummy_{short_names.get(backend, backend)}_objects.py as the main "
                    "__init__ has new objects."
                )
                with open(dummy_file_paths[backend], "w", encoding="utf-8", newline="\n") as f:
                    f.write(dummy_files[backend])
            else:
                raise ValueError(
                    "The main __init__ has objects that are not present in "
                    f"transformers.utils.dummy_{short_names.get(backend, backend)}_objects.py. Run `make fix-copies` "
                    "to fix this."
                )
177

178
179
180
181
182
183
184

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--fix_and_overwrite", action="store_true", help="Whether to fix inconsistencies.")
    args = parser.parse_args()

    check_dummies(args.fix_and_overwrite)