提交 f2fc2197 编写于 作者: 刘琦

Merge branch 'update_mace_tools_to_python' into 'master'

update mace_tools to python sh

See merge request !400
...@@ -21,9 +21,9 @@ ...@@ -21,9 +21,9 @@
#include <unistd.h> #include <unistd.h>
#include <algorithm> #include <algorithm>
#include <cstring>
#include <memory> #include <memory>
#include <utility> #include <utility>
#include <cstring>
#include "mace/utils/logging.h" #include "mace/utils/logging.h"
......
# Copyright 2018 Xiaomi, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse import argparse
import os import os
import sys import sys
...@@ -15,10 +29,10 @@ import numpy as np ...@@ -15,10 +29,10 @@ import numpy as np
FLAGS = None FLAGS = None
def generate_cpp_source(): def generate_cpp_source(binary_dirs, binary_file_name, variable_name):
data_map = {} data_map = {}
for binary_dir in FLAGS.binary_dirs.split(","): for binary_dir in binary_dirs.split(","):
binary_path = os.path.join(binary_dir, FLAGS.binary_file_name) binary_path = os.path.join(binary_dir, binary_file_name)
if not os.path.exists(binary_path): if not os.path.exists(binary_path):
continue continue
...@@ -49,16 +63,19 @@ def generate_cpp_source(): ...@@ -49,16 +63,19 @@ def generate_cpp_source():
return env.get_template('str2vec_maps.cc.jinja2').render( return env.get_template('str2vec_maps.cc.jinja2').render(
maps=data_map, maps=data_map,
data_type='unsigned int', data_type='unsigned int',
variable_name=FLAGS.variable_name) variable_name=variable_name)
def main(unused_args): def tuning_param_codegen(binary_dirs,
cpp_binary_source = generate_cpp_source() binary_file_name,
if os.path.isfile(FLAGS.output_path): output_path,
os.remove(FLAGS.output_path) variable_name):
w_file = open(FLAGS.output_path, "w") cpp_binary_source = generate_cpp_source(
w_file.write(cpp_binary_source) binary_dirs, binary_file_name, variable_name)
w_file.close() if os.path.isfile(output_path):
os.remove(output_path)
with open(output_path, "w") as w_file:
w_file.write(cpp_binary_source)
def parse_args(): def parse_args():
...@@ -87,4 +104,7 @@ def parse_args(): ...@@ -87,4 +104,7 @@ def parse_args():
if __name__ == '__main__': if __name__ == '__main__':
FLAGS, unparsed = parse_args() FLAGS, unparsed = parse_args()
main(unused_args=[sys.argv[0]] + unparsed) tuning_param_codegen(FLAGS.binary_dirs,
FLAGS.binary_file_name,
FLAGS.output_path,
FLAGS.variable_name)
# Copyright 2018 Xiaomi, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from mace.proto import mace_pb2 from mace.proto import mace_pb2
from mace.proto import caffe_pb2 from mace.proto import caffe_pb2
from mace.python.tools import memory_optimizer from mace.python.tools import memory_optimizer
......
# Copyright 2018 Xiaomi, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import tensorflow as tf import tensorflow as tf
from mace.proto import mace_pb2 from mace.proto import mace_pb2
......
# Copyright 2018 Xiaomi, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse import argparse
import sys import sys
import hashlib import hashlib
......
# Copyright 2018 Xiaomi, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
class DspOps(object): class DspOps(object):
def __init__(self): def __init__(self):
self.dsp_ops = { self.dsp_ops = {
......
# Copyright 2018 Xiaomi, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse import argparse
import os import os
import sys import sys
...@@ -22,30 +36,30 @@ def encrypt_code(code_str): ...@@ -22,30 +36,30 @@ def encrypt_code(code_str):
return encrypted_arr return encrypted_arr
def main(unused_args): def encrypt_opencl_codegen(cl_kernel_dir, output_path):
if not os.path.exists(FLAGS.cl_kernel_dir): if not os.path.exists(cl_kernel_dir):
print("Input cl_kernel_dir " + FLAGS.cl_kernel_dir + " doesn't exist!") print("Input cl_kernel_dir " + cl_kernel_dir + " doesn't exist!")
header_code = "" header_code = ""
for file_name in os.listdir(FLAGS.cl_kernel_dir): for file_name in os.listdir(cl_kernel_dir):
file_path = os.path.join(FLAGS.cl_kernel_dir, file_name) file_path = os.path.join(cl_kernel_dir, file_name)
if file_path[-2:] == ".h": if file_path[-2:] == ".h":
f = open(file_path, "r") with open(file_path, "r") as f:
header_code += f.read() header_code += f.read()
encrypted_code_maps = {} encrypted_code_maps = {}
for file_name in os.listdir(FLAGS.cl_kernel_dir): for file_name in os.listdir(cl_kernel_dir):
file_path = os.path.join(FLAGS.cl_kernel_dir, file_name) file_path = os.path.join(cl_kernel_dir, file_name)
if file_path[-3:] == ".cl": if file_path[-3:] == ".cl":
f = open(file_path, "r") with open(file_path, "r") as f:
code_str = "" code_str = ""
for line in f.readlines(): for line in f.readlines():
if "#include <common.h>" in line: if "#include <common.h>" in line:
code_str += header_code code_str += header_code
else: else:
code_str += line code_str += line
encrypted_code_arr = encrypt_code(code_str) encrypted_code_arr = encrypt_code(code_str)
encrypted_code_maps[file_name[:-3]] = encrypted_code_arr encrypted_code_maps[file_name[:-3]] = encrypted_code_arr
env = jinja2.Environment(loader=jinja2.FileSystemLoader(sys.path[0])) env = jinja2.Environment(loader=jinja2.FileSystemLoader(sys.path[0]))
cpp_cl_encrypted_kernel = env.get_template( cpp_cl_encrypted_kernel = env.get_template(
...@@ -54,11 +68,10 @@ def main(unused_args): ...@@ -54,11 +68,10 @@ def main(unused_args):
data_type='unsigned char', data_type='unsigned char',
variable_name='kEncryptedProgramMap') variable_name='kEncryptedProgramMap')
if os.path.isfile(FLAGS.output_path): if os.path.isfile(output_path):
os.remove(FLAGS.output_path) os.remove(output_path)
w_file = open(FLAGS.output_path, "w") with open(output_path, "w") as w_file:
w_file.write(cpp_cl_encrypted_kernel) w_file.write(cpp_cl_encrypted_kernel)
w_file.close()
print("Generate encrypted opencl source done!") print("Generate encrypted opencl source done!")
...@@ -81,4 +94,4 @@ def parse_args(): ...@@ -81,4 +94,4 @@ def parse_args():
if __name__ == '__main__': if __name__ == '__main__':
FLAGS, unparsed = parse_args() FLAGS, unparsed = parse_args()
main(unused_args=[sys.argv[0]] + unparsed) encrypt_opencl_codegen(FLAGS.cl_kernel_dir, FLAGS.output_path)
# Copyright 2018 Xiaomi, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import tensorflow as tf import tensorflow as tf
from mace.proto import mace_pb2 from mace.proto import mace_pb2
from collections import OrderedDict from collections import OrderedDict
......
# Copyright 2018 Xiaomi, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys import sys
import operator import operator
from mace.proto import mace_pb2 from mace.proto import mace_pb2
......
# Copyright 2018 Xiaomi, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse import argparse
import os import os
import sys import sys
...@@ -13,12 +27,14 @@ import jinja2 ...@@ -13,12 +27,14 @@ import jinja2
FLAGS = None FLAGS = None
def generate_cpp_source(): def generate_cpp_source(cl_binary_dirs,
built_kernel_file_name,
platform_info_file_name):
maps = {} maps = {}
platform_info = '' platform_info = ''
binary_dirs = FLAGS.cl_binary_dirs.strip().split(",") binary_dirs = cl_binary_dirs.strip().split(",")
for binary_dir in binary_dirs: for binary_dir in binary_dirs:
binary_path = os.path.join(binary_dir, FLAGS.built_kernel_file_name) binary_path = os.path.join(binary_dir, built_kernel_file_name)
if not os.path.exists(binary_path): if not os.path.exists(binary_path):
continue continue
...@@ -45,7 +61,7 @@ def generate_cpp_source(): ...@@ -45,7 +61,7 @@ def generate_cpp_source():
maps[key].append(hex(ele)) maps[key].append(hex(ele))
cl_platform_info_path = os.path.join(binary_dir, cl_platform_info_path = os.path.join(binary_dir,
FLAGS.platform_info_file_name) platform_info_file_name)
with open(cl_platform_info_path, 'r') as f: with open(cl_platform_info_path, 'r') as f:
curr_platform_info = f.read() curr_platform_info = f.read()
if platform_info != "": if platform_info != "":
...@@ -61,14 +77,17 @@ def generate_cpp_source(): ...@@ -61,14 +77,17 @@ def generate_cpp_source():
) )
def main(unused_args): def opencl_codegen(output_path,
cl_binary_dirs="",
cpp_cl_binary_source = generate_cpp_source() built_kernel_file_name="",
if os.path.isfile(FLAGS.output_path): platform_info_file_name=""):
os.remove(FLAGS.output_path) cpp_cl_binary_source = generate_cpp_source(cl_binary_dirs,
w_file = open(FLAGS.output_path, "w") built_kernel_file_name,
w_file.write(cpp_cl_binary_source) platform_info_file_name)
w_file.close() if os.path.isfile(output_path):
os.remove(output_path)
with open(output_path, "w") as w_file:
w_file.write(cpp_cl_binary_source)
def parse_args(): def parse_args():
...@@ -99,4 +118,7 @@ def parse_args(): ...@@ -99,4 +118,7 @@ def parse_args():
if __name__ == '__main__': if __name__ == '__main__':
FLAGS, unparsed = parse_args() FLAGS, unparsed = parse_args()
main(unused_args=[sys.argv[0]] + unparsed) opencl_codegen(FLAGS.output_path,
FLAGS.cl_binary_dirs,
FLAGS.built_kernel_file_name,
FLAGS.platform_info_file_name)
# Copyright 2018 Xiaomi, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os import os
import uuid import uuid
import numpy as np import numpy as np
...@@ -159,9 +173,8 @@ def convert_to_source(net_def, mode_pb_checksum, template_dir, obfuscate, ...@@ -159,9 +173,8 @@ def convert_to_source(net_def, mode_pb_checksum, template_dir, obfuscate,
with open(output_dir + 'tensor_data' + '.cc', "wb") as f: with open(output_dir + 'tensor_data' + '.cc', "wb") as f:
f.write(source) f.write(source)
if not embed_model_data: if not embed_model_data:
f = open(output_dir + model_tag + '.data', "wb") with open(output_dir + model_tag + '.data', "wb") as f:
f.write(bytearray(model_data)) f.write(bytearray(model_data))
f.close()
# generate op source files # generate op source files
template_name = 'operator.jinja2' template_name = 'operator.jinja2'
......
# Copyright 2018 Xiaomi, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from mace.proto import mace_pb2 from mace.proto import mace_pb2
import tensorflow as tf import tensorflow as tf
import numpy as np import numpy as np
......
# Copyright 2018 Xiaomi, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from mace.proto import mace_pb2 from mace.proto import mace_pb2
import tensorflow as tf import tensorflow as tf
from tensorflow import gfile from tensorflow import gfile
......
# Copyright 2018 Xiaomi, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import operator import operator
import functools import functools
import argparse import argparse
......
#!/usr/bin/env python # Copyright 2018 Xiaomi, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Must run at root dir of libmace project. # Must run at root dir of libmace project.
# python tools/bazel_adb_run.py \ # python tools/bazel_adb_run.py \
...@@ -103,7 +115,8 @@ def main(unused_args): ...@@ -103,7 +115,8 @@ def main(unused_args):
sh_commands.gen_mace_version() sh_commands.gen_mace_version()
for target_abi in target_abis: for target_abi in target_abis:
sh_commands.bazel_build(target, abi=target_abi) sh_commands.bazel_build(target, abi=target_abi,
disable_no_tuning_warning=True)
if FLAGS.run_target: if FLAGS.run_target:
for serialno in target_devices: for serialno in target_devices:
if target_abi not in set( if target_abi not in set(
......
#!/bin/bash
Usage() {
echo "Usage: bash tools/benchmark.sh target_soc model_output_dir option_args"
}
if [ $# -lt 1 ]; then
Usage
exit 1
fi
CURRENT_DIR=`dirname $0`
source ${CURRENT_DIR}/env.sh
TARGET_SOC=$1
MODEL_OUTPUT_DIR=$2
OPTION_ARGS=$3
echo $OPTION_ARGS
RESULT_VALUE=`echo_device_id_by_soc $TARGET_SOC`
if [ $? -ne 0 ]; then
echo $RESULT_VALUE
exit 1
else
DEVICE_ID=$RESULT_VALUE
fi
if [ -f "$MODEL_OUTPUT_DIR/benchmark_model" ]; then
rm -rf $MODEL_OUTPUT_DIR/benchmark_model
fi
if [ "$EMBED_MODEL_DATA" = 0 ]; then
cp codegen/models/${MODEL_TAG}/${MODEL_TAG}.data $MODEL_OUTPUT_DIR
fi
if [ x"$TARGET_ABI" == x"host" ]; then
bazel build --verbose_failures -c opt --strip always \
//mace/benchmark:benchmark_model \
--copt="-std=c++11" \
--copt="-D_GLIBCXX_USE_C99_MATH_TR1" \
--copt="-Werror=return-type" \
--copt="-DMACE_MODEL_TAG=${MODEL_TAG}" \
--copt="-O3" \
--define openmp=true \
--define production=true || exit 1
cp bazel-bin/benchmark/benchmark_model $MODEL_OUTPUT_DIR
MACE_CPP_MIN_VLOG_LEVEL=$VLOG_LEVEL \
${MODEL_OUTPUT_DIR}/benchmark_model \
--model_data_file=${PHONE_DATA_DIR}/${MODEL_TAG}.data \
--device=${DEVICE_TYPE} \
--input_node="${INPUT_NODES}" \
--input_shape="${INPUT_SHAPES}"\
--output_node="${OUTPUT_NODES}" \
--output_shape="${OUTPUT_SHAPES}"\
--input_file=${MODEL_OUTPUT_DIR}/${INPUT_FILE_NAME}_${INPUT_NODES} \
$OPTION_ARGS || exit 1
else
bazel build --verbose_failures -c opt --strip always \
//mace/benchmark:benchmark_model \
--crosstool_top=//external:android/crosstool \
--host_crosstool_top=@bazel_tools//tools/cpp:toolchain \
--cpu=${TARGET_ABI} \
--copt="-std=c++11" \
--copt="-D_GLIBCXX_USE_C99_MATH_TR1" \
--copt="-Werror=return-type" \
--copt="-DMACE_OBFUSCATE_LITERALS" \
--copt="-DMACE_MODEL_TAG=${MODEL_TAG}" \
--define openmp=true \
--define neon=true \
--copt="-O3" \
--define production=true || exit 1
cp bazel-bin/mace/benchmark/benchmark_model $MODEL_OUTPUT_DIR
adb -s $DEVICE_ID shell "mkdir -p ${PHONE_DATA_DIR}" || exit 1
IFS=',' read -r -a INPUT_NAMES <<< "${INPUT_NODES}"
for NAME in "${INPUT_NAMES[@]}";do
FORMATTED_NAME=$(sed s/[^[:alnum:]]/_/g <<< ${NAME})
adb -s $DEVICE_ID push ${MODEL_OUTPUT_DIR}/${INPUT_FILE_NAME}_${FORMATTED_NAME} \
${PHONE_DATA_DIR} > /dev/null || exit 1
done
adb -s $DEVICE_ID push ${MODEL_OUTPUT_DIR}/benchmark_model \
${PHONE_DATA_DIR} > /dev/null || exit 1
if [ "$EMBED_MODEL_DATA" = 0 ]; then
adb -s $DEVICE_ID push ${MODEL_OUTPUT_DIR}/${MODEL_TAG}.data \
${PHONE_DATA_DIR} > /dev/null || exit 1
fi
adb -s $DEVICE_ID </dev/null shell \
LD_LIBRARY_PATH=${PHONE_DATA_DIR} \
MACE_CPP_MIN_VLOG_LEVEL=$VLOG_LEVEL \
MACE_RUN_PARAMETER_PATH=${PHONE_DATA_DIR}/mace_run.config \
MACE_LIMIT_OPENCL_KERNEL_TIME=${LIMIT_OPENCL_KERNEL_TIME} \
MACE_OPENCL_PROFILING=1 \
${PHONE_DATA_DIR}/benchmark_model \
--model_data_file=${PHONE_DATA_DIR}/${MODEL_TAG}.data \
--device=${DEVICE_TYPE} \
--input_node="${INPUT_NODES}" \
--input_shape="${INPUT_SHAPES}"\
--output_node="${OUTPUT_NODES}" \
--output_shape="${OUTPUT_SHAPES}"\
--input_file=${PHONE_DATA_DIR}/${INPUT_FILE_NAME} \
$OPTION_ARGS || exit 1
fi
#!/bin/bash
Usage() {
echo "Usage: bash tools/build_mace_run.sh production_mode model_output_dir hexagon_mode"
}
if [ $# -lt 3 ]; then
Usage
exit 1
fi
CURRENT_DIR=`dirname $0`
source ${CURRENT_DIR}/env.sh
PRODUCTION_MODE=$1
MODEL_OUTPUT_DIR=$2
HEXAGON_MODE=$3
if [ "$PRODUCTION_MODE" = 1 ]; then
PRODUCTION_MODE_BUILD_FLAGS="--define production=true"
fi
if [ x"$TARGET_ABI" = x"host" ]; then
bazel build --verbose_failures -c opt --strip always //mace/codegen:generated_models \
--copt="-std=c++11" \
--copt="-D_GLIBCXX_USE_C99_MATH_TR1" \
--copt="-Werror=return-type" \
--copt="-DMACE_MODEL_TAG=${MODEL_TAG}" \
--define openmp=true \
--copt="-O3" \
$PRODUCTION_MODE_BUILD_FLAGS || exit 1
bazel build --verbose_failures -c opt --strip always //mace/tools/validation:mace_run \
--copt="-std=c++11" \
--copt="-D_GLIBCXX_USE_C99_MATH_TR1" \
--copt="-Werror=return-type" \
--copt="-DMACE_MODEL_TAG=${MODEL_TAG}" \
--define openmp=true \
--copt="-O3" \
$PRODUCTION_MODE_BUILD_FLAGS || exit 1
else
if [ "$HEXAGON_MODE" = 1 ]; then
HEXAGON_MODE_BUILD_FLAG="--define hexagon=true"
fi
if [ x"$TARGET_ABI" = x"arm64-v8a" ]; then
NEON_ENABLE_FLAG="--define neon=true"
fi
bazel build --verbose_failures -c opt --strip always //mace/tools/validation:mace_run \
--crosstool_top=//external:android/crosstool \
--host_crosstool_top=@bazel_tools//tools/cpp:toolchain \
--cpu=${TARGET_ABI} \
--copt="-std=c++11" \
--copt="-D_GLIBCXX_USE_C99_MATH_TR1" \
--copt="-Werror=return-type" \
--copt="-DMACE_OBFUSCATE_LITERALS" \
--copt="-DMACE_MODEL_TAG=${MODEL_TAG}" \
--define openmp=true \
--define neon=true \
--copt="-O3" \
$NEON_ENABLE_FLAG \
$PRODUCTION_MODE_BUILD_FLAGS \
$HEXAGON_MODE_BUILD_FLAG || exit 1
fi
rm -rf $MODEL_OUTPUT_DIR/libmace_${MODEL_TAG}.a
cp $GENERATED_MODEL_LIB_PATH $MODEL_OUTPUT_DIR/libmace_${MODEL_TAG}.a
if [ -f "$MODEL_OUTPUT_DIR/mace_run" ]; then
rm -rf $MODEL_OUTPUT_DIR/mace_run
fi
cp bazel-bin/mace/tools/validation/mace_run $MODEL_OUTPUT_DIR
if [ "$EMBED_MODEL_DATA" = 0 ]; then
cp mace/codegen/models/${MODEL_TAG}/${MODEL_TAG}.data $MODEL_OUTPUT_DIR
fi
# copy model header file to build output dir
cp mace/codegen/models/${MODEL_TAG}/${MODEL_TAG}.h $MODEL_OUTPUT_DIR
#!/bin/bash
Usage() {
echo "Usage: bash tools/build_production_code.sh"
}
CURRENT_DIR=`dirname $0`
source ${CURRENT_DIR}/env.sh
build_host_target()
{
BAZEL_TARGET=$1
bazel build --verbose_failures -c opt --strip always $BAZEL_TARGET \
--copt="-std=c++11" \
--copt="-D_GLIBCXX_USE_C99_MATH_TR1" \
--copt="-Werror=return-type" \
--copt="-DMACE_OBFUSCATE_LITERALS" \
--copt="-O3" \
--define openmp=true || exit -1
}
build_target()
{
BAZEL_TARGET=$1
bazel build --verbose_failures -c opt --strip always $BAZEL_TARGET \
--crosstool_top=//external:android/crosstool \
--host_crosstool_top=@bazel_tools//tools/cpp:toolchain \
--cpu=$TARGET_ABI \
--copt="-std=c++11" \
--copt="-D_GLIBCXX_USE_C99_MATH_TR1" \
--copt="-Werror=return-type" \
--copt="-O3" \
--define openmp=true \
--copt="-DMACE_OBFUSCATE_LITERALS" || exit 1
}
if [ x"$TARGET_ABI" = x"host" ]; then
build_host_target //mace/codegen:generated_opencl
build_host_target //mace/codegen:generated_tuning_params
else
build_target //mace/codegen:generated_opencl
build_target //mace/codegen:generated_tuning_params
fi
#!/bin/bash
Usage() {
echo "Usage: bash tools/build_run_throughput_test.sh target_soc run_seconds merged_lib_file model_input_dir"
}
if [ $# -lt 4 ]; then
Usage
exit 1
fi
CURRENT_DIR=`dirname $0`
source ${CURRENT_DIR}/env.sh
TARGET_SOC=$1
RUN_SECONDS=$2
MERGED_LIB_FILE=$3
MODEL_INPUT_DIR=$4
RESULT_VALUE=`echo_device_id_by_soc $TARGET_SOC`
if [ $? -ne 0 ]; then
echo $RESULT_VALUE
exit 1
else
DEVICE_ID=$RESULT_VALUE
fi
if [ "$CPU_MODEL_TAG" != '' ]; then
CPU_MODEL_TAG_BUILD_FLAGS="--copt=-DMACE_CPU_MODEL_TAG=${CPU_MODEL_TAG}"
fi
if [ "$GPU_MODEL_TAG" != '' ]; then
GPU_MODEL_TAG_BUILD_FLAGS="--copt=-DMACE_GPU_MODEL_TAG=${GPU_MODEL_TAG}"
fi
if [ "$DSP_MODEL_TAG" != '' ]; then
DSP_MODEL_TAG_BUILD_FLAGS="--copt=-DMACE_DSP_MODEL_TAG=${DSP_MODEL_TAG}"
fi
cp $MERGED_LIB_FILE mace/benchmark/libmace_merged.a
bazel build --verbose_failures -c opt --strip always //mace/benchmark:model_throughput_test \
--crosstool_top=//external:android/crosstool \
--host_crosstool_top=@bazel_tools//tools/cpp:toolchain \
--cpu=${TARGET_ABI} \
--copt="-std=c++11" \
--copt="-D_GLIBCXX_USE_C99_MATH_TR1" \
--copt="-Werror=return-type" \
${CPU_MODEL_TAG_BUILD_FLAGS} \
${GPU_MODEL_TAG_BUILD_FLAGS} \
${DSP_MODEL_TAG_BUILD_FLAGS} \
--define openmp=true \
--copt="-O3" || exit 1
rm mace/benchmark/libmace_merged.a
adb -s $DEVICE_ID shell "mkdir -p ${PHONE_DATA_DIR}" || exit 1
adb -s $DEVICE_ID push ${MODEL_INPUT_DIR}/${INPUT_FILE_NAME}_${INPUT_NODES} ${PHONE_DATA_DIR} || exit 1
adb -s $DEVICE_ID push bazel-bin/mace/benchmark/model_throughput_test ${PHONE_DATA_DIR} || exit 1
if [ "$EMBED_MODEL_DATA" = 0 ]; then
adb -s $DEVICE_ID push codegen/models/${CPU_MODEL_TAG}/${CPU_MODEL_TAG}.data ${PHONE_DATA_DIR} || exit 1
adb -s $DEVICE_ID push codegen/models/${GPU_MODEL_TAG}/${GPU_MODEL_TAG}.data ${PHONE_DATA_DIR} || exit 1
adb -s $DEVICE_ID push codegen/models/${DSP_MODEL_TAG}/${DSP_MODEL_TAG}.data ${PHONE_DATA_DIR} || exit 1
fi
adb -s $DEVICE_ID push mace/core/runtime/hexagon/libhexagon_controller.so ${PHONE_DATA_DIR} || exit 1
adb -s $DEVICE_ID </dev/null shell \
LD_LIBRARY_PATH=${PHONE_DATA_DIR} \
MACE_CPP_MIN_VLOG_LEVEL=$VLOG_LEVEL \
MACE_RUN_PARAMETER_PATH=${PHONE_DATA_DIR}/mace_run.config \
MACE_KERNEL_PATH=$KERNEL_DIR \
MACE_LIMIT_OPENCL_KERNEL_TIME=${LIMIT_OPENCL_KERNEL_TIME} \
${PHONE_DATA_DIR}/model_throughput_test \
--input_node="${INPUT_NODES}" \
--input_shape="${INPUT_SHAPES}" \
--output_node="${OUTPUT_NODES}" \
--output_shape="${OUTPUT_SHAPES}" \
--input_file=${PHONE_DATA_DIR}/${INPUT_FILE_NAME} \
--cpu_model_data_file=${PHONE_DATA_DIR}/${CPU_MODEL_TAG}.data \
--gpu_model_data_file=${PHONE_DATA_DIR}/${GPU_MODEL_TAG}.data \
--dsp_model_data_file=${PHONE_DATA_DIR}/${DSP_MODEL_TAG}.data \
--run_seconds=$RUN_SECONDS || exit 1
#!/bin/bash
Usage() {
echo "Usage: bash tools/clear_env.sh target_soc"
}
if [ $# -lt 1 ]; then
Usage
exit 1
fi
CURRENT_DIR=`dirname $0`
source ${CURRENT_DIR}/env.sh
TARGET_SOC=$1
RESULT_VALUE=`echo_device_id_by_soc $TARGET_SOC`
if [ $? -ne 0 ]; then
echo $RESULT_VALUE
exit 1
else
DEVICE_ID=$RESULT_VALUE
fi
if [ x"$TARGET_ABI" != x"host" ]; then
adb -s $DEVICE_ID shell rm -rf $PHONE_DATA_DIR || exit 1
fi
rm -rf mace/codegen/models
#!/usr/bin/env bash
LIBMACE_TAG=`git describe --abbrev=0 --tags`
MACE_SOURCE_DIR=`/bin/pwd`
PHONE_DATA_DIR="/data/local/tmp/mace_run"
COMPILED_PROGRAM_DIR="${PHONE_DATA_DIR}/cl_program/"
CODEGEN_DIR=${MACE_SOURCE_DIR}/mace/codegen
MODEL_CODEGEN_DIR=${CODEGEN_DIR}/models/${MODEL_TAG}
CL_CODEGEN_DIR=${CODEGEN_DIR}/opencl
TUNING_CODEGEN_DIR=${CODEGEN_DIR}/tuning
VERSION_SOURCE_PATH=${CODEGEN_DIR}/version
CL_BUILT_KERNEL_FILE_NAME=mace_cl_compiled_program.bin
CL_PLATFORM_INFO_FILE_NAME=mace_cl_platform_info.txt
if [ -z ${EMBED_MODEL_DATA} ]; then
EMBED_MODEL_DATA=1
fi
if [ x"$RUNTIME" = x"dsp" ]; then
DATA_TYPE="DT_UINT8"
DEVICE_TYPE="HEXAGON"
LIB_FOLDER_NAME="${LIB_FOLDER_NAME}_dsp"
elif [ x"$RUNTIME" = x"gpu" ]; then
DATA_TYPE="DT_HALF"
DEVICE_TYPE="OPENCL"
elif [ x"$RUNTIME" = x"cpu" ]; then
DATA_TYPE="DT_FLOAT"
DEVICE_TYPE="CPU"
elif [ x"$RUNTIME" = x"neon" ]; then
DATA_TYPE="DT_FLOAT"
DEVICE_TYPE="NEON"
fi
GENERATED_MODEL_LIB_NAME="libgenerated_models.a"
if [ x"$TARGET_ABI" = x"host" ]; then
GENERATED_MODEL_LIB_NAME="libgenerated_models.pic.a"
fi
GENERATED_MODEL_LIB_PATH="bazel-bin/mace/codegen/${GENERATED_MODEL_LIB_NAME}"
echo_device_id_by_soc()
{
TARGET_SOC=$1
for device in `adb devices | grep "^[A-Za-z0-9]\+[[:space:]]\+device$"| cut -f1`; do
device_soc=`adb -s ${device} shell getprop | grep ro.board.platform | cut -d [ -f3 | cut -d ] -f1`
if [ x"$TARGET_SOC" = x"$device_soc" ]; then
echo "$device"
return 0
fi
done
echo "MACE ERROR: Not found device with soc ${TARGET_SOC}"
return 1
}
# Copyright 2018 Xiaomi, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json import json
import socket import socket
import itertools import itertools
......
# Copyright 2018 Xiaomi, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse import argparse
import sys import sys
import numpy as np import numpy as np
...@@ -12,22 +26,22 @@ import re ...@@ -12,22 +26,22 @@ import re
# #
def generate_data(name, shape): def generate_data(name, shape, input_file):
np.random.seed() np.random.seed()
data = np.random.random(shape) * 2 - 1 data = np.random.random(shape) * 2 - 1
input_file_name = FLAGS.input_file + "_" + re.sub('[^0-9a-zA-Z]+', '_', input_file_name = input_file + "_" + re.sub('[^0-9a-zA-Z]+', '_',
name) name)
print 'Generate input file: ', input_file_name print 'Generate input file: ', input_file_name
data.astype(np.float32).tofile(input_file_name) data.astype(np.float32).tofile(input_file_name)
def main(unused_args): def generate_input_data(input_file, input_node, input_shape):
input_names = [name for name in FLAGS.input_node.split(',')] input_names = [name for name in input_node.split(',')]
input_shapes = [shape for shape in FLAGS.input_shape.split(':')] input_shapes = [shape for shape in input_shape.split(':')]
assert len(input_names) == len(input_shapes) assert len(input_names) == len(input_shapes)
for i in range(len(input_names)): for i in range(len(input_names)):
shape = [int(x) for x in input_shapes[i].split(',')] shape = [int(x) for x in input_shapes[i].split(',')]
generate_data(input_names[i], shape) generate_data(input_names[i], shape, input_file)
print "Generate input file done." print "Generate input file done."
...@@ -47,4 +61,4 @@ def parse_args(): ...@@ -47,4 +61,4 @@ def parse_args():
if __name__ == '__main__': if __name__ == '__main__':
FLAGS, unparsed = parse_args() FLAGS, unparsed = parse_args()
main(unused_args=[sys.argv[0]] + unparsed) generate_input_data(FLAGS.input_file, FLAGS.input_node, FLAGS.input_shape)
#!/bin/bash
CURRENT_DIR=`dirname $0`
source ${CURRENT_DIR}/env.sh
bazel build //mace/python/tools:converter || exit 1
rm -rf ${MODEL_CODEGEN_DIR}
mkdir -p ${MODEL_CODEGEN_DIR}
if [ ${DSP_MODE} ]; then
DSP_MODE_FLAG="--dsp_mode=${DSP_MODE}"
fi
PYTHONUNBUFFERED=1 bazel-bin/mace/python/tools/converter --platform=${PLATFORM} \
--model_file=${MODEL_FILE_PATH} \
--weight_file=${WEIGHT_FILE_PATH} \
--model_checksum=${MODEL_SHA256_CHECKSUM} \
--output=${MODEL_CODEGEN_DIR}/model.cc \
--input_node=${INPUT_NODES} \
--output_node=${OUTPUT_NODES} \
--data_type=${DATA_TYPE} \
--runtime=${RUNTIME} \
--output_type=source \
--template=${MACE_SOURCE_DIR}/mace/python/tools \
--model_tag=${MODEL_TAG} \
--input_shape=${INPUT_SHAPES} \
${DSP_MODE_FLAG} \
--embed_model_data=${EMBED_MODEL_DATA} \
--winograd=${FAST_CONV} \
--obfuscate=${OBFUSCATE} || exit 1
#!/usr/bin/env bash
Usage() {
echo "Usage: bash tools/genenrate_opencl_code.sh type [target_soc] [cl_bin_dirs] [pull_or_not]"
}
if [ $# -lt 1 ]; then
Usage
exit 1
fi
CURRENT_DIR=`dirname $0`
source ${CURRENT_DIR}/env.sh
TYPE=$1
TARGET_SOC=$2
CL_BIN_DIRS=$3
PULL_OR_NOT=$4
mkdir -p ${CL_CODEGEN_DIR}
if [ x"$TYPE" == x"source" ];then
python mace/python/tools/encrypt_opencl_codegen.py \
--cl_kernel_dir=./mace/kernels/opencl/cl/ \
--output_path=${CODEGEN_DIR}/opencl/opencl_encrypt_program.cc || exit 1
elif [ x"$#" == x"1" ];then
python mace/python/tools/opencl_codegen.py \
--built_kernel_file_name=${CL_BUILT_KERNEL_FILE_NAME} \
--platform_info_file_name=${CL_PLATFORM_INFO_FILE_NAME} \
--output_path=${CL_CODEGEN_DIR}/opencl_compiled_program.cc || exit 1
else
RESULT_VALUE=`echo_device_id_by_soc $TARGET_SOC`
if [ $? -ne 0 ]; then
echo $RESULT_VALUE
exit 1
else
DEVICE_ID=$RESULT_VALUE
fi
if [ "$PULL_OR_NOT" = 1 ]; then
CL_BIN_DIR=${CL_BIN_DIRS}
mkdir -p ${CL_BIN_DIR}
rm -rf ${CL_BIN_DIR}/${CL_BUILT_KERNEL_FILE_NAME}
rm -rf ${CL_BIN_DIR}/${CL_PLATFORM_INFO_FILE_NAME}
if [ x"$TARGET_ABI" != x"host" ]; then
adb -s $DEVICE_ID pull ${COMPILED_PROGRAM_DIR}/. ${CL_BIN_DIR} > /dev/null
fi
fi
python mace/python/tools/opencl_codegen.py \
--built_kernel_file_name=${CL_BUILT_KERNEL_FILE_NAME} \
--platform_info_file_name=${CL_PLATFORM_INFO_FILE_NAME} \
--cl_binary_dirs=${CL_BIN_DIRS} \
--output_path=${CL_CODEGEN_DIR}/opencl_compiled_program.cc || exit 1
fi
#!/bin/bash
CURRENT_DIR=`dirname $0`
source ${CURRENT_DIR}/env.sh
mkdir -p ${TUNING_CODEGEN_DIR}
if [ "$#" -eq "0" ]; then
python mace/python/tools/binary_codegen.py \
--binary_file_name=mace_run.config \
--output_path=${TUNING_CODEGEN_DIR}/tuning_params.cc
else
TARGET_SOC=$1
BIN_DIRS=$2
PULL_OR_NOT=$3
RESULT_VALUE=`echo_device_id_by_soc $TARGET_SOC`
if [ $? -ne 0 ]; then
echo $RESULT_VALUE
exit 1
else
DEVICE_ID=$RESULT_VALUE
fi
if [ "$PULL_OR_NOT" = 1 ]; then
mkdir -p ${BIN_DIRS}
rm -rf ${BIN_DIRS}/mace_run.config
if [ x"$TARGET_ABI" != x"host" ]; then
adb -s $DEVICE_ID pull ${PHONE_DATA_DIR}/mace_run.config ${BIN_DIRS} > /dev/null
fi
fi
python mace/python/tools/binary_codegen.py \
--binary_dirs=${BIN_DIRS} \
--binary_file_name=mace_run.config \
--output_path=${TUNING_CODEGEN_DIR}/tuning_params.cc
fi
#!/bin/bash
CL_KERNEL_DIR_TAG=$1
CURRENT_DIR=`dirname $0`
source ${CURRENT_DIR}/env.sh
rm -rf ${CODEGEN_DIR}/version
mkdir ${CODEGEN_DIR}/version
bash mace/tools/git/gen_version_source.sh ${CODEGEN_DIR}/version/version.cc || exit 1
此差异已折叠。
#!/bin/bash
Usage() {
echo "Usage: bash tools/merge_libs.sh target_soc libmace_output_dir model_output_dirs"
}
if [ $# -lt 3 ]; then
Usage
exit 1
fi
CURRENT_DIR=`dirname $0`
source ${CURRENT_DIR}/env.sh
TARGET_SOC=$1
LIBMACE_BUILD_DIR=$2
MODEL_OUTPUT_DIRS=$3
MODEL_OUTPUT_DIRS_ARR=(${MODEL_OUTPUT_DIRS//,/ })
MODEL_HEADER_DIR=${LIBMACE_BUILD_DIR}/include/mace/public
MODEL_DATA_DIR=${LIBMACE_BUILD_DIR}/data
if [ ! -d "${MODEL_HEADER_DIR}" ]; then
mkdir -p ${MODEL_HEADER_DIR}
fi
cp -rf ${MACE_SOURCE_DIR}/mace/public/*.h ${LIBMACE_BUILD_DIR}/include/mace/public/
if [ ! -d "${LIBMACE_BUILD_DIR}/${TARGET_ABI}" ]; then
mkdir -p ${LIBMACE_BUILD_DIR}/${TARGET_ABI}
fi
if [ ! -d "${MODEL_DATA_DIR}" ]; then
mkdir -p ${MODEL_DATA_DIR}
fi
if [ x"${TARGET_ABI}" = x"armeabi-v7a" ]; then
cp ${MACE_SOURCE_DIR}/mace/core/runtime/hexagon/libhexagon_controller.so ${LIBMACE_BUILD_DIR}/${TARGET_ABI}/
fi
LIBMACE_TEMP_DIR=`mktemp -d -t libmace.XXXX`
# Merge all libraries in to one
echo "create ${LIBMACE_BUILD_DIR}/${TARGET_ABI}/libmace_${PROJECT_NAME}.${TARGET_SOC}.a" > ${LIBMACE_TEMP_DIR}/libmace_${PROJECT_NAME}.mri
if [ x"$TARGET_ABI" = x"host" ]; then
echo "addlib bazel-bin/mace/codegen/libgenerated_opencl.pic.a" >> ${LIBMACE_TEMP_DIR}/libmace_${PROJECT_NAME}.mri
echo "addlib bazel-bin/mace/codegen/libgenerated_tuning_params.pic.a" >> ${LIBMACE_TEMP_DIR}/libmace_${PROJECT_NAME}.mri
else
echo "addlib bazel-bin/mace/codegen/libgenerated_opencl.a" >> ${LIBMACE_TEMP_DIR}/libmace_${PROJECT_NAME}.mri
echo "addlib bazel-bin/mace/codegen/libgenerated_tuning_params.a" >> ${LIBMACE_TEMP_DIR}/libmace_${PROJECT_NAME}.mri
echo "addlib bazel-bin/mace/codegen/libgenerated_version.a" >> ${LIBMACE_TEMP_DIR}/libmace_${PROJECT_NAME}.mri
echo "addlib bazel-bin/mace/core/libcore.a" >> ${LIBMACE_TEMP_DIR}/libmace_${PROJECT_NAME}.mri
echo "addlib bazel-bin/mace/kernels/libkernels.a" >> ${LIBMACE_TEMP_DIR}/libmace_${PROJECT_NAME}.mri
echo "addlib bazel-bin/mace/utils/libutils.a" >> ${LIBMACE_TEMP_DIR}/libmace_${PROJECT_NAME}.mri
echo "addlib bazel-bin/mace/utils/libutils_prod.a" >> ${LIBMACE_TEMP_DIR}/libmace_${PROJECT_NAME}.mri
echo "addlib bazel-bin/mace/ops/libops.lo" >> ${LIBMACE_TEMP_DIR}/libmace_${PROJECT_NAME}.mri
fi
for model_output_dir in ${MODEL_OUTPUT_DIRS_ARR[@]}; do
for lib in ${model_output_dir}/*.a; do
echo "addlib ${lib}" >> ${LIBMACE_TEMP_DIR}/libmace_${PROJECT_NAME}.mri
done
if [ "${EMBED_MODEL_DATA}" == "0" ];then
for data_file in ${model_output_dir}/*.data; do
cp ${data_file} ${MODEL_DATA_DIR}
done
fi
for header_file in ${model_output_dir}/*.h; do
cp ${header_file} ${MODEL_HEADER_DIR}
done
done
echo "save" >> ${LIBMACE_TEMP_DIR}/libmace_${PROJECT_NAME}.mri
echo "end" >> ${LIBMACE_TEMP_DIR}/libmace_${PROJECT_NAME}.mri
$ANDROID_NDK_HOME/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin/aarch64-linux-android-ar \
-M < ${LIBMACE_TEMP_DIR}/libmace_${PROJECT_NAME}.mri || exit 1
rm -rf ${LIBMACE_TEMP_DIR}
echo "Libs merged!"
#!/bin/bash
Usage() {
echo "Usage: bash tools/packaging_lib.sh libmace_output_dir"
}
if [ $# -lt 1 ]; then
Usage
exit 1
fi
CURRENT_DIR=`dirname $0`
source ${CURRENT_DIR}/env.sh
LIBMACE_BUILD_DIR=$1
TAR_PACKAGE_NAME=libmace_${PROJECT_NAME}.tar.gz
pushd $LIBMACE_BUILD_DIR/$PROJECT_NAME
if [ -f $TAR_PACKAGE_NAME ]; then
rm -f $TAR_PACKAGE_NAME
fi
ls | grep -v build | xargs tar cvzf $TAR_PACKAGE_NAME
popd
echo "Packaging done!"
此差异已折叠。
#!/bin/bash
Usage() {
echo "Usage: bash tools/tuning_run.sh target_soc model_output_dir round tuning restart_round opt_args out_of_range_check"
}
if [ $# -lt 7 ]; then
Usage
exit 1
fi
CURRENT_DIR=`dirname $0`
source ${CURRENT_DIR}/env.sh
TARGET_SOC=$1
MODEL_OUTPUT_DIR=$2
ROUND=$3
TUNING_OR_NOT=$4
RESTART_ROUND=$5
OPTION_ARGS=$6
OUT_OF_RANGE_CHECK_OR_NOT=$7
echo $OPTION_ARGS
RESULT_VALUE=`echo_device_id_by_soc $TARGET_SOC`
if [ $? -ne 0 ]; then
echo $RESULT_VALUE
exit 1
else
DEVICE_ID=$RESULT_VALUE
fi
if [ x"$TARGET_ABI" = x"host" ]; then
MACE_CPP_MIN_VLOG_LEVEL=$VLOG_LEVEL \
${MODEL_OUTPUT_DIR}/mace_run \
--input_node="${INPUT_NODES}" \
--input_shape="${INPUT_SHAPES}"\
--output_node="${OUTPUT_NODES}" \
--output_shape="${OUTPUT_SHAPES}"\
--input_file=${MODEL_OUTPUT_DIR}/${INPUT_FILE_NAME} \
--output_file=${MODEL_OUTPUT_DIR}/${OUTPUT_FILE_NAME} \
--model_data_file=${MODEL_OUTPUT_DIR}/${MODEL_TAG}.data \
--device=${DEVICE_TYPE} \
--round=1 \
--restart_round=1 \
$OPTION_ARGS || exit 1
else
if [[ "${TUNING_OR_NOT}" != "0" ]];then
tuning_flag=1
else
tuning_flag=0
fi
if [[ "${OUT_OF_RANGE_CHECK_OR_NOT}" != "0" ]]; then
out_of_range_check_flag=1
else
out_of_range_check_flag=0
fi
adb -s $DEVICE_ID shell "mkdir -p ${PHONE_DATA_DIR}" || exit 1
adb -s $DEVICE_ID shell "mkdir -p ${COMPILED_PROGRAM_DIR}" || exit 1
IFS=',' read -r -a INPUT_NAMES <<< "${INPUT_NODES}"
for NAME in "${INPUT_NAMES[@]}";do
FORMATTED_NAME=$(sed s/[^[:alnum:]]/_/g <<< ${NAME})
adb -s $DEVICE_ID push ${MODEL_OUTPUT_DIR}/${INPUT_FILE_NAME}_${FORMATTED_NAME} ${PHONE_DATA_DIR} > /dev/null || exit 1
done
adb -s $DEVICE_ID </dev/null push ${MODEL_OUTPUT_DIR}/mace_run ${PHONE_DATA_DIR} > /dev/null || exit 1
if [ "$EMBED_MODEL_DATA" = 0 ]; then
adb -s $DEVICE_ID push ${MODEL_OUTPUT_DIR}/${MODEL_TAG}.data ${PHONE_DATA_DIR} > /dev/null || exit 1
fi
adb -s $DEVICE_ID push mace/core/runtime/hexagon/libhexagon_controller.so ${PHONE_DATA_DIR} > /dev/null || exit 1
ADB_CMD_STR="LD_LIBRARY_PATH=${PHONE_DATA_DIR} \
MACE_TUNING=${tuning_flag} \
MACE_OUT_OF_RANGE_CHECK=${out_of_range_check_flag} \
MACE_CPP_MIN_VLOG_LEVEL=$VLOG_LEVEL \
MACE_RUN_PARAMETER_PATH=${PHONE_DATA_DIR}/mace_run.config \
MACE_CL_PROGRAM_PATH=$COMPILED_PROGRAM_DIR \
MACE_LIMIT_OPENCL_KERNEL_TIME=${LIMIT_OPENCL_KERNEL_TIME} \
${PHONE_DATA_DIR}/mace_run \
--input_node="${INPUT_NODES}" \
--input_shape="${INPUT_SHAPES}"\
--output_node="${OUTPUT_NODES}" \
--output_shape="${OUTPUT_SHAPES}"\
--input_file=${PHONE_DATA_DIR}/${INPUT_FILE_NAME} \
--output_file=${PHONE_DATA_DIR}/${OUTPUT_FILE_NAME} \
--model_data_file=${PHONE_DATA_DIR}/${MODEL_TAG}.data \
--device=${DEVICE_TYPE} \
--round=$ROUND \
--restart_round=$RESTART_ROUND \
$OPTION_ARGS; echo \$?"
echo $ADB_CMD_STR
mace_adb_output=`adb -s $DEVICE_ID </dev/null shell "$ADB_CMD_STR"` || exit 1
echo "$mace_adb_output" | head -n -1
mace_adb_return_code=`echo "$mace_adb_output" | tail -1`
if ! [[ ${mace_adb_return_code%?} = 0 || ${mace_adb_return_code} = 0 ]]; then
exit 1
fi
fi
# Copyright 2018 Xiaomi, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse import argparse
import sys import sys
import os import os
...@@ -32,18 +46,19 @@ def format_name(name): ...@@ -32,18 +46,19 @@ def format_name(name):
return re.sub('[^0-9a-zA-Z]+', '_', name) return re.sub('[^0-9a-zA-Z]+', '_', name)
def compare_output(output_name, mace_out_value, out_value): def compare_output(platform, mace_runtime, output_name, mace_out_value,
out_value):
if mace_out_value.size != 0: if mace_out_value.size != 0:
out_value = out_value.reshape(-1) out_value = out_value.reshape(-1)
mace_out_value = mace_out_value.reshape(-1) mace_out_value = mace_out_value.reshape(-1)
assert len(out_value) == len(mace_out_value) assert len(out_value) == len(mace_out_value)
similarity = (1 - spatial.distance.cosine(out_value, mace_out_value)) similarity = (1 - spatial.distance.cosine(out_value, mace_out_value))
print output_name, 'MACE VS', FLAGS.platform.upper( print output_name, 'MACE VS', platform.upper(
), 'similarity: ', similarity ), 'similarity: ', similarity
if (FLAGS.mace_runtime == "cpu" and similarity > 0.999) or \ if (mace_runtime == "cpu" and similarity > 0.999) or \
(FLAGS.mace_runtime == "neon" and similarity > 0.999) or \ (mace_runtime == "neon" and similarity > 0.999) or \
(FLAGS.mace_runtime == "gpu" and similarity > 0.995) or \ (mace_runtime == "gpu" and similarity > 0.995) or \
(FLAGS.mace_runtime == "dsp" and similarity > 0.930): (mace_runtime == "dsp" and similarity > 0.930):
print '===================Similarity Test Passed==================' print '===================Similarity Test Passed=================='
else: else:
print '===================Similarity Test Failed==================' print '===================Similarity Test Failed=================='
...@@ -53,14 +68,15 @@ def compare_output(output_name, mace_out_value, out_value): ...@@ -53,14 +68,15 @@ def compare_output(output_name, mace_out_value, out_value):
sys.exit(-1) sys.exit(-1)
def validate_tf_model(input_names, input_shapes, output_names): def validate_tf_model(platform, mace_runtime, model_file, input_file,
mace_out_file, input_names, input_shapes, output_names):
import tensorflow as tf import tensorflow as tf
if not os.path.isfile(FLAGS.model_file): if not os.path.isfile(model_file):
print("Input graph file '" + FLAGS.model_file + "' does not exist!") print("Input graph file '" + model_file + "' does not exist!")
sys.exit(-1) sys.exit(-1)
input_graph_def = tf.GraphDef() input_graph_def = tf.GraphDef()
with open(FLAGS.model_file, "rb") as f: with open(model_file, "rb") as f:
data = f.read() data = f.read()
input_graph_def.ParseFromString(data) input_graph_def.ParseFromString(data)
tf.import_graph_def(input_graph_def, name="") tf.import_graph_def(input_graph_def, name="")
...@@ -71,7 +87,7 @@ def validate_tf_model(input_names, input_shapes, output_names): ...@@ -71,7 +87,7 @@ def validate_tf_model(input_names, input_shapes, output_names):
input_dict = {} input_dict = {}
for i in range(len(input_names)): for i in range(len(input_names)):
input_value = load_data( input_value = load_data(
FLAGS.input_file + "_" + format_name(input_names[i])) input_file + "_" + format_name(input_names[i]))
input_value = input_value.reshape(input_shapes[i]) input_value = input_value.reshape(input_shapes[i])
input_node = graph.get_tensor_by_name( input_node = graph.get_tensor_by_name(
input_names[i] + ':0') input_names[i] + ':0')
...@@ -83,31 +99,31 @@ def validate_tf_model(input_names, input_shapes, output_names): ...@@ -83,31 +99,31 @@ def validate_tf_model(input_names, input_shapes, output_names):
[graph.get_tensor_by_name(name + ':0')]) [graph.get_tensor_by_name(name + ':0')])
output_values = session.run(output_nodes, feed_dict=input_dict) output_values = session.run(output_nodes, feed_dict=input_dict)
for i in range(len(output_names)): for i in range(len(output_names)):
output_file_name = FLAGS.mace_out_file + "_" + \ output_file_name = mace_out_file + "_" + \
format_name(output_names[i]) format_name(output_names[i])
mace_out_value = load_data(output_file_name) mace_out_value = load_data(output_file_name)
compare_output(output_names[i], mace_out_value, compare_output(platform, mace_runtime, output_names[i],
output_values[i]) mace_out_value, output_values[i])
def validate_caffe_model(input_names, input_shapes, output_names, def validate_caffe_model(platform, mace_runtime, model_file, input_file,
output_shapes): mace_out_file, weight_file, input_names, input_shapes,
os.environ['GLOG_minloglevel'] = '1' # suppress Caffe verbose prints output_names, output_shapes):
os.environ['GLOG_minloglevel'] = '1' # suprress Caffe verbose prints
import caffe import caffe
if not os.path.isfile(FLAGS.model_file): if not os.path.isfile(model_file):
print("Input graph file '" + FLAGS.model_file + "' does not exist!") print("Input graph file '" + model_file + "' does not exist!")
sys.exit(-1) sys.exit(-1)
if not os.path.isfile(FLAGS.weight_file): if not os.path.isfile(weight_file):
print("Input weight file '" + FLAGS.weight_file + "' does not exist!") print("Input weight file '" + weight_file + "' does not exist!")
sys.exit(-1) sys.exit(-1)
caffe.set_mode_cpu() caffe.set_mode_cpu()
net = caffe.Net(FLAGS.model_file, caffe.TEST, weights=FLAGS.weight_file) net = caffe.Net(model_file, caffe.TEST, weights=weight_file)
for i in range(len(input_names)): for i in range(len(input_names)):
input_value = load_data(FLAGS.input_file + "_" + input_value = load_data(input_file + "_" + format_name(input_names[i]))
format_name(input_names[i]))
input_value = input_value.reshape(input_shapes[i]).transpose((0, 3, 1, input_value = input_value.reshape(input_shapes[i]).transpose((0, 3, 1,
2)) 2))
input_blob_name = input_names[i] input_blob_name = input_names[i]
...@@ -126,28 +142,33 @@ def validate_caffe_model(input_names, input_shapes, output_names, ...@@ -126,28 +142,33 @@ def validate_caffe_model(input_names, input_shapes, output_names,
out_shape[1], out_shape[2], out_shape[3] = out_shape[3], out_shape[ out_shape[1], out_shape[2], out_shape[3] = out_shape[3], out_shape[
1], out_shape[2] 1], out_shape[2]
value = value.reshape(out_shape).transpose((0, 2, 3, 1)) value = value.reshape(out_shape).transpose((0, 2, 3, 1))
output_file_name = FLAGS.mace_out_file + "_" + format_name( output_file_name = mace_out_file + "_" + format_name(
output_names[i]) output_names[i])
mace_out_value = load_data(output_file_name) mace_out_value = load_data(output_file_name)
compare_output(output_names[i], mace_out_value, value) compare_output(platform, mace_runtime, output_names[i], mace_out_value,
value)
def main(unused_args): def validate(platform, model_file, weight_file, input_file, mace_out_file,
input_names = [name for name in FLAGS.input_node.split(',')] mace_runtime, input_shape, output_shape, input_node, output_node):
input_shape_strs = [shape for shape in FLAGS.input_shape.split(':')] input_names = [name for name in input_node.split(',')]
input_shape_strs = [shape for shape in input_shape.split(':')]
input_shapes = [[int(x) for x in shape.split(',')] input_shapes = [[int(x) for x in shape.split(',')]
for shape in input_shape_strs] for shape in input_shape_strs]
output_names = [name for name in FLAGS.output_node.split(',')] output_names = [name for name in output_node.split(',')]
assert len(input_names) == len(input_shapes) assert len(input_names) == len(input_shapes)
if FLAGS.platform == 'tensorflow': if platform == 'tensorflow':
validate_tf_model(input_names, input_shapes, output_names) validate_tf_model(platform, mace_runtime, model_file, input_file,
elif FLAGS.platform == 'caffe': mace_out_file, input_names, input_shapes,
output_shape_strs = [shape for shape in FLAGS.output_shape.split(':')] output_names)
elif platform == 'caffe':
output_shape_strs = [shape for shape in output_shape.split(':')]
output_shapes = [[int(x) for x in shape.split(',')] output_shapes = [[int(x) for x in shape.split(',')]
for shape in output_shape_strs] for shape in output_shape_strs]
validate_caffe_model(input_names, input_shapes, output_names, validate_caffe_model(platform, mace_runtime, model_file, input_file,
output_shapes) mace_out_file, weight_file, input_names,
input_shapes, output_names, output_shapes)
def parse_args(): def parse_args():
...@@ -189,4 +210,13 @@ def parse_args(): ...@@ -189,4 +210,13 @@ def parse_args():
if __name__ == '__main__': if __name__ == '__main__':
FLAGS, unparsed = parse_args() FLAGS, unparsed = parse_args()
main(unused_args=[sys.argv[0]] + unparsed) validate(FLAGS.platform,
FLAGS.model_file,
FLAGS.weight_file,
FLAGS.input_file,
FLAGS.mace_out_file,
FLAGS.mace_runtime,
FLAGS.input_shape,
FLAGS.output_shape,
FLAGS.input_node,
FLAGS.output_node)
#!/bin/bash
Usage() {
echo "Usage: bash tools/validate_tools.sh target_soc model_output_dir generate_data_or_not"
}
if [ $# -lt 3 ]; then
Usage
exit 1
fi
CURRENT_DIR=`dirname $0`
source ${CURRENT_DIR}/env.sh
TARGET_SOC=$1
MODEL_OUTPUT_DIR=$2
GENERATE_DATA_OR_NOT=$3
RESULT_VALUE=`echo_device_id_by_soc $TARGET_SOC`
if [ $? -ne 0 ]; then
echo $RESULT_VALUE
exit 1
else
DEVICE_ID=$RESULT_VALUE
fi
IFS=',' read -r -a INPUT_NAMES <<< "${INPUT_NODES}"
IFS=',' read -r -a OUTPUT_NAMES <<< "${OUTPUT_NODES}"
echo $MODEL_OUTPUT_DIR
if [ "$GENERATE_DATA_OR_NOT" = 1 ]; then
for NAME in "${INPUT_NAMES[@]}";do
FORMATTED_NAME=$(sed s/[^[:alnum:]]/_/g <<< ${NAME})
rm -rf ${MODEL_OUTPUT_DIR}/${INPUT_FILE_NAME}_${FORMATTED_NAME}
done
python -u tools/generate_data.py --input_node=${INPUT_NODES} \
--input_file=${MODEL_OUTPUT_DIR}/${INPUT_FILE_NAME} \
--input_shape="${INPUT_SHAPES}" || exit 1
exit 0
fi
if [ "$PLATFORM" == "tensorflow" ];then
if [[ x"$TARGET_ABI" != x"host" ]]; then
for NAME in "${OUTPUT_NAMES[@]}";do
FORMATTED_NAME=$(sed s/[^[:alnum:]]/_/g <<< ${NAME})
rm -rf ${MODEL_OUTPUT_DIR}/${OUTPUT_FILE_NAME}_${FORMATTED_NAME}
adb -s $DEVICE_ID pull ${PHONE_DATA_DIR}/${OUTPUT_FILE_NAME}_${FORMATTED_NAME} ${MODEL_OUTPUT_DIR} > /dev/null || exit 1
done
fi
python -u tools/validate.py --platform=tensorflow \
--model_file ${MODEL_FILE_PATH} \
--input_file ${MODEL_OUTPUT_DIR}/${INPUT_FILE_NAME} \
--mace_out_file ${MODEL_OUTPUT_DIR}/${OUTPUT_FILE_NAME} \
--mace_runtime ${RUNTIME} \
--input_node ${INPUT_NODES} \
--output_node ${OUTPUT_NODES} \
--input_shape ${INPUT_SHAPES} \
--output_shape ${OUTPUT_SHAPES} || exit 1
elif [ "$PLATFORM" == "caffe" ];then
IMAGE_NAME=mace-caffe:latest
CONTAINER_NAME=mace_caffe_validator
RES_FILE=validation.result
if [[ "$(docker images -q mace-caffe:latest 2> /dev/null)" == "" ]]; then
echo "Build caffe docker"
docker build -t ${IMAGE_NAME} docker/caffe || exit 1
fi
if [ ! "$(docker ps -qa -f name=${CONTAINER_NAME})" ]; then
echo "Run caffe container"
docker run -d -it --name ${CONTAINER_NAME} ${IMAGE_NAME} /bin/bash || exit 1
fi
if [ "$(docker inspect -f {{.State.Running}} ${CONTAINER_NAME})" == "false" ];then
echo "Start caffe container"
docker start ${CONTAINER_NAME}
fi
for NAME in "${INPUT_NAMES[@]}";do
FORMATTED_NAME=$(sed s/[^[:alnum:]]/_/g <<< ${NAME})
docker cp ${MODEL_OUTPUT_DIR}/${INPUT_FILE_NAME}_${FORMATTED_NAME} ${CONTAINER_NAME}:/mace
done
if [[ x"$TARGET_ABI" != x"host" ]]; then
for NAME in "${OUTPUT_NAMES[@]}";do
FORMATTED_NAME=$(sed s/[^[:alnum:]]/_/g <<< ${NAME})
rm -rf ${MODEL_OUTPUT_DIR}/${OUTPUT_FILE_NAME}_${FORMATTED_NAME}
adb -s $DEVICE_ID pull ${PHONE_DATA_DIR}/${OUTPUT_FILE_NAME}_${FORMATTED_NAME} ${MODEL_OUTPUT_DIR} > /dev/null || exit 1
done
fi
for NAME in "${OUTPUT_NAMES[@]}";do
FORMATTED_NAME=$(sed s/[^[:alnum:]]/_/g <<< ${NAME})
docker cp ${MODEL_OUTPUT_DIR}/${OUTPUT_FILE_NAME}_${FORMATTED_NAME} ${CONTAINER_NAME}:/mace
done
MODEL_FILE_NAME=$(basename ${MODEL_FILE_PATH})
WEIGHT_FILE_NAME=$(basename ${WEIGHT_FILE_PATH})
docker cp tools/validate.py ${CONTAINER_NAME}:/mace
docker cp ${MODEL_FILE_PATH} ${CONTAINER_NAME}:/mace
docker cp ${WEIGHT_FILE_PATH} ${CONTAINER_NAME}:/mace
docker exec ${CONTAINER_NAME} python -u /mace/validate.py \
--platform=caffe \
--model_file /mace/${MODEL_FILE_NAME} \
--weight_file /mace/${WEIGHT_FILE_NAME} \
--input_file /mace/${INPUT_FILE_NAME} \
--mace_out_file /mace/${OUTPUT_FILE_NAME} \
--mace_runtime ${RUNTIME} \
--input_node ${INPUT_NODES} \
--output_node ${OUTPUT_NODES} \
--input_shape ${INPUT_SHAPES} \
--output_shape ${OUTPUT_SHAPES} || exit 1
fi
# Copyright 2018 Xiaomi, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import numpy as np import numpy as np
import math import math
import tensorflow as tf import tensorflow as tf
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册