check_copies.py 47 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
Yih-Dar's avatar
Yih-Dar committed
44
45
from collections import OrderedDict
from typing import List, Optional, Tuple, Union
46

47
48
from transformers.utils import direct_transformers_import

49
50
51
52

# 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"
53
MODEL_TEST_PATH = "tests/models"
54
PATH_TO_DOCS = "docs/source/en"
55
REPO_PATH = "."
56

57
# Mapping for files that are full copies of others (keys are copies, values the file to keep them up to data with)
58
59
60
61
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",
}
62

63

64
65
66
67
68
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
69
70
71
72
        "format_model_list": (
            "**[{title}]({model_link})** (from {paper_affiliations}) released with the paper {paper_title_link} by"
            " {paper_authors}.{supplements}"
        ),
73
74
75
76
    },
    "README_zh-hans.md": {
        "start_prompt": "🤗 Transformers 目前支持如下的架构",
        "end_prompt": "1. 想要贡献新的模型?",
Sylvain Gugger's avatar
Sylvain Gugger committed
77
78
79
80
        "format_model_list": (
            "**[{title}]({model_link})** (来自 {paper_affiliations}) 伴随论文 {paper_title_link} 由 {paper_authors}"
            " 发布。{supplements}"
        ),
81
82
83
84
    },
    "README_zh-hant.md": {
        "start_prompt": "🤗 Transformers 目前支援以下的架構",
        "end_prompt": "1. 想要貢獻新的模型?",
Sylvain Gugger's avatar
Sylvain Gugger committed
85
86
87
88
        "format_model_list": (
            "**[{title}]({model_link})** (from {paper_affiliations}) released with the paper {paper_title_link} by"
            " {paper_authors}.{supplements}"
        ),
89
    },
90
91
92
    "README_ko.md": {
        "start_prompt": "🤗 Transformers는 다음 모델들을 제공합니다",
        "end_prompt": "1. 새로운 모델을 올리고 싶나요?",
Sylvain Gugger's avatar
Sylvain Gugger committed
93
        "format_model_list": (
Arthur's avatar
Arthur committed
94
95
            "**[{title}]({model_link})** ({paper_affiliations} 에서 제공)은 {paper_authors}.{supplements}의"
            " {paper_title_link}논문과 함께 발표했습니다."
Sylvain Gugger's avatar
Sylvain Gugger committed
96
        ),
97
    },
98
99
100
101
102
103
104
105
    "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}"
        ),
    },
106
107
108
109
    "README_ja.md": {
        "start_prompt": "🤗Transformersは現在、以下のアーキテクチャを提供しています",
        "end_prompt": "1. 新しいモデルを投稿したいですか?",
        "format_model_list": (
110
111
            "**[{title}]({model_link})** ({paper_affiliations} から) {paper_authors}.{supplements} から公開された研究論文"
            " {paper_title_link}"
112
113
        ),
    },
114
115
116
117
    "README_hd.md": {
        "start_prompt": "🤗 ट्रांसफॉर्मर वर्तमान में निम्नलिखित आर्किटेक्चर का समर्थन करते हैं",
        "end_prompt": "1. एक नए मॉडल में योगदान देना चाहते हैं?",
        "format_model_list": (
118
119
            "**[{title}]({model_link})** ({paper_affiliations} से) {paper_authors}.{supplements} द्वारा"
            "अनुसंधान पत्र {paper_title_link} के साथ जारी किया गया"
120
121
        ),
    },
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
    "README_ru.md": {
        "start_prompt": "🤗 В настоящее время Transformers предоставляет следующие архитектуры",
        "end_prompt": "1. Хотите внести новую модель?",
        "format_model_list": (
            "**[{title}]({model_link})** (from {paper_affiliations}) released with the paper {paper_title_link} by"
            " {paper_authors}.{supplements}"
        ),
    },
    "README_pt-br.md": {
        "start_prompt": "🤗 Transformers atualmente fornece as seguintes arquiteturas",
        "end_prompt": "1. Quer contribuir com um novo modelo?",
        "format_model_list": (
            "**[{title}]({model_link})** (from {paper_affiliations}) released with the paper {paper_title_link} by"
            " {paper_authors}.{supplements}"
        ),
    },
    "README_te.md": {
        "start_prompt": "🤗 ట్రాన్స్‌ఫార్మర్లు ప్రస్తుతం కింది ఆర్కిటెక్చర్‌లను అందజేస్తున్నాయి",
        "end_prompt": "1. కొత్త మోడల్‌ను అందించాలనుకుంటున్నారా?",
        "format_model_list": (
            "**[{title}]({model_link})** (from {paper_affiliations}) released with the paper {paper_title_link} by"
            " {paper_authors}.{supplements}"
        ),
    },
