diff --git a/mace/examples/mace_run.cc b/mace/examples/mace_run.cc index 35c41dfdb1e5c1a3ec06b4bfdb3ac20faba95a99..825a19645dc88f743f8b3365d57be958b9496de7 100644 --- a/mace/examples/mace_run.cc +++ b/mace/examples/mace_run.cc @@ -28,8 +28,13 @@ using namespace std; using namespace mace; namespace mace { -extern NetDef MACE_MODEL_FUNCTION(); +namespace MACE_MODEL_TAG { + +extern NetDef CreateNet(); + } +} + void ParseShape(const string &str, vector *shape) { string tmp = str; while (!tmp.empty()) { @@ -149,7 +154,7 @@ int main(int argc, char **argv) { // load model int64_t t0 = utils::NowMicros(); - NetDef net_def = mace::MACE_MODEL_FUNCTION(); + NetDef net_def = mace::MACE_MODEL_TAG::CreateNet(); int64_t t1 = utils::NowMicros(); LOG(INFO) << "CreateNetDef duration: " << t1 - t0 << " us"; int64_t init_micros = t1 - t0; diff --git a/mace/python/tools/model.template b/mace/python/tools/model.template index b4db900d925d997f0fa3f060daa66e9627ec7f8d..068328775b3a16057f6ee8f5f7750cf6ede59a8f 100644 --- a/mace/python/tools/model.template +++ b/mace/python/tools/model.template @@ -7,7 +7,8 @@ #include #include "mace/core/public/mace.h" -namespace {{tag}}{ +namespace mace { +namespace {{tag}} { {% if tensor_info.data_type != 'DT_UINT8' %} alignas(4) {% endif %} unsigned char {{ tensor_info.name }}[] = { {% for d in tensor_info.data %}{{"0x%02X, " % d }}{%endfor%} @@ -19,7 +20,8 @@ void Create{{tensor.name}}(std::vector &tensors) { { {{ tensor.dims|join(', ') }} }, {{ tensor.data_type }}, {{ tensor.node_id }})); } -} // namespace {{tag}} +} // namespace {{tag}} +} // namespace mace {% elif mode == 1 %} #include @@ -43,7 +45,8 @@ void UpdateOp(mace::OperatorDef &op, } } -namespace {{tag}}{ +namespace mace { +namespace {{tag}} { {% for i in range(start, end) %} @@ -119,13 +122,15 @@ void CreateOperator{{i}}(mace::OperatorDef &op) { {% endfor %} -} // namespace {{tag}} +} // namespace {{tag}} +} // namespace mace {% else %} #include #include #include "mace/core/public/mace.h" +namespace mace { namespace {{tag}} { {% for tensor in tensors %} @@ -137,13 +142,14 @@ extern void Create{{ tensor.name }}(std::vector &tensors); extern void CreateOperator{{i}}(mace::OperatorDef &op); {% endfor %} -} // namespace {{ tag }} +} // namespace {{ tag }} +} // namespace mace namespace { {% if net.arg|length != 0 %} -static void CreateNetArg(mace::NetDef &net_def) { +void CreateNetArg(mace::NetDef &net_def) { net_def.mutable_arg().reserve({{ net.arg|length }}); mace::Argument *arg = nullptr; {% for arg in net.arg %} @@ -178,7 +184,7 @@ static void CreateNetArg(mace::NetDef &net_def) { {% endif %} {% if net.output_info | length > 0 %} -static void CreateOutputInfo(mace::NetDef &net_def) { +void CreateOutputInfo(mace::NetDef &net_def) { std::vector> dims { {{net.output_info | map(attribute='dims') | join(', ') | replace('[', '{') | replace(']', '}') }} }; std::vector data_types_int { {{ net.output_info | map(attribute='data_type') | join(', ') }} }; @@ -194,28 +200,28 @@ static void CreateOutputInfo(mace::NetDef &net_def) { } {% endif %} -static void CreateOperators(std::vector &ops) { +void CreateOperators(std::vector &ops) { ops.resize({{ net.op|length }}); {% for i in range(net.op|length) %} - {{tag}}::CreateOperator{{i}}(ops[{{i}}]); + mace::{{tag}}::CreateOperator{{i}}(ops[{{i}}]); {% endfor %} } -static void CreateTensors(std::vector &tensors) { +void CreateTensors(std::vector &tensors) { tensors.reserve({{ net.tensors|length }}); {% for tensor in net.tensors %} - {{tag}}::Create{{tensor.name}}(tensors); + mace::{{tag}}::Create{{tensor.name}}(tensors); {% endfor %} } {% if net.mem_arena.mem_block|length != 0 %} -static void CreateMemoryArena(mace::MemoryArena &mem_arena) { +void CreateMemoryArena(mace::MemoryArena &mem_arena) { std::vector &mem_block = mem_arena.mutable_mem_block(); mem_block.reserve({{ net.mem_arena.mem_block|length }}); @@ -231,8 +237,9 @@ static void CreateMemoryArena(mace::MemoryArena &mem_arena) { } namespace mace { +namespace {{tag}} { -NetDef {{'Create' + tag}}() { +NetDef CreateNet() { NetDef net_def; net_def.set_name("{{ net.name}}"); net_def.set_version("{{ net.version }}"); @@ -256,5 +263,6 @@ NetDef {{'Create' + tag}}() { return net_def; } -} // namespace mace +} // namespace {{tag}} +} // namespace mace {% endif %} diff --git a/mace/python/tools/source_converter_lib.py b/mace/python/tools/source_converter_lib.py index ce343f3a3cc3f34bccef611b1a72a68042dd45b3..c6be3a3e8ce6cabd8550d9e2e4095224be54c6bd 100644 --- a/mace/python/tools/source_converter_lib.py +++ b/mace/python/tools/source_converter_lib.py @@ -42,7 +42,7 @@ def generate_in_out_map(ops, tensor_map): in_out_map[output_name] = generate_random_name() return in_out_map -def confuse_name(net_def): +def obfuscate_name(net_def): input_node = "mace_input_node" output_node = "mace_output_node" tensor_map = generate_tensor_map(net_def.tensors) @@ -86,9 +86,9 @@ class TensorInfo: def stringfy(value): return ', '.join('"{0}"'.format(w) for w in value) -def convert_to_source(net_def, template, confuse, model_tag, output, runtime): - if confuse: - confuse_name(net_def) +def convert_to_source(net_def, template, obfuscate, model_tag, output, runtime): + if obfuscate: + obfuscate_name(net_def) else: rename_tensor(net_def) diff --git a/mace/python/tools/tf_converter.py b/mace/python/tools/tf_converter.py index 99383535f20ea2bcfb70bf4444599acc039b5e2c..303fd143ba90d48557925939fe5dffe48d502c90 100644 --- a/mace/python/tools/tf_converter.py +++ b/mace/python/tools/tf_converter.py @@ -29,7 +29,7 @@ def main(unused_args): input_graph_def, FLAGS.input_node, FLAGS.output_node, FLAGS.data_type, FLAGS.runtime) if FLAGS.output_type == 'source': - source_converter_lib.convert_to_source(output_graph_def, FLAGS.template, FLAGS.confuse, + source_converter_lib.convert_to_source(output_graph_def, FLAGS.template, FLAGS.obfuscate, FLAGS.model_tag, FLAGS.output, FLAGS.runtime) else: with gfile.GFile(FLAGS.output, "wb") as f: @@ -97,12 +97,12 @@ def parse_args(): default="", help="template path") parser.add_argument( - "--confuse", + "--obfuscate", type=str2bool, nargs='?', const=False, default=False, - help="confuse model names") + help="obfuscate model names") parser.add_argument( "--model_tag", type=str, diff --git a/tools/validate_gcn.sh b/tools/validate_gcn.sh index 8d01b110122366a7839aa4fb49c7685784369667..88ed9d7b76b0a8891e8753ab8167d34a9acfef2c 100755 --- a/tools/validate_gcn.sh +++ b/tools/validate_gcn.sh @@ -2,34 +2,34 @@ # Must run at root dir of mace project. set +x Usage() { - echo 'Usage: bash tools/validate_gcn.sh tools/gcn.config tf_model_path image_size [tuning]' + echo 'Usage: bash tools/validate_gcn.sh tools/gcn.config tf_model_path model_tag image_size [tuning]' } -if [ $# -lt 2 ];then +if [ $# -lt 4 ];then Usage exit -1 fi source $1 -VLOG_LEVEL=0 TF_MODEL_FILE_PATH=$2 +MODEL_TAG=$3 +IMAGE_SIZE=$4 +TUNING_OR_NOT=${5:-0} + +VLOG_LEVEL=0 MODEL_DIR=$(dirname ${TF_MODEL_FILE_PATH}) MACE_SOURCE_DIR=`/bin/pwd` -MACE_MODEL_NAME='mace_model.pb' INPUT_FILE_NAME='model_input' OUTPUT_FILE_NAME='gcn.out' OUTPUT_LIST_FILE='gcn.list' -PHONE_DATA_DIR="/data/local/tmp/${MACE_MODEL_NAME}" +PHONE_DATA_DIR="/data/local/tmp/${MODEL_TAG}" KERNEL_DIR="${PHONE_DATA_DIR}/cl/" -IMAGE_SIZE=$3 -MODEL_TAG=GCN${IMAGE_SIZE} CODEGEN_DIR=${MACE_SOURCE_DIR}/mace/codegen -MODEL_CODEGEN_DIR=${CODEGEN_DIR}/models/gcn-$IMAGE_SIZE +MODEL_CODEGEN_DIR=${CODEGEN_DIR}/models/${MODEL_TAG} CL_CODEGEN_DIR=${CODEGEN_DIR}/opencl CL_BIN_DIR=${CODEGEN_DIR}/opencl_bin TUNING_CODEGEN_DIR=${CODEGEN_DIR}/tuning -TUNING_OR_NOT=${4:-0} VERSION_SOURCE_PATH=${CODEGEN_DIR}/version build_and_run() @@ -46,7 +46,7 @@ build_and_run() --copt="-std=c++11" \ --copt="-D_GLIBCXX_USE_C99_MATH_TR1" \ --copt="-Werror=return-type" \ - --copt="-DMACE_MODEL_FUNCTION=Create${MODEL_TAG}" \ + --copt="-DMACE_MODEL_TAG=${MODEL_TAG}" \ $PRODUCTION_MODE_BUILD_FLAGS || exit -1 adb shell "mkdir -p ${PHONE_DATA_DIR}" || exit -1 @@ -88,7 +88,7 @@ bazel build //mace/python/tools:tf_converter || exit -1 rm -rf ${MODEL_CODEGEN_DIR} mkdir -p ${MODEL_CODEGEN_DIR} bazel-bin/mace/python/tools/tf_converter --input=${TF_MODEL_FILE_PATH} \ - --output=${MODEL_CODEGEN_DIR}/mace_gcn${IMAGE_SIZE}.cc \ + --output=${MODEL_CODEGEN_DIR}/model.cc \ --input_node=${TF_INPUT_NODE} \ --output_node=${TF_OUTPUT_NODE} \ --data_type=DT_HALF \ @@ -96,7 +96,7 @@ bazel-bin/mace/python/tools/tf_converter --input=${TF_MODEL_FILE_PATH} \ --output_type=source \ --template=${MACE_SOURCE_DIR}/mace/python/tools/model.template \ --model_tag=${MODEL_TAG} \ - --confuse=True || exit -1 + --obfuscate=True || exit -1 echo "Step 3: Generate version source" rm -rf ${VERSION_SOURCE_PATH} diff --git a/tools/validate_gcn_dsp.sh b/tools/validate_gcn_dsp.sh index 06d6ac717fd45169151c4cb473f0566e458e2dc3..d155136b923b1075bdf21e2bcdd55dd1dba668d2 100755 --- a/tools/validate_gcn_dsp.sh +++ b/tools/validate_gcn_dsp.sh @@ -2,30 +2,30 @@ # Must run at root dir of mace project. set +x Usage() { - echo 'Usage: bash tools/validate_gcn.sh tools/gcn.config tf_model_path image_size [tuning]' + echo 'Usage: bash tools/validate_gcn.sh tools/gcn.config tf_model_path model_tag image_size [tuning]' } -if [ $# -lt 2 ];then +if [ $# -lt 4 ];then Usage exit -1 fi source $1 -VLOG_LEVEL=0 TF_MODEL_FILE_PATH=$2 +MODEL_TAG=$3 +IMAGE_SIZE=$4 + +VLOG_LEVEL=0 MODEL_DIR=$(dirname ${TF_MODEL_FILE_PATH}) MACE_SOURCE_DIR=`/bin/pwd` -MACE_MODEL_NAME='mace_model.pb' INPUT_FILE_NAME='model_input' OUTPUT_FILE_NAME='gcn.out' OUTPUT_LIST_FILE='gcn.list' -PHONE_DATA_DIR="/data/local/tmp/${MACE_MODEL_NAME}" +PHONE_DATA_DIR="/data/local/tmp/${MODEL_TAG}" KERNEL_DIR="${PHONE_DATA_DIR}/cl/" -IMAGE_SIZE=$3 -MODEL_TAG=GCN${IMAGE_SIZE} CODEGEN_DIR=${MACE_SOURCE_DIR}/mace/codegen -MODEL_CODEGEN_DIR=${CODEGEN_DIR}/models/gcn-$IMAGE_SIZE +MODEL_CODEGEN_DIR=${CODEGEN_DIR}/models/${MODEL_TAG} VERSION_SOURCE_PATH=${CODEGEN_DIR}/version build_and_run() @@ -73,7 +73,7 @@ bazel-bin/mace/python/tools/tf_converter --input=${TF_MODEL_FILE_PATH} \ --output_type=source \ --template=${MACE_SOURCE_DIR}/mace/python/tools/model.template \ --model_tag=${MODEL_TAG} \ - --confuse=True + --obfuscate=True echo "Step 3: Generate version source" rm -rf ${VERSION_SOURCE_PATH}