diff --git a/mace/examples/BUILD b/mace/examples/BUILD index 7ce5af055a470d8a42c0039341e1c8f86ac4b4a4..d8978654d8bc59e48f2f28c1668b5cf2bc65cabf 100644 --- a/mace/examples/BUILD +++ b/mace/examples/BUILD @@ -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 = [ diff --git a/mace/python/tools/model.template b/mace/python/tools/model.template index 7fa60ae6b55ac8185db40d7c88494f71c686a231..bd81d06a4c0868c2656b27868de3ff2626b0c677 100644 --- a/mace/python/tools/model.template +++ b/mace/python/tools/model.template @@ -12,41 +12,47 @@ alignas(4) unsigned char {{ tensor.name }}[] = { }; } // namespace {{tag}} -{% else %} + +{% elif mode == 1 %} #include #include #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 &inputs, + const std::vector &outputs, + const std::vector &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 &inputs, - const std::vector &outputs, - const std::vector &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 &ops) { - ops.resize({{ net.op|length }}); +{% endfor %} + +} // namespace {{tag}} + +{% else %} +#include +#include +#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 &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 &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 &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 %} } diff --git a/mace/python/tools/source_converter_lib.py b/mace/python/tools/source_converter_lib.py index cd6495fd18b4305730fd9e575fa59c8212b3cf98..a26d5c13cb7c585a7b99740d45b73604036763e7 100644 --- a/mace/python/tools/source_converter_lib.py +++ b/mace/python/tools/source_converter_lib.py @@ -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) diff --git a/tools/validate_gcn.sh b/tools/validate_gcn.sh index 04e0f0a5e4d9be430c64ecda1571f80bcc2fadd0..377749ff55b4631f05817cef27f4c9343bf5f0e0 100644 --- a/tools/validate_gcn.sh +++ b/tools/validate_gcn.sh @@ -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 \