146
147
148
149
150
151
152
153
    "README_fr.md": {
        "start_prompt": "🤗 Transformers fournit actuellement les architectures suivantes",
        "end_prompt": "1. Vous souhaitez contribuer avec un nouveau modèle ?",
        "format_model_list": (
            "**[{title}]({model_link})** (de {paper_affiliations}) publié dans l'article {paper_title_link} par"
            "{paper_authors}.{supplements}"
        ),
    },
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
    "README_de.md": {
        "start_prompt": "🤗 Transformers bietet derzeit die folgenden Architekturen an",
        "end_prompt": "1. Möchten Sie ein neues Modell beitragen?",
        "format_model_list": (
            "**[{title}]({model_link})** (from {paper_affiliations}) released with the paper {paper_title_link} by"
            " {paper_authors}.{supplements}"
        ),
    },
    "README_vi.md": {
        "start_prompt": "🤗 Transformers hiện đang cung cấp các kiến trúc sau đây",
        "end_prompt": "1. Muốn đóng góp một mô hình mới?",
        "format_model_list": (
            "**[{title}]({model_link})** (từ {paper_affiliations}) được phát hành với bài báo {paper_title_link} by"
            " {paper_authors}.{supplements}"
        ),
    },
170
171
}

172
# This is to make sure the transformers module imported is the one in the repo.
173
transformers_module = direct_transformers_import(TRANSFORMERS_PATH)
174
175


Yih-Dar's avatar
Yih-Dar committed
176
177
178
179
180
def _is_definition_header_ending_line(line: str) -> bool:
    # Helper function. Returns `True` if `line` is the end parenthesis of a class/function definition
    return re.search(r"^\s*\)(\s*->.*:|:)\s*$", line) is not None


Sylvain Gugger's avatar
Sylvain Gugger committed
181
def _should_continue(line: str, indent: str) -> bool:
182
    # Helper function. Returns `True` if `line` is empty, starts with the `indent` or is the end parenthesis of a
Yih-Dar's avatar
Yih-Dar committed
183
184
185
186
    # class/function definition
    return line.startswith(indent) or len(line.strip()) == 0 or _is_definition_header_ending_line(line)


187
def _sanity_check_splits(splits_1, splits_2, is_class, filename):
Yih-Dar's avatar
Yih-Dar committed
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
    """Check the two (inner) block structures of the corresponding code block given by `split_code_into_blocks` match.

    For the case of `class`, they must be of one of the following 3 cases:

        - a single block without name:

            class foo:
                a = 1

        - a consecutive sequence of (1 or more) blocks with name

            class foo:

                def f(x):
                    return x

        - a block without name, followed by a consecutive sequence of (1 or more) blocks with name

            class foo:
                a = 1

                def f(x):
                    return x

                def g(x):
                    return None

    The 2 code snippets that give `splits_1` and `splits_2` have to be in the same case to pass this check, but the
    number of blocks with name in the consecutive sequence is not taken into account.

    For the case of `function or method`, we don't require it to be in one of the above 3 cases. However, the structure
    of`splits_1` and `splits_2` have to match exactly. In particular, the number of blocks with name in a consecutive
    sequence is taken into account.
    """
    block_names_1 = []
    block_names_2 = []

    for block in splits_1[1:]:
        if block[0].startswith("_block_without_name_"):
            block_names_1.append("block_without_name")
        elif not block[0].startswith("_empty_block_") and (
            not is_class or len(block_names_1) == 0 or block_names_1[-1].startswith("block_without_name")
        ):
            block_names_1.append("block_with_name")

    for block in splits_2[1:]:
        if block[0].startswith("_block_without_name_"):
            block_names_2.append("block_without_name")
        elif not block[0].startswith("_empty_block_") and (
            not is_class or len(block_names_2) == 0 or block_names_2[-1].startswith("block_without_name")
        ):
            block_names_2.append("block_with_name")

    if is_class:
        if block_names_1 not in [
            ["block_without_name"],
            ["block_with_name"],
            ["block_without_name", "block_with_name"],
        ]:
            raise ValueError(
248
249
                f"""Class defined in {filename} doesn't have the expected stucture.
                See the docstring of `_sanity_check_splits` in the file `utils/check_copies.py`""",
Yih-Dar's avatar
Yih-Dar committed
250
251
252
            )

    if block_names_1 != block_names_2:
253
        raise ValueError(f"In {filename}, two code blocks expected to be copies have different structures.")
Yih-Dar's avatar
Yih-Dar committed
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292


def find_block_end(lines: List[str], start_index: int, indent: int) -> int:
    """
    Find the end of the class/func block starting at `start_index` in a source code (defined by `lines`).

    Args:
        lines (`List[str]`):
            The source code, represented by a list of lines.
        start_index (`int`):
            The starting index of the target class/func block.
        indent (`int`):
            The indent of the class/func body.

    Returns:
        `int`: The index of the block's ending line plus by 1 (i.e. exclusive).
    """
    indent = " " * indent
    # enter the block body
    line_index = start_index + 1

    while line_index < len(lines) and _should_continue(lines[line_index], indent):
        line_index += 1
    # Clean up empty lines at the end (if any).
    while len(lines[line_index - 1]) <= 1:
        line_index -= 1

    return line_index


