Unverified Commit 651408a0 authored by Arthur's avatar Arthur Committed by GitHub
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: default avatarcharliermarsh <charlie.r.marsh@gmail.com>

* style

* nits

* revert notebook changes

* Add # fmt skip when possible

* Add # fmt skip when possible

...
parent acb5b4af
...@@ -157,11 +157,10 @@ jobs: ...@@ -157,11 +157,10 @@ jobs:
command: pip freeze | tee installed.txt command: pip freeze | tee installed.txt
- store_artifacts: - store_artifacts:
path: ~/transformers/installed.txt path: ~/transformers/installed.txt
- run: black --check examples tests src utils - run: ruff check examples tests src utils
- run: ruff examples tests src utils - run: ruff format tests src utils --check
- run: python utils/custom_init_isort.py --check_only - run: python utils/custom_init_isort.py --check_only
- run: python utils/sort_auto_mappings.py --check_only - run: python utils/sort_auto_mappings.py --check_only
- run: doc-builder style src/transformers docs/source --max_len 119 --check_only --path_to_docs docs/source
- run: python utils/check_doc_toc.py - run: python utils/check_doc_toc.py
check_repository_consistency: check_repository_consistency:
......
...@@ -15,7 +15,6 @@ ...@@ -15,7 +15,6 @@
import argparse import argparse
import copy import copy
import glob
import os import os
import random import random
from dataclasses import dataclass from dataclasses import dataclass
...@@ -239,7 +238,7 @@ class CircleCIJob: ...@@ -239,7 +238,7 @@ class CircleCIJob:
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()' 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()'
check_test_command += f"$(python3 -c '{py_command}'); " check_test_command += f"$(python3 -c '{py_command}'); "
check_test_command += f'cat summary_short.txt; echo ""; exit -1; ' check_test_command += 'cat summary_short.txt; echo ""; exit -1; '
# Deeal with failed tests # Deeal with failed tests
check_test_command += f'elif [ -s reports/{self.job_name}/failures_short.txt ]; ' check_test_command += f'elif [ -s reports/{self.job_name}/failures_short.txt ]; '
...@@ -249,7 +248,7 @@ class CircleCIJob: ...@@ -249,7 +248,7 @@ class CircleCIJob:
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()' 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()'
check_test_command += f"$(python3 -c '{py_command}'); " check_test_command += f"$(python3 -c '{py_command}'); "
check_test_command += f'cat summary_short.txt; echo ""; exit -1; ' check_test_command += 'cat summary_short.txt; echo ""; exit -1; '
check_test_command += f'elif [ -s reports/{self.job_name}/stats.txt ]; then echo "All tests pass!"; ' check_test_command += f'elif [ -s reports/{self.job_name}/stats.txt ]; then echo "All tests pass!"; '
......
...@@ -9,8 +9,8 @@ modified_only_fixup: ...@@ -9,8 +9,8 @@ modified_only_fixup:
$(eval modified_py_files := $(shell python utils/get_modified_files.py $(check_dirs))) $(eval modified_py_files := $(shell python utils/get_modified_files.py $(check_dirs)))
@if test -n "$(modified_py_files)"; then \ @if test -n "$(modified_py_files)"; then \
echo "Checking/fixing $(modified_py_files)"; \ echo "Checking/fixing $(modified_py_files)"; \
black $(modified_py_files); \ ruff check $(modified_py_files) --fix; \
ruff $(modified_py_files) --fix; \ ruff format $(modified_py_files);\
else \ else \
echo "No library .py files were modified"; \ echo "No library .py files were modified"; \
fi fi
...@@ -48,11 +48,10 @@ repo-consistency: ...@@ -48,11 +48,10 @@ repo-consistency:
# this target runs checks on all files # this target runs checks on all files
quality: quality:
black --check $(check_dirs) setup.py conftest.py ruff check $(check_dirs) setup.py conftest.py
ruff format --check $(check_dirs) setup.py conftest.py
python utils/custom_init_isort.py --check_only python utils/custom_init_isort.py --check_only
python utils/sort_auto_mappings.py --check_only python utils/sort_auto_mappings.py --check_only
ruff $(check_dirs) setup.py conftest.py
doc-builder style src/transformers docs/source --max_len 119 --check_only --path_to_docs docs/source
python utils/check_doc_toc.py python utils/check_doc_toc.py
# Format source code automatically and check is there are any problems left that need manual fixing # Format source code automatically and check is there are any problems left that need manual fixing
...@@ -60,14 +59,13 @@ quality: ...@@ -60,14 +59,13 @@ quality:
extra_style_checks: extra_style_checks:
python utils/custom_init_isort.py python utils/custom_init_isort.py
python utils/sort_auto_mappings.py python utils/sort_auto_mappings.py
doc-builder style src/transformers docs/source --max_len 119 --path_to_docs docs/source
python utils/check_doc_toc.py --fix_and_overwrite python utils/check_doc_toc.py --fix_and_overwrite
# this target runs checks on all files and potentially modifies some of them # this target runs checks on all files and potentially modifies some of them
style: style:
black $(check_dirs) setup.py conftest.py ruff check $(check_dirs) setup.py conftest.py --fix
ruff $(check_dirs) setup.py conftest.py --fix ruff format $(check_dirs) setup.py conftest.py
${MAKE} autogenerate_code ${MAKE} autogenerate_code
${MAKE} extra_style_checks ${MAKE} extra_style_checks
......
...@@ -245,7 +245,7 @@ logits first, and then reshaped to match the size of the labels before you can c ...@@ -245,7 +245,7 @@ logits first, and then reshaped to match the size of the labels before you can c
... reduce_labels=False, ... reduce_labels=False,
... ) ... )
... for key, value in metrics.items(): ... for key, value in metrics.items():
... if type(value) is np.ndarray: ... if isinstance(value, np.ndarray):
... metrics[key] = value.tolist() ... metrics[key] = value.tolist()
... return metrics ... return metrics
``` ```
......
...@@ -242,7 +242,7 @@ pip install -q datasets transformers evaluate ...@@ -242,7 +242,7 @@ pip install -q datasets transformers evaluate
... reduce_labels=False, ... reduce_labels=False,
... ) ... )
... for key, value in metrics.items(): ... for key, value in metrics.items():
... if type(value) is np.ndarray: ... if isinstance(value, np.ndarray):
... metrics[key] = value.tolist() ... metrics[key] = value.tolist()
... return metrics ... return metrics
``` ```
......
...@@ -212,7 +212,7 @@ class DataTrainingArguments: ...@@ -212,7 +212,7 @@ class DataTrainingArguments:
if self.validation_file is not None: if self.validation_file is not None:
extension = self.validation_file.split(".")[-1] extension = self.validation_file.split(".")[-1]
assert extension in ["csv", "json"], "`validation_file` should be a csv or a json file." assert extension in ["csv", "json"], "`validation_file` should be a csv or a json file."
self.task_name = self.task_name.lower() if type(self.task_name) == str else self.task_name self.task_name = self.task_name.lower() if isinstance(self.task_name, str) else self.task_name
def create_train_state( def create_train_state(
......
...@@ -23,7 +23,7 @@ class GLUETransformer(BaseTransformer): ...@@ -23,7 +23,7 @@ class GLUETransformer(BaseTransformer):
mode = "sequence-classification" mode = "sequence-classification"
def __init__(self, hparams): def __init__(self, hparams):
if type(hparams) == dict: if isinstance(hparams, dict):
hparams = Namespace(**hparams) hparams = Namespace(**hparams)
hparams.glue_output_mode = glue_output_modes[hparams.task] hparams.glue_output_mode = glue_output_modes[hparams.task]
num_labels = glue_tasks_num_labels[hparams.task] num_labels = glue_tasks_num_labels[hparams.task]
......
...@@ -25,7 +25,7 @@ class NERTransformer(BaseTransformer): ...@@ -25,7 +25,7 @@ class NERTransformer(BaseTransformer):
mode = "token-classification" mode = "token-classification"
def __init__(self, hparams): def __init__(self, hparams):
if type(hparams) == dict: if isinstance(hparams, dict):
hparams = Namespace(**hparams) hparams = Namespace(**hparams)
module = import_module("tasks") module = import_module("tasks")
try: try:
......
...@@ -32,7 +32,7 @@ class DeeBertEncoder(nn.Module): ...@@ -32,7 +32,7 @@ class DeeBertEncoder(nn.Module):
self.early_exit_entropy = [-1 for _ in range(config.num_hidden_layers)] self.early_exit_entropy = [-1 for _ in range(config.num_hidden_layers)]
def set_early_exit_entropy(self, x): def set_early_exit_entropy(self, x):
if (type(x) is float) or (type(x) is int): if isinstance(x, (float, int)):
for i in range(len(self.early_exit_entropy)): for i in range(len(self.early_exit_entropy)):
self.early_exit_entropy[i] = x self.early_exit_entropy[i] = x
else: else:
...@@ -232,9 +232,7 @@ class DeeBertModel(BertPreTrainedModel): ...@@ -232,9 +232,7 @@ class DeeBertModel(BertPreTrainedModel):
outputs = ( outputs = (
sequence_output, sequence_output,
pooled_output, pooled_output,
) + encoder_outputs[ ) + encoder_outputs[1:] # add hidden_states and attentions if they are here
1:
] # add hidden_states and attentions if they are here
return outputs # sequence_output, pooled_output, (hidden_states), (attentions), highway exits return outputs # sequence_output, pooled_output, (hidden_states), (attentions), highway exits
......
...@@ -158,9 +158,7 @@ header_full = """ ...@@ -158,9 +158,7 @@ header_full = """
</span> </span>
</body> </body>
</html> </html>
""" % ( """ % (header_html,)
header_html,
)
st.sidebar.markdown( st.sidebar.markdown(
header_full, header_full,
unsafe_allow_html=True, unsafe_allow_html=True,
......
...@@ -1706,9 +1706,7 @@ class GeneralizedRCNN(nn.Module): ...@@ -1706,9 +1706,7 @@ class GeneralizedRCNN(nn.Module):
elif os.path.isfile(pretrained_model_name_or_path) or is_remote_url(pretrained_model_name_or_path): elif os.path.isfile(pretrained_model_name_or_path) or is_remote_url(pretrained_model_name_or_path):
archive_file = pretrained_model_name_or_path archive_file = pretrained_model_name_or_path
elif os.path.isfile(pretrained_model_name_or_path + ".index"): elif os.path.isfile(pretrained_model_name_or_path + ".index"):
assert ( assert from_tf, "We found a TensorFlow checkpoint at {}, please set from_tf to True to load from this checkpoint".format(
from_tf
), "We found a TensorFlow checkpoint at {}, please set from_tf to True to load from this checkpoint".format(
pretrained_model_name_or_path + ".index" pretrained_model_name_or_path + ".index"
) )
archive_file = pretrained_model_name_or_path + ".index" archive_file = pretrained_model_name_or_path + ".index"
......
...@@ -652,9 +652,7 @@ class MaskedBertModel(MaskedBertPreTrainedModel): ...@@ -652,9 +652,7 @@ class MaskedBertModel(MaskedBertPreTrainedModel):
outputs = ( outputs = (
sequence_output, sequence_output,
pooled_output, pooled_output,
) + encoder_outputs[ ) + encoder_outputs[1:] # add hidden_states and attentions if they are here
1:
] # add hidden_states and attentions if they are here
return outputs # sequence_output, pooled_output, (hidden_states), (attentions) return outputs # sequence_output, pooled_output, (hidden_states), (attentions)
......
...@@ -311,8 +311,7 @@ def train(args, train_dataset, model, tokenizer, teacher=None): ...@@ -311,8 +311,7 @@ def train(args, train_dataset, model, tokenizer, teacher=None):
tr_loss += loss.item() tr_loss += loss.item()
if (step + 1) % args.gradient_accumulation_steps == 0 or ( if (step + 1) % args.gradient_accumulation_steps == 0 or (
# last step in epoch but step is always smaller than gradient_accumulation_steps # last step in epoch but step is always smaller than gradient_accumulation_steps
len(epoch_iterator) <= args.gradient_accumulation_steps len(epoch_iterator) <= args.gradient_accumulation_steps and (step + 1) == len(epoch_iterator)
and (step + 1) == len(epoch_iterator)
): ):
if args.fp16: if args.fp16:
nn.utils.clip_grad_norm_(amp.master_params(optimizer), args.max_grad_norm) nn.utils.clip_grad_norm_(amp.master_params(optimizer), args.max_grad_norm)
......
...@@ -239,7 +239,7 @@ def print_model_summary(model, name_width=25, line_width=180, ignore=None): ...@@ -239,7 +239,7 @@ def print_model_summary(model, name_width=25, line_width=180, ignore=None):
continue continue
if type(mod) in ignore: if type(mod) in ignore:
continue continue
if [True for s in ignore if type(s) is str and s in name]: if [True for s in ignore if isinstance(s, str) and s in name]:
continue continue
act_str = f"Act:{input_q.extra_repr()}" act_str = f"Act:{input_q.extra_repr()}"
wgt_str = f"Wgt:{weight_q.extra_repr()}" wgt_str = f"Wgt:{weight_q.extra_repr()}"
......
...@@ -1706,9 +1706,7 @@ class GeneralizedRCNN(nn.Module): ...@@ -1706,9 +1706,7 @@ class GeneralizedRCNN(nn.Module):
elif os.path.isfile(pretrained_model_name_or_path) or is_remote_url(pretrained_model_name_or_path): elif os.path.isfile(pretrained_model_name_or_path) or is_remote_url(pretrained_model_name_or_path):
archive_file = pretrained_model_name_or_path archive_file = pretrained_model_name_or_path
elif os.path.isfile(pretrained_model_name_or_path + ".index"): elif os.path.isfile(pretrained_model_name_or_path + ".index"):
assert ( assert from_tf, "We found a TensorFlow checkpoint at {}, please set from_tf to True to load from this checkpoint".format(
from_tf
), "We found a TensorFlow checkpoint at {}, please set from_tf to True to load from this checkpoint".format(
pretrained_model_name_or_path + ".index" pretrained_model_name_or_path + ".index"
) )
archive_file = pretrained_model_name_or_path + ".index" archive_file = pretrained_model_name_or_path + ".index"
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
import os import os
import sys import sys
SRC_DIR = os.path.join(os.path.dirname(__file__), "src") SRC_DIR = os.path.join(os.path.dirname(__file__), "src")
sys.path.append(SRC_DIR) sys.path.append(SRC_DIR)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment