check_copies.py 30.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 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.
15
16
17
"""
Utility that checks whether the copies defined in the library match the original or not. This includes:
- All code commented with `# Copied from` comments,
18
- The list of models in the main README.md matches the ones in the localized READMEs,
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
- Files that are registered as full copies of one another in the `FULL_COPIES` constant of this script.

This also checks the list of models in the README is complete (has all models) and add a line to complete if there is
a model missing.

Use from the root of the repo with:

```bash
python utils/check_copies.py
```

for a check that will error in case of inconsistencies (used by `make repo-consistency`) or

```bash
python utils/check_copies.py --fix_and_overwrite
```

for a check that will fix all inconsistencies automatically (used by `make fix-copies`).
"""
38
39
40
41
42

import argparse
import glob
import os
import re
43
import subprocess
Sylvain Gugger's avatar
Sylvain Gugger committed
44
from typing import List, Optional, Tuple
45

46
47
from transformers.utils import direct_transformers_import

48
49
50
51

# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_copies.py
TRANSFORMERS_PATH = "src/transformers"
52
MODEL_TEST_PATH = "tests/models"
53
PATH_TO_DOCS = "docs/source/en"
54
REPO_PATH = "."
55

56
# Mapping for files that are full copies of others (keys are copies, values the file to keep them up to data with)
57
58
59
60
FULL_COPIES = {
    "examples/tensorflow/question-answering/utils_qa.py": "examples/pytorch/question-answering/utils_qa.py",
    "examples/flax/question-answering/utils_qa.py": "examples/pytorch/question-answering/utils_qa.py",
}
61

62

63
64
65
66
67
LOCALIZED_READMES = {
    # If the introduction or the conclusion of the list change, the prompts may need to be updated.
    "README.md": {
        "start_prompt": "🤗 Transformers currently provides the following architectures",
        "end_prompt": "1. Want to contribute a new model?",
Sylvain Gugger's avatar
Sylvain Gugger committed
68
69
70
71
        "format_model_list": (
            "**[{title}]({model_link})** (from {paper_affiliations}) released with the paper {paper_title_link} by"
            " {paper_authors}.{supplements}"
        ),
72
73
74
75
    },
    "README_zh-hans.md": {
        "start_prompt": "🤗 Transformers 目前支持如下的架构",
        "end_prompt": "1. 想要贡献新的模型?",
Sylvain Gugger's avatar
Sylvain Gugger committed
76
77
78
79
        "format_model_list": (
            "**[{title}]({model_link})** (来自 {paper_affiliations}) 伴随论文 {paper_title_link} 由 {paper_authors}"
            " 发布。{supplements}"
        ),
80
81
82
83
    },
    "README_zh-hant.md": {
        "start_prompt": "🤗 Transformers 目前支援以下的架構",
        "end_prompt": "1. 想要貢獻新的模型?",
Sylvain Gugger's avatar
Sylvain Gugger committed
84
85
86
87
        "format_model_list": (
            "**[{title}]({model_link})** (from {paper_affiliations}) released with the paper {paper_title_link} by"
            " {paper_authors}.{supplements}"
        ),
88
    },
89
90
91
    "README_ko.md": {
        "start_prompt": "🤗 Transformers는 다음 모델들을 제공합니다",
        "end_prompt": "1. 새로운 모델을 올리고 싶나요?",
Sylvain Gugger's avatar
Sylvain Gugger committed
92
        "format_model_list": (
Arthur's avatar
Arthur committed
93
94
            "**[{title}]({model_link})** ({paper_affiliations} 에서 제공)은 {paper_authors}.{supplements}의"
            " {paper_title_link}논문과 함께 발표했습니다."
Sylvain Gugger's avatar
Sylvain Gugger committed
95
        ),
96
    },
97
98
99
100
101
102
103
104
    "README_es.md": {
        "start_prompt": "🤗 Transformers actualmente proporciona las siguientes arquitecturas",
        "end_prompt": "1. ¿Quieres aportar un nuevo modelo?",
        "format_model_list": (
            "**[{title}]({model_link})** (from {paper_affiliations}) released with the paper {paper_title_link} by"
            " {paper_authors}.{supplements}"
        ),
    },
105
106
107
108
    "README_ja.md": {
        "start_prompt": "🤗Transformersは現在、以下のアーキテクチャを提供しています",
        "end_prompt": "1. 新しいモデルを投稿したいですか?",
        "format_model_list": (
109
110
            "**[{title}]({model_link})** ({paper_affiliations} から) {paper_authors}.{supplements} から公開された研究論文"
            " {paper_title_link}"
111
112
        ),
    },
113
114
115
116
    "README_hd.md": {
        "start_prompt": "🤗 ट्रांसफॉर्मर वर्तमान में निम्नलिखित आर्किटेक्चर का समर्थन करते हैं",
        "end_prompt": "1. एक नए मॉडल में योगदान देना चाहते हैं?",
        "format_model_list": (
117
118
            "**[{title}]({model_link})** ({paper_affiliations} से) {paper_authors}.{supplements} द्वारा"
            "अनुसंधान पत्र {paper_title_link} के साथ जारी किया गया"
119
120
        ),
    },
121
122
123
}