def split_code_into_blocks(
    lines: List[str], start_index: int, end_index: int, indent: int, backtrace: bool = False
) -> List[Tuple[str, int, int]]:
    """
    Split the class/func block starting at `start_index` in a source code (defined by `lines`) into *inner blocks*.

    The block's header is included as the first element. The contiguous regions (without empty lines) that are not
    inside any inner block are included as blocks. The contiguous regions of empty lines that are not inside any inner
    block are also included as (dummy) blocks.
293

Yih-Dar's avatar
Yih-Dar committed
294
295
296
297
298
299
300
301
302
303
304
305
    Args:
        lines (`List[str]`):
            The source code, represented by a list of lines.
        start_index (`int`):
            The starting index of the target class/func block.
        end_index (`int`):
            The ending index of the target class/func block.
        indent (`int`):
            The indent of the class/func body.
        backtrace (`bool`, *optional*, defaults to `False`):
            Whether or not to include the lines before the inner class/func block's header (e.g. comments, decorators,
            etc.) until an empty line is encountered.
306

Yih-Dar's avatar
Yih-Dar committed
307
308
309
310
311
    Returns:
        `List[Tuple[str, int, int]]`: A list of elements with the form `(block_name, start_index, end_index)`.
    """
    splits = []
    # `indent - 4` is the indent level of the target class/func header
312
313
314
315
    try:
        target_block_name = re.search(
            rf"^{' ' * (indent - 4)}((class|def)\s+\S+)(\(|\:)", lines[start_index]
        ).groups()[0]
Arthur's avatar
Arthur committed
316
317
318
    except Exception:
        start_context = min(start_index - 10, 0)
        end_context = min(end_index + 10, len(lines))
319
        raise ValueError(
Arthur's avatar
Arthur committed
320
321
            f"Tried to split a class or function. It did not work. Error comes from line {start_index}: \n```\n"
            + "".join(lines[start_context:end_context])
322
323
            + "```\n"
        )
Yih-Dar's avatar
Yih-Dar committed
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394

    # from now on, the `block` means inner blocks unless explicitly specified
    indent_str = " " * indent
    block_without_name_idx = 0
    empty_block_idx = 0

    # Find the lines for the definition header
    index = start_index
    if "(" in lines[start_index] and "):" not in lines[start_index] in lines[start_index]:
        while index < end_index:
            if _is_definition_header_ending_line(lines[index]):
                break
            index += 1

    # the first line outside the definition header
    index += 1
    splits.append((target_block_name, start_index, index))

    block_start_index, prev_block_end_index = index, index
    while index < end_index:
        # if found, it will be an inner block
        block_found = re.search(rf"^{indent_str}((class|def)\s+\S+)(\(|\:)", lines[index])
        if block_found:
            name = block_found.groups()[0]

            block_end_index = find_block_end(lines, index, indent + 4)

            # backtrace to include the lines before the found block's definition header (e.g. comments, decorators,
            # etc.) until an empty line is encountered.
            block_start_index = index
            if index > prev_block_end_index and backtrace:
                idx = index - 1
                for idx in range(index - 1, prev_block_end_index - 2, -1):
                    if not (len(lines[idx].strip()) > 0 and lines[idx].startswith(indent_str)):
                        break
                idx += 1
                if idx < index:
                    block_start_index = idx

            # between the current found block and the previous found block
            if block_start_index > prev_block_end_index:
                # give it a dummy name
                if len("".join(lines[prev_block_end_index:block_start_index]).strip()) == 0:
                    prev_block_name = f"_empty_block_{empty_block_idx}"
                    empty_block_idx += 1
                else:
                    prev_block_name = f"_block_without_name_{block_without_name_idx}"
                    block_without_name_idx += 1
                # Add it as a block
                splits.append((prev_block_name, prev_block_end_index, block_start_index))

            # Add the current found block
            splits.append((name, block_start_index, block_end_index))
            prev_block_end_index = block_end_index
            index = block_end_index - 1

        index += 1

    if index > prev_block_end_index:
        if len("".join(lines[prev_block_end_index:index]).strip()) == 0:
            prev_block_name = f"_empty_block_{empty_block_idx}"
        else:
            prev_block_name = f"_block_without_name_{block_without_name_idx}"
        splits.append((prev_block_name, prev_block_end_index, index))

    return splits


def find_code_in_transformers(
    object_name: str, base_path: str = None, return_indices: bool = False
) -> Union[str, Tuple[List[str], int, int]]:
Sylvain Gugger's avatar
Sylvain Gugger committed
395
396
397
398
    """
    Find and return the source code of an object.

    Args:
399
400
401
402
        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`.
Yih-Dar's avatar
Yih-Dar committed
403
404
405
406
        return_indices(`bool`, *optional*, defaults to `False`):
            If `False`, will only return the code (as a string), otherwise it will also return the whole lines of the
            file where the object specified by `object_name` is defined, together the start/end indices of the block in
            the file that defines the object.
Sylvain Gugger's avatar
Sylvain Gugger committed
407
408

    Returns:
Yih-Dar's avatar
Yih-Dar committed
409
410
411
        `Union[str, Tuple[List[str], int, int]]`: If `return_indices=False`, only the source code of the object will be
        returned. Otherwise, it also returns the whole lines of the file where the object specified by `object_name` is
        defined, together the start/end indices of the block in the file that defines the object.
Sylvain Gugger's avatar
Sylvain Gugger committed
412
    """
