Skip to content

Commit e83df11

Browse files
authored
add lint and format check (Oneflow-Inc#59)
1 parent 652dc8e commit e83df11

Some content is hidden

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

78 files changed

+726
-570
lines changed

.flake8

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# This is an example .flake8 config, used when developing *Black* itself.
2+
# Keep in sync with setup.cfg which is used for source packages.
3+
4+
[flake8]
5+
ignore = W503, E203, E221, C901, C408, E741, C407, B017
6+
max-line-length = 100
7+
max-complexity = 18
8+
select = B,C,E,F,W,T4,B9
9+
exclude = build
10+
per-file-ignores =
11+
**/__init__.py:F401,F403,E402
12+
**/configs/**.py:F401,E402
13+
configs/**.py:F401,E402
14+
**/tests/config/**.py:F401,E402
15+
tests/config/**.py:F401,E402
16+
tests/**.py: E402

.github/workflows/py.yml

+21-25
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,30 @@ jobs:
1919
if: github.event.pull_request.draft == false && contains(github.event.pull_request.requested_reviewers.*.login, 'oneflow-ci-bot')
2020
steps:
2121
- uses: actions/checkout@v2
22+
23+
- name: Set up Python 3.6
24+
uses: actions/setup-python@v2
2225
with:
23-
ref: ${{ github.event.pull_request.head.ref }}
24-
repository: ${{github.event.pull_request.head.repo.full_name}}
25-
- name: Checkout Oneflow-Inc/oneflow
26-
uses: actions/checkout@v2
27-
with:
28-
repository: Oneflow-Inc/oneflow
29-
path: ${{ env.ONEFLOW_SRC }}
30-
- name: Check Python format
31-
id: py_check
32-
run: |
33-
python3 -m pip install black==19.10b0
34-
python3 ${{ env.ONEFLOW_SRC }}/ci/check/run_py_format.py --source_dir $PWD
35-
- name: Run Python Format
36-
id: py_fmt
37-
if: ${{ failure() }}
26+
python-version: 3.6
27+
28+
- name: Install dependencies
29+
# flake8-bugbear flake8-comprehensions are useful but not available internally
3830
run: |
39-
python3 -m pip install black==19.10b0
40-
python3 ${{ env.ONEFLOW_SRC }}/ci/check/run_py_format.py --source_dir $PWD --fix
41-
- name: Git push
42-
id: git_push
43-
if: ${{ failure() }}
31+
python -m pip install --upgrade pip
32+
python -m pip install flake8==3.8.1 isort==5.10.1 autoflake==1.4
33+
python -m pip install black==21.4b2
34+
flake8 --version
35+
36+
- name: Lint
37+
id: py_check
4438
run: |
45-
git config --global user.email "[email protected]"
46-
git config --global user.name "oneflow-ci-bot"
47-
git add -u
48-
git commit -m "auto format by CI"
49-
git push
39+
echo "Running autoflake"
40+
autoflake --ignore-init-module-imports --remove-all-unused-imports -r .
41+
echo "Running isort"
42+
isort --diff --check .
43+
echo "Running flake8"
44+
flake8 .
45+
5046
- name: Please request CI again
5147
if: ${{ failure() }}
5248
run: |

Makefile

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
format:
2+
autoflake -i --ignore-init-module-imports --remove-all-unused-imports -r .
3+
black .
4+
isort .
5+
flake8 .
6+
7+
lint:
8+
autoflake --ignore-init-module-imports --remove-all-unused-imports -r .
9+
isort --diff --check .
10+
flake8 .

configs/bert_large_pretrain.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
from libai.config import LazyCall
22
from .common.models.bert import pretrain_model as model
33
from .common.train import train
4-
from .common.optim import optim, scheduler
5-
from .common.data.nlp_data import data
64

75
from libai.models import BertForPretrainingGraph
86

@@ -21,13 +19,13 @@
2119
graph = dict(
2220
# options for graph or eager mode
2321
enabled=True,
24-
debug=-1, # debug mode for graph
22+
debug=-1, # debug mode for graph
2523
train_graph=LazyCall(BertForPretrainingGraph)(
2624
fp16=train.amp.enabled,
2725
is_train=True,
2826
),
2927
eval_graph=LazyCall(BertForPretrainingGraph)(
30-
fp16=train.amp.enabled,
28+
fp16=train.amp.enabled,
3129
is_train=False
3230
),
3331
)

configs/common/data/cv_data.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
from omegaconf import OmegaConf
2-
from libai.config.instantiate import instantiate
32

4-
from libai.data.datasets import (
5-
ImageNetDataset,
6-
CIFAR10Dataset,
7-
CIFAR100Dataset,
8-
MNISTDataset,
9-
)
3+
from libai.data.datasets import ImageNetDataset
104
from libai.data.build import build_image_train_loader, build_image_test_loader
115
from libai.config import LazyCall
126

configs/common/data/nlp_data.py

+16-5
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,23 @@
55

66
tokenizer = dict(
77
tokenizer_name="BertTokenizer",
8-
tokenizer_cfg=dict(vocab_file="bert-vocab.txt", do_lower_case=True,),
8+
tokenizer_cfg=dict(
9+
vocab_file="bert-vocab.txt",
10+
do_lower_case=True,
11+
),
912
append_eod=False,
1013
)
1114

1215
dataloader = OmegaConf.create()
1316

1417
dataloader.train = LazyCall(build_nlp_train_val_test_loader)(
1518
dataset=[
16-
LazyCall(DemoNlpDataset)(data_root="train1",),
17-
LazyCall(DemoNlpDataset)(data_root="train2",),
19+
LazyCall(DemoNlpDataset)(
20+
data_root="train1",
21+
),
22+
LazyCall(DemoNlpDataset)(
23+
data_root="train2",
24+
),
1825
],
1926
splits=[[949.0, 50.0, 1.0], [900.0, 99.0, 1.0]],
2027
weights=[0.5, 0.5],
@@ -23,9 +30,13 @@
2330

2431
dataloader.test = [
2532
LazyCall(build_nlp_test_loader)(
26-
dataset=LazyCall(DemoNlpDataset)(data_root="test1",)
33+
dataset=LazyCall(DemoNlpDataset)(
34+
data_root="test1",
35+
)
2736
),
2837
LazyCall(build_nlp_test_loader)(
29-
dataset=LazyCall(DemoNlpDataset)(data_root="test2",)
38+
dataset=LazyCall(DemoNlpDataset)(
39+
data_root="test2",
40+
)
3041
),
3142
]

configs/common/data/transform.py

+18-11
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
from flowvision import transforms
22
from flowvision.transforms import InterpolationMode
3-
from flowvision.transforms.functional import str_to_interp_mode, str_to_pil_interp
3+
from flowvision.transforms.functional import str_to_interp_mode
44

55
from flowvision.data.constants import (
66
IMAGENET_DEFAULT_MEAN,
77
IMAGENET_DEFAULT_STD,
88
)
9-
from flowvision.data.auto_augment import (
10-
rand_augment_transform,
11-
augment_and_mix_transform,
12-
auto_augment_transform,
13-
)
9+
from flowvision.data.auto_augment import rand_augment_transform
1410
from flowvision.data.random_erasing import RandomErasing
1511

1612
from libai.config import LazyCall
@@ -37,22 +33,33 @@
3733
),
3834
LazyCall(transforms.ToTensor)(),
3935
LazyCall(transforms.Normalize)(
40-
mean=IMAGENET_DEFAULT_MEAN, std=IMAGENET_DEFAULT_STD,
36+
mean=IMAGENET_DEFAULT_MEAN,
37+
std=IMAGENET_DEFAULT_STD,
4138
),
4239
LazyCall(RandomErasing)(
43-
probability=0.25, mode="pixel", max_count=1, num_splits=0, device="cpu",
40+
probability=0.25,
41+
mode="pixel",
42+
max_count=1,
43+
num_splits=0,
44+
device="cpu",
4445
),
4546
]
4647
)
4748

4849

4950
default_test_transform = LazyCall(transforms.Compose)(
5051
transforms=[
51-
LazyCall(transforms.Resize)(size=256, interpolation=InterpolationMode.BICUBIC,),
52-
LazyCall(transforms.CenterCrop)(size=224,),
52+
LazyCall(transforms.Resize)(
53+
size=256,
54+
interpolation=InterpolationMode.BICUBIC,
55+
),
56+
LazyCall(transforms.CenterCrop)(
57+
size=224,
58+
),
5359
LazyCall(transforms.ToTensor)(),
5460
LazyCall(transforms.Normalize)(
55-
mean=IMAGENET_DEFAULT_MEAN, std=IMAGENET_DEFAULT_STD,
61+
mean=IMAGENET_DEFAULT_MEAN,
62+
std=IMAGENET_DEFAULT_STD,
5663
),
5764
]
5865
)

configs/common/optim.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
optim = LazyCall(flow.optim.AdamW)(
88
parameters=LazyCall(get_default_optimizer_params)(
9-
# parameters.model is meant to be set to the model object, before instantiating the optimizer.
9+
# parameters.model is meant to be set to the model object,
10+
# before instantiating the optimizer.
1011
clip_grad_max_norm=1.0,
1112
clip_grad_norm_type=2.0,
1213
weight_decay_norm=0.0,

configs/common/tokenizer.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
tokenizer = dict(
22
tokenizer_name="BertTokenizer",
3-
tokenizer_cfg=dict(vocab_file="bert-vocab.txt", do_lower_case=True,),
3+
tokenizer_cfg=dict(
4+
vocab_file="bert-vocab.txt",
5+
do_lower_case=True,
6+
),
47
append_eod=False,
58
)

libai/config/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16-
from .lazy import LazyCall, LazyConfig
17-
from .instantiate import instantiate
1816
from .arguments import default_argument_parser
1917
from .config import configurable, try_get_key
18+
from .instantiate import instantiate
19+
from .lazy import LazyCall, LazyConfig
2020

2121
__all__ = [
2222
"LazyCall",

libai/config/arguments.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
# limitations under the License.
1515

1616

17-
import sys
1817
import argparse
18+
import sys
1919

2020

2121
def default_argument_parser(epilog=None):
@@ -35,18 +35,22 @@ def default_argument_parser(epilog=None):
3535
3636
Run on single machine:
3737
$ python3 -m oneflow.distributed.launch \
38-
--nproc_per_node 8 --nnodes 1 --node_rank 0 --master_addr 127.0.0.1 {sys.argv[0]} --config-file cfg.yaml
38+
--nproc_per_node 8 --nnodes 1 --node_rank 0 --master_addr 127.0.0.1 {sys.argv[0]} \
39+
--config-file cfg.yaml
3940
4041
Change some config options:
4142
$ python3 -m oneflow.distributed.launch \
42-
--nproc_per_node 8 --nnodes 1 --node_rank 0 --master_addr 127.0.0.1 {sys.argv[0]} --config-file cfg.yaml train.load_weight=/path/to/weight.pth optim.lr=0.001
43+
--nproc_per_node 8 --nnodes 1 --node_rank 0 --master_addr 127.0.0.1 {sys.argv[0]} \
44+
--config-file cfg.yaml train.load_weight=/path/to/weight.pth optim.lr=0.001
4345
4446
Run on multiple machines:
4547
(machine0)$ python3 -m oneflow.distributed.launch \
46-
--nproc_per_node 8 --nnodes 2 --node_rank 0 --master_addr <URL> {sys.argv[0]} --config-file cfg.yaml
47-
48+
--nproc_per_node 8 --nnodes 2 --node_rank 0 --master_addr <URL> {sys.argv[0]} \
49+
--config-file cfg.yaml
50+
4851
$ python3 -m oneflow.distributed.launch \
49-
--nproc_per_node 8 --nnodes 2 --node_rank 1 --master_addr <URL> {sys.argv[0]} --config-file cfg.yaml
52+
--nproc_per_node 8 --nnodes 2 --node_rank 1 --master_addr <URL> {sys.argv[0]} \
53+
--config-file cfg.yaml
5054
5155
""",
5256
formatter_class=argparse.RawDescriptionHelpFormatter,

libai/config/config.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16-
from omegaconf import OmegaConf
1716
import functools
1817
import inspect
1918

19+
from omegaconf import OmegaConf
20+
2021

2122
def configurable(init_func=None, *, from_config=None):
2223
"""

libai/config/lazy.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,20 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16-
import pydoc
1716
import ast
1817
import builtins
1918
import importlib
2019
import inspect
2120
import logging
2221
import os
22+
import pydoc
2323
import uuid
2424
from collections import abc
2525
from contextlib import contextmanager
2626
from copy import deepcopy
2727
from dataclasses import is_dataclass
28-
from typing import List, Tuple, Union, Any
28+
from typing import Any, List, Tuple, Union
29+
2930
import cloudpickle
3031
import yaml
3132
from omegaconf import DictConfig, ListConfig, OmegaConf

libai/data/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
from .build import build_image_test_loader, build_image_train_loader
1617
from .structures import DistTensorData, Instance
17-
from .build import build_image_train_loader, build_image_test_loader

libai/data/build.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
from oneflow.utils.data.dataset import ConcatDataset
1919

2020
from libai.utils import distributed as dist
21+
2122
from .structures import Instance
22-
from .temp_file import CyclicSampler, SingleRoundSampler, BlendableDataset, split_ds
23+
from .temp_file import CyclicSampler, SingleRoundSampler, split_ds
2324

2425

2526
def build_nlp_train_val_test_loader(
@@ -35,8 +36,8 @@ def build_nlp_train_val_test_loader(
3536
collate_fn=None,
3637
blendable_dataset=ConcatDataset,
3738
):
38-
"""
39-
Build nlp train_val_test dataloder
39+
"""
40+
Build nlp train_val_test dataloader
4041
"""
4142
# TODO: add input type
4243

@@ -115,9 +116,14 @@ def build_nlp_train_val_test_loader(
115116

116117

117118
def build_nlp_test_loader(
118-
dataset, test_batch_size, sampler=None, num_workers=4, seed=0, collate_fn=None,
119+
dataset,
120+
test_batch_size,
121+
sampler=None,
122+
num_workers=4,
123+
seed=0,
124+
collate_fn=None,
119125
):
120-
"""
126+
"""
121127
Build nlp test dataloder
122128
"""
123129
# TODO: add input type

libai/data/data_utils/__init__.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
from .blendable_dataset import BlendableDataset
17+
from .data_utils import get_prefixes_and_weights
1618
from .indexed_dataset import (
17-
get_indexed_dataset,
18-
IndexedDataset,
1919
IndexedCachedDataset,
20+
IndexedDataset,
2021
MMapIndexedDataset,
22+
get_indexed_dataset,
2123
)
22-
from .blendable_dataset import BlendableDataset
23-
from .data_utils import get_prefixes_and_weights
24-
from .reindexed_dataset import SentenceIndexedDataset, BlockIndexedDataset
25-
from .split_dataset import split_ds, SplitDataset
24+
from .reindexed_dataset import BlockIndexedDataset, SentenceIndexedDataset
25+
from .split_dataset import SplitDataset, split_ds

0 commit comments

Comments
 (0)