124
# This is to make sure the transformers module imported is the one in the repo.
125
transformers_module = direct_transformers_import(TRANSFORMERS_PATH)
126
127


Sylvain Gugger's avatar
Sylvain Gugger committed
128
def _should_continue(line: str, indent: str) -> bool:
129
130
131
    # Helper function. Returns `True` if `line` is empty, starts with the `indent` or is the end parenthesis of a
    # function definition
    return line.startswith(indent) or len(line.strip()) == 0 or re.search(r"^\s*\)(\s*->.*:|:)\s*$", line) is not None
132
133


134
def find_code_in_transformers(object_name: str, base_path: str = None) -> str:
Sylvain Gugger's avatar
Sylvain Gugger committed
135
136
137
138
    """
    Find and return the source code of an object.

    Args:
139
140
141
142
        object_name (`str`):
            The name of the object we want the source code of.
        base_path (`str`, *optional*):
            The path to the base folder where files are checked. If not set, it will be set to `TRANSFORMERS_PATH`.
Sylvain Gugger's avatar
Sylvain Gugger committed
143
144
145
146

    Returns:
        `str`: The source code of the object.
    """
147
148
149
    parts = object_name.split(".")
    i = 0

150
151
152
153
154
155
156
157
158
159
160
161
    # We can't set this as the default value in the argument, otherwise `CopyCheckTester` will fail, as it uses a
    # patched temp directory.
    if base_path is None:
        base_path = TRANSFORMERS_PATH

    # Detail: the `Copied from` statement is originally designed to work with the last part of `TRANSFORMERS_PATH`,
    # (which is `transformers`). The same should be applied for `MODEL_TEST_PATH`. However, its last part is `models`
    # (to only check and search in it) which is a bit confusing. So we keep the copied statement staring with
    # `tests.models.` and change it to `tests` here.
    if base_path == MODEL_TEST_PATH:
        base_path = "tests"

162
163
    # First let's find the module where our object lives.
    module = parts[i]
164
    while i < len(parts) and not os.path.isfile(os.path.join(base_path, f"{module}.py")):
165
        i += 1
166
167
        if i < len(parts):
            module = os.path.join(module, parts[i])
168
169
170
171
172
    if i >= len(parts):
        raise ValueError(
            f"`object_name` should begin with the name of a module of transformers but got {object_name}."
        )

173
    with open(os.path.join(base_path, f"{module}.py"), "r", encoding="utf-8", newline="\n") as f:
174
175
176
177
178
179
        lines = f.readlines()

    # Now let's find the class / func in the code!
    indent = ""
    line_index = 0
    for name in parts[i + 1 :]:
180
        while (
181
            line_index < len(lines) and re.search(rf"^{indent}(class|def)\s+{name}(\(|\:)", lines[line_index]) is None
182
        ):
183
184
185
186
187
188
189
190
            line_index += 1
        indent += "    "
        line_index += 1

    if line_index >= len(lines):
        raise ValueError(f" {object_name} does not match any function or class in {module}.")

    # We found the beginning of the class / func, now let's find the end (when the indent diminishes).
191
    start_index = line_index - 1
192
    while line_index < len(lines) and _should_continue(lines[line_index], indent):