413
414
415
    parts = object_name.split(".")
    i = 0

416
417
418
419
420
421
422
423
424
425
426
427
    # 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"

428
429
    # First let's find the module where our object lives.
    module = parts[i]
430
    while i < len(parts) and not os.path.isfile(os.path.join(base_path, f"{module}.py")):
431
        i += 1
432
433
        if i < len(parts):
            module = os.path.join(module, parts[i])
434
435
436
437
438
    if i >= len(parts):
        raise ValueError(
            f"`object_name` should begin with the name of a module of transformers but got {object_name}."
        )

439
    with open(os.path.join(base_path, f"{module}.py"), "r", encoding="utf-8", newline="\n") as f:
440
441
442
443
444
445
        lines = f.readlines()

    # Now let's find the class / func in the code!
    indent = ""
    line_index = 0
    for name in parts[i + 1 :]:
446
        while (
447
            line_index < len(lines) and re.search(rf"^{indent}(class|def)\s+{name}(\(|\:)", lines[line_index]) is None
448
        ):
449
            line_index += 1
Yih-Dar's avatar
Yih-Dar committed
450
        # find the target specified in the current level in `parts` -> increase `indent` so we can search the next
451
        indent += "    "
Yih-Dar's avatar
Yih-Dar committed
452
        # the index of the first line in the (currently found) block *body*
453
454
455
456
457
        line_index += 1

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

Yih-Dar's avatar
Yih-Dar committed
458
459
    # `indent` is already one level deeper than the (found) class/func block's definition header

460
    # We found the beginning of the class / func, now let's find the end (when the indent diminishes).
Yih-Dar's avatar
Yih-Dar committed
461
    # `start_index` is the index of the class/func block's definition header
462
    start_index = line_index - 1
Yih-Dar's avatar
Yih-Dar committed
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
    end_index = find_block_end(lines, start_index, len(indent))

    code = "".join(lines[start_index:end_index])
    return (code, (lines, start_index, end_index)) if return_indices else code


def replace_code(code: str, replace_pattern: str) -> str:
    """Replace `code` by a pattern of the form `with X1->X2,Y1->Y2,Z1->Z2`.

    Args:
        code (`str`): The code to be modified.
        replace_pattern (`str`): The pattern used to modify `code`.

    Returns:
        `str`: The modified code.
    """
    if len(replace_pattern) > 0:
        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()
            code = re.sub(obj1, obj2, code)
            if option.strip() == "all-casing":
                code = re.sub(obj1.lower(), obj2.lower(), code)
                code = re.sub(obj1.upper(), obj2.upper(), code)

    return code

493

Yih-Dar's avatar
Yih-Dar committed
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
def find_code_and_splits(object_name: str, base_path: str, buffer: dict = None):
    """Find the code of an object (specified by `object_name`) and split it into blocks.

    Args:
        object_name (`str`):
            The name of the object, e.g. `transformers.models.bert.modeling_bert.BertAttention` or
            `tests.models.llama.test_modeling_llama.LlamaModelTest.test_config`.
        base_path (`str`):
            The path to the base directory within which the search will be performed. It could be either
            `TRANSFORMERS_PATH` or `MODEL_TEST_PATH`.
        buffer (`dict`, *optional*):
            The buffer used to store the previous results in order to speed up the process.

    Returns:
        lines (`List[str]`):
            The lines of the whole file where the object is defined.
        code (`str`):
            The object's code.
        code_splits (`List[Tuple[str, int, int]]`):
            `code` splitted into blocks. See `split_code_into_blocks`.
    """
    if buffer is None:
        buffer = {}

    if (object_name, base_path) in buffer:
        lines, code, code_splits = buffer[(object_name, base_path)]
    else:
        code, (lines, target_start_index, target_end_index) = find_code_in_transformers(
            object_name, base_path=base_path, return_indices=True
        )
        indent = get_indent(code)

        # Split the code into blocks
        # `indent` is the indent of the class/func definition header, but `code_splits` expects the indent level of the
        # block body.
        code_splits = split_code_into_blocks(
            lines, target_start_index, target_end_index, len(indent) + 4, backtrace=True
        )
        buffer[(object_name, base_path)] = lines, code, code_splits

    return lines, code, code_splits
535
536
537


_re_copy_warning = re.compile(r"^(\s*)#\s*Copied from\s+transformers\.(\S+\.\S+)\s*($|\S.*$)")
538
_re_copy_warning_for_test_file = re.compile(r"^(\s*)#\s*Copied from\s+tests\.(\S+\.\S+)\s*($|\S.*$)")
539
_re_replace_pattern = re.compile(r"^\s*(\S+)->(\S+)(\s+.*|$)")
540
_re_fill_pattern = re.compile(r"<FILL\s+[^>]*>")
541
542


