Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
慢慢CG
Mace
提交
5553184f
Mace
项目概览
慢慢CG
/
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看板
提交
5553184f
编写于
1月 02, 2018
作者:
Y
yejianwu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
encrypt cl source, extract embed code for tuning and get opencl program
上级
b6ad52d6
变更
15
隐藏空白更改
内联
并排
Showing
15 changed file
with
332 addition
and
153 deletion
+332
-153
mace/codegen/BUILD
mace/codegen/BUILD
+4
-7
mace/core/BUILD
mace/core/BUILD
+2
-3
mace/core/runtime/opencl/opencl_runtime.cc
mace/core/runtime/opencl/opencl_runtime.cc
+61
-54
mace/core/runtime/opencl/opencl_runtime.h
mace/core/runtime/opencl/opencl_runtime.h
+1
-0
mace/examples/BUILD
mace/examples/BUILD
+1
-1
mace/kernels/BUILD
mace/kernels/BUILD
+1
-2
mace/mace.bzl
mace/mace.bzl
+0
-6
mace/python/tools/binary.cc.tmpl
mace/python/tools/binary.cc.tmpl
+0
-27
mace/python/tools/binary_codegen.py
mace/python/tools/binary_codegen.py
+4
-4
mace/python/tools/embed_code.cc.tmpl
mace/python/tools/embed_code.cc.tmpl
+111
-0
mace/python/tools/encrypt_opencl_codegen.py
mace/python/tools/encrypt_opencl_codegen.py
+81
-0
mace/python/tools/opencl_codegen.py
mace/python/tools/opencl_codegen.py
+3
-3
mace/python/tools/read_tuning_codegen.py
mace/python/tools/read_tuning_codegen.py
+40
-0
mace/utils/tuner.h
mace/utils/tuner.h
+6
-33
tools/validate_gcn.sh
tools/validate_gcn.sh
+17
-13
未找到文件。
mace/codegen/BUILD
浏览文件 @
5553184f
...
@@ -5,8 +5,6 @@ package(
...
@@ -5,8 +5,6 @@ package(
default_visibility
=
[
"//visibility:public"
],
default_visibility
=
[
"//visibility:public"
],
)
)
load
(
"//mace:mace.bzl"
,
"if_embed_binary_program"
)
cc_library
(
cc_library
(
name
=
"generated_models_lib"
,
name
=
"generated_models_lib"
,
srcs
=
glob
([
"models/*/*.cc"
]),
srcs
=
glob
([
"models/*/*.cc"
]),
...
@@ -15,11 +13,10 @@ cc_library(
...
@@ -15,11 +13,10 @@ cc_library(
deps
=
[
deps
=
[
"//mace/core"
,
"//mace/core"
,
"//mace/ops"
,
"//mace/ops"
,
'//mace/codegen:version_lib'
,
"//mace/codegen:version_lib"
,
]
+
if_embed_binary_program
([
"//mace/codegen:generated_opencl_lib"
,
'//mace/codegen:generated_opencl_lib'
,
"//mace/codegen:generated_tuning_lib"
,
'//mace/codegen:generated_tuning_lib'
,
],
]),
)
)
cc_library
(
cc_library
(
...
...
mace/core/BUILD
浏览文件 @
5553184f
...
@@ -7,7 +7,7 @@ package(
...
@@ -7,7 +7,7 @@ package(
licenses
([
"notice"
])
# Apache 2.0
licenses
([
"notice"
])
# Apache 2.0
load
(
"//mace:mace.bzl"
,
"if_android"
,
"if_profiling_enabled"
,
"if_embed_binary_program"
)
load
(
"//mace:mace.bzl"
,
"if_android"
,
"if_profiling_enabled"
)
cc_library
(
cc_library
(
name
=
"core"
,
name
=
"core"
,
...
@@ -22,8 +22,7 @@ cc_library(
...
@@ -22,8 +22,7 @@ cc_library(
"runtime/opencl/*.h"
,
"runtime/opencl/*.h"
,
]),
]),
copts
=
[
"-std=c++11"
,
"-D_GLIBCXX_USE_C99_MATH_TR1"
,
"-Werror=return-type"
]
+
copts
=
[
"-std=c++11"
,
"-D_GLIBCXX_USE_C99_MATH_TR1"
,
"-Werror=return-type"
]
+
if_profiling_enabled
([
"-DMACE_OPENCL_PROFILING"
])
+
if_profiling_enabled
([
"-DMACE_OPENCL_PROFILING"
]),
if_embed_binary_program
([
"-DMACE_EMBED_BINARY_PROGRAM"
]),
linkopts
=
if_android
([
"-pie"
,
"-ldl"
]),
linkopts
=
if_android
([
"-pie"
,
"-ldl"
]),
deps
=
[
deps
=
[
"//mace/utils:utils_hdrs"
,
"//mace/utils:utils_hdrs"
,
...
...
mace/core/runtime/opencl/opencl_runtime.cc
浏览文件 @
5553184f
...
@@ -174,40 +174,48 @@ std::string OpenCLRuntime::GenerateCLBinaryFilenamePrefix(
...
@@ -174,40 +174,48 @@ std::string OpenCLRuntime::GenerateCLBinaryFilenamePrefix(
return
filename_prefix
;
return
filename_prefix
;
}
}
void
OpenCLRuntime
::
BuildProgram
(
const
std
::
string
&
program_file_name
,
extern
bool
GetOpenCLProgram
(
const
std
::
string
&
program_name
,
const
std
::
string
&
binary_file_name_prefix
,
std
::
vector
<
unsigned
char
>
*
program_vec
,
bool
*
is_opencl_binary
);
const
std
::
vector
<
unsigned
char
>
OpenCLRuntime
::
DecryptOpenCLSource
(
const
std
::
vector
<
unsigned
char
>
&
src
)
{
std
::
vector
<
unsigned
char
>
res
;
res
.
reserve
(
src
.
size
());
std
::
string
decrypt_lookup_table
=
"Xiaomi-AI-Platform-Mace"
;
size_t
lookup_table_size
=
decrypt_lookup_table
.
size
();
for
(
int
i
=
0
;
i
<
src
.
size
();
i
++
)
{
res
.
push_back
(
src
[
i
]
^
decrypt_lookup_table
[
i
%
lookup_table_size
]);
}
return
res
;
}
void
OpenCLRuntime
::
BuildProgram
(
const
std
::
string
&
program_name
,
const
std
::
string
&
binary_file_name_prefix
,
const
std
::
string
&
binary_file_name_prefix
,
const
std
::
string
&
build_options
,
const
std
::
string
&
build_options
,
cl
::
Program
*
program
)
{
cl
::
Program
*
program
)
{
MACE_CHECK_NOTNULL
(
program
);
MACE_CHECK_NOTNULL
(
program
);
#ifdef MACE_EMBED_BINARY_PROGRAM
bool
is_opencl_binary
=
false
;
extern
const
std
::
map
<
std
::
string
,
std
::
vector
<
unsigned
char
>>
kCompiledProgramMap
;
std
::
vector
<
unsigned
char
>
program_vec
;
VLOG
(
1
)
<<
"Create program with merged binary map"
;
const
bool
is_success
=
GetOpenCLProgram
(
program_name
,
auto
it_binary
=
kCompiledProgramMap
.
find
(
binary_file_name_prefix
);
binary_file_name_prefix
,
if
(
it_binary
==
kCompiledProgramMap
.
end
())
{
&
program_vec
,
LOG
(
FATAL
)
<<
"Cannot found the binary key '"
<<
binary_file_name_prefix
&
is_opencl_binary
);
<<
"' in kCompiledProgramMap"
;
MACE_CHECK
(
is_success
,
"Failed in GetOpenCLProgram!"
);
}
if
(
is_opencl_binary
)
{
std
::
vector
<
unsigned
char
>
binary
=
it_binary
->
second
;
*
program
=
cl
::
Program
(
this
->
context
(),
{
device
()},
{
program_vec
});
*
program
=
cl
::
Program
(
this
->
context
(),
{
device
()},
{
binary
});
VLOG
(
1
)
<<
"Use opencl binaries"
;
#else
}
else
{
std
::
string
source_filename
=
kernel_path_
+
program_file_name
;
// Create program
if
(
std
::
ifstream
(
source_filename
).
is_open
())
{
VLOG
(
1
)
<<
"Create program with source: "
<<
source_filename
;
std
::
vector
<
unsigned
char
>
kernel_source
;
MACE_CHECK
(
ReadFile
(
source_filename
,
false
,
&
kernel_source
));
cl
::
Program
::
Sources
sources
;
cl
::
Program
::
Sources
sources
;
sources
.
push_back
(
std
::
string
(
kernel_source
.
begin
(),
kernel_source
.
end
()));
const
std
::
vector
<
unsigned
char
>
decrypt_source
=
DecryptOpenCLSource
(
program_vec
);
sources
.
push_back
(
std
::
string
(
decrypt_source
.
begin
(),
decrypt_source
.
end
()));
*
program
=
cl
::
Program
(
this
->
context
(),
sources
);
*
program
=
cl
::
Program
(
this
->
context
(),
sources
);
VLOG
(
1
)
<<
"Use opencl sources"
;
}
else
{
LOG
(
FATAL
)
<<
"Failed to open kernel file "
<<
source_filename
;
}
}
#endif
// Build program
// Build program
std
::
string
build_options_str
=
std
::
string
build_options_str
=
...
@@ -225,41 +233,40 @@ void OpenCLRuntime::BuildProgram(const std::string &program_file_name,
...
@@ -225,41 +233,40 @@ void OpenCLRuntime::BuildProgram(const std::string &program_file_name,
LOG
(
FATAL
)
<<
"Build program failed: "
<<
ret
;
LOG
(
FATAL
)
<<
"Build program failed: "
<<
ret
;
}
}
#ifndef MACE_EMBED_BINARY_PROGRAM
if
(
!
is_opencl_binary
)
{
// Write binary if necessary
// Write binary if necessary
std
::
string
binary_filename
=
kernel_path_
+
binary_file_name_prefix
+
".bin"
;
std
::
string
binary_filename
=
kernel_path_
+
binary_file_name_prefix
+
".bin"
;
size_t
device_list_size
=
1
;
size_t
device_list_size
=
1
;
std
::
unique_ptr
<
size_t
[]
>
program_binary_sizes
(
std
::
unique_ptr
<
size_t
[]
>
program_binary_sizes
(
new
size_t
[
device_list_size
]);
new
size_t
[
device_list_size
]);
cl_int
err
=
clGetProgramInfo
((
*
program
)(),
CL_PROGRAM_BINARY_SIZES
,
cl_int
err
=
clGetProgramInfo
((
*
program
)(),
CL_PROGRAM_BINARY_SIZES
,
sizeof
(
size_t
)
*
device_list_size
,
sizeof
(
size_t
)
*
device_list_size
,
program_binary_sizes
.
get
(),
nullptr
);
program_binary_sizes
.
get
(),
nullptr
);
MACE_CHECK
(
err
==
CL_SUCCESS
)
<<
"Error code: "
<<
err
;
MACE_CHECK
(
err
==
CL_SUCCESS
)
<<
"Error code: "
<<
err
;
std
::
unique_ptr
<
std
::
unique_ptr
<
unsigned
char
[]
>
[]
>
program_binaries
(
std
::
unique_ptr
<
std
::
unique_ptr
<
unsigned
char
[]
>
[]
>
program_binaries
(
new
std
::
unique_ptr
<
unsigned
char
[]
>
[
device_list_size
]);
new
std
::
unique_ptr
<
unsigned
char
[]
>
[
device_list_size
]);
for
(
cl_uint
i
=
0
;
i
<
device_list_size
;
++
i
)
{
for
(
cl_uint
i
=
0
;
i
<
device_list_size
;
++
i
)
{
program_binaries
[
i
]
=
std
::
unique_ptr
<
unsigned
char
[]
>
(
program_binaries
[
i
]
=
std
::
unique_ptr
<
unsigned
char
[]
>
(
new
unsigned
char
[
program_binary_sizes
[
i
]]);
new
unsigned
char
[
program_binary_sizes
[
i
]]);
}
}
err
=
clGetProgramInfo
((
*
program
)(),
CL_PROGRAM_BINARIES
,
err
=
clGetProgramInfo
((
*
program
)(),
CL_PROGRAM_BINARIES
,
sizeof
(
unsigned
char
*
)
*
device_list_size
,
sizeof
(
unsigned
char
*
)
*
device_list_size
,
program_binaries
.
get
(),
nullptr
);
program_binaries
.
get
(),
nullptr
);
MACE_CHECK
(
err
==
CL_SUCCESS
)
<<
"Error code: "
<<
err
;
MACE_CHECK
(
err
==
CL_SUCCESS
)
<<
"Error code: "
<<
err
;
std
::
vector
<
unsigned
char
>
content
(
std
::
vector
<
unsigned
char
>
content
(
reinterpret_cast
<
unsigned
char
const
*>
(
program_binaries
[
0
].
get
()),
reinterpret_cast
<
unsigned
char
const
*>
(
program_binaries
[
0
].
get
()),
reinterpret_cast
<
unsigned
char
const
*>
(
program_binaries
[
0
].
get
())
+
reinterpret_cast
<
unsigned
char
const
*>
(
program_binaries
[
0
].
get
())
+
program_binary_sizes
[
0
]);
program_binary_sizes
[
0
]);
MACE_CHECK
(
WriteFile
(
binary_filename
,
true
,
content
));
MACE_CHECK
(
WriteFile
(
binary_filename
,
true
,
content
));
#endif
}
}
}
cl
::
Kernel
OpenCLRuntime
::
BuildKernel
(
cl
::
Kernel
OpenCLRuntime
::
BuildKernel
(
const
std
::
string
&
program_name
,
const
std
::
string
&
program_name
,
const
std
::
string
&
kernel_name
,
const
std
::
string
&
kernel_name
,
const
std
::
set
<
std
::
string
>
&
build_options
)
{
const
std
::
set
<
std
::
string
>
&
build_options
)
{
std
::
string
program_file_name
=
program_name
+
".cl"
;
std
::
string
build_options_str
;
std
::
string
build_options_str
;
for
(
auto
&
option
:
build_options
)
{
for
(
auto
&
option
:
build_options
)
{
build_options_str
+=
" "
+
option
;
build_options_str
+=
" "
+
option
;
...
@@ -274,7 +281,7 @@ cl::Kernel OpenCLRuntime::BuildKernel(
...
@@ -274,7 +281,7 @@ cl::Kernel OpenCLRuntime::BuildKernel(
}
else
{
}
else
{
std
::
string
binary_file_name_prefix
=
std
::
string
binary_file_name_prefix
=
GenerateCLBinaryFilenamePrefix
(
built_program_key
);
GenerateCLBinaryFilenamePrefix
(
built_program_key
);
this
->
BuildProgram
(
program_
file_
name
,
binary_file_name_prefix
,
this
->
BuildProgram
(
program_name
,
binary_file_name_prefix
,
build_options_str
,
&
program
);
build_options_str
,
&
program
);
built_program_map_
.
emplace
(
built_program_key
,
program
);
built_program_map_
.
emplace
(
built_program_key
,
program
);
}
}
...
...
mace/core/runtime/opencl/opencl_runtime.h
浏览文件 @
5553184f
...
@@ -55,6 +55,7 @@ class OpenCLRuntime {
...
@@ -55,6 +55,7 @@ class OpenCLRuntime {
const
std
::
string
&
build_options
,
const
std
::
string
&
build_options
,
cl
::
Program
*
program
);
cl
::
Program
*
program
);
std
::
string
GenerateCLBinaryFilenamePrefix
(
const
std
::
string
&
filename_msg
);
std
::
string
GenerateCLBinaryFilenamePrefix
(
const
std
::
string
&
filename_msg
);
const
std
::
vector
<
unsigned
char
>
DecryptOpenCLSource
(
const
std
::
vector
<
unsigned
char
>
&
src
);
private:
private:
// All OpenCL object must be a pointer and manually deleted before unloading
// All OpenCL object must be a pointer and manually deleted before unloading
...
...
mace/examples/BUILD
浏览文件 @
5553184f
# Examples
# Examples
load
(
"//mace:mace.bzl"
,
"if_android"
,
"if_enable_neon"
,
"if_embed_binary_program"
)
load
(
"//mace:mace.bzl"
,
"if_android"
,
"if_enable_neon"
)
cc_binary
(
cc_binary
(
name
=
"helloworld"
,
name
=
"helloworld"
,
...
...
mace/kernels/BUILD
浏览文件 @
5553184f
...
@@ -7,7 +7,7 @@ package(
...
@@ -7,7 +7,7 @@ package(
licenses
([
"notice"
])
# Apache 2.0
licenses
([
"notice"
])
# Apache 2.0
load
(
"//mace:mace.bzl"
,
"if_android"
,
"if_enable_neon"
,
"if_embed_binary_program"
)
load
(
"//mace:mace.bzl"
,
"if_android"
,
"if_enable_neon"
)
cc_library
(
cc_library
(
name
=
"kernels"
,
name
=
"kernels"
,
...
@@ -22,7 +22,6 @@ cc_library(
...
@@ -22,7 +22,6 @@ cc_library(
"-D_GLIBCXX_USE_C99_MATH_TR1"
,
"-D_GLIBCXX_USE_C99_MATH_TR1"
,
"-Werror=return-type"
,
"-Werror=return-type"
,
]
+
]
+
if_embed_binary_program
([
"-DMACE_EMBED_BINARY_PROGRAM"
])
+
if_enable_neon
([
"-fopenmp"
]),
if_enable_neon
([
"-fopenmp"
]),
linkopts
=
if_android
([
"-lm"
]),
linkopts
=
if_android
([
"-lm"
]),
deps
=
[
deps
=
[
...
...
mace/mace.bzl
浏览文件 @
5553184f
...
@@ -30,12 +30,6 @@ def if_profiling_enabled(a):
...
@@ -30,12 +30,6 @@ def if_profiling_enabled(a):
"//conditions:default"
:
[],
"//conditions:default"
:
[],
})
})
def
if_embed_binary_program
(
a
):
return
select
({
"//mace:embed_binary_program"
:
a
,
"//conditions:default"
:
[],
})
def
if_enable_neon
(
a
):
def
if_enable_neon
(
a
):
return
select
({
return
select
({
"//mace:enable_neon"
:
a
,
"//mace:enable_neon"
:
a
,
...
...
mace/python/tools/binary.cc.tmpl
已删除
100644 → 0
浏览文件 @
b6ad52d6
//
// Copyright (c) 2017 XiaoMi All rights reserved.
//
// This is a generated file, DO NOT EDIT
#include <map>
#include <string>
#include <vector>
namespace mace {
extern const std::map<std::string, std::vector<{{data_type}}>> {{variable_name}}=
{
{% for key, value in maps.iteritems() %}
{
"{{key}}",
{
{%- for ele in value -%}
{{ele}},
{%- endfor -%}
}
}, // {{key}}
{% endfor %}
};
} // namespace
mace/python/tools/binary_codegen.py
浏览文件 @
5553184f
...
@@ -38,14 +38,14 @@ def generate_cpp_source():
...
@@ -38,14 +38,14 @@ def generate_cpp_source():
idx
+=
params_size
idx
+=
params_size
env
=
jinja2
.
Environment
(
loader
=
jinja2
.
FileSystemLoader
(
sys
.
path
[
0
]))
env
=
jinja2
.
Environment
(
loader
=
jinja2
.
FileSystemLoader
(
sys
.
path
[
0
]))
return
env
.
get_template
(
'
binary
.cc.tmpl'
).
render
(
return
env
.
get_template
(
'
embed_code
.cc.tmpl'
).
render
(
maps
=
data_map
,
maps
=
data_map
,
data_type
=
'int'
,
data_type
=
'unsigned int'
,
variable_name
=
FLAGS
.
variable_name
variable_name
=
FLAGS
.
variable_name
,
mode
=
"tuning_binary"
)
)
def
main
(
unused_args
):
def
main
(
unused_args
):
cpp_binary_source
=
generate_cpp_source
()
cpp_binary_source
=
generate_cpp_source
()
if
os
.
path
.
isfile
(
FLAGS
.
output_path
):
if
os
.
path
.
isfile
(
FLAGS
.
output_path
):
os
.
remove
(
FLAGS
.
output_path
)
os
.
remove
(
FLAGS
.
output_path
)
...
...
mace/python/tools/embed_code.cc.tmpl
0 → 100644
浏览文件 @
5553184f
//
// Copyright (c) 2017 XiaoMi All rights reserved.
//
// This is a generated file, DO NOT EDIT
{% if mode == "read_tuning_config" %}
#include <fstream>
#include <string>
#include <vector>
#include <unordered_map>
namespace mace {
bool GetTuningParams(const char *path,
std::unordered_map<std::string, std::vector<{{data_type}}>> *param_table) {
if (path != nullptr) {
std::ifstream ifs(path, std::ios::binary | std::ios::in);
if (ifs.is_open()) {
int32_t key_size = 0;
int32_t params_size = 0;
int32_t params_count = 0;
int64_t num_pramas = 0;
ifs.read(reinterpret_cast<char *>(&num_pramas), sizeof(num_pramas));
while (num_pramas--) {
ifs.read(reinterpret_cast<char *>(&key_size), sizeof(key_size));
std::string key(key_size, ' ');
ifs.read(&key[0], key_size);
ifs.read(reinterpret_cast<char *>(¶ms_size), sizeof(params_size));
params_count = params_size / sizeof({{data_type}});
std::vector<{{data_type}}> params(params_count);
for (int i = 0; i < params_count; ++i) {
ifs.read(reinterpret_cast<char *>(¶ms[i]), sizeof({{data_type}}));
}
param_table->emplace(key, params);
}
ifs.close();
} else {
return false;
}
}
return true;
}
} // namespace
{% else %}
#include <map>
#include <string>
#include <vector>
{% if mode == "tuning_binary" %}
#include <unordered_map>
{% endif %}
namespace mace {
extern const std::map<std::string, std::vector<{{data_type}}>> {{variable_name}}=
{
{% for key, value in maps.iteritems() %}
{
"{{key}}",
{
{%- for ele in value -%}
{{ele}},
{%- endfor -%}
}
}, // {{key}}
{% endfor %}
};
{% if mode == "cl_encrypt" %}
bool GetOpenCLProgram(const std::string &program_name,
const std::string &binary_file_name_prefix,
std::vector<unsigned char> *program_vec,
bool *is_binary) {
*is_binary = false;
auto it_source = {{variable_name}}.find(program_name);
if (it_source == {{variable_name}}.end()) {
return false;
}
*program_vec = it_source->second;
return true;
}
{% elif mode == "cl_binary" %}
bool GetOpenCLProgram(const std::string &program_name,
const std::string &binary_file_name_prefix,
std::vector<unsigned char> *program_vec,
bool *is_binary) {
*is_binary = true;
auto it_source = {{variable_name}}.find(binary_file_name_prefix);
if (it_source == {{variable_name}}.end()) {
return false;
}
*program_vec = it_source->second;
return true;
}
{% elif mode == "tuning_binary" %}
bool GetTuningParams(const char *path,
std::unordered_map<std::string, std::vector<{{data_type}}>> *param_table) {
for (auto it = kTuningParamsData.begin(); it != kTuningParamsData.end(); ++it) {
param_table->emplace(it->first, std::vector<{{data_type}}>(it->second.begin(), it->second.end()));
}
return true;
}
{% endif %}
} // namespace
{% endif %}
mace/python/tools/encrypt_opencl_codegen.py
0 → 100644
浏览文件 @
5553184f
import
argparse
import
os
import
sys
import
jinja2
# python encrypt_opencl_codegen.py --cl_kernel_dir=./mace/kernels/opencl/cl/ \
# --output_path=./mace/codegen/opencl_encrypt/opencl_encrypted_program.cc
FLAGS
=
None
encrypt_lookup_table
=
"Xiaomi-AI-Platform-Mace"
def
encrypt_code
(
code_str
):
encrypted_arr
=
[]
for
i
in
range
(
len
(
code_str
)):
encrypted_char
=
hex
(
ord
(
code_str
[
i
])
^
ord
(
encrypt_lookup_table
[
i
%
len
(
encrypt_lookup_table
)]))
encrypted_arr
.
append
(
encrypted_char
)
return
encrypted_arr
def
main
(
unused_args
):
if
not
os
.
path
.
exists
(
FLAGS
.
cl_kernel_dir
):
print
(
"Input cl_kernel_dir "
+
FLAGS
.
cl_kernel_dir
+
" doesn't exist!"
)
header_code
=
""
for
file_name
in
os
.
listdir
(
FLAGS
.
cl_kernel_dir
):
file_path
=
os
.
path
.
join
(
FLAGS
.
cl_kernel_dir
,
file_name
)
if
file_path
[
-
2
:]
==
".h"
:
f
=
open
(
file_path
,
"r"
)
header_code
+=
f
.
read
()
encrypted_code_maps
=
{}
for
file_name
in
os
.
listdir
(
FLAGS
.
cl_kernel_dir
):
file_path
=
os
.
path
.
join
(
FLAGS
.
cl_kernel_dir
,
file_name
)
if
file_path
[
-
3
:]
==
".cl"
:
f
=
open
(
file_path
,
"r"
)
code_str
=
""
for
line
in
f
.
readlines
():
if
"#include <common.h>"
in
line
:
code_str
+=
header_code
else
:
code_str
+=
line
encrypted_code_arr
=
encrypt_code
(
code_str
)
encrypted_code_maps
[
file_name
[:
-
3
]]
=
encrypted_code_arr
env
=
jinja2
.
Environment
(
loader
=
jinja2
.
FileSystemLoader
(
sys
.
path
[
0
]))
cpp_cl_encrypted_kernel
=
env
.
get_template
(
'embed_code.cc.tmpl'
).
render
(
maps
=
encrypted_code_maps
,
data_type
=
'unsigned char'
,
variable_name
=
'kEncryptedProgramMap'
,
mode
=
'cl_encrypt'
)
if
os
.
path
.
isfile
(
FLAGS
.
output_path
):
os
.
remove
(
FLAGS
.
output_path
)
w_file
=
open
(
FLAGS
.
output_path
,
"w"
)
w_file
.
write
(
cpp_cl_encrypted_kernel
)
w_file
.
close
()
print
(
"Generate encrypted opencl source done!"
)
def
parse_args
():
"""Parses command line arguments."""
parser
=
argparse
.
ArgumentParser
()
parser
.
add_argument
(
"--cl_kernel_dir"
,
type
=
str
,
default
=
"./mace/kernels/opencl/cl/"
,
help
=
"The cl kernels directory."
)
parser
.
add_argument
(
"--output_path"
,
type
=
str
,
default
=
"./mace/examples/codegen/opencl/opencl_encrypted_program.cc"
,
help
=
"The path of encrypted opencl kernels."
)
return
parser
.
parse_known_args
()
if
__name__
==
'__main__'
:
FLAGS
,
unparsed
=
parse_args
()
main
(
unused_args
=
[
sys
.
argv
[
0
]]
+
unparsed
)
mace/python/tools/opencl_codegen.py
浏览文件 @
5553184f
import
argparse
import
argparse
import
os
import
os
import
subprocess
import
sys
import
sys
import
numpy
as
np
import
numpy
as
np
...
@@ -28,10 +27,11 @@ def generate_cpp_source():
...
@@ -28,10 +27,11 @@ def generate_cpp_source():
maps
[
file_name
[:
-
4
]].
append
(
hex
(
ele
))
maps
[
file_name
[:
-
4
]].
append
(
hex
(
ele
))
env
=
jinja2
.
Environment
(
loader
=
jinja2
.
FileSystemLoader
(
sys
.
path
[
0
]))
env
=
jinja2
.
Environment
(
loader
=
jinja2
.
FileSystemLoader
(
sys
.
path
[
0
]))
return
env
.
get_template
(
'
binary
.cc.tmpl'
).
render
(
return
env
.
get_template
(
'
embed_code
.cc.tmpl'
).
render
(
maps
=
maps
,
maps
=
maps
,
data_type
=
'unsigned char'
,
data_type
=
'unsigned char'
,
variable_name
=
'kCompiledProgramMap'
variable_name
=
'kCompiledProgramMap'
,
mode
=
"cl_binary"
)
)
...
...
mace/python/tools/read_tuning_codegen.py
0 → 100644
浏览文件 @
5553184f
import
argparse
import
os
import
sys
import
jinja2
# python mace/python/tools/read_tuning_codegen.py \
# --output_path=./mace/codegen/tuning/read_tuning_params.cc
FLAGS
=
None
def
main
(
unused_args
):
env
=
jinja2
.
Environment
(
loader
=
jinja2
.
FileSystemLoader
(
sys
.
path
[
0
]))
cpp_cl_encrypted_kernel
=
env
.
get_template
(
'embed_code.cc.tmpl'
).
render
(
data_type
=
'unsigned int'
,
mode
=
'read_tuning_config'
)
if
os
.
path
.
isfile
(
FLAGS
.
output_path
):
os
.
remove
(
FLAGS
.
output_path
)
w_file
=
open
(
FLAGS
.
output_path
,
"w"
)
w_file
.
write
(
cpp_cl_encrypted_kernel
)
w_file
.
close
()
print
(
"Generate reading tuning method done!"
)
def
parse_args
():
"""Parses command line arguments."""
parser
=
argparse
.
ArgumentParser
()
parser
.
add_argument
(
"--output_path"
,
type
=
str
,
default
=
"./mace/examples/codegen/tuning/read_tuning_params.cc"
,
help
=
"The path of codes to read tuning params."
)
return
parser
.
parse_known_args
()
if
__name__
==
'__main__'
:
FLAGS
,
unparsed
=
parse_args
()
main
(
unused_args
=
[
sys
.
argv
[
0
]]
+
unparsed
)
mace/utils/tuner.h
浏览文件 @
5553184f
...
@@ -18,6 +18,9 @@
...
@@ -18,6 +18,9 @@
namespace
mace
{
namespace
mace
{
extern
bool
GetTuningParams
(
const
char
*
path
,
std
::
unordered_map
<
std
::
string
,
std
::
vector
<
unsigned
int
>>
*
param_table
);
template
<
typename
param_type
>
template
<
typename
param_type
>
class
Tuner
{
class
Tuner
{
public:
public:
...
@@ -99,40 +102,10 @@ class Tuner {
...
@@ -99,40 +102,10 @@ class Tuner {
}
}
inline
void
ReadRunParamters
()
{
inline
void
ReadRunParamters
()
{
#ifdef MACE_EMBED_BINARY_PROGRAM
bool
success
=
GetTuningParams
(
path_
,
&
param_table_
);
extern
const
std
::
map
<
std
::
string
,
std
::
vector
<
int
>>
kTuningParamsData
;
if
(
!
success
)
{
VLOG
(
1
)
<<
"Read tuning parameters from source"
;
LOG
(
WARNING
)
<<
"Read run parameter failed."
;
for
(
auto
it
=
kTuningParamsData
.
begin
();
it
!=
kTuningParamsData
.
end
();
++
it
)
{
param_table_
.
emplace
(
it
->
first
,
std
::
vector
<
param_type
>
(
it
->
second
.
begin
(),
it
->
second
.
end
()));
}
#else
if
(
path_
!=
nullptr
)
{
std
::
ifstream
ifs
(
path_
,
std
::
ios
::
binary
|
std
::
ios
::
in
);
if
(
ifs
.
is_open
())
{
int32_t
key_size
=
0
;
int32_t
params_size
=
0
;
int32_t
params_count
=
0
;
int64_t
num_pramas
=
0
;
ifs
.
read
(
reinterpret_cast
<
char
*>
(
&
num_pramas
),
sizeof
(
num_pramas
));
while
(
num_pramas
--
)
{
ifs
.
read
(
reinterpret_cast
<
char
*>
(
&
key_size
),
sizeof
(
key_size
));
std
::
string
key
(
key_size
,
' '
);
ifs
.
read
(
&
key
[
0
],
key_size
);
ifs
.
read
(
reinterpret_cast
<
char
*>
(
&
params_size
),
sizeof
(
params_size
));
params_count
=
params_size
/
sizeof
(
param_type
);
std
::
vector
<
param_type
>
params
(
params_count
);
for
(
int
i
=
0
;
i
<
params_count
;
++
i
)
{
ifs
.
read
(
reinterpret_cast
<
char
*>
(
&
params
[
i
]),
sizeof
(
param_type
));
}
param_table_
.
emplace
(
key
,
params
);
}
ifs
.
close
();
}
else
{
LOG
(
WARNING
)
<<
"Read run parameter file failed."
;
}
}
}
#endif
}
}
template
<
typename
RetType
>
template
<
typename
RetType
>
...
...
tools/validate_gcn.sh
浏览文件 @
5553184f
...
@@ -35,9 +35,6 @@ VERSION_SOURCE_PATH=${CODEGEN_DIR}/version
...
@@ -35,9 +35,6 @@ VERSION_SOURCE_PATH=${CODEGEN_DIR}/version
build_and_run
()
build_and_run
()
{
{
EMBED_OPENCL_BINARY
=
$1
EMBED_OPENCL_BINARY
=
$1
if
[
"
$EMBED_OPENCL_BINARY
"
=
true
]
;
then
EMBED_OPENCL_BINARY_BUILD_FLAGS
=
"--define embed_binary_program=true"
fi
bazel build
-c
opt
--strip
always mace/examples:mace_run
\
bazel build
-c
opt
--strip
always mace/examples:mace_run
\
--crosstool_top
=
//external:android/crosstool
\
--crosstool_top
=
//external:android/crosstool
\
...
@@ -47,10 +44,6 @@ build_and_run()
...
@@ -47,10 +44,6 @@ build_and_run()
--copt
=
-DMACE_MODEL_FUNCTION
=
Create
${
MODEL_TAG
}
--copt
=
-DMACE_MODEL_FUNCTION
=
Create
${
MODEL_TAG
}
adb shell
"mkdir -p
${
PHONE_DATA_DIR
}
"
adb shell
"mkdir -p
${
PHONE_DATA_DIR
}
"
if
[
"
$EMBED_OPENCL_BINARY
"
=
false
]
;
then
adb shell
"mkdir -p
${
KERNEL_DIR
}
"
adb push mace/kernels/opencl/cl/.
${
KERNEL_DIR
}
fi
adb push
${
MODEL_DIR
}
/
${
INPUT_FILE_NAME
}
${
PHONE_DATA_DIR
}
adb push
${
MODEL_DIR
}
/
${
INPUT_FILE_NAME
}
${
PHONE_DATA_DIR
}
adb push bazel-bin/mace/examples/mace_run
${
PHONE_DATA_DIR
}
adb push bazel-bin/mace/examples/mace_run
${
PHONE_DATA_DIR
}
...
@@ -104,10 +97,20 @@ rm -rf ${VERSION_SOURCE_PATH}
...
@@ -104,10 +97,20 @@ rm -rf ${VERSION_SOURCE_PATH}
mkdir
-p
${
VERSION_SOURCE_PATH
}
mkdir
-p
${
VERSION_SOURCE_PATH
}
bash mace/tools/git/gen_version_source.sh
${
VERSION_SOURCE_PATH
}
/version.cc
bash mace/tools/git/gen_version_source.sh
${
VERSION_SOURCE_PATH
}
/version.cc
echo
"Step 4: Run model on the phone with files"
echo
"Step 4: Generate encrypted opencl source and read tuning method"
rm
-rf
${
CL_CODEGEN_DIR
}
rm
-rf
${
TUNING_CODEGEN_DIR
}
mkdir
-p
${
CL_CODEGEN_DIR
}
mkdir
-p
${
TUNING_CODEGEN_DIR
}
python mace/python/tools/encrypt_opencl_codegen.py
\
--cl_kernel_dir
=
./mace/kernels/opencl/cl/
--output_path
=
${
CL_CODEGEN_DIR
}
/opencl_encrypt_program.cc
python mace/python/tools/read_tuning_codegen.py
\
--output_path
=
${
TUNING_CODEGEN_DIR
}
/read_tuning_params.cc
echo
"Step 5: Run model on the phone with files"
build_and_run
false
build_and_run
false
echo
"Step
5
: Generate OpenCL binary program and config code"
echo
"Step
6
: Generate OpenCL binary program and config code"
rm
-rf
${
CL_BIN_DIR
}
rm
-rf
${
CL_BIN_DIR
}
adb pull
${
KERNEL_DIR
}
${
CL_BIN_DIR
}
adb pull
${
KERNEL_DIR
}
${
CL_BIN_DIR
}
rm
-rf
${
CL_CODEGEN_DIR
}
rm
-rf
${
CL_CODEGEN_DIR
}
...
@@ -115,20 +118,21 @@ mkdir -p ${CL_CODEGEN_DIR}
...
@@ -115,20 +118,21 @@ mkdir -p ${CL_CODEGEN_DIR}
python mace/python/tools/opencl_codegen.py
\
python mace/python/tools/opencl_codegen.py
\
--cl_binary_dir
=
${
CL_BIN_DIR
}
--output_path
=
${
CL_CODEGEN_DIR
}
/opencl_compiled_program.cc
--cl_binary_dir
=
${
CL_BIN_DIR
}
--output_path
=
${
CL_CODEGEN_DIR
}
/opencl_compiled_program.cc
echo
"Step
6
: Generate tuning source file"
echo
"Step
7
: Generate tuning source file"
adb pull
${
PHONE_DATA_DIR
}
/mace_run.config
${
CL_BIN_DIR
}
adb pull
${
PHONE_DATA_DIR
}
/mace_run.config
${
CL_BIN_DIR
}
rm
-rf
${
TUNING_CODEGEN_DIR
}
mkdir
-p
${
TUNING_CODEGEN_DIR
}
mkdir
-p
${
TUNING_CODEGEN_DIR
}
python mace/python/tools/binary_codegen.py
\
python mace/python/tools/binary_codegen.py
\
--binary_file
=
${
CL_BIN_DIR
}
/mace_run.config
--output_path
=
${
TUNING_CODEGEN_DIR
}
/tuning_params.cc
--binary_file
=
${
CL_BIN_DIR
}
/mace_run.config
--output_path
=
${
TUNING_CODEGEN_DIR
}
/tuning_params.cc
echo
"Step
7
: Run model on the phone using binary"
echo
"Step
8
: Run model on the phone using binary"
build_and_run
true
build_and_run
true
echo
"Step
8
: Pull the mace run result."
echo
"Step
9
: Pull the mace run result."
rm
-rf
${
MODEL_DIR
}
/
${
OUTPUT_FILE_NAME
}
rm
-rf
${
MODEL_DIR
}
/
${
OUTPUT_FILE_NAME
}
adb </dev/null pull
${
PHONE_DATA_DIR
}
/
${
OUTPUT_FILE_NAME
}
${
MODEL_DIR
}
adb </dev/null pull
${
PHONE_DATA_DIR
}
/
${
OUTPUT_FILE_NAME
}
${
MODEL_DIR
}
echo
"Step
9
: Validate the result"
echo
"Step
10
: Validate the result"
python tools/validate.py
--model_file
${
TF_MODEL_FILE_PATH
}
\
python tools/validate.py
--model_file
${
TF_MODEL_FILE_PATH
}
\
--input_file
${
MODEL_DIR
}
/
${
INPUT_FILE_NAME
}
\
--input_file
${
MODEL_DIR
}
/
${
INPUT_FILE_NAME
}
\
--mace_out_file
${
MODEL_DIR
}
/
${
OUTPUT_FILE_NAME
}
\
--mace_out_file
${
MODEL_DIR
}
/
${
OUTPUT_FILE_NAME
}
\
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录