193
194
195
196
197
198
199
200
201
202
        line_index += 1
    # Clean up empty lines at the end (if any).
    while len(lines[line_index - 1]) <= 1:
        line_index -= 1

    code_lines = lines[start_index:line_index]
    return "".join(code_lines)


_re_copy_warning = re.compile(r"^(\s*)#\s*Copied from\s+transformers\.(\S+\.\S+)\s*($|\S.*$)")
203
_re_copy_warning_for_test_file = re.compile(r"^(\s*)#\s*Copied from\s+tests\.(\S+\.\S+)\s*($|\S.*$)")
204
_re_replace_pattern = re.compile(r"^\s*(\S+)->(\S+)(\s+.*|$)")
205
_re_fill_pattern = re.compile(r"<FILL\s+[^>]*>")
206
207


Sylvain Gugger's avatar
Sylvain Gugger committed
208
209
210
211
212
213
214
215
216
217
def get_indent(code: str) -> str:
    """
    Find the indent in the first non empty line in a code sample.

    Args:
        code (`str`): The code to inspect.

    Returns:
        `str`: The indent looked at (as string).
    """
218
219
220
221
222
223
    lines = code.split("\n")
    idx = 0
    while idx < len(lines) and len(lines[idx]) == 0:
        idx += 1
    if idx < len(lines):
        return re.search(r"^(\s*)\S", lines[idx]).groups()[0]
224
225
226
    return ""


227
228
229
230
231
232
233
234
def run_ruff(code):
    command = ["ruff", "format", "-", "--config", "pyproject.toml", "--silent"]
    process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
    stdout, _ = process.communicate(input=code.encode())
    return stdout.decode()


def stylify(code: str) -> str:
235
    """
236
237
    Applies the ruff part of our `make style` command to some code. This formats the code using `ruff format`.
    As `ruff` does not provide a python api this cannot be done on the fly.
Sylvain Gugger's avatar
Sylvain Gugger committed
238
239
240
241
242
243

    Args:
        code (`str`): The code to format.

    Returns:
        `str`: The formatted code.
244
245
246
247
    """
    has_indent = len(get_indent(code)) > 0
    if has_indent:
        code = f"class Bla:\n{code}"
248
249
    formatted_code = run_ruff(code)
    return formatted_code[len("class Bla:\n") :] if has_indent else formatted_code
250
251


Sylvain Gugger's avatar
Sylvain Gugger committed
252
def check_codes_match(observed_code: str, theoretical_code: str) -> Optional[int]:
253
    """
Sylvain Gugger's avatar
Sylvain Gugger committed
254
255
256
257
258
259
260
261
262
    Checks if two version of a code match with the exception of the class/function name.

    Args:
        observed_code (`str`): The code found.
        theoretical_code (`str`): The code to match.

    Returns:
        `Optional[int]`: The index of the first line where there is a difference (if any) and `None` if the codes
        match.
263
264
265
266
    """
    observed_code_header = observed_code.split("\n")[0]
    theoretical_code_header = theoretical_code.split("\n")[0]

Sylvain Gugger's avatar
Sylvain Gugger committed
267
    # Catch the function/class name: it is expected that those do not match.
268
269
270
271
272
273
274
275
    _re_class_match = re.compile(r"class\s+([^\(:]+)(?:\(|:)")
    _re_func_match = re.compile(r"def\s+([^\(]+)\(")
    for re_pattern in [_re_class_match, _re_func_match]:
        if re_pattern.match(observed_code_header) is not None:
            observed_obj_name = re_pattern.search(observed_code_header).groups()[0]
            theoretical_name = re_pattern.search(theoretical_code_header).groups()[0]
            theoretical_code_header = theoretical_code_header.replace(theoretical_name, observed_obj_name)

Sylvain Gugger's avatar
Sylvain Gugger committed
276
    # Find the first diff. Line 0 is special since we need to compare with the function/class names ignored.
277
278
279
280
281
282
283
284
285
286
287
    diff_index = 0
    if theoretical_code_header != observed_code_header:
        return 0

    diff_index = 1
    for observed_line, theoretical_line in zip(observed_code.split("\n")[1:], theoretical_code.split("\n")[1:]):
        if observed_line != theoretical_line:
            return diff_index
        diff_index += 1


