提交 792ca746 编写于 作者: L Liangliang He

Merge branch 'generate-header' into 'master'

Fix source converter bug for ndk 12 and move create op to single file.

See merge request !167
...@@ -31,7 +31,7 @@ cc_test( ...@@ -31,7 +31,7 @@ cc_test(
cc_binary( cc_binary(
name = "mace_run", name = "mace_run",
srcs = glob(["models/*/*.cc"] + ["mace_run.cc"]), srcs = glob(["models/*/*.cc"] + ["mace_run.cc"]),
copts = ["-std=c++11", "-D_GLIBCXX_USE_C99_MATH_TR1", "-v", "-ftime-report"], copts = ["-std=c++11", "-D_GLIBCXX_USE_C99_MATH_TR1"],
linkopts = ["-fopenmp"], linkopts = ["-fopenmp"],
linkstatic = 1, linkstatic = 1,
deps = [ deps = [
......
...@@ -12,41 +12,47 @@ alignas(4) unsigned char {{ tensor.name }}[] = { ...@@ -12,41 +12,47 @@ alignas(4) unsigned char {{ tensor.name }}[] = {
}; };
} // namespace {{tag}} } // namespace {{tag}}
{% else %}
{% elif mode == 1 %}
#include <vector> #include <vector>
#include <string> #include <string>
#include "mace/core/mace.h" #include "mace/core/mace.h"
namespace {{tag}} { namespace {
static void UpdateOp(mace::OperatorDef &op,
{% for tensor in tensors %} const std::string &name,
extern unsigned char {{ tensor.name }}[]; const std::string &type,
{% endfor %} const std::vector<std::string> &inputs,
const std::vector<std::string> &outputs,
const std::vector<mace::DataType> &output_types) {
op.set_name(name);
op.set_type(type);
op.set_input(inputs);
op.set_output(outputs);
op.set_output_type(output_types);
}
}
} // namespace {{ tag }} namespace {{tag}}{
namespace { {% for i in range(start, end) %}
{% if net.arg|length != 0 %} void CreateOperator{{i}}(mace::OperatorDef &op) {
static void CreateNetArg(mace::NetDef &net_def) {
net_def.mutable_arg().reserve({{ net.arg|length }});
mace::Argument *arg = nullptr; mace::Argument *arg = nullptr;
{% for arg in net.arg %} {% for arg in net.op[i].arg %}
arg = net_def.add_arg(); arg = op.add_arg();
arg->set_name({{ arg.name|tojson }}); arg->set_name({{ arg.name|tojson }});
{%- if arg.HasField('f') %} {%- if arg.HasField('f') %}
arg->set_f({{ arg.f }}); arg->set_f({{ arg.f }});
{% endif %} {%- endif %}
{%- if arg.HasField('i') %} {%- if arg.HasField('i') %}
arg->set_i({{ arg.i }}); arg->set_i({{ arg.i }});
{% endif %} {%- endif %}
{%- if arg.HasField('s') %} {%- if arg.HasField('s') %}
arg->set_s({{ arg.s|tojson }}); arg->set_s({{ arg.s|tojson }});
{% endif %} {%- endif %}
{% if arg.floats|length != 0 %} {% if arg.floats|length != 0 %}
arg->set_floats({ {{ arg.floats|join(', ') }} }); arg->set_floats({ {{ arg.floats|join(', ') }} });
...@@ -57,43 +63,68 @@ static void CreateNetArg(mace::NetDef &net_def) { ...@@ -57,43 +63,68 @@ static void CreateNetArg(mace::NetDef &net_def) {
{% if arg.strings|length != 0 %} {% if arg.strings|length != 0 %}
arg->set_strings({ {{ arg.strings|stringfy() }} }); arg->set_strings({ {{ arg.strings|stringfy() }} });
{% endif %} {% endif %}
{% endfor %}
{% if net.op[i].HasField('mem_id') %}
op.set_mem_id({{net.op[i].mem_id}});
{% endif %}
{% for shape in net.op[i].output_shape %}
op.add_output_shape(mace::OutputShape({ {{ shape.dims|join(', ') }} }));
{% endfor %} {% endfor %}
} UpdateOp(op, {{ net.op[i].name|tojson }}, {{ net.op[i].type|tojson}},
{% endif %} { {{ net.op[i].input|stringfy }} },
{ {{ net.op[i].output|stringfy }} },
{ {{ net.op[i].output_type|join(', ') }} });
static void UpdateOp(mace::OperatorDef &op,
const std::string &name,
const std::string &type,
const std::vector<std::string> &inputs,
const std::vector<std::string> &outputs,
const std::vector<mace::DataType> &output_types) {
op.set_name(name);
op.set_type(type);
op.set_input(inputs);
op.set_output(outputs);
op.set_output_type(output_types);
} }
static void CreateOperators(std::vector<mace::OperatorDef> &ops) { {% endfor %}
ops.resize({{ net.op|length }});
} // namespace {{tag}}
{% else %}
#include <vector>
#include <string>
#include "mace/core/mace.h"
namespace {{tag}} {
{% for tensor in tensors %}
extern unsigned char {{ tensor.name }}[];
{% endfor %}
{% for i in range(net.op|length) %}
extern void CreateOperator{{i}}(mace::OperatorDef &op);
{% endfor %}
} // namespace {{ tag }}
namespace {
{% if net.arg|length != 0 %}
static void CreateNetArg(mace::NetDef &net_def) {
net_def.mutable_arg().reserve({{ net.arg|length }});
mace::Argument *arg = nullptr; mace::Argument *arg = nullptr;
{% for i in range(net.op|length) %} {% for arg in net.arg %}
{% for arg in net.op[i].arg %}
arg = ops[{{i}}].add_arg(); arg = net_def.add_arg();
arg->set_name({{ arg.name|tojson }}); arg->set_name({{ arg.name|tojson }});
{%- if arg.HasField('f') %} {%- if arg.HasField('f') %}
arg->set_f({{ arg.f }}); arg->set_f({{ arg.f }});
{%- endif %} {% endif %}
{%- if arg.HasField('i') %} {%- if arg.HasField('i') %}
arg->set_i({{ arg.i }}); arg->set_i({{ arg.i }});
{%- endif %} {% endif %}
{%- if arg.HasField('s') %} {%- if arg.HasField('s') %}
arg->set_s({{ arg.s|tojson }}); arg->set_s({{ arg.s|tojson }});
{%- endif %} {% endif %}
{% if arg.floats|length != 0 %} {% if arg.floats|length != 0 %}
arg->set_floats({ {{ arg.floats|join(', ') }} }); arg->set_floats({ {{ arg.floats|join(', ') }} });
...@@ -104,21 +135,18 @@ static void CreateOperators(std::vector<mace::OperatorDef> &ops) { ...@@ -104,21 +135,18 @@ static void CreateOperators(std::vector<mace::OperatorDef> &ops) {
{% if arg.strings|length != 0 %} {% if arg.strings|length != 0 %}
arg->set_strings({ {{ arg.strings|stringfy() }} }); arg->set_strings({ {{ arg.strings|stringfy() }} });
{% endif %} {% endif %}
{% endfor %} {% endfor %}
{% if net.op[i].HasField('mem_id') %} }
ops[{{i}}].set_mem_id({{net.op[i].mem_id}}); {% endif %}
{% endif %}
{% for shape in net.op[i].output_shape %}
ops[{{i}}].add_output_shape(mace::OutputShape({ {{ shape.dims|join(', ') }} }));
{% endfor %}
UpdateOp(ops[{{i}}], {{ net.op[i].name|tojson }}, {{ net.op[i].type|tojson}}, static void CreateOperators(std::vector<mace::OperatorDef> &ops) {
{ {{ net.op[i].input|stringfy }} }, ops.resize({{ net.op|length }});
{ {{ net.op[i].output|stringfy }} }, {% for i in range(net.op|length) %}
{ {{ net.op[i].output_type|join(', ') }} });
{{tag}}::CreateOperator{{i}}(ops[{{i}}]);
{% endfor %} {% endfor %}
} }
...@@ -130,10 +158,7 @@ static void CreateTensors(std::vector<mace::TensorProto> &tensors) { ...@@ -130,10 +158,7 @@ static void CreateTensors(std::vector<mace::TensorProto> &tensors) {
tensors.emplace_back(mace::TensorProto( tensors.emplace_back(mace::TensorProto(
{{ tensor.name|tojson }}, {{ tag + '::' + tensor.name }}, {{ tensor.name|tojson }}, {{ tag + '::' + tensor.name }},
{ {{ tensor.dims|join(', ') }} }, {{ tensor.data_type }}, { {{ tensor.dims|join(', ') }} }, {{ tensor.data_type }}, {{ tensor.node_id }}));
{{ tensor.node_id }}
));
{% endfor %} {% endfor %}
} }
......
...@@ -110,13 +110,28 @@ def convert_to_source(net_def, template, confuse, model_tag, output): ...@@ -110,13 +110,28 @@ def convert_to_source(net_def, template, confuse, model_tag, output):
f.write(source) f.write(source)
counter += 1 counter += 1
# generate op source files
counter = 0
op_size = len(net_def.op)
for start in range(0, op_size, 10):
source = j2_env.get_template(template_name).render(
start = start,
end = min(start+10, op_size),
net = net_def,
tag = model_tag,
mode = 1
)
with gfile.GFile(output_dir + 'op' + str(counter) + '.cc', "wb") as f:
f.write(source)
counter += 1
# generate model source files # generate model source files
tensors = [TensorInfo(t) for t in net_def.tensors] tensors = [TensorInfo(t) for t in net_def.tensors]
source = j2_env.get_template(template_name).render( source = j2_env.get_template(template_name).render(
tensors = tensors, tensors = tensors,
net = net_def, net = net_def,
tag = model_tag, tag = model_tag,
mode = 1 mode = 2
) )
with gfile.GFile(output, "wb") as f: with gfile.GFile(output, "wb") as f:
f.write(source) f.write(source)
...@@ -30,8 +30,8 @@ python tools/validate.py --generate_data true --random_seed 1 \ ...@@ -30,8 +30,8 @@ python tools/validate.py --generate_data true --random_seed 1 \
# Step 2: convert tf model to mace model # Step 2: convert tf model to mace model
echo "Step 2: convert tf model to mace model and optimize memory" echo "Step 2: convert tf model to mace model and optimize memory"
echo $MACE_SOURCE_DIR
bazel build //mace/python/tools:tf_converter bazel build //mace/python/tools:tf_converter
rm -rf ${MACE_SOURCE_DIR}/mace/examples/models/gcn
mkdir -p ${MACE_SOURCE_DIR}/mace/examples/models/gcn mkdir -p ${MACE_SOURCE_DIR}/mace/examples/models/gcn
bazel-bin/mace/python/tools/tf_converter --input=${TF_MODEL_FILE_PATH} \ bazel-bin/mace/python/tools/tf_converter --input=${TF_MODEL_FILE_PATH} \
--output=${MACE_SOURCE_DIR}/mace/examples/models/gcn/mace_gcn.cc \ --output=${MACE_SOURCE_DIR}/mace/examples/models/gcn/mace_gcn.cc \
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册