Skip to content

Commit 37ef3c1

Browse files
committed
style: 格式化所有文件
Signed-off-by: YdrMaster <[email protected]>
1 parent 87936b1 commit 37ef3c1

File tree

511 files changed

+18169
-17760
lines changed

Some content is hidden

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

511 files changed

+18169
-17760
lines changed

.github/workflows/build.yml

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
name: Build and test cpu
22
on:
3+
pull_request:
34
push:
45
paths-ignore:
56
- '**.md'
67
- 'LICENSE'
7-
pull_request:
8-
paths:
9-
- '**.md'
10-
- 'LICENSE'
118

129
jobs:
1310
build:
@@ -23,6 +20,12 @@ jobs:
2320
with:
2421
submodules: recursive
2522

23+
- name: install black
24+
run: pip install black
25+
26+
- name: check format
27+
run: python3 format.py --path src --check
28+
2629
- name: build ${{ matrix.type }}
2730
run: make TYPE=${{ matrix.type }}
2831

format.py

+53-16
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
".py": "py",
2020
}
2121

22+
2223
def format_file(file: Path, check: bool, formatter) -> bool:
2324
formatter = formatter.get(SUPPORTED_FILES.get(file.suffix, None), None)
2425
if not formatter:
@@ -38,7 +39,9 @@ def format_file(file: Path, check: bool, formatter) -> bool:
3839
)
3940
if process.stderr:
4041
print(f"{Fore.YELLOW}{file} is not formatted.{Style.RESET_ALL}")
41-
print(f"Use {Fore.CYAN}{formatter} -style=file -i {file}{Style.RESET_ALL} to format it.")
42+
print(
43+
f"Use {Fore.CYAN}{formatter} -style=file -i {file}{Style.RESET_ALL} to format it."
44+
)
4245
return False
4346
else:
4447
subprocess.run(
@@ -60,7 +63,9 @@ def format_file(file: Path, check: bool, formatter) -> bool:
6063
)
6164
if process.stderr:
6265
print(f"{Fore.YELLOW}{file} is not formatted.{Style.RESET_ALL}")
63-
print(f"Use {Fore.CYAN}{formatter} {file}{Style.RESET_ALL} to format it.")
66+
print(
67+
f"Use {Fore.CYAN}{formatter} {file}{Style.RESET_ALL} to format it."
68+
)
6469
return False
6570
else:
6671
subprocess.run(
@@ -71,12 +76,15 @@ def format_file(file: Path, check: bool, formatter) -> bool:
7176
)
7277
print(f"{Fore.CYAN}Formatted: {file}{Style.RESET_ALL}")
7378
except FileNotFoundError:
74-
print(f"{Fore.RED}Formatter {formatter} not found, {file} skipped.{Style.RESET_ALL}")
79+
print(
80+
f"{Fore.RED}Formatter {formatter} not found, {file} skipped.{Style.RESET_ALL}"
81+
)
7582
except subprocess.CalledProcessError as e:
7683
print(f"{Fore.RED}Formatter {formatter} failed: {e}{Style.RESET_ALL}")
7784

7885
return True
7986

87+
8088
def git_added_files():
8189
"""获取所有已暂存更改的文件"""
8290
try:
@@ -92,6 +100,7 @@ def git_added_files():
92100
except subprocess.CalledProcessError as e:
93101
print(f"{Fore.RED}Git diff failed: {e}{Style.RESET_ALL}")
94102

103+
95104
def git_modified_since_ref(ref):
96105
"""获取从指定的 Git 引用到当前状态的修改文件列表"""
97106
try:
@@ -106,6 +115,7 @@ def git_modified_since_ref(ref):
106115
except subprocess.CalledProcessError as e:
107116
print(f"{Fore.RED}Git diff failed: {e}{Style.RESET_ALL}")
108117

118+
109119
def list_files(paths):
110120
"""递归获取指定路径下的所有文件"""
111121
files = []
@@ -117,7 +127,10 @@ def list_files(paths):
117127
for name in filenames:
118128
yield Path(dirpath) / name
119129
else:
120-
print(f"{Fore.RED}Error: {path} is not a file or directory.{Style.RESET_ALL}")
130+
print(
131+
f"{Fore.RED}Error: {path} is not a file or directory.{Style.RESET_ALL}"
132+
)
133+
121134