Sylvain Gugger's avatar
Sylvain Gugger committed
288
def is_copy_consistent(filename: str, overwrite: bool = False) -> Optional[List[Tuple[str, int]]]:
289
    """
Sylvain Gugger's avatar
Sylvain Gugger committed
290
    Check if the code commented as a copy in a file matches the original.
291

Sylvain Gugger's avatar
Sylvain Gugger committed
292
293
294
295
296
297
298
299
300
    Args:
        filename (`str`):
            The name of the file to check.
        overwrite (`bool`, *optional*, defaults to `False`):
            Whether or not to overwrite the copies when they don't match.

    Returns:
        `Optional[List[Tuple[str, int]]]`: If `overwrite=False`, returns the list of differences as tuples `(str, int)`
        with the name of the object having a diff and the line number where theere is the first diff.
301
    """
302
    with open(filename, "r", encoding="utf-8", newline="\n") as f:
303
        lines = f.readlines()
304
    diffs = []
305
    line_index = 0
306
    # Not a for loop cause `lines` is going to change (if `overwrite=True`).
307
    while line_index < len(lines):
308
309
310
311
312
        search_re = _re_copy_warning
        if filename.startswith("tests"):
            search_re = _re_copy_warning_for_test_file

        search = search_re.search(lines[line_index])
313
314
315
316
317
318
        if search is None:
            line_index += 1
            continue

        # There is some copied code here, let's retrieve the original.
        indent, object_name, replace_pattern = search.groups()
319
320
321

        base_path = TRANSFORMERS_PATH if not filename.startswith("tests") else MODEL_TEST_PATH
        theoretical_code = find_code_in_transformers(object_name, base_path=base_path)
322
        theoretical_indent = get_indent(theoretical_code)
323

324
325
        start_index = line_index + 1 if indent == theoretical_indent else line_index
        line_index = start_index + 1
326

327
328
        subcode = "\n".join(theoretical_code.split("\n")[1:])
        indent = get_indent(subcode)
329
330
331
332
333
334
335
        # Loop to check the observed code, stop when indentation diminishes or if we see a End copy comment.
        should_continue = True
        while line_index < len(lines) and should_continue:
            line_index += 1
            if line_index >= len(lines):
                break
            line = lines[line_index]
336
337
            # There is a special pattern `# End copy` to stop early. It's not documented cause it shouldn't really be
            # used.
338
            should_continue = _should_continue(line, indent) and re.search(f"^{indent}# End copy", line) is None
339
340
341
342
343
344
345
346
347
        # Clean up empty lines at the end (if any).
        while len(lines[line_index - 1]) <= 1:
            line_index -= 1

        observed_code_lines = lines[start_index:line_index]
        observed_code = "".join(observed_code_lines)

        # Before comparing, use the `replace_pattern` on the original code.
        if len(replace_pattern) > 0:
348
349
350
351
352
353
            patterns = replace_pattern.replace("with", "").split(",")
            patterns = [_re_replace_pattern.search(p) for p in patterns]
            for pattern in patterns:
                if pattern is None:
                    continue
                obj1, obj2, option = pattern.groups()
354
                theoretical_code = re.sub(obj1, obj2, theoretical_code)
355
356
357
                if option.strip() == "all-casing":
                    theoretical_code = re.sub(obj1.lower(), obj2.lower(), theoretical_code)
                    theoretical_code = re.sub(obj1.upper(), obj2.upper(), theoretical_code)
358
            theoretical_code = stylify(theoretical_code)
359

360
        # Test for a diff and act accordingly.
361
362
363
        diff_index = check_codes_match(observed_code, theoretical_code)
        if diff_index is not None:
            diffs.append([object_name, diff_index + start_index + 1])
364
365
366
367
            if overwrite:
                lines = lines[:start_index] + [theoretical_code] + lines[line_index:]
                line_index = start_index + 1

368
    if overwrite and len(diffs) > 0:
369
370
        # Warn the user a file has been modified.
        print(f"Detected changes, rewriting {filename}.")
371
        with open(filename, "w", encoding="utf-8", newline="\n") as f:
372
            f.writelines(lines)
373
    return diffs
374
375
376


