Skip to content

Commit 651408a

Browse files
[Styling] stylify using ruff (#27144)
* try to stylify using ruff * might need to remove these changes? * use ruf format andruff check * use isinstance instead of type comparision * use # fmt: skip * use # fmt: skip * nits * soem styling changes * update ci job * nits isinstance * more files update * nits * more nits * small nits * check and format * revert wrong changes * actually use formatter instead of checker * nits * well docbuilder is overwriting this commit * revert notebook changes * try to nuke docbuilder * style * fix feature exrtaction test * remve `indent-width = 4` * fixup * more nits * update the ruff version that we use * style * nuke docbuilder styling * leve the print for detected changes * nits * Remove file I/O Co-authored-by: charliermarsh <[email protected]> * style * nits * revert notebook changes * Add # fmt skip when possible * Add # fmt skip when possible * Fix * More ` # fmt: skip` usage * More ` # fmt: skip` usage * More ` # fmt: skip` usage * NIts * more fixes * fix tapas * Another way to skip * Recommended way * Fix two more fiels * Remove asynch Remove asynch --------- Co-authored-by: charliermarsh <[email protected]>
1 parent acb5b4a commit 651408a

File tree

480 files changed

+868
-1060
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

480 files changed

+868
-1060
lines changed

.circleci/config.yml

+2-3
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,10 @@ jobs:
157157
command: pip freeze | tee installed.txt
158158
- store_artifacts:
159159
path: ~/transformers/installed.txt
160-
- run: black --check examples tests src utils
161-
- run: ruff examples tests src utils
160+
- run: ruff check examples tests src utils
161+
- run: ruff format tests src utils --check
162162
- run: python utils/custom_init_isort.py --check_only
163163
- run: python utils/sort_auto_mappings.py --check_only
164-
- run: doc-builder style src/transformers docs/source --max_len 119 --check_only --path_to_docs docs/source
165164
- run: python utils/check_doc_toc.py
166165

167166
check_repository_consistency:

.circleci/create_circleci_config.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
import argparse
1717
import copy
18-
import glob
1918
import os
2019
import random
2120
from dataclasses import dataclass
@@ -239,7 +238,7 @@ def to_dict(self):
239238

240239
py_command = f'import os; fp = open("reports/{self.job_name}/summary_short.txt"); failed = os.linesep.join([x for x in fp.read().split(os.linesep) if x.startswith("ERROR ")]); fp.close(); fp = open("summary_short.txt", "w"); fp.write(failed); fp.close()'
241240
check_test_command += f"$(python3 -c '{py_command}'); "
242-
check_test_command += f'cat summary_short.txt; echo ""; exit -1; '
241+
check_test_command += 'cat summary_short.txt; echo ""; exit -1; '
243242

244243
# Deeal with failed tests
245244
check_test_command += f'elif [ -s reports/{self.job_name}/failures_short.txt ]; '
@@ -249,7 +248,7 @@ def to_dict(self):
249248

250249
py_command = f'import os; fp = open("reports/{self.job_name}/summary_short.txt"); failed = os.linesep.join([x for x in fp.read().split(os.linesep) if x.startswith("FAILED ")]); fp.close(); fp = open("summary_short.txt", "w"); fp.write(failed); fp.close()'
251250
check_test_command += f"$(python3 -c '{py_command}'); "
252-
check_test_command += f'cat summary_short.txt; echo ""; exit -1; '
251+
check_test_command += 'cat summary_short.txt; echo ""; exit -1; '
253252

254253
check_test_command += f'elif [ -s reports/{self.job_name}/stats.txt ]; then echo "All tests pass!"; '
255254

Makefile

+6-8
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ modified_only_fixup:
99
$(eval modified_py_files := $(shell python utils/get_modified_files.py $(check_dirs)))
1010
@if test -n "$(modified_py_files)"; then \
1111
echo "Checking/fixing $(modified_py_files)"; \
12-
black $(modified_py_files); \
13-
ruff $(modified_py_files) --fix; \
12+
ruff check $(modified_py_files) --fix; \
13+
ruff format $(modified_py_files);\
1414
else \
1515
echo "No library .py files were modified"; \
1616
fi
@@ -48,26 +48,24 @@ repo-consistency:
4848
# this target runs checks on all files
4949

5050
quality:
51-
black --check $(check_dirs) setup.py conftest.py
51+
ruff check $(check_dirs) setup.py conftest.py
52+
ruff format --check $(check_dirs) setup.py conftest.py
5253
python utils/custom_init_isort.py --check_only
5354
python utils/sort_auto_mappings.py --check_only
54-
ruff $(check_dirs) setup.py conftest.py
55-
doc-builder style src/transformers docs/source --max_len 119 --check_only --path_to_docs docs/source
5655
python utils/check_doc_toc.py
5756

5857
# Format source code automatically and check is there are any problems left that need manual fixing
5958

6059
extra_style_checks:
6160
python utils/custom_init_isort.py
6261
python utils/sort_auto_mappings.py
63-
doc-builder style src/transformers docs/source --max_len 119 --path_to_docs docs/source
6462
python utils/check_doc_toc.py --fix_and_overwrite
6563

6664
# this target runs checks on all files and potentially modifies some of them
6765

6866
style:
69-
black $(check_dirs) setup.py conftest.py
70-
ruff $(check_dirs) setup.py conftest.py --fix
67+
ruff check $(check_dirs) setup.py conftest.py --fix
68+
ruff format $(check_dirs) setup.py conftest.py
7169
${MAKE} autogenerate_code
7270
${MAKE} extra_style_checks
7371

docs/source/_config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
black_avoid_patterns = {
1111
"{processor_class}": "FakeProcessorClass",
1212
"{model_class}": "FakeModelClass",
13-
"{object_class}": "FakeObjectClass",
13+
"{object_class}": "FakeObjectClass",
1414
}

docs/source/en/_config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
black_avoid_patterns = {
1111
"{processor_class}": "FakeProcessorClass",
1212
"{model_class}": "FakeModelClass",
13-
"{object_class}": "FakeObjectClass",
13+
"{object_class}": "FakeObjectClass",
1414
}

docs/source/en/tasks/semantic_segmentation.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ logits first, and then reshaped to match the size of the labels before you can c
245245
... reduce_labels=False,
246246
... )
247247
... for key, value in metrics.items():
248-
... if type(value) is np.ndarray:
248+
... if isinstance(value, np.ndarray):
249249
... metrics[key] = value.tolist()
250250
... return metrics
251251
```

docs/source/ko/_config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
black_avoid_patterns = {
1111
"{processor_class}": "FakeProcessorClass",
1212
"{model_class}": "FakeModelClass",
13-
"{object_class}": "FakeObjectClass",
13+
"{object_class}": "FakeObjectClass",
1414
}

docs/source/ko/tasks/semantic_segmentation.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ pip install -q datasets transformers evaluate
242242
... reduce_labels=False,
243243
... )
244244
... for key, value in metrics.items():
245-
... if type(value) is np.ndarray:
245+
... if isinstance(value, np.ndarray):
246246
... metrics[key] = value.tolist()
247247
... return metrics
248248
```