122135
def filter_in_path(file: Path, path) -> bool:
123136
"""判断文件是否在指定路径下"""
@@ -126,13 +139,24 @@ def filter_in_path(file: Path, path) -> bool:
126139
return True
127140
return False
128141

142+
129143
def main():
130144
parser = argparse.ArgumentParser()
131-
parser.add_argument("--ref", type=str, help="Git reference (commit hash) to compare against.")
132-
parser.add_argument("--path", nargs="*", type=Path, help="Files to format or check.")
133-
parser.add_argument("--check", action="store_true", help="Check files without modifying them.")
134-
parser.add_argument("--c", default="clang-format-16", help="C formatter (default: clang-format-16)")
135-
parser.add_argument("--py", default="black", help="Python formatter (default: black)")
145+
parser.add_argument(
146+
"--ref", type=str, help="Git reference (commit hash) to compare against."
147+
)
148+
parser.add_argument(
149+
"--path", nargs="*", type=Path, help="Files to format or check."
150+
)
151+
parser.add_argument(
152+
"--check", action="store_true", help="Check files without modifying them."
153+
)
154+
parser.add_argument(
155+
"--c", default="clang-format-16", help="C formatter (default: clang-format-16)"
156+
)
157+
parser.add_argument(
158+
"--py", default="black", help="Python formatter (default: black)"
159+
)
136160
args = parser.parse_args()
137161

138162
if args.ref is None and args.path is None:
@@ -145,22 +169,35 @@ def main():
145169
print(f"{Fore.GREEN}Formating files in {args.path}.{Style.RESET_ALL}")
146170
files = list_files(args.path)
147171
elif args.path is None:
148-
print(f"{Fore.GREEN}Formating git modified files from {args.ref}.{Style.RESET_ALL}")
172+
print(
173+
f"{Fore.GREEN}Formating git modified files from {args.ref}.{Style.RESET_ALL}"
174+
)
149175
files = git_modified_since_ref(args.ref)
150176
else:
151-
print(f"{Fore.GREEN}Formating git modified files from {args.ref} in {args.path}.{Style.RESET_ALL}")
152-
files = (file for file in git_modified_since_ref(args.ref) if filter_in_path(file, args.path))
177+
print(
178+
f"{Fore.GREEN}Formating git modified files from {args.ref} in {args.path}.{Style.RESET_ALL}"
179+
)
180+
files = (
181+
file
182+
for file in git_modified_since_ref(args.ref)
183+
if filter_in_path(file, args.path)
184+
)
153185

154186
formatted = True
155187
for file in files:
156-
if not format_file(file, args.check,{
157-
"c": args.c,
158-
"py": args.py,
159-
}):
188+
if not format_file(
189+
file,
190+
args.check,
191+
{
192+
"c": args.c,
193+
"py": args.py,
194+
},
195+
):
160196
formatted = False
161197

162198
if not formatted:
163199
exit(1)
164200

201+
165202
if __name__ == "__main__":
166203
main()

src/00common/include/common.h

+21-19
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,29 @@
1313
#include <sstream>
1414

1515
namespace refactor {
16-
/// @brief 用于表示维度/形状的差的数值,主要是一些属性。
17-
using ddim_t = int16_t;
18-
/// @brief 用于表示形状的数值。
19-
using dim_t = uint32_t;
20-
/// @brief 用于表示带符号的形状的数值。
21-
using sdim_t = int32_t;
22-
/// @brief 用于表示对象的数量。
23-
using count_t = uint32_t;
16+
/// @brief 用于表示维度/形状的差的数值,主要是一些属性。
17+
using ddim_t = int16_t;
18+
/// @brief 用于表示形状的数值。
19+
using dim_t = uint32_t;
20+
/// @brief 用于表示带符号的形状的数值。
21+
using sdim_t = int32_t;
22+
/// @brief 用于表示对象的数量。
23+
using count_t = uint32_t;
2424

25-
template<class T> using Arc = std::shared_ptr<T>;
25+
template <class T>
26+
using Arc = std::shared_ptr<T>;
2627

27-
template<class Container> std::string vec2str(Container const &vec) {
28-
std::stringstream ss;
29-
ss << "[ ";
30-
for (auto d : vec) {
31-
ss << d << ' ';
32-
}
33-
ss << ']';
34-
return ss.str();
28+
template <class Container>
29+
std::string vec2str(Container const &vec) {
30+
std::stringstream ss;
31+
ss << "[ ";
32+
for (auto d : vec) {
33+
ss << d << ' ';
3534
}
35+
ss << ']';
36+
return ss.str();
37+
}
3638

37-
}// namespace refactor
39+
} // namespace refactor
3840