Sylvain Gugger's avatar
Sylvain Gugger committed
543
544
545
546
547
548
549
550
551
552
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).
    """
553
554
555
556
557
558
    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]
559
560
561
    return ""


Arthur's avatar
Arthur committed
562
563
564
565
566
def run_ruff(code, check=False):
    if check:
        command = ["ruff", "check", "-", "--fix", "--exit-zero"]
    else:
        command = ["ruff", "format", "-", "--config", "pyproject.toml", "--silent"]
567
568
569
570
571
572
    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:
573
    """
574
575
    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
576
577
578
579
580
581

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

    Returns:
        `str`: The formatted code.
582
583
584
585
    """
    has_indent = len(get_indent(code)) > 0
    if has_indent:
        code = f"class Bla:\n{code}"
586
587
    formatted_code = run_ruff(code)
    return formatted_code[len("class Bla:\n") :] if has_indent else formatted_code
588
589


Sylvain Gugger's avatar
Sylvain Gugger committed
590
def check_codes_match(observed_code: str, theoretical_code: str) -> Optional[int]:
591
    """
Sylvain Gugger's avatar
Sylvain Gugger committed
592
593
594
595
596
597
598
599
600
    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.
601
602
603
604
    """
    observed_code_header = observed_code.split("\n")[0]
    theoretical_code_header = theoretical_code.split("\n")[0]

Sylvain Gugger's avatar
Sylvain Gugger committed
605
    # Catch the function/class name: it is expected that those do not match.
606
607
608
609
    _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:
Arthur's avatar
Arthur committed
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
            try:
                observed_obj_name = re_pattern.search(observed_code_header).groups()[0]
            except Exception:
                raise ValueError(
                    "Tried to split a class or function. It did not work. Error comes from: \n```\n"
                    + observed_code_header
                    + "\n```\n"
                )

            try:
                theoretical_name = re_pattern.search(theoretical_code_header).groups()[0]
            except Exception:
                raise ValueError(
                    "Tried to split a class or function. It did not work. Error comes from: \n```\n"
                    + theoretical_code_header
                    + "\n```\n"
                )
627
628
            theoretical_code_header = theoretical_code_header.replace(theoretical_name, observed_obj_name)

Sylvain Gugger's avatar
Sylvain Gugger committed
629
    # Find the first diff. Line 0 is special since we need to compare with the function/class names ignored.
630
631
632
633
634
635
636
637
638
639
640
    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


Yih-Dar's avatar
Yih-Dar committed
641
def is_copy_consistent(filename: str, overwrite: bool = False, buffer: dict = None) -> Optional[List[Tuple[str, int]]]:
642
    """
Sylvain Gugger's avatar
Sylvain Gugger committed
643
    Check if the code commented as a copy in a file matches the original.
644

Sylvain Gugger's avatar
Sylvain Gugger committed
645
646
647
648
649
    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.
Yih-Dar's avatar
Yih-Dar committed
650
651
        buffer (`dict`, *optional*):
            The buffer used to store the previous results in order to speed up the process.
Sylvain Gugger's avatar
Sylvain Gugger committed
652
653
654
655

    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.
656
    """
Yih-Dar's avatar
Yih-Dar committed
657
658
    base_path = TRANSFORMERS_PATH if not filename.startswith("tests") else MODEL_TEST_PATH

659
    with open(filename, "r", encoding="utf-8", newline="\n") as f:
660
        lines = f.readlines()
661
    diffs = []
662
    line_index = 0
663
    # Not a for loop cause `lines` is going to change (if `overwrite=True`).
664
    search_re = _re_copy_warning_for_test_file if filename.startswith("tests") else _re_copy_warning
665
    while line_index < len(lines):
666
        search = search_re.search(lines[line_index])
667
668
669
670
671
672
        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()
673

Yih-Dar's avatar
Yih-Dar committed
674
675
676
677
678
679
680
681
682
683
684
685
686
        # Find the file lines, the object's code, and its blocks
        target_lines, theoretical_code, theoretical_code_splits = find_code_and_splits(
            object_name, base_path, buffer=buffer
        )

        # code replaced by the patterns
        theoretical_code_blocks = OrderedDict()
        for name, start, end in theoretical_code_splits:
            name = replace_code(name, replace_pattern)
            code = "".join(target_lines[start:end])
            code = replace_code(code, replace_pattern)
            theoretical_code_blocks[name] = code

687
        theoretical_indent = get_indent(theoretical_code)
688

Yih-Dar's avatar
Yih-Dar committed
689
690
        # `start_index` is the index of the first line (the definition header) after `# Copied from`.
        # (`indent != theoretical_indent` doesn't seem to occur so far, not sure what this case is for.)
691
        start_index = line_index + 1 if indent == theoretical_indent else line_index
Yih-Dar's avatar
Yih-Dar committed
692
        # enter the block body
693
        line_index = start_index + 1
694

695
696
        subcode = "\n".join(theoretical_code.split("\n")[1:])
        indent = get_indent(subcode)
697
        # Loop to check the observed code, stop when indentation diminishes or if we see a End copy comment.
Yih-Dar's avatar
Yih-Dar committed
698
        # We can't call `find_block_end` directly as there is sth. special `# End copy"` here.
699
700
701
702
703
704
        should_continue = True
        while line_index < len(lines) and should_continue:
            line_index += 1
            if line_index >= len(lines):
                break
            line = lines[line_index]
705
706
            # There is a special pattern `# End copy` to stop early. It's not documented cause it shouldn't really be
            # used.
707
            should_continue = _should_continue(line, indent) and re.search(f"^{indent}# End copy", line) is None
Yih-Dar's avatar
Yih-Dar committed
708
        # `line_index` is outside the block
709
710
711
712
        # Clean up empty lines at the end (if any).
        while len(lines[line_index - 1]) <= 1:
            line_index -= 1

Yih-Dar's avatar
Yih-Dar committed
713
714
715
716
717
        # Split the observed code into blocks
        observed_code_splits = split_code_into_blocks(lines, start_index, line_index, len(indent), backtrace=True)

        is_class = lines[start_index].startswith(f"{' ' * (len(indent) - 4)}class ")
        # sanity check
718
        _sanity_check_splits(theoretical_code_splits, observed_code_splits, is_class=is_class, filename=filename)
Yih-Dar's avatar
Yih-Dar committed
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755

        # observed code in a structured way (a dict mapping block names to blocks' code)
        observed_code_blocks = OrderedDict()
        for name, start, end in observed_code_splits:
            code = "".join(lines[start:end])
            observed_code_blocks[name] = code

        # Below, we change some names in `theoretical_code_blocks` and `observed_code_blocks`. These mappings map the
        # original names to the modified names: this is used to restore the original order of the code blocks.
        name_mappings_1 = {k: k for k in theoretical_code_blocks.keys()}
        name_mappings_2 = {k: k for k in observed_code_blocks.keys()}

        # Update code blocks' name and content:
        #   If `"# Ignore copy"` is found in a block of the observed code:
        #     1. if it's a block only in the observed code --> add it to the theoretical code.
        #     2. if it's also in the theoretical code () --> put its content (body) to the corresponding block under the
        #        same name in the theoretical code.
        #   In both cases, we change the name to have a prefix `_ignored_` so we know if we can discard them during the
        #   comparison.
        ignored_existing_block_index = 0
        ignored_new_block_index = 0
        for name in list(observed_code_blocks.keys()):
            code = observed_code_blocks[name]
            if "# Ignore copy" in code:
                if name in theoretical_code_blocks:
                    # in the target --> just copy the content
                    del theoretical_code_blocks[name]
                    theoretical_code_blocks[f"_ignored_existing_block_{ignored_existing_block_index}"] = code
                    name_mappings_1[name] = f"_ignored_existing_block_{ignored_existing_block_index}"

                    del observed_code_blocks[name]
                    observed_code_blocks[f"_ignored_existing_block_{ignored_existing_block_index}"] = code
                    name_mappings_2[name] = f"_ignored_existing_block_{ignored_existing_block_index}"
                    ignored_existing_block_index += 1
                else:
                    # not in the target --> add it
                    theoretical_code_blocks[f"_ignored_new_block_{ignored_new_block_index}"] = code
Arthur's avatar
Arthur committed
756
                    name_mappings_1[f"_ignored_new_block_{ignored_new_block_index}"] = (
Yih-Dar's avatar
Yih-Dar committed
757
                        f"_ignored_new_block_{ignored_new_block_index}"
Arthur's avatar
Arthur committed
758
                    )
Yih-Dar's avatar
Yih-Dar committed
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787

                    del observed_code_blocks[name]
                    observed_code_blocks[f"_ignored_new_block_{ignored_new_block_index}"] = code
                    name_mappings_2[name] = f"_ignored_new_block_{ignored_new_block_index}"
                    ignored_new_block_index += 1

        # Respect the original block order:
        #   1. in `theoretical_code_blocks`: the new blocks will follow the existing ones
        #   2. in `observed_code_blocks`: the original order are kept with names modified potentially. This is necessary
        #      to compute the correct `diff_index` if `overwrite=True` and there is a diff.
        theoretical_code_blocks = {
            name_mappings_1[orig_name]: theoretical_code_blocks[name_mappings_1[orig_name]]
            for orig_name in name_mappings_1
        }
        observed_code_blocks = {
            name_mappings_2[orig_name]: observed_code_blocks[name_mappings_2[orig_name]]
            for orig_name in name_mappings_2
        }

        # Ignore the blocks specified to be ignored. This is the version used to check if there is a mismatch
        theoretical_code_blocks_clean = {
            k: v
            for k, v in theoretical_code_blocks.items()
            if not (k.startswith(("_ignored_existing_block_", "_ignored_new_block_")))
        }
        theoretical_code = "".join(list(theoretical_code_blocks_clean.values()))

        # stylify `theoretical_code` before compare (this is needed only when `replace_pattern` is not empty)
        if replace_pattern:
788
            theoretical_code = stylify(theoretical_code)
Yih-Dar's avatar
Yih-Dar committed
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
        # Remove `\n\n` in `theoretical_code` before compare (so no empty line)
        while "\n\n" in theoretical_code:
            theoretical_code = theoretical_code.replace("\n\n", "\n")

        # Compute `observed_code` where we don't include any empty line + keep track the line index between the
        # original/processed `observed_code` so we can have the correct `diff_index`.
        idx_to_orig_idx_mapping_for_observed_code_lines = {}
        idx = -1
        orig_idx = -1
        observed_code = ""
        for name, code in observed_code_blocks.items():
            if code.endswith("\n"):
                code = code[:-1]
            for code_line in code.split("\n"):
                orig_idx += 1
                if code_line.strip() and not name.startswith(("_ignored_existing_block_", "_ignored_new_block_")):
                    idx += 1
                    observed_code += code_line + "\n"
                    idx_to_orig_idx_mapping_for_observed_code_lines[idx] = orig_idx
808

809
        # Test for a diff and act accordingly.
810
811
        diff_index = check_codes_match(observed_code, theoretical_code)
        if diff_index is not None:
Yih-Dar's avatar
Yih-Dar committed
812
813
            # switch to the index in the original `observed_code` (i.e. before removing empty lines)
            diff_index = idx_to_orig_idx_mapping_for_observed_code_lines[diff_index]
814
            diffs.append([object_name, diff_index + start_index + 1])
815
            if overwrite:
Yih-Dar's avatar
Yih-Dar committed
816
817
818
819
                # `theoretical_code_to_write` is a single string but may have several lines.
                theoretical_code_to_write = stylify("".join(list(theoretical_code_blocks.values())))
                lines = lines[:start_index] + [theoretical_code_to_write] + lines[line_index:]
                # Here we treat it as a single entry in `lines`.
820
821
                line_index = start_index + 1

822
    if overwrite and len(diffs) > 0:
823
824
        # Warn the user a file has been modified.
        print(f"Detected changes, rewriting {filename}.")
825
        with open(filename, "w", encoding="utf-8", newline="\n") as f:
826
            f.writelines(lines)
827
    return diffs
828
829


Yih-Dar's avatar
Yih-Dar committed
830
def check_copies(overwrite: bool = False, file: str = None):
831
    """
