提交 a038d23f 编写于 作者: 叶剑武

Merge branch 'obfuscate' into 'master'

Modify codegen code

See merge request !202
......@@ -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<int64_t> *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;
......
......@@ -7,7 +7,8 @@
#include <vector>
#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%}
......@@ -20,6 +21,7 @@ void Create{{tensor.name}}(std::vector<mace::ConstTensor> &tensors) {
}
} // namespace {{tag}}
} // namespace mace
{% elif mode == 1 %}
#include <vector>
......@@ -43,7 +45,8 @@ void UpdateOp(mace::OperatorDef &op,
}
}
namespace {{tag}}{
namespace mace {
namespace {{tag}} {
{% for i in range(start, end) %}
......@@ -120,12 +123,14 @@ void CreateOperator{{i}}(mace::OperatorDef &op) {
{% endfor %}
} // namespace {{tag}}
} // namespace mace
{% else %}
#include <vector>
#include <string>
#include "mace/core/public/mace.h"
namespace mace {
namespace {{tag}} {
{% for tensor in tensors %}
......@@ -138,12 +143,13 @@ extern void CreateOperator{{i}}(mace::OperatorDef &op);
{% endfor %}
} // 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<std::vector<int>> dims { {{net.output_info | map(attribute='dims') | join(', ') | replace('[', '{') | replace(']', '}') }} };
std::vector<int> 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<mace::OperatorDef> &ops) {
void CreateOperators(std::vector<mace::OperatorDef> &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<mace::ConstTensor> &tensors) {
void CreateTensors(std::vector<mace::ConstTensor> &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<mace::MemoryBlock> &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 {{tag}}
} // namespace mace
{% endif %}
......@@ -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)
......
......@@ -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,
......
......@@ -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}
......
......@@ -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}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册