forked from pytorch/FBGEMM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_forward_quantized.py
More file actions
189 lines (169 loc) · 5.39 KB
/
generate_forward_quantized.py
File metadata and controls
189 lines (169 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
# pyre-strict
# flake8: noqa F401
import sys
from dataclasses import dataclass
try:
from .common import CodeTemplate
except:
# pyre-ignore[21]
from common import CodeTemplate
@dataclass
class TemplateParams:
output_rows_per_thread: int
input_rows_in_flight: int
min_128b_rows: int
max_128b_rows: int
@dataclass
class ElemType:
cpp_type_name: str
primitive_type: str
bit_width: int
template_params: list[TemplateParams]
@property
def enum_name(self) -> str:
return f"{self.primitive_type}{self.bit_width}"
ELEM_TYPES = [
ElemType(
"float",
"FP",
32,
[
TemplateParams(2, 4, 0, 4),
TemplateParams(2, 2, 4, 16),
TemplateParams(1, 1, 16, 32),
TemplateParams(1, 1, 32, 64),
],
),
ElemType(
"__half2",
"FP",
16,
[
TemplateParams(2, 8, 0, 2),
TemplateParams(2, 8, 2, 4),
TemplateParams(2, 4, 4, 8),
TemplateParams(2, 2, 8, 16),
TemplateParams(1, 2, 16, 32),
TemplateParams(1, 1, 32, 64),
],
),
ElemType(
"uint32_t",
"FP",
8,
[
TemplateParams(2, 8, 0, 1),
TemplateParams(2, 4, 1, 2),
TemplateParams(2, 4, 2, 4),
TemplateParams(2, 4, 4, 8),
TemplateParams(2, 2, 8, 16),
TemplateParams(1, 2, 16, 32),
],
),
ElemType(
"uint32_t",
"INT",
8,
[
TemplateParams(2, 8, 0, 1),
TemplateParams(2, 4, 1, 2),
TemplateParams(2, 4, 0, 2),
TemplateParams(2, 4, 2, 4),
TemplateParams(2, 2, 2, 4),
TemplateParams(2, 1, 4, 8),
TemplateParams(2, 4, 4, 8),
TemplateParams(1, 1, 8, 16),
TemplateParams(2, 2, 8, 16),
TemplateParams(1, 1, 16, 32),
TemplateParams(1, 2, 16, 32),
],
),
ElemType(
"uint32_t",
"INT",
4,
[
TemplateParams(4, 8, 0, 1),
TemplateParams(2, 8, 1, 2),
TemplateParams(2, 4, 0, 2),
TemplateParams(1, 4, 2, 4),
TemplateParams(1, 4, 4, 8),
TemplateParams(1, 2, 4, 8),
TemplateParams(1, 4, 8, 16),
TemplateParams(1, 1, 8, 16),
],
),
ElemType(
"uint32_t",
"INT",
2,
[
TemplateParams(2, 16, 0, 1),
TemplateParams(2, 8, 1, 2),
TemplateParams(2, 8, 2, 4),
TemplateParams(2, 4, 4, 8),
],
),
]
ELEM_TYPES_MAP: dict[str, ElemType] = {etype.enum_name: etype for etype in ELEM_TYPES}
class ForwardQuantizedGenerator:
@staticmethod
def generate_nbit_kernel() -> None:
# Generate the CUDA nbit (kernel) templates
template = CodeTemplate.load(
"inference/embedding_forward_quantized_split_nbit_kernel_template.cu"
)
for weighted in [True, False]:
for nobag in [True, False]:
if not nobag or not weighted:
for emb_weight_type in ELEM_TYPES:
wdesc = f"{ 'weighted' if weighted else 'unweighted' }{ '_nobag' if nobag else '' }_{ emb_weight_type.enum_name.lower() }"
template.write(
f"gen_embedding_forward_quantized_split_nbit_kernel_{ wdesc }_codegen_cuda.cu",
weighted=weighted,
nobag=nobag,
emb_weight_type=emb_weight_type,
)
@staticmethod
def generate_nbit_host() -> None:
# Generate the CUDA nbit (host) templates
template = CodeTemplate.load(
"inference/embedding_forward_quantized_split_nbit_host_template.cu"
)
for weighted in [True, False]:
for nobag in [True, False]:
if not nobag or not weighted:
wdesc = f"{ 'weighted' if weighted else 'unweighted' }{ '_nobag' if nobag else '' }"
template.write(
f"gen_embedding_forward_quantized_split_nbit_host_{ wdesc }_codegen_cuda.cu",
weighted=weighted,
nobag=nobag,
type_map=ELEM_TYPES_MAP,
)
@staticmethod
def generate_nbit_cpu() -> None:
# Generate the CPU templates
template = CodeTemplate.load(
"inference/embedding_forward_quantized_cpu_template.cpp"
)
for weighted in [True, False]:
template.write(
f"gen_embedding_forward_quantized_{ 'weighted' if weighted else 'unweighted' }_codegen_cpu.cpp",
weighted=weighted,
type_map=ELEM_TYPES_MAP,
)
@staticmethod
def generate() -> None:
ForwardQuantizedGenerator.generate_nbit_kernel()
ForwardQuantizedGenerator.generate_nbit_host()
ForwardQuantizedGenerator.generate_nbit_cpu()
def main() -> None:
ForwardQuantizedGenerator.generate()
if __name__ == "__main__":
print(f"[GENERATE FORWARD QUANTIZED]: {sys.argv}")
main()