def check_copies(overwrite: bool = False):
377
    """
Sylvain Gugger's avatar
Sylvain Gugger committed
378
    Check every file is copy-consistent with the original. Also check the model list in the main README and other
379
    READMEs are consistent.
Sylvain Gugger's avatar
Sylvain Gugger committed
380
381
382
383

    Args:
        overwrite (`bool`, *optional*, defaults to `False`):
            Whether or not to overwrite the copies when they don't match.
384
    """
385
    all_files = glob.glob(os.path.join(TRANSFORMERS_PATH, "**/*.py"), recursive=True)
386
387
388
    all_test_files = glob.glob(os.path.join(MODEL_TEST_PATH, "**/*.py"), recursive=True)
    all_files = list(all_files) + list(all_test_files)

389
390
    diffs = []
    for filename in all_files:
391
392
        new_diffs = is_copy_consistent(filename, overwrite)
        diffs += [f"- {filename}: copy does not match {d[0]} at line {d[1]}" for d in new_diffs]
393
394
395
    if not overwrite and len(diffs) > 0:
        diff = "\n".join(diffs)
        raise Exception(
396
            "Found the following copy inconsistencies:\n"
397
            + diff
398
            + "\nRun `make fix-copies` or `python utils/check_copies.py --fix_and_overwrite` to fix them."
399
        )
400
401
402
    check_model_list_copy(overwrite=overwrite)


403
def check_full_copies(overwrite: bool = False):
404
    """
Sylvain Gugger's avatar
Sylvain Gugger committed
405
406
407
408
409
    Check the files that are full copies of others (as indicated in `FULL_COPIES`) are copy-consistent.

    Args:
        overwrite (`bool`, *optional*, defaults to `False`):
            Whether or not to overwrite the copies when they don't match.
410
    """
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
    diffs = []
    for target, source in FULL_COPIES.items():
        with open(source, "r", encoding="utf-8") as f:
            source_code = f.read()
        with open(target, "r", encoding="utf-8") as f:
            target_code = f.read()
        if source_code != target_code:
            if overwrite:
                with open(target, "w", encoding="utf-8") as f:
                    print(f"Replacing the content of {target} by the one of {source}.")
                    f.write(source_code)
            else:
                diffs.append(f"- {target}: copy does not match {source}.")

    if not overwrite and len(diffs) > 0:
        diff = "\n".join(diffs)
        raise Exception(
            "Found the following copy inconsistencies:\n"
            + diff
            + "\nRun `make fix-copies` or `python utils/check_copies.py --fix_and_overwrite` to fix them."
        )


Sylvain Gugger's avatar
Sylvain Gugger committed
434
435
436
437
438
439
440
441
442
443
444
445
def get_model_list(filename: str, start_prompt: str, end_prompt: str) -> str:
    """
    Extracts the model list from a README.

    Args:
        filename (`str`): The name of the README file to check.
        start_prompt (`str`): The string to look for that introduces the model list.
        end_prompt (`str`): The string to look for that ends the model list.

    Returns:
        `str`: The model list.
    """
446
    with open(os.path.join(REPO_PATH, filename), "r", encoding="utf-8", newline="\n") as f:
447
448
449
        lines = f.readlines()
    # Find the start of the list.
    start_index = 0
450
    while not lines[start_index].startswith(start_prompt):
451
452
453
454
455
456
457
        start_index += 1
    start_index += 1

    result = []
    current_line = ""
    end_index = start_index

Sylvain Gugger's avatar
Sylvain Gugger committed
458
    # Keep going until the end of the list.
459
    while not lines[end_index].startswith(end_prompt):
460
461
462
463
464
465
466
467
468
469
470
471
472
        if lines[end_index].startswith("1."):
            if len(current_line) > 1:
                result.append(current_line)
            current_line = lines[end_index]
        elif len(lines[end_index]) > 1:
            current_line = f"{current_line[:-1]} {lines[end_index].lstrip()}"
        end_index += 1
    if len(current_line) > 1:
        result.append(current_line)

    return "".join(result)


Sylvain Gugger's avatar
Sylvain Gugger committed
473
def convert_to_localized_md(model_list: str, localized_model_list: str, format_str: str) -> Tuple[bool, str]:
474
475
476
477
478
479
480
481
482
483
484
485
486
487
    """
    Compare the model list from the main README to the one in a localized README.

    Args:
        model_list (`str`): The model list in the main README.
        localized_model_list (`str`): The model list in one of the localized README.
        format_str (`str`):
            The template for a model entry in the localized README (look at the `format_model_list` in the entries of
            `LOCALIZED_READMES` for examples).

    Returns:
        `Tuple[bool, str]`: A tuple where the first value indicates if the READMEs match or not, and the second value
        is the correct localized README.
    """