Sylvain Gugger's avatar
Sylvain Gugger committed
832
    Check every file is copy-consistent with the original. Also check the model list in the main README and other
833
    READMEs are consistent.
Sylvain Gugger's avatar
Sylvain Gugger committed
834
835
836
837

    Args:
        overwrite (`bool`, *optional*, defaults to `False`):
            Whether or not to overwrite the copies when they don't match.
Yih-Dar's avatar
Yih-Dar committed
838
839
        file (`bool`, *optional*):
            The path to a specific file to check and/or fix.
840
    """
Yih-Dar's avatar
Yih-Dar committed
841
842
843
844
845
846
847
848
    buffer = {}

    if file is None:
        all_files = glob.glob(os.path.join(TRANSFORMERS_PATH, "**/*.py"), recursive=True)
        all_test_files = glob.glob(os.path.join(MODEL_TEST_PATH, "**/*.py"), recursive=True)
        all_files = list(all_files) + list(all_test_files)
    else:
        all_files = [file]
849

850
851
    diffs = []
    for filename in all_files:
Yih-Dar's avatar
Yih-Dar committed
852
        new_diffs = is_copy_consistent(filename, overwrite, buffer)
853
        diffs += [f"- {filename}: copy does not match {d[0]} at line {d[1]}" for d in new_diffs]
854
855
856
    if not overwrite and len(diffs) > 0:
        diff = "\n".join(diffs)
        raise Exception(
857
            "Found the following copy inconsistencies:\n"
858
            + diff
859
            + "\nRun `make fix-copies` or `python utils/check_copies.py --fix_and_overwrite` to fix them."
860
        )
861
862


863
def check_full_copies(overwrite: bool = False):
864
    """
Sylvain Gugger's avatar
Sylvain Gugger committed
865
866
867
868
869
    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.
870
    """
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
    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
894
895
896
897
898
899
900
901
902
903
904
905
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.
    """
906
    with open(os.path.join(REPO_PATH, filename), "r", encoding="utf-8", newline="\n") as f:
907
908
909
        lines = f.readlines()
    # Find the start of the list.
    start_index = 0
910
    while not lines[start_index].startswith(start_prompt):
911
912
913
914
915
916
917
        start_index += 1
    start_index += 1

    result = []
    current_line = ""
    end_index = start_index

Sylvain Gugger's avatar
Sylvain Gugger committed
918
    # Keep going until the end of the list.
919
    while not lines[end_index].startswith(end_prompt):
920
921
922
923
924
925
926
927
928
929
930
931
932
        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
933
def convert_to_localized_md(model_list: str, localized_model_list: str, format_str: str) -> Tuple[bool, str]:
934
935
936
937
938
939
940
941
942
943
944
945
946
947
    """
    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.
    """
