Skip to content

Commit

Permalink
Update quality tooling for formatting (huggingface#21480)
Browse files Browse the repository at this point in the history
* Result of black 23.1

* Update target to Python 3.7

* Switch flake8 to ruff

* Configure isort

* Configure isort

* Apply isort with line limit

* Put the right black version

* adapt black in check copies

* Fix copies
  • Loading branch information
sgugger authored Feb 6, 2023
1 parent b7bb2b5 commit 6f79d26
Show file tree
Hide file tree
Showing 1,211 changed files with 1,518 additions and 2,673 deletions.
5 changes: 2 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,10 @@ jobs:
command: pip freeze | tee installed.txt
- store_artifacts:
path: ~/transformers/installed.txt
- run: black --check --preview examples tests src utils
- run: isort --check-only examples tests src utils
- run: black --check examples tests src utils
- run: ruff examples tests src utils
- run: python utils/custom_init_isort.py --check_only
- run: python utils/sort_auto_mappings.py --check_only
- run: flake8 examples tests src utils
- 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

Expand Down
14 changes: 6 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ modified_only_fixup:
$(eval modified_py_files := $(shell python utils/get_modified_files.py $(check_dirs)))
@if test -n "$(modified_py_files)"; then \
echo "Checking/fixing $(modified_py_files)"; \
black --preview $(modified_py_files); \
isort $(modified_py_files); \
flake8 $(modified_py_files); \
black $(modified_py_files); \
ruff $(modified_py_files) --fix; \
else \
echo "No library .py files were modified"; \
fi
Expand Down Expand Up @@ -48,11 +47,10 @@ repo-consistency:
# this target runs checks on all files

quality:
black --check --preview $(check_dirs)
isort --check-only $(check_dirs)
black --check $(check_dirs)
python utils/custom_init_isort.py --check_only
python utils/sort_auto_mappings.py --check_only
flake8 $(check_dirs)
ruff $(check_dirs)
doc-builder style src/transformers docs/source --max_len 119 --check_only --path_to_docs docs/source
python utils/check_doc_toc.py

Expand All @@ -67,8 +65,8 @@ extra_style_checks:
# this target runs checks on all files and potentially modifies some of them

style:
black --preview $(check_dirs)
isort $(check_dirs)
black $(check_dirs)
ruff $(check_dirs) --fix
${MAKE} autogenerate_code
${MAKE} extra_style_checks

Expand Down
2 changes: 1 addition & 1 deletion docs/source/en/pipeline_webserver.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ while True:
queues.append(rq)
strings
outs = pipe(strings, batch_size=len(strings))
for (rq, out) in zip(queues, outs):
for rq, out in zip(queues, outs):
await rq.put(out)
```

Expand Down
1 change: 0 additions & 1 deletion docs/source/en/tasks/asr.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ Unlike other data collators, this specific data collator needs to apply a differ

>>> @dataclass
... class DataCollatorCTCWithPadding:

... processor: AutoProcessor
... padding: Union[bool, str] = "longest"

Expand Down
2 changes: 1 addition & 1 deletion docs/source/en/tasks/object_detection.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ The `image_processor` expects the annotations to be in the following format: `{'

```py
>>> def formatted_anns(image_id, category, area, bbox):

... annotations = []
... for i in range(0, len(category)):
... new_ann = {
Expand Down Expand Up @@ -399,6 +398,7 @@ First, prepare the `cppe5["test"]` set: format the annotations and save the data
```py
>>> import json


>>> # format annotations the same as for training, no need for data augmentation
>>> def val_formatted_anns(image_id, objects):
... annotations = []
Expand Down
1 change: 0 additions & 1 deletion docs/source/es/tasks/asr.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ A diferencia de otros collators de datos, este tiene que aplicarle un método de

>>> @dataclass
... class DataCollatorCTCWithPadding:

... processor: AutoProcessor
... padding: Union[bool, str] = "longest"

Expand Down
14 changes: 7 additions & 7 deletions examples/flax/image-captioning/run_image_captioning_flax.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,23 @@
from typing import Callable, Optional

import datasets
import nltk # Here to have a nice missing dependency error message early on
import numpy as np
from datasets import Dataset, load_dataset
from PIL import Image
from tqdm import tqdm

import evaluate
import jax
import jax.numpy as jnp
import nltk # Here to have a nice missing dependency error message early on
import numpy as np
import optax
import transformers
from datasets import Dataset, load_dataset
from filelock import FileLock
from flax import jax_utils, traverse_util
from flax.jax_utils import unreplicate
from flax.training import train_state
from flax.training.common_utils import get_metrics, onehot, shard, shard_prng_key
from huggingface_hub import Repository, create_repo
from PIL import Image
from tqdm import tqdm

import transformers
from transformers import (
AutoImageProcessor,
AutoTokenizer,
Expand Down
10 changes: 5 additions & 5 deletions examples/flax/language-modeling/run_bart_dlm_flax.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@
from pathlib import Path
from typing import Dict, List, Optional

import nltk
import numpy as np
from datasets import load_dataset
from tqdm import tqdm

import flax
import jax
import jax.numpy as jnp
import nltk
import numpy as np
import optax
from datasets import load_dataset
from flax import jax_utils, traverse_util
from flax.jax_utils import pad_shard_unpad
from flax.training import train_state
from flax.training.common_utils import get_metrics, onehot, shard
from huggingface_hub import Repository, create_repo
from tqdm import tqdm

from transformers import (
CONFIG_MAPPING,
FLAX_MODEL_FOR_MASKED_LM_MAPPING,
Expand Down
10 changes: 5 additions & 5 deletions examples/flax/language-modeling/run_clm_flax.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@
from typing import Callable, Optional

import datasets
import numpy as np
from datasets import Dataset, load_dataset
from tqdm import tqdm

import jax
import jax.numpy as jnp
import numpy as np
import optax
import transformers
from datasets import Dataset, load_dataset
from flax import jax_utils, traverse_util
from flax.jax_utils import pad_shard_unpad, unreplicate
from flax.training import train_state
from flax.training.common_utils import get_metrics, onehot, shard, shard_prng_key
from huggingface_hub import Repository, create_repo
from tqdm import tqdm

import transformers
from transformers import (
CONFIG_MAPPING,
FLAX_MODEL_FOR_CAUSAL_LM_MAPPING,
Expand Down
8 changes: 4 additions & 4 deletions examples/flax/language-modeling/run_mlm_flax.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@
from pathlib import Path
from typing import Dict, List, Optional, Tuple

import numpy as np
from datasets import load_dataset
from tqdm import tqdm

import flax
import jax
import jax.numpy as jnp
import numpy as np
import optax
from datasets import load_dataset
from flax import jax_utils, traverse_util
from flax.jax_utils import pad_shard_unpad
from flax.training import train_state
from flax.training.common_utils import get_metrics, onehot, shard
from huggingface_hub import Repository, create_repo
from tqdm import tqdm

from transformers import (
CONFIG_MAPPING,
FLAX_MODEL_FOR_MASKED_LM_MAPPING,
Expand Down
8 changes: 4 additions & 4 deletions examples/flax/language-modeling/run_t5_mlm_flax.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@
from pathlib import Path
from typing import Dict, List, Optional

import numpy as np
from datasets import load_dataset
from tqdm import tqdm

import flax
import jax
import jax.numpy as jnp
import numpy as np
import optax
from datasets import load_dataset
from flax import jax_utils, traverse_util
from flax.jax_utils import pad_shard_unpad
from flax.training import train_state
from flax.training.common_utils import get_metrics, onehot, shard
from huggingface_hub import Repository, create_repo
from tqdm import tqdm

from transformers import (
CONFIG_MAPPING,
FLAX_MODEL_FOR_MASKED_LM_MAPPING,
Expand Down
17 changes: 9 additions & 8 deletions examples/flax/question-answering/run_qa.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,21 @@
from typing import Any, Callable, Dict, Optional, Tuple

import datasets
import numpy as np
from datasets import load_dataset
from tqdm import tqdm

import evaluate
import jax
import jax.numpy as jnp
import numpy as np
import optax
import transformers
from datasets import load_dataset
from flax import struct, traverse_util
from flax.jax_utils import pad_shard_unpad, replicate, unreplicate
from flax.training import train_state
from flax.training.common_utils import get_metrics, onehot, shard
from huggingface_hub import Repository, create_repo
from tqdm import tqdm
from utils_qa import postprocess_qa_predictions

import transformers
from transformers import (
AutoConfig,
AutoTokenizer,
Expand All @@ -55,7 +56,6 @@
is_tensorboard_available,
)
from transformers.utils import check_min_version, get_full_repo_name, send_example_telemetry
from utils_qa import postprocess_qa_predictions


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -301,6 +301,7 @@ def __post_init__(self):

# endregion


# region Create a train state
def create_train_state(
model: FlaxAutoModelForQuestionAnswering,
Expand Down Expand Up @@ -387,6 +388,7 @@ def create_learning_rate_fn(

# endregion


# region train data iterator
def train_data_collator(rng: PRNGKey, dataset: Dataset, batch_size: int):
"""Returns shuffled batches of size `batch_size` from truncated `train dataset`, sharded over all local devices."""
Expand All @@ -405,6 +407,7 @@ def train_data_collator(rng: PRNGKey, dataset: Dataset, batch_size: int):

# endregion


# region eval data iterator
def eval_data_collator(dataset: Dataset, batch_size: int):
"""Returns batches of size `batch_size` from `eval dataset`. Sharding handled by `pad_shard_unpad` in the eval loop."""
Expand Down Expand Up @@ -934,7 +937,6 @@ def eval_step(state, batch):
total_steps = step_per_epoch * num_epochs
epochs = tqdm(range(num_epochs), desc=f"Epoch ... (1/{num_epochs})", position=0)
for epoch in epochs:

train_start = time.time()
train_metrics = []

Expand Down Expand Up @@ -975,7 +977,6 @@ def eval_step(state, batch):
and (cur_step % training_args.eval_steps == 0 or cur_step % step_per_epoch == 0)
and cur_step > 0
):

eval_metrics = {}
all_start_logits = []
all_end_logits = []
Expand Down
12 changes: 6 additions & 6 deletions examples/flax/summarization/run_summarization_flax.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@
from typing import Callable, Optional

import datasets
import nltk # Here to have a nice missing dependency error message early on
import numpy as np
from datasets import Dataset, load_dataset
from tqdm import tqdm

import evaluate
import jax
import jax.numpy as jnp
import nltk # Here to have a nice missing dependency error message early on
import numpy as np
import optax
import transformers
from datasets import Dataset, load_dataset
from filelock import FileLock
from flax import jax_utils, traverse_util
from flax.jax_utils import pad_shard_unpad, unreplicate
from flax.training import train_state
from flax.training.common_utils import get_metrics, onehot, shard, shard_prng_key
from huggingface_hub import Repository, create_repo
from tqdm import tqdm

import transformers
from transformers import (
CONFIG_MAPPING,
FLAX_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
Expand Down
12 changes: 5 additions & 7 deletions examples/flax/text-classification/run_flax_glue.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@
from typing import Any, Callable, Dict, Optional, Tuple

import datasets
import numpy as np
from datasets import load_dataset
from tqdm import tqdm

import evaluate
import jax
import jax.numpy as jnp
import numpy as np
import optax
import transformers
from datasets import load_dataset
from flax import struct, traverse_util
from flax.jax_utils import pad_shard_unpad, replicate, unreplicate
from flax.training import train_state
from flax.training.common_utils import get_metrics, onehot, shard
from huggingface_hub import Repository, create_repo
from tqdm import tqdm

import transformers
from transformers import (
AutoConfig,
AutoTokenizer,
Expand Down Expand Up @@ -586,7 +586,6 @@ def eval_step(state, batch):
total_steps = steps_per_epoch * num_epochs
epochs = tqdm(range(num_epochs), desc=f"Epoch ... (0/{num_epochs})", position=0)
for epoch in epochs:

train_start = time.time()
train_metrics = []

Expand Down Expand Up @@ -623,7 +622,6 @@ def eval_step(state, batch):
train_metrics = []

if (cur_step % training_args.eval_steps == 0 or cur_step % steps_per_epoch == 0) and cur_step > 0:

# evaluate
eval_loader = glue_eval_data_collator(eval_dataset, eval_batch_size)
for batch in tqdm(
Expand Down
Loading

0 comments on commit 6f79d26

Please sign in to comment.