488
489
490
491
492
493
494
495
496
497
498
499
500

    def _rep(match):
        title, model_link, paper_affiliations, paper_title_link, paper_authors, supplements = match.groups()
        return format_str.format(
            title=title,
            model_link=model_link,
            paper_affiliations=paper_affiliations,
            paper_title_link=paper_title_link,
            paper_authors=paper_authors,
            supplements=" " + supplements.strip() if len(supplements) != 0 else "",
        )

    # This regex captures metadata from an English model description, including model title, model link,
501
502
    # affiliations of the paper, title of the paper, authors of the paper, and supplemental data (see DistilBERT for
    # example).
503
504
505
    _re_capture_meta = re.compile(
        r"\*\*\[([^\]]*)\]\(([^\)]*)\)\*\* \(from ([^)]*)\)[^\[]*([^\)]*\)).*?by (.*?[A-Za-z\*]{2,}?)\. (.*)$"
    )
506
507
    # This regex is used to synchronize link.
    _re_capture_title_link = re.compile(r"\*\*\[([^\]]*)\]\(([^\)]*)\)\*\*")
508
509
510
511
512
513
514
515
516
517
518
519

    if len(localized_model_list) == 0:
        localized_model_index = {}
    else:
        try:
            localized_model_index = {
                re.search(r"\*\*\[([^\]]*)", line).groups()[0]: line
                for line in localized_model_list.strip().split("\n")
            }
        except AttributeError:
            raise AttributeError("A model name in localized READMEs cannot be recognized.")

520
521
522
    model_keys = [re.search(r"\*\*\[([^\]]*)", line).groups()[0] for line in model_list.strip().split("\n")]

    # We exclude keys in localized README not in the main one.
523
    readmes_match = not any(k not in model_keys for k in localized_model_index)
524
525
    localized_model_index = {k: v for k, v in localized_model_index.items() if k in model_keys}

526
    for model in model_list.strip().split("\n"):
527
        title, model_link = _re_capture_title_link.search(model).groups()
528
        if title not in localized_model_index:
529
            readmes_match = False
530
531
            # Add an anchor white space behind a model description string for regex.
            # If metadata cannot be captured, the English version will be directly copied.
532
            localized_model_index[title] = _re_capture_meta.sub(_rep, model + " ")
533
534
535
536
537
        elif _re_fill_pattern.search(localized_model_index[title]) is not None:
            update = _re_capture_meta.sub(_rep, model + " ")
            if update != localized_model_index[title]:
                readmes_match = False
                localized_model_index[title] = update
538
539
540
541
542
        else:
            # Synchronize link
            localized_model_index[title] = _re_capture_title_link.sub(
                f"**[{title}]({model_link})**", localized_model_index[title], count=1
            )
543
544
545

    sorted_index = sorted(localized_model_index.items(), key=lambda x: x[0].lower())

546
    return readmes_match, "\n".join((x[1] for x in sorted_index)) + "\n"
547
548


Sylvain Gugger's avatar
Sylvain Gugger committed
549
def _find_text_in_file(filename: str, start_prompt: str, end_prompt: str) -> Tuple[str, int, int, List[str]]:
Sylvain Gugger's avatar
Sylvain Gugger committed
550
    """
Sylvain Gugger's avatar
Sylvain Gugger committed
551
552
553
554
555
556
557
558
559
560
    Find the text in a file between two prompts.

    Args:
        filename (`str`): The name of the file to look into.
        start_prompt (`str`): The string to look for that introduces the content looked for.
        end_prompt (`str`): The string to look for that ends the content looked for.

    Returns:
        Tuple[str, int, int, List[str]]: The content between the two prompts, the index of the start line in the
        original file, the index of the end line in the original file and the list of lines of that file.
Sylvain Gugger's avatar
Sylvain Gugger committed
561
562
    """
    with open(filename, "r", encoding="utf-8", newline="\n") as f:
