Some checks failed
Self-hosted runner (nightly-past-ci-caller) / Get number (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.11 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.10 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.9 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.8 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.7 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.6 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.5 (push) Has been cancelled
Self-hosted runner (benchmark) / Benchmark (aws-g5-4xlarge-cache) (push) Has been cancelled
Build documentation / build (push) Has been cancelled
Build documentation / build_other_lang (push) Has been cancelled
CodeQL Security Analysis / CodeQL Analysis (push) Has been cancelled
New model PR merged notification / Notify new model (push) Has been cancelled
PR CI / pr-ci (push) Has been cancelled
Slow tests on important models (on Push - A10) / Get all modified files (push) Has been cancelled
Secret Leaks / trufflehog (push) Has been cancelled
Update Transformers metadata / build_and_package (push) Has been cancelled
Slow tests on important models (on Push - A10) / Model CI (push) Has been cancelled
Check Tiny Models / Check tiny models (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / Model CI (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / Pipeline CI (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / Example CI (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / DeepSpeed CI (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / Trainer/FSDP CI (push) Has been cancelled
Nvidia CI - Flash Attn / Setup (push) Has been cancelled
Nvidia CI - Flash Attn / Model CI (push) Has been cancelled
Nvidia CI / Setup (push) Has been cancelled
Nvidia CI / Model CI (push) Has been cancelled
Nvidia CI / Torch pipeline CI (push) Has been cancelled
Nvidia CI / Example CI (push) Has been cancelled
Nvidia CI / Trainer/FSDP CI (push) Has been cancelled
Nvidia CI / DeepSpeed CI (push) Has been cancelled
Nvidia CI / Quantization CI (push) Has been cancelled
Nvidia CI / Kernels CI (push) Has been cancelled
Doctests / Setup (push) Has been cancelled
Doctests / Call doctest jobs (push) Has been cancelled
Doctests / Send results to webhook (push) Has been cancelled
Extras Smoke Test / Get supported Python versions (push) Has been cancelled
Extras Smoke Test / Test extras on Python ${{ matrix.python-version }} (push) Has been cancelled
Extras Smoke Test / Check Slack token availability (push) Has been cancelled
Extras Smoke Test / Notify failures to Slack (push) Has been cancelled
Self-hosted runner (AMD scheduled CI caller) / Trigger Scheduled AMD CI (push) Has been cancelled
Stale Bot / Close Stale Issues (push) Has been cancelled
131 lines
4.5 KiB
Python
131 lines
4.5 KiB
Python
# Copyright 2022 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.
|
|
"""
|
|
Utility that sorts the names in the auto mappings defines in the auto modules in alphabetical order.
|
|
|
|
Use from the root of the repo with:
|
|
|
|
```bash
|
|
python utils/sort_auto_mappings.py
|
|
```
|
|
|
|
to auto-fix all the auto mappings (used in `make style`).
|
|
|
|
To only check if the mappings are properly sorted (as used in `make check-repo`), do:
|
|
|
|
```bash
|
|
python utils/sort_auto_mappings.py --check_only
|
|
```
|
|
"""
|
|
|
|
import argparse
|
|
import os
|
|
import re
|
|
|
|
|
|
CHECKER_CONFIG = {
|
|
"name": "sort_auto_mappings",
|
|
"label": "Sort auto mappings",
|
|
"cache_globs": ["src/transformers/models/auto/*.py"],
|
|
"check_args": ["--check_only"],
|
|
"fix_args": [],
|
|
}
|
|
|
|
# Path are set with the intent you should run this script from the root of the repo.
|
|
PATH_TO_AUTO_MODULE = "src/transformers/models/auto"
|
|
|
|
|
|
# re pattern that matches XXX_MAPPING_NAMES or SPECIAL_MODEL_TYPE_TO_MODULE_NAMES
|
|
_re_intro_mapping = re.compile(r"[A-Z_]+(_MAPPING|_MODEL_TYPE_TO_MODULE)(\s+|_[A-Z_]+\s+)=\s+OrderedDict(?!\(\*)")
|
|
# re pattern that matches identifiers in mappings
|
|
_re_identifier = re.compile(r'\s*\(\s*"(\S[^"]+)"')
|
|
|
|
|
|
def sort_auto_mapping(fname: str, overwrite: bool = False) -> bool | None:
|
|
"""
|
|
Sort all auto mappings in a file.
|
|
|
|
Args:
|
|
fname (`str`): The name of the file where we want to sort auto-mappings.
|
|
overwrite (`bool`, *optional*, defaults to `False`): Whether or not to fix and overwrite the file.
|
|
|
|
Returns:
|
|
`Optional[bool]`: Returns `None` if `overwrite=True`. Otherwise returns `True` if the file has an auto-mapping
|
|
improperly sorted, `False` if the file is okay.
|
|
"""
|
|
with open(fname, "r", encoding="utf-8") as f:
|
|
content = f.read()
|
|
|
|
lines = content.split("\n")
|
|
new_lines = []
|
|
line_idx = 0
|
|
while line_idx < len(lines):
|
|
if _re_intro_mapping.search(lines[line_idx]) is not None:
|
|
# Start of a new mapping!
|
|
indent = len(re.search(r"^(\s*)\S", lines[line_idx]).groups()[0]) + 8
|
|
while not lines[line_idx].startswith(" " * indent + "("):
|
|
new_lines.append(lines[line_idx])
|
|
line_idx += 1
|
|
|
|
blocks = []
|
|
while lines[line_idx].strip() != "]":
|
|
# Blocks either fit in one line or not
|
|
if lines[line_idx].strip() == "(":
|
|
start_idx = line_idx
|
|
while not lines[line_idx].startswith(" " * indent + ")"):
|
|
line_idx += 1
|
|
blocks.append("\n".join(lines[start_idx : line_idx + 1]))
|
|
else:
|
|
blocks.append(lines[line_idx])
|
|
line_idx += 1
|
|
|
|
# Sort blocks by their identifiers
|
|
blocks = sorted(blocks, key=lambda x: _re_identifier.search(x).groups()[0])
|
|
new_lines += blocks
|
|
else:
|
|
new_lines.append(lines[line_idx])
|
|
line_idx += 1
|
|
|
|
if overwrite:
|
|
with open(fname, "w", encoding="utf-8") as f:
|
|
f.write("\n".join(new_lines))
|
|
else:
|
|
return "\n".join(new_lines) != content
|
|
|
|
|
|
def sort_all_auto_mappings(overwrite: bool = False):
|
|
"""
|
|
Sort all auto mappings in the library.
|
|
|
|
Args:
|
|
overwrite (`bool`, *optional*, defaults to `False`): Whether or not to fix and overwrite the file.
|
|
"""
|
|
fnames = [os.path.join(PATH_TO_AUTO_MODULE, f) for f in os.listdir(PATH_TO_AUTO_MODULE) if f.endswith(".py")]
|
|
diffs = [sort_auto_mapping(fname, overwrite=overwrite) for fname in fnames]
|
|
|
|
if not overwrite and any(diffs):
|
|
failures = [f for f, d in zip(fnames, diffs) if d]
|
|
raise ValueError(
|
|
f"The following files have auto mappings that need sorting: {', '.join(failures)}. Run `make style` to fix"
|
|
" this."
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--check_only", action="store_true", help="Whether to only check or fix style.")
|
|
args = parser.parse_args()
|
|
|
|
sort_all_auto_mappings(not args.check_only)
|