提交 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(
cc_binary(
name = "mace_run",
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"],
linkstatic = 1,
deps = [
......
......@@ -12,41 +12,47 @@ alignas(4) unsigned char {{ tensor.name }}[] = {
};
} // namespace {{tag}}
{% else %}
{% elif mode == 1 %}
#include <vector>
#include <string>
#include "mace/core/mace.h"
namespace {{tag}} {
{% for tensor in tensors %}
extern unsigned char {{ tensor.name }}[];
{% endfor %}
namespace {
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);
}
}
} // namespace {{ tag }}
namespace {{tag}}{
namespace {
{% for i in range(start, end) %}
{% if net.arg|length != 0 %}
static void CreateNetArg(mace::NetDef &net_def) {
net_def.mutable_arg().reserve({{ net.arg|length }});
void CreateOperator{{i}}(mace::OperatorDef &op) {
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 }});
{%- if arg.HasField('f') %}
arg->set_f({{ arg.f }});
{% endif %}
{%- endif %}
{%- if arg.HasField('i') %}
arg->set_i({{ arg.i }});
{% endif %}
{%- endif %}
{%- if arg.HasField('s') %}
arg->set_s({{ arg.s|tojson }});
{% endif %}
{%- endif %}
{% if arg.floats|length != 0 %}
arg->set_floats({ {{ arg.floats|join(', ') }} });
......@@ -57,43 +63,68 @@ static void CreateNetArg(mace::NetDef &net_def) {
{% if arg.strings|length != 0 %}
arg->set_strings({ {{ arg.strings|stringfy() }} });
{% 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 %}
}
{% endif %}
UpdateOp(op, {{ net.op[i].name|tojson }}, {{ net.op[i].type|tojson}},
{ {{ 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) {
ops.resize({{ net.op|length }});
{% endfor %}
} // 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;
{% for i in range(net.op|length) %}
{% for arg in net.op[i].arg %}
{% for arg in net.arg %}
arg = ops[{{i}}].add_arg();
arg = net_def.add_arg();
arg->set_name({{ arg.name|tojson }});
{%- if arg.HasField('f') %}
arg->set_f({{ arg.f }});
{%- endif %}
{% endif %}
{%- if arg.HasField('i') %}
arg->set_i({{ arg.i }});
{%- endif %}
{% endif %}
{%- if arg.HasField('s') %}
arg->set_s({{ arg.s|tojson }});
{%- endif %}
{% endif %}
{% if arg.floats|length != 0 %}
arg->set_floats({ {{ arg.floats|join(', ') }} });
......@@ -104,21 +135,18 @@ static void CreateOperators(std::vector<mace::OperatorDef> &ops) {
{% if arg.strings|length != 0 %}
arg->set_strings({ {{ arg.strings|stringfy() }} });
{% endif %}
{% 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}},
{ {{ net.op[i].input|stringfy }} },
{ {{ net.op[i].output|stringfy }} },
{ {{ net.op[i].output_type|join(', ') }} });
static void CreateOperators(std::vector<mace::OperatorDef> &ops) {
ops.resize({{ net.op|length }});
{% for i in range(net.op|length) %}
{{tag}}::CreateOperator{{i}}(ops[{{i}}]);
{% endfor %}
}
......@@ -130,10 +158,7 @@ static void CreateTensors(std::vector<mace::TensorProto> &tensors) {
tensors.emplace_back(mace::TensorProto(
{{ tensor.name|tojson }}, {{ tag + '::' + tensor.name }},
{ {{ tensor.dims|join(', ') }} }, {{ tensor.data_type }},
{{ tensor.node_id }}
));
{ {{ tensor.dims|join(', ') }} }, {{ tensor.data_type }}, {{ tensor.node_id }}));
{% endfor %}
}
......
......@@ -110,13 +110,28 @@ def convert_to_source(net_def, template, confuse, model_tag, output):
f.write(source)
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
tensors = [TensorInfo(t) for t in net_def.tensors]
source = j2_env.get_template(template_name).render(
tensors = tensors,
net = net_def,
tag = model_tag,
mode = 1
mode = 2
)
with gfile.GFile(output, "wb") as f:
f.write(source)
......@@ -30,8 +30,8 @@ python tools/validate.py --generate_data true --random_seed 1 \
# Step 2: convert tf model to mace model
echo "Step 2: convert tf model to mace model and optimize memory"
echo $MACE_SOURCE_DIR
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
bazel-bin/mace/python/tools/tf_converter --input=${TF_MODEL_FILE_PATH} \
--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.
先完成此消息的编辑!
想要评论请 注册