563
        lines = f.readlines()
Sylvain Gugger's avatar
Sylvain Gugger committed
564
    # Find the start prompt.
565
    start_index = 0
Sylvain Gugger's avatar
Sylvain Gugger committed
566
    while not lines[start_index].startswith(start_prompt):
567
568
569
570
        start_index += 1
    start_index += 1

    end_index = start_index
Sylvain Gugger's avatar
Sylvain Gugger committed
571
    while not lines[end_index].startswith(end_prompt):
572
573
574
575
576
577
578
579
        end_index += 1
    end_index -= 1

    while len(lines[start_index]) <= 1:
        start_index += 1
    while len(lines[end_index]) <= 1:
        end_index -= 1
    end_index += 1
Sylvain Gugger's avatar
Sylvain Gugger committed
580
581
    return "".join(lines[start_index:end_index]), start_index, end_index, lines

582

Sylvain Gugger's avatar
Sylvain Gugger committed
583
def check_model_list_copy(overwrite: bool = False):
584
585
    """
    Check the model lists in the README is consistent with the ones in the other READMES and also with `index.nmd`.
Sylvain Gugger's avatar
Sylvain Gugger committed
586
587
588
589

    Args:
        overwrite (`bool`, *optional*, defaults to `False`):
            Whether or not to overwrite the copies when they don't match.
590
    """
591
592
593
594
    # Fix potential doc links in the README
    with open(os.path.join(REPO_PATH, "README.md"), "r", encoding="utf-8", newline="\n") as f:
        readme = f.read()
    new_readme = readme.replace("https://huggingface.co/transformers", "https://huggingface.co/docs/transformers")
595
    new_readme = new_readme.replace(
596
597
598
599
600
601
602
603
604
605
606
        "https://huggingface.co/docs/main/transformers", "https://huggingface.co/docs/transformers/main"
    )
    if new_readme != readme:
        if overwrite:
            with open(os.path.join(REPO_PATH, "README.md"), "w", encoding="utf-8", newline="\n") as f:
                f.write(new_readme)
        else:
            raise ValueError(
                "The main README contains wrong links to the documentation of Transformers. Run `make fix-copies` to "
                "automatically fix them."
            )
607
608
609
610
611
612
613

    md_list = get_model_list(
        filename="README.md",
        start_prompt=LOCALIZED_READMES["README.md"]["start_prompt"],
        end_prompt=LOCALIZED_READMES["README.md"]["end_prompt"],
    )

614
    # Build the converted Markdown.
615
616
617
618
619
620
621
    converted_md_lists = []
    for filename, value in LOCALIZED_READMES.items():
        _start_prompt = value["start_prompt"]
        _end_prompt = value["end_prompt"]
        _format_model_list = value["format_model_list"]

        localized_md_list = get_model_list(filename, _start_prompt, _end_prompt)
622
        readmes_match, converted_md_list = convert_to_localized_md(md_list, localized_md_list, _format_model_list)
623

624
        converted_md_lists.append((filename, readmes_match, converted_md_list, _start_prompt, _end_prompt))
625

Sylvain Gugger's avatar
Sylvain Gugger committed
626
    # Compare the converted Markdowns
627
    for converted_md_list in converted_md_lists:
628
        filename, readmes_match, converted_md, _start_prompt, _end_prompt = converted_md_list
629

630
631
632
633
634
635
636
637
        if filename == "README.md":
            continue
        if overwrite:
            _, start_index, end_index, lines = _find_text_in_file(
                filename=os.path.join(REPO_PATH, filename), start_prompt=_start_prompt, end_prompt=_end_prompt
            )
            with open(os.path.join(REPO_PATH, filename), "w", encoding="utf-8", newline="\n") as f:
                f.writelines(lines[:start_index] + [converted_md] + lines[end_index:])
638
        elif not readmes_match:
639
640
641
642
            raise ValueError(
                f"The model list in the README changed and the list in `{filename}` has not been updated. Run "
                "`make fix-copies` to fix this."
            )
643

Sylvain Gugger's avatar
Sylvain Gugger committed
644