39-
#endif// COMMON_TYPES
41+
#endif // COMMON_TYPES

src/00common/include/common/bf16_t.h

+57-54
Original file line numberDiff line numberDiff line change
@@ -7,59 +7,62 @@
77

88
namespace refactor {
99

10-
class bf16_t final {
11-
uint16_t code;
12-
13-
union converter {
14-
float f32;
15-
uint16_t u16[2];
16-
};
17-
18-
constexpr static uint16_t MASK_SIGN16 = 0b1'00000'00000'00000;
19-
20-
public:
21-
constexpr bf16_t(uint16_t code) noexcept : code(code) {}
22-
constexpr bf16_t(float value) noexcept : bf16_t(converter{value}.u16[1]) {}
23-
constexpr bf16_t() noexcept : bf16_t(0.f) {}
24-
constexpr bf16_t(bf16_t const &) noexcept = default;
25-
constexpr bf16_t(bf16_t &&) noexcept = default;
26-
27-
constexpr uint16_t as_code() const noexcept {
28-
return code;
29-
}
30-
31-
constexpr float to_f32() const noexcept {
32-
converter c{};
33-
c.u16[1] = code;
34-
c.u16[0] = 0;
35-
return c.f32;
36-
}
37-
38-
constexpr bool is_inf() const noexcept {
39-
return std::isinf(to_f32());
40-
}
41-
42-
constexpr bool is_nan() const noexcept {
43-
return std::isnan(to_f32());
44-
}
45-
46-
constexpr bf16_t operator-() const noexcept {
47-
return static_cast<decltype(code)>(code ^ (code | MASK_SIGN16));
48-
}
49-
50-
constexpr bool operator==(bf16_t const &others) const noexcept {
51-
return code == others.code && !is_nan();
52-
}
53-
54-
constexpr bool operator!=(bf16_t const &others) const noexcept {
55-
return !operator==(others);
56-
}
57-
58-
std::string to_string() const noexcept {
59-
return std::to_string(to_f32());
60-
}
61-
};
10+
class bf16_t final {
11+
uint16_t code;
6212

63-
}// namespace refactor
13+
union converter {
14+
float f32;
15+
uint16_t u16[2];
16+
};
6417

65-
#endif// BF16_H
18+
constexpr static uint16_t MASK_SIGN16 = 0b1'00000'00000'00000;
19+
20+
public:
21+
constexpr bf16_t(uint16_t code) noexcept
22+
: code(code) {}
23+
constexpr bf16_t(float value) noexcept
24+
: bf16_t(converter{value}.u16[1]) {}
25+
constexpr bf16_t() noexcept
26+
: bf16_t(0.f) {}
27+
constexpr bf16_t(bf16_t const &) noexcept = default;
28+
constexpr bf16_t(bf16_t &&) noexcept = default;
29+
30+
constexpr uint16_t as_code() const noexcept {
31+
return code;
32+
}
33+
34+
constexpr float to_f32() const noexcept {
35+
converter c{};
36+
c.u16[1] = code;
37+
c.u16[0] = 0;
38+
return c.f32;
39+
}
40+
41+
constexpr bool is_inf() const noexcept {
42+
return std::isinf(to_f32());
43+
}
44+
45+
constexpr bool is_nan() const noexcept {
46+
return std::isnan(to_f32());
47+
}
48+
49+
constexpr bf16_t operator-() const noexcept {
50+
return static_cast<decltype(code)>(code ^ (code | MASK_SIGN16));
51+
}
52+
53+
constexpr bool operator==(bf16_t const &others) const noexcept {
54+
return code == others.code && !is_nan();
55+
}
56+
57+
constexpr bool operator!=(bf16_t const &others) const noexcept {
58+
return !operator==(others);
59+
}
60+
61+
std::string to_string() const noexcept {
62+
return std::to_string(to_f32());
63+
}
64+
};
65+
66+
} // namespace refactor
67+
68+
#endif // BF16_H

0 commit comments

Comments
 (0)