Skip to content

Commit f779d6c

Browse files
committed
Refactor code about fuse code
1 parent 36c15fb commit f779d6c

File tree

10 files changed

+197
-184
lines changed

10 files changed

+197
-184
lines changed

common/daq.fbs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ table CONV_2D_Input {
7171
padding_bottom: int;
7272
stride_x: int;
7373
stride_y: int;
74-
fuse:FuseCode;
74+
fuse_code: FuseCode;
7575
}
7676

7777
table CONV_2D_Output {
@@ -93,7 +93,7 @@ table AVERAGE_POOL_2D_Input {
9393
stride_y: int;
9494
kernel_width: int;
9595
kernel_height: int;
96-
fuse:FuseCode;
96+
fuse_code: FuseCode;
9797
}
9898

9999
table AVERAGE_POOL_2D_Output {
@@ -115,7 +115,7 @@ table MAX_POOL_2D_Input {
115115
stride_y: int;
116116
kernel_width: int;
117117
kernel_height: int;
118-
fuse:FuseCode;
118+
fuse_code: FuseCode;
119119
}
120120

121121
table MAX_POOL_2D_Output {
@@ -158,7 +158,7 @@ table FULLY_CONNECTED_Input {
158158
input: string;
159159
weight: string;
160160
bias: string;
161-
fuse:FuseCode;
161+
fuse_code: FuseCode;
162162
}
163163

164164
table FULLY_CONNECTED_Output {
@@ -173,7 +173,7 @@ table FULLY_CONNECTED {
173173
table ADD_Input {
174174
input1: string;
175175
input2: string;
176-
fuse:FuseCode;
176+
fuse_code: FuseCode;
177177
}
178178

179179
table ADD_Output {
@@ -210,7 +210,7 @@ table DEPTHWISE_CONV_2D_Input {
210210
stride_x: int;
211211
stride_y: int;
212212
depth_multiplier: int;
213-
fuse:FuseCode;
213+
fuse_code: FuseCode;
214214
}
215215

216216
table DEPTHWISE_CONV_2D_Output {
@@ -273,7 +273,7 @@ table STRIDED_SLICE {
273273
table MUL_Input {
274274
input1: string;
275275
input2: string;
276-
fuse:FuseCode;
276+
fuse_code: FuseCode;
277277
}
278278

279279
table MUL_Output {

dnnlibrary/DaqReader.cpp

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -218,38 +218,38 @@ expected<Unit, std::string> AddLayers(const DNN::Model &model,
218218
case DNN::LayerType::CONV_2D: {
219219
UNPACK_LAYER_QUANT(CONV_2D, input, weight, bias, padding_left,
220220
padding_right, padding_top, padding_bottom,
221-
stride_x, stride_y, fuse);
221+
stride_x, stride_y, fuse_code);
222222
const dnn::optional<std::string> bias_right_type =
223223
(bias == "") ? dnn::nullopt : dnn::make_optional(bias);
224224

225225
TRY(builder.AddLayer_CONV_2D(
226226
input, weight, bias_right_type, padding_left, padding_right,
227-
padding_top, padding_bottom, stride_x, stride_y, fuse,
227+
padding_top, padding_bottom, stride_x, stride_y, fuse_code,
228228
output, quant_info));
229229
break;
230230
}
231231
case DNN::LayerType::AVERAGE_POOL_2D: {
232232
UNPACK_LAYER_QUANT(AVERAGE_POOL_2D, input, padding_left,
233233
padding_right, padding_top, padding_bottom,
234234
stride_x, stride_y, kernel_width,
235-
kernel_height, fuse);
235+
kernel_height, fuse_code);
236236

237237
TRY(builder.AddLayer_AVERAGE_POOL_2D(
238238
input, padding_left, padding_right, padding_top,
239239
padding_bottom, stride_x, stride_y, kernel_width,
240-
kernel_height, fuse, output, quant_info));
240+
kernel_height, fuse_code, output, quant_info));
241241
break;
242242
}
243243
case DNN::LayerType::MAX_POOL_2D: {
244244
UNPACK_LAYER_QUANT(MAX_POOL_2D, input, padding_left,
245245
padding_right, padding_top, padding_bottom,
246246
stride_x, stride_y, kernel_width,
247-
kernel_height, fuse);
247+
kernel_height, fuse_code);
248248

249249
TRY(builder.AddLayer_MAX_POOL_2D(
250250
input, padding_left, padding_right, padding_top,
251251
padding_bottom, stride_x, stride_y, kernel_width,
252-
kernel_height, fuse, output, quant_info));
252+
kernel_height, fuse_code, output, quant_info));
253253
break;
254254
}
255255
case DNN::LayerType::RELU: {
@@ -265,18 +265,20 @@ expected<Unit, std::string> AddLayers(const DNN::Model &model,
265265
break;
266266
}
267267
case DNN::LayerType::FULLY_CONNECTED: {
268-
UNPACK_LAYER_QUANT(FULLY_CONNECTED, input, weight, bias, fuse);
268+
UNPACK_LAYER_QUANT(FULLY_CONNECTED, input, weight, bias,
269+
fuse_code);
269270
const dnn::optional<std::string> bias_right_type =
270271
(bias == "") ? dnn::nullopt : dnn::make_optional(bias);
271272

272-
TRY(builder.AddLayer_FULLY_CONNECTED(
273-
input, weight, bias_right_type, fuse, output, quant_info));
273+
TRY(builder.AddLayer_FULLY_CONNECTED(input, weight,
274+
bias_right_type, fuse_code,
275+
output, quant_info));
274276
break;
275277
}
276278
case DNN::LayerType::ADD: {
277-
UNPACK_LAYER_QUANT(ADD, input1, input2, fuse);
279+
UNPACK_LAYER_QUANT(ADD, input1, input2, fuse_code);
278280

279-
TRY(builder.AddLayer_ADD(input1, input2, fuse, output,
281+
TRY(builder.AddLayer_ADD(input1, input2, fuse_code, output,
280282
quant_info));
281283
break;
282284
}
@@ -290,14 +292,14 @@ expected<Unit, std::string> AddLayers(const DNN::Model &model,
290292
UNPACK_LAYER_QUANT(DEPTHWISE_CONV_2D, input, weight, bias,
291293
padding_left, padding_right, padding_top,
292294
padding_bottom, stride_x, stride_y,
293-
depth_multiplier, fuse);
295+
depth_multiplier, fuse_code);
294296
const dnn::optional<std::string> bias_right_type =
295297
(bias == "") ? dnn::nullopt : dnn::make_optional(bias);
296298

297299
TRY(builder.AddLayer_DEPTHWISE_CONV_2D(
298300
input, weight, bias_right_type, padding_left, padding_right,
299301
padding_top, padding_bottom, stride_x, stride_y,
300-
depth_multiplier, fuse, output, quant_info));
302+
depth_multiplier, fuse_code, output, quant_info));
301303
break;
302304
}
303305
case DNN::LayerType::BATCH_TO_SPACE_ND: {
@@ -324,9 +326,9 @@ expected<Unit, std::string> AddLayers(const DNN::Model &model,
324326
break;
325327
}
326328
case DNN::LayerType::MUL: {
327-
UNPACK_LAYER_QUANT(MUL, input1, input2, fuse);
329+
UNPACK_LAYER_QUANT(MUL, input1, input2, fuse_code);
328330

329-
TRY(builder.AddLayer_MUL(input1, input2, fuse, output,
331+
TRY(builder.AddLayer_MUL(input1, input2, fuse_code, output,
330332
quant_info));
331333
break;
332334
}

dnnlibrary/ModelBuilderImpl.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ expected<Unit, std::string> ModelBuilder::AddLayer_CONV_2D(
1515
const std::string &input, const std::string &weight,
1616
const dnn::optional<std::string> &bias, int32_t padding_left,
1717
int32_t padding_right, int32_t padding_top, int32_t padding_bottom,
18-
int32_t stride_x, int32_t stride_y, int32_t fuse_code,
18+
int32_t stride_x, int32_t stride_y, FuseCode fuse_code,
1919
const std::string &output,
2020
const dnn::optional<QuantInfo> &output_quant_info) {
2121
if (nnapi_->android_sdk_version < 27) {
@@ -69,7 +69,7 @@ expected<Unit, std::string> ModelBuilder::AddLayer_AVERAGE_POOL_2D(
6969
const std::string &input, int32_t padding_left, int32_t padding_right,
7070
int32_t padding_top, int32_t padding_bottom, int32_t stride_x,
7171
int32_t stride_y, int32_t kernel_width, int32_t kernel_height,
72-
int32_t fuse_code, const std::string &output,
72+
FuseCode fuse_code, const std::string &output,
7373
const dnn::optional<QuantInfo> &output_quant_info) {
7474
if (nnapi_->android_sdk_version < 27) {
7575
return make_unexpected("AVERAGE_POOL_2D requires API 27");
@@ -97,7 +97,7 @@ expected<Unit, std::string> ModelBuilder::AddLayer_MAX_POOL_2D(
9797
const std::string &input, int32_t padding_left, int32_t padding_right,
9898
int32_t padding_top, int32_t padding_bottom, int32_t stride_x,
9999
int32_t stride_y, int32_t kernel_width, int32_t kernel_height,
100-
int32_t fuse_code, const std::string &output,
100+
FuseCode fuse_code, const std::string &output,
101101
const dnn::optional<QuantInfo> &output_quant_info) {
102102
if (nnapi_->android_sdk_version < 27) {
103103
return make_unexpected("MAX_POOL_2D requires API 27");
@@ -162,7 +162,7 @@ expected<Unit, std::string> ModelBuilder::AddLayer_SOFTMAX(
162162

163163
expected<Unit, std::string> ModelBuilder::AddLayer_FULLY_CONNECTED(
164164
const std::string &input, const std::string &weight,
165-
const dnn::optional<std::string> &bias, int32_t fuse_code,
165+
const dnn::optional<std::string> &bias, FuseCode fuse_code,
166166
const std::string &output,
167167
const dnn::optional<QuantInfo> &output_quant_info) {
168168
if (nnapi_->android_sdk_version < 27) {
@@ -211,7 +211,7 @@ expected<Unit, std::string> ModelBuilder::AddLayer_FULLY_CONNECTED(
211211
}
212212

213213
expected<Unit, std::string> ModelBuilder::AddLayer_ADD(
214-
const std::string &input1, const std::string &input2, int32_t fuse_code,
214+
const std::string &input1, const std::string &input2, FuseCode fuse_code,
215215
const std::string &output,
216216
const dnn::optional<QuantInfo> &output_quant_info) {
217217
if (nnapi_->android_sdk_version < 27) {
@@ -262,7 +262,7 @@ expected<Unit, std::string> ModelBuilder::AddLayer_DEPTHWISE_CONV_2D(
262262
const dnn::optional<std::string> &bias, int32_t padding_left,
263263
int32_t padding_right, int32_t padding_top, int32_t padding_bottom,
264264
int32_t stride_x, int32_t stride_y, int32_t depth_multiplier,
265-
int32_t fuse_code, const std::string &output,
265+
FuseCode fuse_code, const std::string &output,
266266
const dnn::optional<QuantInfo> &output_quant_info) {
267267
if (nnapi_->android_sdk_version < 27) {
268268
return make_unexpected("DEPTHWISE_CONV_2D requires API 27");
@@ -402,7 +402,7 @@ expected<Unit, std::string> ModelBuilder::AddLayer_STRIDED_SLICE(
402402
}
403403

404404
expected<Unit, std::string> ModelBuilder::AddLayer_MUL(
405-
const std::string &input1, const std::string &input2, int32_t fuse_code,
405+
const std::string &input1, const std::string &input2, FuseCode fuse_code,
406406
const std::string &output,
407407
const dnn::optional<QuantInfo> &output_quant_info) {
408408
if (nnapi_->android_sdk_version < 27) {

generate_code.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ def add_tensor_operand(operand):
9696
raise Exception('Unknown cpp_type {}'.format(operand['cpp_type']))
9797

9898

99+
def has_fuse_code_attr(op: dict):
100+
return any([x['predefined'] == 'fuse_code' for x in op['input']])
101+
102+
99103
def infer_cfg(cfg, target: Target):
100104
next_pos = 0
101105
for i, op in enumerate(cfg):
@@ -135,14 +139,10 @@ def infer_cfg(cfg, target: Target):
135139
# op['nnapi'] = op['name'].upper()
136140
# if 'dnn' not in op:
137141
# op['dnn'] = op['name']
138-
if 'fused' not in op:
139-
op['fused'] = False
140142
if target == Target.ModelBuilder and 'nnapi_input' in op:
141143
op['input'].extend(op['nnapi_input'])
142144
elif target == Target.OnnxConverter and 'dnn_input' in op:
143145
op['input'].extend(op['dnn_input'])
144-
if op['fused'] and target == Target.ModelBuilder:
145-
op['input'].append({'name': 'fuse_code', 'nnapi_type': 'scalar', 'cpp_type': 'int32_t'})
146146
if 'support_quant_asymm' not in op:
147147
op['support_quant_asymm'] = False
148148
if 'converter_simple' not in op:
@@ -160,6 +160,10 @@ def infer_cfg(cfg, target: Target):
160160
ipt['cpp_type'] = 'optional_str'
161161
ipt['is_onnx_attr'] = False
162162
ipt['convert_func'] = 'OnnxToNnapiIdentity'
163+
elif ipt['predefined'] == 'fuse_code':
164+
ipt['name'] = 'fuse_code'
165+
ipt['nnapi_type'] = 'scalar'
166+
ipt['cpp_type'] = 'FuseCode'
163167
if 'is_onnx_attr' not in ipt:
164168
ipt['is_onnx_attr'] = True
165169
if 'convert_func' not in ipt:
@@ -199,8 +203,8 @@ def generate_onnx_converter():
199203
params = list(map(get_param, ipt_opt))
200204
params_str = ', '.join(map(lambda param: "{} {}".format(*param), params))
201205
cogoutl(f"void OnnxConverter::WriteDaqLayer_{op['nnapi']}{'' if op['converter_simple'] else 'Impl'}({params_str}) {{")
202-
if op['fused']:
203-
cogoutl(f"const auto activation = FindActivation(model_proto_, output);")
206+
# if has_fuse_code_attr(op):
207+
# cogoutl(f"const auto activation = FindActivation(model_proto_, output);")
204208
for x in op['input']:
205209
if not x['is_onnx_attr']:
206210
if x['cpp_type'] == 'str':
@@ -247,13 +251,13 @@ def get_input_param(x):
247251
return f"&{x['name']}_fb"
248252
elif x['cpp_type'] == 'int32_list':
249253
return f"&{x['name']}"
254+
elif x['predefined'] == 'fuse_code':
255+
return f"ConvertFuseCodeType({x['name']})"
250256
else:
251257
return x['name']
252258

253259
cogout(f"const auto input_param = DNN::Create{op['nnapi']}_InputDirect(builder_, ")
254260
cogout(', '.join(list(map(get_input_param, op['input']))))
255-
if op['fused']:
256-
cogout(', ConvertFuseCodeType(activation.second)')
257261
cogoutl(');')
258262
# cogout(', ')
259263
cogout(f"const auto output_param = DNN::Create{op['nnapi']}_OutputDirect(builder_, ")
@@ -287,8 +291,6 @@ def generate_daq_reader():
287291
cogoutl(f"case DNN::LayerType::{op['nnapi']}: {{")
288292

289293
arg_names = [x['name'] for x in op['input']]
290-
if op['fused']:
291-
arg_names += ['fuse']
292294
cogoutl(f"UNPACK_LAYER_QUANT({op['nnapi']}, {', '.join(arg_names)});")
293295
arg_names += [x['name'] for x in op['output']]
294296
for i, x in enumerate(op['input']):
@@ -318,13 +320,12 @@ def generate_fbs():
318320
'optional_str': 'string',
319321
'str_list': '[string]',
320322
'float': 'float',
323+
'FuseCode': 'FuseCode'
321324
}
322325
for i, op in enumerate(cfg):
323326
cogoutl(f"table {op['nnapi']}_Input {{")
324327
for x in op['input']:
325328
cogoutl(f" {x['name']}: {d[x['cpp_type']]};")
326-
if op['fused']:
327-
cogoutl(' fuse:FuseCode;')
328329
cogoutl('}')
329330
cogoutl('')
330331

0 commit comments

Comments
 (0)