645
# Map a model name with the name it has in the README for the check_readme check
646
647
648
649
650
651
SPECIAL_MODEL_NAMES = {
    "Bert Generation": "BERT For Sequence Generation",
    "BigBird": "BigBird-RoBERTa",
    "Data2VecAudio": "Data2Vec",
    "Data2VecText": "Data2Vec",
    "Data2VecVision": "Data2Vec",
652
    "DonutSwin": "Swin Transformer",
653
    "Marian": "MarianMT",
654
    "MaskFormerSwin": "Swin Transformer",
655
656
657
    "OpenAI GPT-2": "GPT-2",
    "OpenAI GPT": "GPT",
    "Perceiver": "Perceiver IO",
658
    "SAM": "Segment Anything",
659
660
661
662
663
664
665
666
667
668
669
670
671
672
    "ViT": "Vision Transformer (ViT)",
}

# Update this list with the models that shouldn't be in the README. This only concerns modular models or those who do
# not have an associated paper.
MODELS_NOT_IN_README = [
    "BertJapanese",
    "Encoder decoder",
    "FairSeq Machine-Translation",
    "HerBERT",
    "RetriBERT",
    "Speech Encoder decoder",
    "Speech2Text",
    "Speech2Text2",
amyeroberts's avatar
amyeroberts committed
673
    "TimmBackbone",
674
675
    "Vision Encoder decoder",
    "VisionTextDualEncoder",
676
    "CLIPVisionModel",
677
678
]

679
# Template for new entries to add in the main README when we have missing models.
680
README_TEMPLATE = (
681
682
    "1. **[{model_name}](https://huggingface.co/docs/main/transformers/model_doc/{model_type})** (from "
    "<FILL INSTITUTION>) released with the paper [<FILL PAPER TITLE>](<FILL ARKIV LINK>) by <FILL AUTHORS>."
683
684
685
)


Sylvain Gugger's avatar
Sylvain Gugger committed
686
def check_readme(overwrite: bool = False):
687
    """
Sylvain Gugger's avatar
Sylvain Gugger committed
688
689
690
691
692
    Check if the main README contains all the models in the library or not.

    Args:
        overwrite (`bool`, *optional*, defaults to `False`):
            Whether or not to add an entry for the missing models using `README_TEMPLATE`.
693
    """
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
    info = LOCALIZED_READMES["README.md"]
    models, start_index, end_index, lines = _find_text_in_file(
        os.path.join(REPO_PATH, "README.md"),
        info["start_prompt"],
        info["end_prompt"],
    )
    models_in_readme = [re.search(r"\*\*\[([^\]]*)", line).groups()[0] for line in models.strip().split("\n")]

    model_names_mapping = transformers_module.models.auto.configuration_auto.MODEL_NAMES_MAPPING
    absents = [
        (key, name)
        for key, name in model_names_mapping.items()
        if SPECIAL_MODEL_NAMES.get(name, name) not in models_in_readme
    ]
    # Remove exceptions
    absents = [(key, name) for key, name in absents if name not in MODELS_NOT_IN_README]
    if len(absents) > 0 and not overwrite:
        print(absents)
        raise ValueError(
            "The main README doesn't contain all models, run `make fix-copies` to fill it with the missing model(s)"
            " then complete the generated entries.\nIf the model is not supposed to be in the main README, add it to"
            " the list `MODELS_NOT_IN_README` in utils/check_copies.py.\nIf it has a different name in the repo than"
            " in the README, map the correspondence in `SPECIAL_MODEL_NAMES` in utils/check_copies.py."
        )

    new_models = [README_TEMPLATE.format(model_name=name, model_type=key) for key, name in absents]

    all_models = models.strip().split("\n") + new_models
    all_models = sorted(all_models, key=lambda x: re.search(r"\*\*\[([^\]]*)", x).groups()[0].lower())
    all_models = "\n".join(all_models) + "\n"

    if all_models != models:
        if overwrite:
            print("Fixing the main README.")
            with open(os.path.join(REPO_PATH, "README.md"), "w", encoding="utf-8", newline="\n") as f:
                f.writelines(lines[:start_index] + [all_models] + lines[end_index:])
        else:
            raise ValueError("The main README model list is not properly sorted. Run `make fix-copies` to fix this.")


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

739
    check_readme(args.fix_and_overwrite)
740
    check_copies(args.fix_and_overwrite)
741
    check_full_copies(args.fix_and_overwrite)