docs/source/pt/_config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
black_avoid_patterns = {
1111
"{processor_class}": "FakeProcessorClass",
1212
"{model_class}": "FakeModelClass",
13-
"{object_class}": "FakeObjectClass",
13+
"{object_class}": "FakeObjectClass",
1414
}

examples/flax/text-classification/run_flax_glue.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def __post_init__(self):
212212
if self.validation_file is not None:
213213
extension = self.validation_file.split(".")[-1]
214214
assert extension in ["csv", "json"], "`validation_file` should be a csv or a json file."
215-
self.task_name = self.task_name.lower() if type(self.task_name) == str else self.task_name
215+
self.task_name = self.task_name.lower() if isinstance(self.task_name, str) else self.task_name
216216

217217

218218
def create_train_state(

examples/legacy/pytorch-lightning/run_glue.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class GLUETransformer(BaseTransformer):
2323
mode = "sequence-classification"
2424

2525
def __init__(self, hparams):
26-
if type(hparams) == dict:
26+
if isinstance(hparams, dict):
2727
hparams = Namespace(**hparams)
2828
hparams.glue_output_mode = glue_output_modes[hparams.task]
2929
num_labels = glue_tasks_num_labels[hparams.task]

examples/legacy/pytorch-lightning/run_ner.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class NERTransformer(BaseTransformer):
2525
mode = "token-classification"
2626

2727
def __init__(self, hparams):
28-
if type(hparams) == dict:
28+
if isinstance(hparams, dict):
2929
hparams = Namespace(**hparams)
3030
module = import_module("tasks")
3131
try:

examples/research_projects/deebert/src/modeling_highway_bert.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def __init__(self, config):
3232
self.early_exit_entropy = [-1 for _ in range(config.num_hidden_layers)]
3333

3434
def set_early_exit_entropy(self, x):
35-
if (type(x) is float) or (type(x) is int):
35+
if isinstance(x, (float, int)):
3636
for i in range(len(self.early_exit_entropy)):
3737
self.early_exit_entropy[i] = x
3838
else:
@@ -232,9 +232,7 @@ def forward(
232232
outputs = (
233233
sequence_output,
234234
pooled_output,
235-
) + encoder_outputs[
236-
1:
237-
] # add hidden_states and attentions if they are here
235+
) + encoder_outputs[1:] # add hidden_states and attentions if they are here
238236
return outputs # sequence_output, pooled_output, (hidden_states), (attentions), highway exits
239237

240238

examples/research_projects/longform-qa/eli5_app.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,7 @@ def answer_question(
158158
</span>
159159
</body>
160160
</html>
161-
""" % (
162-
header_html,
163-
)
161+
""" % (header_html,)
164162
st.sidebar.markdown(
165163
header_full,
166164
unsafe_allow_html=True,

examples/research_projects/lxmert/modeling_frcnn.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1706,9 +1706,7 @@ def from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs):
17061706
elif os.path.isfile(pretrained_model_name_or_path) or is_remote_url(pretrained_model_name_or_path):
17071707
archive_file = pretrained_model_name_or_path
17081708
elif os.path.isfile(pretrained_model_name_or_path + ".index"):
1709-
assert (
1710-
from_tf
1711-
), "We found a TensorFlow checkpoint at {}, please set from_tf to True to load from this checkpoint".format(
1709+
assert from_tf, "We found a TensorFlow checkpoint at {}, please set from_tf to True to load from this checkpoint".format(
17121710
pretrained_model_name_or_path + ".index"
17131711
)
17141712
archive_file = pretrained_model_name_or_path + ".index"

examples/research_projects/movement-pruning/emmental/modeling_bert_masked.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -652,9 +652,7 @@ def forward(
652652
outputs = (
653653
sequence_output,
654654
pooled_output,
655-
) + encoder_outputs[
656-
1:
657-
] # add hidden_states and attentions if they are here
655+
) + encoder_outputs[1:] # add hidden_states and attentions if they are here
658656
return outputs # sequence_output, pooled_output, (hidden_states), (attentions)
659657

660658

examples/research_projects/movement-pruning/masked_run_glue.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,7 @@ def train(args, train_dataset, model, tokenizer, teacher=None):
311311
tr_loss += loss.item()
312312
if (step + 1) % args.gradient_accumulation_steps == 0 or (
313313
# last step in epoch but step is always smaller than gradient_accumulation_steps
314-
len(epoch_iterator) <= args.gradient_accumulation_steps
315-
and (step + 1) == len(epoch_iterator)
314+
len(epoch_iterator) <= args.gradient_accumulation_steps and (step + 1) == len(epoch_iterator)
316315
):
317316
if args.fp16:
318317
nn.utils.clip_grad_norm_(amp.master_params(optimizer), args.max_grad_norm)

examples/research_projects/quantization-qdqbert/quant_trainer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def print_model_summary(model, name_width=25, line_width=180, ignore=None):
239239
continue
240240
if type(mod) in ignore:
241241
continue
242-
if [True for s in ignore if type(s) is str and s in name]:
242+
if [True for s in ignore if isinstance(s, str) and s in name]:
243243
continue
244244
act_str = f"Act:{input_q.extra_repr()}"
245245
wgt_str = f"Wgt:{weight_q.extra_repr()}"

examples/research_projects/visual_bert/modeling_frcnn.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1706,9 +1706,7 @@ def from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs):
17061706
elif os.path.isfile(pretrained_model_name_or_path) or is_remote_url(pretrained_model_name_or_path):
17071707
archive_file = pretrained_model_name_or_path
17081708
elif os.path.isfile(pretrained_model_name_or_path + ".index"):
1709-
assert (
1710-
from_tf
1711-
), "We found a TensorFlow checkpoint at {}, please set from_tf to True to load from this checkpoint".format(
1709+
assert from_tf, "We found a TensorFlow checkpoint at {}, please set from_tf to True to load from this checkpoint".format(
17121710
pretrained_model_name_or_path + ".index"
17131711
)
17141712
archive_file = pretrained_model_name_or_path + ".index"

hubconf.py

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import os
1616
import sys
1717

18+
1819
SRC_DIR = os.path.join(os.path.dirname(__file__), "src")
1920
sys.path.append(SRC_DIR)
2021

pyproject.toml

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
[tool.black]
2-
line-length = 119
3-
target-version = ['py37']
4-
51
[tool.ruff]
62
# Never enforce `E501` (line length violations).
7-
ignore = ["C901", "E501", "E741"]
3+
ignore = ["C901", "E501", "E741", "F402", "F823" ]
84
select = ["C", "E", "F", "I", "W"]
95
line-length = 119
106

@@ -18,6 +14,19 @@ line-length = 119
1814
lines-after-imports = 2
1915
known-first-party = ["transformers"]
2016

17+
[tool.ruff.format]
18+
# Like Black, use double quotes for strings.
19+
quote-style = "double"
20+
21+
# Like Black, indent with spaces, rather than tabs.
22+
indent-style = "space"
23+
24+
# Like Black, respect magic trailing commas.
25+
skip-magic-trailing-comma = false
26+
27+
# Like Black, automatically detect the appropriate line ending.
28+
line-ending = "auto"
29+
2130
[tool.pytest.ini_options]
2231
doctest_optionflags="NUMBER NORMALIZE_WHITESPACE ELLIPSIS"
2332
doctest_glob="**/*.md"

scripts/check_tokenizers.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from collections import Counter
2+
23
import datasets
4+
35
import transformers
46
from transformers.convert_slow_tokenizer import SLOW_TO_FAST_CONVERTERS
5-
67
from transformers.utils import logging
78

9+
810
logging.set_verbosity_info()
911

1012
TOKENIZER_CLASSES = {
@@ -101,8 +103,8 @@ def check_details(line, spm_ids, tok_ids, slow, fast):
101103
except Exception:
102104
pass
103105

104-
ok_start = fast.decode(spm_ids[:first])
105-
ok_end = fast.decode(spm_ids[last:])
106+
fast.decode(spm_ids[:first])
107+
fast.decode(spm_ids[last:])
106108
wrong = fast.decode(spm_ids[first:last])
107109
print()
108110
print(wrong)

scripts/fsmt/fsmt-make-super-tiny-model.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,19 @@
2424
#
2525
# It will be used then as "stas/tiny-wmt19-en-ru"
2626

27-
from pathlib import Path
2827
import json
2928
import tempfile
29+
from pathlib import Path
3030

31-
from transformers import FSMTTokenizer, FSMTConfig, FSMTForConditionalGeneration
31+
from transformers import FSMTConfig, FSMTForConditionalGeneration, FSMTTokenizer
3232
from transformers.models.fsmt.tokenization_fsmt import VOCAB_FILES_NAMES
3333

34+
3435
mname_tiny = "tiny-wmt19-en-ru"
3536

3637
# Build
3738

38-
# borrowed from a test
39+
# borrowed from a test
3940
vocab = [ "l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "w</w>", "r</w>", "t</w>", "lo", "low", "er</w>", "low</w>", "lowest</w>", "newer</w>", "wider</w>", "<unk>", ]
4041
vocab_tokens = dict(zip(vocab, range(len(vocab))))
4142
merges = ["l o 123", "lo w 1456", "e r</w> 1789", ""]
@@ -57,7 +58,7 @@
5758
tgt_vocab_file=tgt_vocab_file,
5859
merges_file=merges_file,
5960
)
60-
61+
6162
config = FSMTConfig(
6263
langs=['ru', 'en'],
6364
src_vocab_size=1000, tgt_vocab_size=1000,

scripts/fsmt/fsmt-make-tiny-model.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,18 @@
2727
# It will be used then as "stas/tiny-wmt19-en-de"
2828

2929
# Build
30-
from transformers import FSMTTokenizer, FSMTConfig, FSMTForConditionalGeneration
30+
from transformers import FSMTConfig, FSMTForConditionalGeneration, FSMTTokenizer
31+
32+
3133
mname = "facebook/wmt19-en-de"
3234
tokenizer = FSMTTokenizer.from_pretrained(mname)
3335
# get the correct vocab sizes, etc. from the master model
3436
config = FSMTConfig.from_pretrained(mname)
35-
config.update(dict(
36-
d_model=4,
37-
encoder_layers=1, decoder_layers=1,
38-
encoder_ffn_dim=4, decoder_ffn_dim=4,
39-
encoder_attention_heads=1, decoder_attention_heads=1))
37+
config.update({
38+
"d_model": 4,
39+
"encoder_layers": 1, "decoder_layers": 1,
40+
"encoder_ffn_dim": 4, "decoder_ffn_dim": 4,
41+
"encoder_attention_heads": 1, "decoder_attention_heads": 1})
4042

4143
tiny_model = FSMTForConditionalGeneration(config)
4244
print(f"num of params {tiny_model.num_parameters()}")

scripts/fsmt/gen-card-allenai-wmt16.py

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import os
2020
from pathlib import Path
2121

22+
2223
def write_model_card(model_card_dir, src_lang, tgt_lang, model_name):
2324

2425
texts = {

scripts/fsmt/gen-card-allenai-wmt19.py

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import os
2020
from pathlib import Path
2121

22+
2223
def write_model_card(model_card_dir, src_lang, tgt_lang, model_name):
2324

2425
texts = {

scripts/fsmt/gen-card-facebook-wmt19.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import os
2020
from pathlib import Path
2121

22+
2223
def write_model_card(model_card_dir, src_lang, tgt_lang):
2324

2425
texts = {
@@ -39,7 +40,7 @@ def write_model_card(model_card_dir, src_lang, tgt_lang):
3940

4041
readme = f"""
4142
---
42-
language:
43+
language:
4344
- {src_lang}
4445
- {tgt_lang}
4546
thumbnail:

0 commit comments

Comments
 (0)