Skip to content

Commit

Permalink
Merge pull request #55 from JDAI-CV/support_onnx_input
Browse files Browse the repository at this point in the history
Support onnx input
  • Loading branch information
daquexian authored Jun 18, 2019
2 parents 8cab557 + 550c68f commit cf20ec9
Show file tree
Hide file tree
Showing 6 changed files with 336 additions and 64 deletions.
3 changes: 2 additions & 1 deletion common/Shaper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,9 @@ void Shaper::Eltwise(const std::string &input1_name,
const std::string &output_name) {
auto shape1 = shape_map_.at(input1_name);
auto shape2 = shape_map_.at(input2_name);
// TODO: broadcasting
auto output_shape =
shape1.size() > shape2.size() ? shape1 : shape2; // broadcasting
shape1.size() >= shape2.size() ? shape1 : shape2;
shape_map_[output_name] = output_shape;
}

Expand Down
49 changes: 29 additions & 20 deletions generate_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@ def infer_cfg(cfg, target: Target):
op['input'] = []
if 'base_input_num' not in op or op['base_input_num'] == 1:
op['input'].insert(0,
{'name': 'input', 'nnapi_type': 'tensor', 'cpp_type': 'str', 'needed_by_shaper': True})
{'name': 'input', 'nnapi_type': 'tensor', 'cpp_type': 'str', 'input': True, 'needed_by_shaper': True})
elif op['base_input_num'] == 2:
op['input'] = [{'name': 'input1', 'nnapi_type': 'tensor', 'cpp_type': 'str', 'needed_by_shaper': True},
{'name': 'input2', 'nnapi_type': 'tensor', 'cpp_type': 'str', 'needed_by_shaper': True}] \
op['input'] = [{'name': 'input1', 'nnapi_type': 'tensor', 'cpp_type': 'str', 'input': True, 'needed_by_shaper': True},
{'name': 'input2', 'nnapi_type': 'tensor', 'cpp_type': 'str', 'input': True, 'needed_by_shaper': True}] \
+ op['input']
elif op['base_input_num'] == 'n':
op['input'].insert(0,
{'name': 'inputs', 'nnapi_type': 'tensor', 'cpp_type': 'str_list',
{'name': 'inputs', 'nnapi_type': 'tensor', 'cpp_type': 'str_list', 'input': True,
'needed_by_shaper': True})
elif op['base_input_num'] == 0:
pass
Expand Down Expand Up @@ -145,11 +145,12 @@ def infer_cfg(cfg, target: Target):
ipt['name'] = 'bias'
ipt['nnapi_type'] = 'tensor'
ipt['cpp_type'] = 'optional_str'
ipt['learnable'] = True
if 'learnable' not in ipt:
ipt['learnable'] = False
if ipt['learnable'] and 'convert_func' not in ipt:
ipt['input'] = True
ipt['convert_func'] = 'OnnxToNnapiIdentity'
if 'input' not in ipt:
ipt['input'] = False
if 'convert_func' not in ipt:
ipt['convert_func'] = 'OnnxToNnapiAxes0231'
if 'needed_by_shaper' not in ipt:
ipt['needed_by_shaper'] = False

Expand Down Expand Up @@ -186,20 +187,28 @@ def generate_onnx_converter():
if op['fused']:
cogoutl(f"const auto activation = FindActivation(model_proto_, output);")
for x in op['input']:
if x['learnable']:
assert x['cpp_type'] in ['str', 'optional_str']
if x['input']:
if x['cpp_type'] == 'str':
cogoutl(f"""{{
const auto name = {x['name']};""")
cogoutl(f"""
{{
const auto name = {x['name']};""")
elif x['cpp_type'] == 'optional_str':
cogoutl(f"""if ({x['name']}.has_value()) {{
const auto name = {x['name']}.value();""")
cogoutl(f"""const auto &onnx_tensor = onnx_tensors_.at(name);
const auto new_tensor = {x['convert_func']}(onnx_tensor);
shaper_.AddShape(name, new_tensor.shape);
nnapi_tensors_[name] = new_tensor;
CreateTensorFb(name, new_tensor);""")
cogoutl("}")
cogoutl(f"""
if ({x['name']}.has_value()) {{
const auto name = {x['name']}.value();""")
elif x['cpp_type'] == 'str_list':
cogoutl(f"""
for (const auto &name : {x['name']}) {{""")
cogoutl(f"""
if (onnx_tensors_.has(name)) {{
const auto &onnx_tensor = onnx_tensors_.at(name);
const auto new_tensor = {x['convert_func']}(onnx_tensor);
shaper_.AddShape(name, new_tensor.shape);
nnapi_tensors_[name] = new_tensor;
CreateTensorFb(name, new_tensor);
}}
}}
""")
if x['cpp_type'] == 'str_list':
cogoutl(f"const auto {x['name']}_fb = FbStrVector({x['name']});")

Expand Down
2 changes: 1 addition & 1 deletion include/dnnlibrary/ModelBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
#include <string>
#include <vector>

#include <common/data_types.h>
#include <common/Shaper.h>
#include <common/StrKeyMap.h>
#include <common/data_types.h>
#include <dnnlibrary/Model.h>
#include <dnnlibrary/NeuralNetworksWrapper.h>

Expand Down
9 changes: 7 additions & 2 deletions include/tools/onnx2daq/OnnxConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,21 @@ class OnnxConverter {
// OnnxConverter auto generated methods end

/**
* transpose axes to [1, 2, 3, 0]
* for onnx dw conv weight to nnapi dw conv weight
* onnx: [filter_out_channel, filter_in_channel / group, height, width]
* nnapi: [1, height, width, depth_out]
*/
Tensor OnnxToNnapiDwConvWeight(const Tensor &src);
Tensor OnnxToNnapiAxes1230(const Tensor &src);

/**
* transpose axes to [0, 2, 3, 1]
* for nchw (onnx) -> nhwc (nnapi)
* or onnx conv weight to nnapi conv (not dw conv) weight:
* onnx: [filter_out_channel, filter_in_channel, height, width]
* nnapi: [depth_out, height, width, depth_in]
*/
Tensor OnnxToNnapiVanillaConvWeight(const Tensor &src);
Tensor OnnxToNnapiAxes0231(const Tensor &src);

/**
* Just return the same tensor
Expand Down
12 changes: 6 additions & 6 deletions ops.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
-
name: weight
nnapi_type: tensor
# "learnable" stands for the tensor is read from serialized model file
learnable: true
convert_func: OnnxToNnapiVanillaConvWeight
# "input" stands for onnx input instead of attribute (TODO: a new name)
input: true
cpp_type: str
needed_by_shaper: true
-
Expand Down Expand Up @@ -202,9 +201,10 @@
-
name: weight
nnapi_type: tensor
learnable: true
input: true
cpp_type: str
needed_by_shaper: true
convert_func: OnnxToNnapiIdentity
-
predefined: optional_bias
nnapi: FULLY_CONNECTED
Expand Down Expand Up @@ -238,8 +238,8 @@
-
name: weight
nnapi_type: tensor
learnable: true
convert_func: OnnxToNnapiDwConvWeight
input: true
convert_func: OnnxToNnapiAxes1230
cpp_type: str
needed_by_shaper: true
-
Expand Down
Loading

0 comments on commit cf20ec9

Please sign in to comment.