948
949
950
951
952
953
954
955
956
957
958
959
960

    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,
961
962
    # affiliations of the paper, title of the paper, authors of the paper, and supplemental data (see DistilBERT for
    # example).
963
964
965
    _re_capture_meta = re.compile(
        r"\*\*\[([^\]]*)\]\(([^\)]*)\)\*\* \(from ([^)]*)\)[^\[]*([^\)]*\)).*?by (.*?[A-Za-z\*]{2,}?)\. (.*)$"
    )
966
    # This regex is used to synchronize title link.
967
    _re_capture_title_link = re.compile(r"\*\*\[([^\]]*)\]\(([^\)]*)\)\*\*")
968
969
    # This regex is used to synchronize paper title and link.
    _re_capture_paper_link = re.compile(r" \[([^\]]*)\]\(([^\)]*)\)")
970
971
972
973
974
975
976
977
978
979
980
981

    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.")

982
983
984
    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.
985
    readmes_match = not any(k not in model_keys for k in localized_model_index)
986
987
    localized_model_index = {k: v for k, v in localized_model_index.items() if k in model_keys}

988
    for model in model_list.strip().split("\n"):
989
        title, model_link = _re_capture_title_link.search(model).groups()
990
        if title not in localized_model_index:
991
            readmes_match = False
992
993
            # Add an anchor white space behind a model description string for regex.
            # If metadata cannot be captured, the English version will be directly copied.
994
            localized_model_index[title] = _re_capture_meta.sub(_rep, model + " ")
995
996
997
998
999
        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
1000
        else:
1001
1002
            # Synchronize title link
            converted_model = _re_capture_title_link.sub(
1003
1004
                f"**[{title}]({model_link})**", localized_model_index[title], count=1
            )
1005

1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
            # Synchronize paper title and its link (if found)
            paper_title_link = _re_capture_paper_link.search(model)
            if paper_title_link is not None:
                paper_title, paper_link = paper_title_link.groups()
                converted_model = _re_capture_paper_link.sub(
                    f" [{paper_title}]({paper_link})", converted_model, count=1
                )

            if converted_model != localized_model_index[title]:
                readmes_match = False
                localized_model_index[title] = converted_model

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

1020
    return readmes_match, "\n".join((x[1] for x in sorted_index)) + "\n"
1021
1022


Sylvain Gugger's avatar
Sylvain Gugger committed
1023
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
1024
    """
Sylvain Gugger's avatar
Sylvain Gugger committed
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
    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
1035
1036
    """
    with open(filename, "r", encoding="utf-8", newline="\n") as f:
1037
        lines = f.readlines()
Sylvain Gugger's avatar
Sylvain Gugger committed
1038
    # Find the start prompt.
1039
    start_index = 0
Sylvain Gugger's avatar
Sylvain Gugger committed
1040
    while not lines[start_index].startswith(start_prompt):
1041
1042
1043
1044
        start_index += 1
    start_index += 1

    end_index = start_index
Sylvain Gugger's avatar
Sylvain Gugger committed
1045
    while not lines[end_index].startswith(end_prompt):
1046
1047
1048
1049
1050
1051
1052
1053
        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
1054
1055
    return "".join(lines[start_index:end_index]), start_index, end_index, lines

1056

1057
# Map a model name with the name it has in the README for the check_readme check
1058
1059
1060
1061
1062
1063
SPECIAL_MODEL_NAMES = {
    "Bert Generation": "BERT For Sequence Generation",
    "BigBird": "BigBird-RoBERTa",
    "Data2VecAudio": "Data2Vec",
    "Data2VecText": "Data2Vec",
    "Data2VecVision": "Data2Vec",
1064
    "DonutSwin": "Swin Transformer",
1065
    "Marian": "MarianMT",
1066
    "MaskFormerSwin": "Swin Transformer",
1067
1068
1069
    "OpenAI GPT-2": "GPT-2",
    "OpenAI GPT": "GPT",
    "Perceiver": "Perceiver IO",
1070
    "SAM": "Segment Anything",
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
    "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
1085
    "TimmBackbone",
1086
1087
    "Vision Encoder decoder",
    "VisionTextDualEncoder",
1088
    "CLIPVisionModel",
NielsRogge's avatar
NielsRogge committed
1089
    "SiglipVisionModel",
1090
    "ChineseCLIPVisionModel",
1091
1092
]

1093
# Template for new entries to add in the main README when we have missing models.
1094
README_TEMPLATE = (
1095
1096
    "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>."
1097
1098
1099
)


1100
1101
if __name__ == "__main__":
    parser = argparse.ArgumentParser()
Yih-Dar's avatar
Yih-Dar committed
1102
    parser.add_argument("--file", type=str, default=None, help="A specific file to check and/or fix")
1103
1104
1105
    parser.add_argument("--fix_and_overwrite", action="store_true", help="Whether to fix inconsistencies.")
    args = parser.parse_args()

Yih-Dar's avatar
Yih-Dar committed
1106
    check_copies(args.fix_and_overwrite, args.file)
1107
    check_full_copies(args.fix_and_overwrite)