Skip to content

Commit 99a19b2

Browse files
airidas-metafacebook-github-bot
authored andcommitted
add lint to oss (pytorch#1427)
Summary: Pull Request resolved: pytorch#1427 Pull Request resolved: pytorch#1417 Enforce format check to fbgemm OSS. Leave this to Airidas Korolkovas as the starter rampup task. Differential Revision: D40620055 fbshipit-source-id: 632a8c9f2d800fbc4963eeb72a5e21f192dc0557
1 parent bcc69ed commit 99a19b2

File tree

10 files changed

+197
-15
lines changed

10 files changed

+197
-15
lines changed

.github/workflows/pylint.yaml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: Lint
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
pull_request:
9+
branches:
10+
- main
11+
jobs:
12+
build:
13+
runs-on: ubuntu-latest
14+
strategy:
15+
matrix:
16+
python-version: ["3.8"]
17+
steps:
18+
- uses: actions/checkout@v2
19+
- name: Set up Python ${{ matrix.python-version }}
20+
uses: actions/setup-python@v2
21+
with:
22+
python-version: ${{ matrix.python-version }}
23+
- name: Install dependencies
24+
run: |
25+
python -m pip install --upgrade pip
26+
pip install ufmt
27+
pip install click
28+
pip install flake8
29+
- name: Analyzing the code with flake8
30+
run: |
31+
echo "::add-matcher::fbgemm_gpu/test/lint/flake8_problem_matcher.json"
32+
flake8 --ignore=E501,E402,E231,W503,F841,F401,W291,E302,E503,E203,E266 . # Ignore errors
33+
- name: Analyzing the code with ufmt
34+
run: |
35+
ufmt diff fbgemm_gpu/fbgemm_gpu
36+
ufmt diff fbgemm_gpu/test
37+
ufmt diff fbgemm_gpu/bench
38+
- name: Check Meta copyright header
39+
run: |
40+
python fbgemm_gpu/test/lint/check_meta_header.py --path=./fbgemm_gpu/fbgemm_gpu --fixit=False
41+
python fbgemm_gpu/test/lint/check_meta_header.py --path=./fbgemm_gpu/test --fixit=False
42+
python fbgemm_gpu/test/lint/check_meta_header.py --path=./fbgemm_gpu/bench --fixit=False

fbgemm_gpu/bench/merge_embeddings_benchmark.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def generate_requests(
7272
E: int,
7373
# inter-batch indices reuse rate
7474
reuse: float = 0.0,
75-
) -> List[Tuple[torch.IntTensor, torch.IntTensor,]]:
75+
) -> List[Tuple[torch.IntTensor, torch.IntTensor, ]]:
7676
rs = []
7777
for gpu_num in range(num_gpus):
7878
all_indices = torch.randint(

fbgemm_gpu/fbgemm_gpu/_fbgemm_gpu_docs.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
# This source code is licensed under the BSD-style license found in the
44
# LICENSE file in the root directory of this source tree.
55

6+
import torch
7+
68
import fbgemm_gpu
79
import fbgemm_gpu.split_table_batched_embeddings_ops
8-
import torch
10+
911

1012
Tensor = torch.Tensor
1113

@@ -85,12 +87,12 @@ def add_docs(method, docstr):
8587
"""
8688
dense_to_jagged(dense, x_offsets, total_L) -> (Tensor, Tensor[])
8789
88-
Converts a dense tensor into a jagged tensor, given the desired offsets of the resulting dense tensor.
90+
Converts a dense tensor into a jagged tensor, given the desired offsets of the resulting dense tensor.
8991
9092
Args:
9193
dense (Tensor): A dense input tensor to be converted
9294
93-
x_offsets (Tensor[]): A list of jagged offset tensors, one for each jagged dimension.
95+
x_offsets (Tensor[]): A list of jagged offset tensors, one for each jagged dimension.
9496
9597
total_L (int, Optional): Total number of values in the resulting jagged tensor.
9698
@@ -119,7 +121,7 @@ def add_docs(method, docstr):
119121
Args:
120122
values (Tensor): Jagged tensor values
121123
122-
offsets (Tensor[]): A list of jagged offset tensors, one for each jagged dimension.
124+
offsets (Tensor[]): A list of jagged offset tensors, one for each jagged dimension.
123125
124126
max_lengths (int[]): A list with max_length for each jagged dimension.
125127
@@ -147,13 +149,13 @@ def add_docs(method, docstr):
147149
"""
148150
jagged_dense_elementwise_add(x_values, x_offsets, y) -> Tensor
149151
150-
Adds a jagged tensor to a dense tensor, resulting in dense tensor. Jagged
152+
Adds a jagged tensor to a dense tensor, resulting in dense tensor. Jagged
151153
tensor input will be padded with zeros for the purposes of the addition.
152154
153155
Args:
154156
x_values (Tensor): Jagged tensor values
155157
156-
offsets (Tensor[]): A list of jagged offset tensors, one for each jagged dimension.
158+
offsets (Tensor[]): A list of jagged offset tensors, one for each jagged dimension.
157159
158160
y (Tensor): A dense tensor
159161
@@ -174,7 +176,7 @@ def add_docs(method, docstr):
174176
Args:
175177
x_values (Tensor): Jagged tensor values
176178
177-
x_offsets (Tensor[]): A list of jagged offset tensors, one for each jagged dimension.
179+
x_offsets (Tensor[]): A list of jagged offset tensors, one for each jagged dimension.
178180
179181
y (Tensor): A dense tensor
180182
@@ -195,7 +197,7 @@ def add_docs(method, docstr):
195197
Args:
196198
x_values (Tensor): Jagged tensor values
197199
198-
x_offsets (Tensor[]): A list of jagged offset tensors, one for each jagged dimension.
200+
x_offsets (Tensor[]): A list of jagged offset tensors, one for each jagged dimension.
199201
200202
y_0 (Tensor): A dense tensor
201203
@@ -218,7 +220,7 @@ def add_docs(method, docstr):
218220
Args:
219221
x_values (Tensor): Jagged tensor values
220222
221-
x_offsets (Tensor[]): A list of jagged offset tensors, one for each jagged dimension.
223+
x_offsets (Tensor[]): A list of jagged offset tensors, one for each jagged dimension.
222224
223225
y (Tensor): A dense tensor
224226
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env python3
2+
3+
# Copyright (c) Meta Platforms, Inc. and affiliates.
4+
# All rights reserved.
5+
# This source code is licensed under the BSD-style license found in the
6+
# LICENSE file in the root directory of this source tree.

fbgemm_gpu/fbgemm_gpu/quantize_comm.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from typing import Optional, TypeVar
1515

1616
import torch
17+
from torch.autograd.profiler import record_function
1718

1819
from fbgemm_gpu.quantize_utils import (
1920
bf16_to_fp32,
@@ -24,7 +25,7 @@
2425
hfp8_to_fp32,
2526
)
2627
from fbgemm_gpu.split_embedding_configs import SparseType
27-
from torch.autograd.profiler import record_function
28+
2829

2930
logger: logging.Logger = logging.getLogger()
3031

fbgemm_gpu/fbgemm_gpu/split_embedding_inference_converter.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
import math
1212
from typing import Optional, Tuple
1313

14-
import fbgemm_gpu.split_table_batched_embeddings_ops as split_table_batched_embeddings_ops
1514
import numpy as np
1615
import torch
17-
from fbgemm_gpu.split_embedding_configs import QuantizationConfig, SparseType
1816
from torch import nn, Tensor
1917

18+
import fbgemm_gpu.split_table_batched_embeddings_ops as split_table_batched_embeddings_ops
19+
from fbgemm_gpu.split_embedding_configs import QuantizationConfig, SparseType
20+
2021
# TODO: add per-feature based converter option (based on embedding_specs during inference)
2122
# TODO: optimize embedding pruning and quantization latency.
2223
class SplitEmbInferenceConverter:

fbgemm_gpu/fbgemm_gpu/split_table_batched_embeddings_ops.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
from math import log2
1515
from typing import Dict, List, NamedTuple, Optional, Tuple, Type, Union
1616

17-
import fbgemm_gpu.split_embedding_codegen_lookup_invokers as invokers
1817
import torch
19-
from fbgemm_gpu.split_embedding_configs import EmbOptimType as OptimType, SparseType
2018
from torch import nn, Tensor
2119

20+
import fbgemm_gpu.split_embedding_codegen_lookup_invokers as invokers
21+
from fbgemm_gpu.split_embedding_configs import EmbOptimType as OptimType, SparseType
22+
2223
DEFAULT_ASSOC = 32 if torch.version.hip is None else 64
2324
# Maximum number of times prefetch() can be called without
2425
# a corresponding forward() call

fbgemm_gpu/fbgemm_gpu/uvm.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from typing import Optional
1010

1111
import torch
12+
1213
from fbgemm_gpu.enums import create_enums
1314

1415
try:
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#!/usr/bin/env python3
2+
3+
# Copyright (c) Meta Platforms, Inc. and affiliates.
4+
# All rights reserved.
5+
# This source code is licensed under the BSD-style license found in the
6+
# LICENSE file in the root directory of this source tree.
7+
8+
# pyre-unsafe
9+
10+
"""Check Python source code contains Meta copyright header
11+
"""
12+
13+
from __future__ import annotations
14+
15+
import os
16+
import sys
17+
18+
import click
19+
20+
21+
def process_header(header, comment):
22+
lines = header.split("\n")
23+
new_lines = []
24+
for line in lines:
25+
if line is None or line == "":
26+
new_lines.append(comment)
27+
else:
28+
new_lines.append(comment + " " + line)
29+
return "\n".join(new_lines) + "\n"
30+
31+
32+
HEADER = """Copyright (c) Meta Platforms, Inc. and affiliates.
33+
All rights reserved.
34+
This source code is licensed under the BSD-style license found in the
35+
LICENSE file in the root directory of this source tree.
36+
"""
37+
HEADER_lines = HEADER.splitlines()[1:]
38+
PY_HEADER = process_header(HEADER, "#")
39+
CPP_HEADER = process_header(HEADER, "//")
40+
41+
42+
def dfs(root_path: str) -> list[str]:
43+
"""DFS source code tree to find python files missing header
44+
45+
Parameters
46+
----------
47+
root_path : str
48+
root source directory path
49+
50+
Returns
51+
-------
52+
list[str]
53+
file list missing header
54+
"""
55+
ret = []
56+
for root, _, files in os.walk(root_path, topdown=False):
57+
for name in files:
58+
path = os.path.join(root, name)
59+
if path.endswith(".py"):
60+
with open(path) as fi:
61+
src = fi.read()
62+
flag = True
63+
for line in HEADER_lines:
64+
if line not in src:
65+
flag = False
66+
break
67+
if not flag:
68+
ret.append(path)
69+
return ret
70+
71+
72+
def fix_header(file_list: list[str]) -> None:
73+
"""Adding Meta header to to source files
74+
75+
Parameters
76+
----------
77+
file_list : list[str]
78+
file list missing header
79+
"""
80+
for path in file_list:
81+
src = ""
82+
with open(path) as fi:
83+
src = fi.read()
84+
with open(path, "w") as fo:
85+
fo.write(PY_HEADER)
86+
fo.write(src)
87+
88+
89+
@click.command()
90+
@click.option(
91+
"--path", help="Root directory of source to be checked", required=True, type=str
92+
)
93+
@click.option(
94+
"--fixit", default=False, help="Fix missing header", required=False, type=bool
95+
)
96+
def check_header(path, fixit):
97+
ret = dfs(path)
98+
if len(ret) == 0:
99+
sys.exit(0)
100+
print("Need to add Meta header to the following files.")
101+
print("----------------File List----------------")
102+
for line in ret:
103+
print(line)
104+
print("-----------------------------------------")
105+
if fixit:
106+
fix_header(ret)
107+
sys.exit(1)
108+
109+
110+
if __name__ == "__main__":
111+
check_header()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"problemMatcher": [
3+
{
4+
"owner": "flake8",
5+
"severity": "error",
6+
"pattern": [
7+
{
8+
"regexp": "^([^:]+):(\\d+):(\\d+):\\s+(.*)$",
9+
"file": 1,
10+
"line": 2,
11+
"column": 3,
12+
"message": 4
13+
}
14+
]
15+
}
16+
]
17+
}

0 commit comments

Comments
 (0)