Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
毕竟曾有刹那
Mace
提交
bb10ae3b
Mace
项目概览
毕竟曾有刹那
/
Mace
与 Fork 源项目一致
Fork自
Xiaomi / Mace
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Mace
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
bb10ae3b
编写于
7月 05, 2018
作者:
L
liuqi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add mace static target for bazel build.
上级
07a33cd5
变更
20
隐藏空白更改
内联
并排
Showing
20 changed file
with
293 addition
and
36 deletion
+293
-36
WORKSPACE
WORKSPACE
+7
-0
mace/BUILD
mace/BUILD
+35
-2
mace/benchmark/benchmark_model.cc
mace/benchmark/benchmark_model.cc
+5
-0
mace/codegen/BUILD
mace/codegen/BUILD
+7
-7
mace/core/BUILD
mace/core/BUILD
+0
-1
mace/core/mace_runtime.cc
mace/core/mace_runtime.cc
+6
-0
mace/examples/cli/BUILD
mace/examples/cli/BUILD
+1
-1
mace/examples/cli/example.cc
mace/examples/cli/example.cc
+5
-0
mace/mace.bzl
mace/mace.bzl
+18
-0
mace/public/mace_runtime.h
mace/public/mace_runtime.h
+6
-0
mace/python/tools/BUILD
mace/python/tools/BUILD
+7
-0
mace/python/tools/archive_static_lib.py
mace/python/tools/archive_static_lib.py
+40
-0
mace/python/tools/encrypt_opencl_codegen.py
mace/python/tools/encrypt_opencl_codegen.py
+16
-2
mace/tools/git/gen_version_source.sh
mace/tools/git/gen_version_source.sh
+13
-0
mace/tools/validation/mace_run.cc
mace/tools/validation/mace_run.cc
+5
-0
mace/utils/BUILD
mace/utils/BUILD
+0
-1
mace/utils/tuner.h
mace/utils/tuner.h
+22
-7
tools/bazel.rc
tools/bazel.rc
+1
-0
tools/converter.py
tools/converter.py
+35
-2
tools/sh_commands.py
tools/sh_commands.py
+64
-13
未找到文件。
WORKSPACE
浏览文件 @
bb10ae3b
workspace
(
name
=
"mace"
)
# generate version and opencl kernel code.
load
(
"//repository/git:git_configure.bzl"
,
"git_version_repository"
)
load
(
"//repository/opencl-kernel:opencl_kernel_configure.bzl"
,
"encrypt_opencl_kernel_repository"
)
git_version_repository
(
name
=
"local_version_config"
)
encrypt_opencl_kernel_repository
(
name
=
"local_opencl_kernel_encrypt"
)
# proto_library rules implicitly depend on @com_google_protobuf//:protoc,
# which is the proto-compiler.
# This statement defines the @com_google_protobuf repo.
...
...
mace/BUILD
浏览文件 @
bb10ae3b
...
...
@@ -73,8 +73,41 @@ cc_library(
visibility
=
[
"//visibility:public"
],
)
cc_library
(
genrule
(
name
=
"libmace_static"
,
srcs
=
[
"libmace.a"
],
srcs
=
[
"//mace/codegen:generated_opencl"
,
"//mace/codegen:generated_version"
,
"//mace/core"
,
"//mace/kernels"
,
"//mace/ops"
,
"//mace/utils"
,
"//mace/proto:mace_cc"
,
"@com_google_protobuf//:protobuf_lite"
,
],
outs
=
[
"libmace.a"
],
cmd
=
"tmp_mri_file=$$(mktemp mace-static-lib-mri.XXXXXXXXXX);"
+
"mri_stream=$$(python $(location //mace/python/tools:archive_static_lib) "
+
"$(locations //mace/codegen:generated_opencl) "
+
"$(locations //mace/codegen:generated_version) "
+
"$(locations //mace/core:core) "
+
"$(locations //mace/kernels:kernels) "
+
"$(locations //mace/ops:ops) "
+
"$(locations //mace/utils:utils) "
+
"$(locations //mace/proto:mace_cc) "
+
"$(locations @com_google_protobuf//:protobuf_lite) "
+
"$@ "
+
"$$tmp_mri_file);"
+
"$(AR) -M <$$tmp_mri_file;"
+
"rm -rf $$tmp_mri_file;"
,
tools
=
[
"//mace/python/tools:archive_static_lib"
],
visibility
=
[
"//visibility:public"
],
)
cc_library
(
name
=
"libmace_static_lib"
,
srcs
=
[
":libmace_static"
],
linkstatic
=
1
,
visibility
=
[
"//visibility:public"
],
)
mace/benchmark/benchmark_model.cc
浏览文件 @
bb10ae3b
...
...
@@ -191,6 +191,9 @@ DEFINE_int32(warmup_runs, 1, "how many runs to initialize model");
DEFINE_string
(
opencl_binary_file
,
""
,
"compiled opencl binary file path"
);
DEFINE_string
(
opencl_parameter_file
,
""
,
"tuned OpenCL parameter file path"
);
DEFINE_string
(
model_data_file
,
""
,
"model data file name, used when EMBED_MODEL_DATA set to 0"
);
DEFINE_string
(
model_file
,
""
,
...
...
@@ -264,6 +267,8 @@ int Main(int argc, char **argv) {
std
::
vector
<
std
::
string
>
opencl_binary_paths
=
{
FLAGS_opencl_binary_file
};
mace
::
SetOpenCLBinaryPaths
(
opencl_binary_paths
);
mace
::
SetOpenCLParameterPath
(
FLAGS_opencl_parameter_file
);
}
#endif // MACE_ENABLE_OPENCL
...
...
mace/codegen/BUILD
浏览文件 @
bb10ae3b
...
...
@@ -5,6 +5,8 @@ package(
default_visibility
=
[
"//visibility:public"
],
)
load
(
"//mace:mace.bzl"
,
"mace_version_genrule"
,
"encrypt_opencl_kernel_genrule"
)
cc_library
(
name
=
"generated_models"
,
srcs
=
glob
([
"models/*/*.cc"
]),
...
...
@@ -16,15 +18,13 @@ cc_library(
],
)
cc_library
(
name
=
"generated_opencl"
,
srcs
=
glob
([
"opencl/*.cc"
]),
copts
=
[
"-Werror"
,
"-Wextra"
,
"-Wno-missing-field-initializers"
],
)
mace_version_genrule
()
encrypt_opencl_kernel_genrule
()
cc_library
(
name
=
"generated_
tuning_params
"
,
srcs
=
[
"
tuning/tuning_params
.cc"
],
name
=
"generated_
opencl
"
,
srcs
=
[
"
opencl/encrypt_opencl_kernel
.cc"
],
copts
=
[
"-Werror"
,
"-Wextra"
,
"-Wno-missing-field-initializers"
],
)
...
...
mace/core/BUILD
浏览文件 @
bb10ae3b
...
...
@@ -61,7 +61,6 @@ cc_library(
]),
deps
=
[
"//mace/codegen:generated_version"
,
"//mace/codegen:generated_tuning_params"
,
"//mace/proto:mace_cc"
,
"//mace/utils"
,
]
+
if_android
([
...
...
mace/core/mace_runtime.cc
浏览文件 @
bb10ae3b
...
...
@@ -24,4 +24,10 @@ void SetKVStorageFactory(std::shared_ptr<KVStorageFactory> storage_factory) {
kStorageFactory
=
storage_factory
;
}
std
::
string
kOpenCLParameterPath
;
void
SetOpenCLParameterPath
(
const
std
::
string
&
path
)
{
kOpenCLParameterPath
=
path
;
}
};
// namespace mace
mace/examples/cli/BUILD
浏览文件 @
bb10ae3b
...
...
@@ -23,7 +23,7 @@ cc_binary(
deps
=
[
"//external:gflags_nothreads"
,
"//mace/codegen:generated_mace_engine_factory"
,
"//mace:libmace_static"
,
"//mace:libmace_static
_lib
"
,
]
+
if_hexagon_enabled
([
"//third_party/nnlib:libhexagon"
,
]),
...
...
mace/examples/cli/example.cc
浏览文件 @
bb10ae3b
...
...
@@ -108,6 +108,9 @@ DEFINE_string(output_file,
DEFINE_string
(
opencl_binary_file
,
""
,
"compiled opencl binary file path"
);
DEFINE_string
(
opencl_parameter_file
,
""
,
"tuned OpenCL parameter file path"
);
DEFINE_string
(
model_data_file
,
""
,
"model data file name, used when EMBED_MODEL_DATA set to 0"
);
...
...
@@ -172,6 +175,8 @@ bool RunModel(const std::vector<std::string> &input_names,
// you should update the binary when OpenCL Driver changed.
std
::
vector
<
std
::
string
>
opencl_binary_paths
=
{
FLAGS_opencl_binary_file
};
mace
::
SetOpenCLBinaryPaths
(
opencl_binary_paths
);
mace
::
SetOpenCLParameterPath
(
FLAGS_opencl_parameter_file
);
}
#endif // MACE_ENABLE_OPENCL
...
...
mace/mace.bzl
浏览文件 @
bb10ae3b
...
...
@@ -47,3 +47,21 @@ def if_openmp_enabled(a):
"//mace:openmp_enabled"
:
a
,
"//conditions:default"
:
[],
})
def
mace_version_genrule
():
native
.
genrule
(
name
=
"mace_version_gen"
,
srcs
=
[
str
(
Label
(
"@local_version_config//:gen/version"
))],
outs
=
[
"version/version.cc"
],
cmd
=
"cat $(SRCS) > $@;"
)
def
encrypt_opencl_kernel_genrule
():
native
.
genrule
(
name
=
"encrypt_opencl_kernel_gen"
,
srcs
=
[
str
(
Label
(
"@local_opencl_kernel_encrypt//:gen/encrypt_opencl_kernel"
))],
outs
=
[
"opencl/encrypt_opencl_kernel.cc"
],
cmd
=
"cat $(SRCS) > $@;"
)
mace/public/mace_runtime.h
浏览文件 @
bb10ae3b
...
...
@@ -90,6 +90,12 @@ void SetKVStorageFactory(std::shared_ptr<KVStorageFactory> storage_factory);
__attribute__
((
visibility
(
"default"
)))
void
SetOpenCLBinaryPaths
(
const
std
::
vector
<
std
::
string
>
&
paths
);
// Just call once. (Not thread-safe)
// Set the path of Generated OpenCL parameter file if you use gpu of specific soc.
// The parameters is the local work group size tuned for specific SOC, which
// may be faster than the general parameters.
void
SetOpenCLParameterPath
(
const
std
::
string
&
path
);
// Set GPU hints, currently only supports Adreno GPU.
//
// Caution: this function may hurt performance if improper parameters provided.
...
...
mace/python/tools/BUILD
浏览文件 @
bb10ae3b
...
...
@@ -48,3 +48,10 @@ py_binary(
"@six_archive//:six"
,
],
)
py_binary
(
name
=
"archive_static_lib"
,
srcs
=
[
"archive_static_lib.py"
],
srcs_version
=
"PY2AND3"
,
visibility
=
[
"//visibility:public"
],
)
mace/python/tools/archive_static_lib.py
0 → 100644
浏览文件 @
bb10ae3b
# 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
os
# python encrypt_opencl_codegen.py --cl_kernel_dir=./mace/kernels/opencl/cl/ \
# --output_path=./mace/codegen/opencl_encrypt/opencl_encrypted_program.cc
def
is_static_lib
(
lib_name
):
return
lib_name
.
endswith
(
'.a'
)
or
lib_name
.
endswith
(
'.lo'
)
def
merge_libs
(
input_libs
,
output_lib_path
,
mri_script
):
# make static library
mri_stream
=
""
mri_stream
+=
"create %s
\n
"
%
output_lib_path
for
lib
in
input_libs
:
if
is_static_lib
(
lib
):
mri_stream
+=
(
"addlib %s
\n
"
%
lib
)
mri_stream
+=
"save
\n
"
mri_stream
+=
"end
\n
"
with
open
(
mri_script
,
'w'
)
as
tmp
:
tmp
.
write
(
mri_stream
)
if
__name__
==
'__main__'
:
merge_libs
(
sys
.
argv
[
1
:
-
2
],
sys
.
argv
[
-
2
],
sys
.
argv
[
-
1
])
mace/python/tools/encrypt_opencl_codegen.py
浏览文件 @
bb10ae3b
...
...
@@ -14,6 +14,7 @@
import
argparse
import
os
import
shutil
import
sys
import
jinja2
...
...
@@ -68,8 +69,21 @@ def encrypt_opencl_codegen(cl_kernel_dir, output_path):
data_type
=
'unsigned char'
,
variable_name
=
'kEncryptedProgramMap'
)
if
os
.
path
.
isfile
(
output_path
):
os
.
remove
(
output_path
)
output_dir
=
os
.
path
.
dirname
(
output_path
)
if
os
.
path
.
exists
(
output_dir
):
if
os
.
path
.
isdir
(
output_dir
):
try
:
shutil
.
rmtree
(
output_dir
)
except
OSError
:
raise
RuntimeError
(
"Cannot delete directory %s due to permission "
"error, inspect and remove manually"
%
output_dir
)
else
:
raise
RuntimeError
(
"Cannot delete non-directory %s, inspect "
,
"and remove manually"
%
output_dir
)
os
.
makedirs
(
output_dir
)
with
open
(
output_path
,
"w"
)
as
w_file
:
w_file
.
write
(
cpp_cl_encrypted_kernel
)
...
...
mace/tools/git/gen_version_source.sh
浏览文件 @
bb10ae3b
...
...
@@ -13,12 +13,23 @@
# See the License for the specific language governing permissions and
# limitations under the License.
MACE_SOURCE_DIR
=
$(
dirname
$0
)
OUTPUT_FILENAME
=
$1
if
[[
-z
"
${
OUTPUT_FILENAME
}
}"
]]
;
then
echo
"Usage:
$0
<filename>"
exit
1
fi
OUTPUT_DIR
=
$(
dirname
$OUTPUT_FILENAME
)
if
[
-d
$OUTPUT_DIR
]
;
then
rm
-rf
$OUTPUT_DIR
fi
mkdir
-p
$OUTPUT_DIR
pushd
$MACE_SOURCE_DIR
DATE_STR
=
$(
date
+%Y%m%d
)
GIT_VERSION
=
$(
git describe
--long
--tags
)
if
[[
$?
!=
0
]]
;
then
...
...
@@ -49,3 +60,5 @@ __attribute__((visibility ("default")))
const char *MaceVersion() { return "MACEVER-
${
GIT_VERSION
}
" + 8; }
} // namespace mace
EOF
popd
mace/tools/validation/mace_run.cc
浏览文件 @
bb10ae3b
...
...
@@ -175,6 +175,9 @@ DEFINE_string(output_file,
DEFINE_string
(
opencl_binary_file
,
""
,
"compiled opencl binary file path"
);
DEFINE_string
(
opencl_parameter_file
,
""
,
"tuned OpenCL parameter file path"
);
DEFINE_string
(
model_data_file
,
""
,
"model data file name, used when EMBED_MODEL_DATA set to 0 or 2"
);
...
...
@@ -209,6 +212,8 @@ bool RunModel(const std::string &model_name,
std
::
vector
<
std
::
string
>
opencl_binary_paths
=
{
FLAGS_opencl_binary_file
};
mace
::
SetOpenCLBinaryPaths
(
opencl_binary_paths
);
mace
::
SetOpenCLParameterPath
(
FLAGS_opencl_parameter_file
);
}
#endif // MACE_ENABLE_OPENCL
...
...
mace/utils/BUILD
浏览文件 @
bb10ae3b
...
...
@@ -31,7 +31,6 @@ cc_library(
copts
=
[
"-Werror"
,
"-Wextra"
,
"-Wno-missing-field-initializers"
],
deps
=
[
"//mace/public"
,
"//mace/codegen:generated_tuning_params"
,
],
)
...
...
mace/utils/tuner.h
浏览文件 @
bb10ae3b
...
...
@@ -117,13 +117,28 @@ class Tuner {
}
inline
void
ReadRunParamters
()
{
extern
const
std
::
map
<
std
::
string
,
std
::
vector
<
unsigned
int
>>
kTuningParamsData
;
if
(
!
kTuningParamsData
.
empty
())
{
for
(
auto
it
=
kTuningParamsData
.
begin
();
it
!=
kTuningParamsData
.
end
();
++
it
)
{
param_table_
.
emplace
(
it
->
first
,
std
::
vector
<
unsigned
int
>
(
it
->
second
.
begin
(),
it
->
second
.
end
()));
extern
std
::
string
kOpenCLParameterPath
;
if
(
!
kOpenCLParameterPath
.
empty
())
{
std
::
ifstream
ifs
(
kOpenCLParameterPath
,
std
::
ios
::
binary
|
std
::
ios
::
in
);
if
(
ifs
.
is_open
())
{
int64_t
num_params
=
0
;
ifs
.
read
(
reinterpret_cast
<
char
*>
(
&
num_params
),
sizeof
(
num_params
));
while
(
num_params
--
)
{
int32_t
key_size
=
0
;
ifs
.
read
(
reinterpret_cast
<
char
*>
(
&
key_size
),
sizeof
(
key_size
));
std
::
string
key
(
key_size
,
' '
);
ifs
.
read
(
&
key
[
0
],
key_size
);
int32_t
params_size
=
0
;
ifs
.
read
(
reinterpret_cast
<
char
*>
(
&
params_size
),
sizeof
(
params_size
));
int32_t
params_count
=
params_size
/
sizeof
(
unsigned
int
);
std
::
vector
<
unsigned
int
>
params
(
params_count
);
for
(
int
i
=
0
;
i
<
params_count
;
++
i
)
{
ifs
.
read
(
reinterpret_cast
<
char
*>
(
&
params
[
i
]),
sizeof
(
unsigned
int
));
}
param_table_
.
emplace
(
key
,
params
);
}
ifs
.
close
();
}
}
else
{
LOG
(
INFO
)
<<
"There is no tuned parameters."
;
...
...
tools/bazel.rc
浏览文件 @
bb10ae3b
...
...
@@ -9,6 +9,7 @@ build --verbose_failures
build --copt=-std=c++11
build --copt=-D_GLIBCXX_USE_C99_MATH_TR1
build --copt=-DMACE_OBFUSCATE_LITERALS
build --define openmp=true
# Usage example: bazel build --config android
build:android --crosstool_top=//external:android/crosstool
...
...
tools/converter.py
浏览文件 @
bb10ae3b
...
...
@@ -53,7 +53,9 @@ BUILD_TMP_GENERAL_OUTPUT_DIR_NAME = 'general'
OUTPUT_LIBRARY_DIR_NAME
=
'lib'
OUTPUT_OPENCL_BINARY_DIR_NAME
=
'opencl'
OUTPUT_OPENCL_BINARY_FILE_NAME
=
'compiled_opencl_kernel'
OUTPUT_OPENCL_PARAMETER_FILE_NAME
=
'tuned_opencl_parameter'
CL_COMPILED_BINARY_FILE_NAME
=
"mace_cl_compiled_program.bin"
CL_TUNED_PARAMETER_FILE_NAME
=
"mace_run.config"
CODEGEN_BASE_DIR
=
'mace/codegen'
MODEL_CODEGEN_DIR
=
CODEGEN_BASE_DIR
+
'/models'
LIBMACE_SO_TARGET
=
"//mace:libmace.so"
...
...
@@ -505,6 +507,21 @@ def get_opencl_binary_output_path(library_name, target_abi,
target_soc
)
def
get_opencl_parameter_output_path
(
library_name
,
target_abi
,
target_soc
,
serial_num
):
device_name
=
\
sh_commands
.
adb_get_device_name_by_serialno
(
serial_num
)
return
'%s/%s/%s/%s/%s_%s.%s.%s.bin'
%
\
(
BUILD_OUTPUT_DIR
,
library_name
,
OUTPUT_OPENCL_BINARY_DIR_NAME
,
target_abi
,
library_name
,
OUTPUT_OPENCL_PARAMETER_FILE_NAME
,
device_name
,
target_soc
)
def
get_shared_library_dir
(
library_name
,
abi
):
return
'%s/%s/%s/%s'
%
(
BUILD_OUTPUT_DIR
,
library_name
,
...
...
@@ -689,7 +706,6 @@ def build_specific_lib(target_abi, target_soc, serial_num,
sh
.
rm
(
"-rf"
,
build_tmp_binary_dir
)
os
.
makedirs
(
build_tmp_binary_dir
)
sh_commands
.
gen_tuning_param_code
(
model_output_dirs
)
if
linkshared
==
0
:
mace_run_name
=
MACE_RUN_STATIC_NAME
mace_run_target
=
MACE_RUN_STATIC_TARGET
...
...
@@ -773,6 +789,7 @@ def build_specific_lib(target_abi, target_soc, serial_num,
phone_data_dir
=
PHONE_DATA_DIR
,
build_type
=
build_type
,
opencl_binary_file
=
""
,
opencl_parameter_file
=
""
,
shared_library_dir
=
get_shared_library_dir
(
library_name
,
target_abi
),
# noqa
linkshared
=
linkshared
,
)
...
...
@@ -786,10 +803,15 @@ def build_specific_lib(target_abi, target_soc, serial_num,
opencl_output_bin_path
=
get_opencl_binary_output_path
(
library_name
,
target_abi
,
target_soc
,
serial_num
)
opencl_parameter_bin_path
=
get_opencl_parameter_output_path
(
library_name
,
target_abi
,
target_soc
,
serial_num
)
sh_commands
.
merge_opencl_binaries
(
model_output_dirs
,
CL_COMPILED_BINARY_FILE_NAME
,
opencl_output_bin_path
)
sh_commands
.
gen_tuning_param_code
(
model_output_dirs
)
sh_commands
.
merge_opencl_parameters
(
model_output_dirs
,
CL_TUNED_PARAMETER_FILE_NAME
,
opencl_parameter_bin_path
)
sh_commands
.
bazel_build
(
mace_run_target
,
abi
=
target_abi
,
...
...
@@ -950,6 +972,7 @@ def run_specific_target(flags, configs, target_abi,
build_type
=
configs
[
YAMLKeyword
.
build_type
]
embed_model_data
=
configs
[
YAMLKeyword
.
embed_model_data
]
opencl_output_bin_path
=
""
opencl_parameter_path
=
""
linkshared
=
configs
[
YAMLKeyword
.
linkshared
]
if
not
configs
[
YAMLKeyword
.
target_socs
]
or
target_abi
==
ABIType
.
host
:
build_tmp_binary_dir
=
get_build_binary_dir
(
library_name
,
target_abi
,
...
...
@@ -960,6 +983,10 @@ def run_specific_target(flags, configs, target_abi,
opencl_output_bin_path
=
get_opencl_binary_output_path
(
library_name
,
target_abi
,
target_soc
,
serial_num
)
opencl_parameter_path
=
get_opencl_parameter_output_path
(
library_name
,
target_abi
,
target_soc
,
serial_num
)
mace_check
(
os
.
path
.
exists
(
build_tmp_binary_dir
),
ModuleName
.
RUN
,
'You should build before run.'
)
...
...
@@ -1051,6 +1078,7 @@ def run_specific_target(flags, configs, target_abi,
runtime_failure_ratio
=
flags
.
runtime_failure_ratio
,
address_sanitizer
=
flags
.
address_sanitizer
,
opencl_binary_file
=
opencl_output_bin_path
,
opencl_parameter_file
=
opencl_parameter_path
,
shared_library_dir
=
get_shared_library_dir
(
library_name
,
target_abi
),
# noqa
linkshared
=
linkshared
,
)
...
...
@@ -1114,6 +1142,7 @@ def bm_specific_target(flags, configs, target_abi, target_soc, serial_num):
build_type
=
configs
[
YAMLKeyword
.
build_type
]
embed_model_data
=
configs
[
YAMLKeyword
.
embed_model_data
]
opencl_output_bin_path
=
""
opencl_parameter_path
=
""
linkshared
=
configs
[
YAMLKeyword
.
linkshared
]
if
not
configs
[
YAMLKeyword
.
target_socs
]
or
target_abi
==
ABIType
.
host
:
build_tmp_binary_dir
=
get_build_binary_dir
(
library_name
,
target_abi
,
...
...
@@ -1124,6 +1153,9 @@ def bm_specific_target(flags, configs, target_abi, target_soc, serial_num):
opencl_output_bin_path
=
get_opencl_binary_output_path
(
library_name
,
target_abi
,
target_soc
,
serial_num
)
opencl_parameter_path
=
get_opencl_parameter_output_path
(
library_name
,
target_abi
,
target_soc
,
serial_num
)
mace_check
(
os
.
path
.
exists
(
build_tmp_binary_dir
),
ModuleName
.
BENCHMARK
,
'You should build before benchmark.'
)
...
...
@@ -1194,6 +1226,7 @@ def bm_specific_target(flags, configs, target_abi, target_soc, serial_num):
gpu_perf_hint
=
flags
.
gpu_perf_hint
,
gpu_priority_hint
=
flags
.
gpu_priority_hint
,
opencl_binary_file
=
opencl_output_bin_path
,
opencl_parameter_file
=
opencl_parameter_path
,
shared_library_dir
=
get_shared_library_dir
(
library_name
,
target_abi
),
# noqa
linkshared
=
linkshared
)
...
...
tools/sh_commands.py
浏览文件 @
bb10ae3b
...
...
@@ -454,6 +454,54 @@ def merge_opencl_binaries(binaries_dirs,
np
.
array
(
output_byte_array
).
tofile
(
output_file_path
)
def
merge_opencl_parameters
(
binaries_dirs
,
cl_parameter_file_name
,
output_file_path
):
cl_bin_dirs
=
[]
for
d
in
binaries_dirs
:
cl_bin_dirs
.
append
(
os
.
path
.
join
(
d
,
"opencl_bin"
))
# create opencl binary output dir
opencl_binary_dir
=
os
.
path
.
dirname
(
output_file_path
)
if
not
os
.
path
.
exists
(
opencl_binary_dir
):
sh
.
mkdir
(
"-p"
,
opencl_binary_dir
)
kvs
=
{}
for
binary_dir
in
cl_bin_dirs
:
binary_path
=
os
.
path
.
join
(
binary_dir
,
cl_parameter_file_name
)
if
not
os
.
path
.
exists
(
binary_path
):
continue
print
'generate opencl parameter from'
,
binary_path
with
open
(
binary_path
,
"rb"
)
as
f
:
binary_array
=
np
.
fromfile
(
f
,
dtype
=
np
.
uint8
)
idx
=
0
size
,
=
struct
.
unpack
(
"Q"
,
binary_array
[
idx
:
idx
+
8
])
idx
+=
8
for
_
in
xrange
(
size
):
key_size
,
=
struct
.
unpack
(
"i"
,
binary_array
[
idx
:
idx
+
4
])
idx
+=
4
key
,
=
struct
.
unpack
(
str
(
key_size
)
+
"s"
,
binary_array
[
idx
:
idx
+
key_size
])
idx
+=
key_size
value_size
,
=
struct
.
unpack
(
"i"
,
binary_array
[
idx
:
idx
+
4
])
idx
+=
4
kvs
[
key
]
=
binary_array
[
idx
:
idx
+
value_size
]
idx
+=
value_size
output_byte_array
=
bytearray
()
data_size
=
len
(
kvs
)
output_byte_array
.
extend
(
struct
.
pack
(
"Q"
,
data_size
))
for
key
,
value
in
kvs
.
iteritems
():
key_size
=
len
(
key
)
output_byte_array
.
extend
(
struct
.
pack
(
"i"
,
key_size
))
output_byte_array
.
extend
(
struct
.
pack
(
str
(
key_size
)
+
"s"
,
key
))
value_size
=
len
(
value
)
output_byte_array
.
extend
(
struct
.
pack
(
"i"
,
value_size
))
output_byte_array
.
extend
(
value
)
np
.
array
(
output_byte_array
).
tofile
(
output_file_path
)
def
gen_tuning_param_code
(
model_output_dirs
,
codegen_path
=
"mace/codegen"
):
mace_run_param_file
=
"mace_run.config"
...
...
@@ -646,6 +694,7 @@ def tuning_run(abi,
phone_data_dir
,
build_type
,
opencl_binary_file
,
opencl_parameter_file
,
shared_library_dir
,
omp_num_threads
=-
1
,
cpu_affinity_policy
=
1
,
...
...
@@ -713,9 +762,11 @@ def tuning_run(abi,
adb_push
(
"%s/%s.data"
%
(
mace_model_dir
,
model_tag
),
phone_data_dir
,
serialno
)
if
device_type
==
common
.
DeviceType
.
GPU
\
and
os
.
path
.
exists
(
opencl_binary_file
):
adb_push
(
opencl_binary_file
,
phone_data_dir
,
serialno
)
if
device_type
==
common
.
DeviceType
.
GPU
:
if
os
.
path
.
exists
(
opencl_binary_file
):
adb_push
(
opencl_binary_file
,
phone_data_dir
,
serialno
)
if
os
.
path
.
exists
(
opencl_parameter_file
):
adb_push
(
opencl_parameter_file
,
phone_data_dir
,
serialno
)
adb_push
(
"third_party/nnlib/libhexagon_controller.so"
,
phone_data_dir
,
serialno
)
...
...
@@ -774,6 +825,8 @@ def tuning_run(abi,
"--model_file=%s"
%
mace_model_phone_path
,
"--opencl_binary_file=%s/%s"
%
(
phone_data_dir
,
os
.
path
.
basename
(
opencl_binary_file
)),
"--opencl_parameter_file=%s/%s"
%
(
phone_data_dir
,
os
.
path
.
basename
(
opencl_parameter_file
)),
])
adb_cmd
=
' '
.
join
(
adb_cmd
)
cmd_file_name
=
"%s-%s-%s"
%
(
'cmd_file'
,
model_tag
,
str
(
time
.
time
()))
...
...
@@ -930,7 +983,6 @@ def build_host_libraries(model_build_type, abi):
bazel_build
(
"@com_google_protobuf//:protobuf_lite"
,
abi
=
abi
)
bazel_build
(
"//mace/proto:mace_cc"
,
abi
=
abi
)
bazel_build
(
"//mace/codegen:generated_opencl"
,
abi
=
abi
)
bazel_build
(
"//mace/codegen:generated_tuning_params"
,
abi
=
abi
)
bazel_build
(
"//mace/codegen:generated_version"
,
abi
=
abi
)
bazel_build
(
"//mace/utils:utils"
,
abi
=
abi
)
bazel_build
(
"//mace/core:core"
,
abi
=
abi
)
...
...
@@ -993,9 +1045,6 @@ def merge_libs(target_soc,
mri_stream
+=
(
"addlib "
"bazel-bin/mace/codegen/libgenerated_opencl.pic.a
\n
"
)
mri_stream
+=
(
"addlib "
"bazel-bin/mace/codegen/libgenerated_tuning_params.pic.a
\n
"
)
mri_stream
+=
(
"addlib "
"bazel-bin/mace/codegen/libgenerated_version.pic.a
\n
"
)
...
...
@@ -1030,9 +1079,6 @@ def merge_libs(target_soc,
mri_stream
+=
(
"addlib "
"bazel-bin/mace/codegen/libgenerated_opencl.a
\n
"
)
mri_stream
+=
(
"addlib "
"bazel-bin/mace/codegen/libgenerated_tuning_params.a
\n
"
)
mri_stream
+=
(
"addlib "
"bazel-bin/mace/codegen/libgenerated_version.a
\n
"
)
...
...
@@ -1189,6 +1235,7 @@ def benchmark_model(abi,
phone_data_dir
,
build_type
,
opencl_binary_file
,
opencl_parameter_file
,
shared_library_dir
,
omp_num_threads
=-
1
,
cpu_affinity_policy
=
1
,
...
...
@@ -1240,9 +1287,11 @@ def benchmark_model(abi,
if
not
embed_model_data
:
adb_push
(
"%s/%s.data"
%
(
mace_model_dir
,
model_tag
),
phone_data_dir
,
serialno
)
if
device_type
==
common
.
DeviceType
.
GPU
\
and
os
.
path
.
exists
(
opencl_binary_file
):
adb_push
(
opencl_binary_file
,
phone_data_dir
,
serialno
)
if
device_type
==
common
.
DeviceType
.
GPU
:
if
os
.
path
.
exists
(
opencl_binary_file
):
adb_push
(
opencl_binary_file
,
phone_data_dir
,
serialno
)
if
os
.
path
.
exists
(
opencl_parameter_file
):
adb_push
(
opencl_parameter_file
,
phone_data_dir
,
serialno
)
mace_model_phone_path
=
""
if
build_type
==
BuildType
.
proto
:
mace_model_phone_path
=
"%s/%s.pb"
%
(
phone_data_dir
,
model_tag
)
...
...
@@ -1283,6 +1332,8 @@ def benchmark_model(abi,
"--model_file=%s"
%
mace_model_phone_path
,
"--opencl_binary_file=%s/%s"
%
(
phone_data_dir
,
os
.
path
.
basename
(
opencl_binary_file
)),
"--opencl_parameter_file=%s/%s"
%
(
phone_data_dir
,
os
.
path
.
basename
(
opencl_parameter_file
)),
]
adb_cmd
=
' '
.
join
(
adb_cmd
)
cmd_file_name
=
"%s-%s-%s"
%
(
'cmd_file'
,
model_tag
,
str
(
time
.
time
()))
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录