Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
9141bee1
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
9141bee1
编写于
6月 07, 2018
作者:
C
cuichaowen
提交者:
Yan Chunwei
6月 07, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add Anakin api for paddle (#11228)
上级
d48172f2
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
123 addition
and
38 deletion
+123
-38
paddle/contrib/inference/CMakeLists.txt
paddle/contrib/inference/CMakeLists.txt
+23
-17
paddle/contrib/inference/paddle_inference_api.h
paddle/contrib/inference/paddle_inference_api.h
+0
-1
paddle/contrib/inference/paddle_inference_api_anakin_engine.cc
...e/contrib/inference/paddle_inference_api_anakin_engine.cc
+43
-11
paddle/contrib/inference/paddle_inference_api_anakin_engine.h
...le/contrib/inference/paddle_inference_api_anakin_engine.h
+16
-6
paddle/contrib/inference/paddle_inference_api_anakin_engine_tester.cc
...ib/inference/paddle_inference_api_anakin_engine_tester.cc
+41
-3
未找到文件。
paddle/contrib/inference/CMakeLists.txt
浏览文件 @
9141bee1
...
...
@@ -24,31 +24,37 @@ set(ANAKIN_LIBRARY "" CACHE STRING "path of Anakin library")
set
(
inference_deps paddle_inference_api paddle_fluid_api
)
# if anakin is set enable anakin api implementation
if
(
ANAKIN_INCLUDE
_DIR
AND ANAKIN_LIBRARY
)
if
(
ANAKIN_INCLUDE AND ANAKIN_LIBRARY
)
set
(
ANAKIN_FOUND ON
)
else
()
set
(
ANAKIN_FOUND OFF
)
endif
()
function
(
fetch_include_recursively root_dir
)
if
(
IS_DIRECTORY
${
root_dir
}
)
include_directories
(
${
root_dir
}
)
endif
()
file
(
GLOB ALL_SUB RELATIVE
${
root_dir
}
${
root_dir
}
/*
)
foreach
(
sub
${
ALL_SUB
}
)
if
(
IS_DIRECTORY
${
root_dir
}
/
${
sub
}
)
fetch_include_recursively
(
${
root_dir
}
/
${
sub
}
)
endif
()
endforeach
()
endfunction
()
if
(
ANAKIN_FOUND
)
# Anakin's code style doesn't follow google c style.
set
(
CMAKE_CXX_FLAGS
"
${
CMAKE_CXX_FLAGS
}
-Wno-error=comment
-Wno-error=reorder
-Wno-error=format
-Wno-error=switch
-Wno-error=return-type
-Wno-error=non-virtual-dtor
-Wno-error=cpp"
)
set
(
CMAKE_CXX_FLAGS
"
${
CMAKE_CXX_FLAGS
}
-Wno-error=unused-variable -Wno-error=format-extra-args -Wno-error=comment -Wno-error=format -Wno-error=switch -Wno-error=return-type -Wno-error=non-virtual-dtor -Wno-reorder -Wno-error=cpp"
)
message
(
STATUS
"Anakin for inference is enabled"
)
message
(
STATUS
"Anakin is set INCLUDE:
${
ANAKIN_INCLUDE
}
LIBRARY:
${
ANAKIN_LIBRARY
}
"
)
include_directories
(
"
${
ANAKIN_INCLUDE
}
"
)
# Anakin's source path is a mass, need to set sub-directories trivially.
include_directories
(
"
${
ANAKIN_INCLUDE
}
/saber"
)
link_directories
(
"
${
ANAKIN_LIBRARY
}
"
)
fetch_include_recursively
(
${
ANAKIN_INCLUDE
}
)
link_directories
(
${
ANAKIN_LIBRARY
}
)
nv_library
(
inference_anakin_api S
RCS
paddle_inference_api_anakin_engine.cc
)
target_link_libraries
(
inference_anakin_api anakin
)
nv_library
(
inference_anakin_api S
HARED SRCS paddle_inference_api.cc
paddle_inference_api_anakin_engine.cc
)
target_link_libraries
(
inference_anakin_api anakin
anakin_saber_common
)
list
(
APPEND inference_deps inference_anakin_api
)
endif
()
...
...
@@ -73,7 +79,7 @@ function(inference_api_test TARGET_NAME)
endfunction
(
inference_api_test
)
cc_library
(
paddle_inference_api
SRCS paddle_inference_api.cc paddle_inference_api_impl.cc
SRCS paddle_inference_api.cc paddle_inference_api_impl.cc
DEPS
${
FLUID_CORE_MODULES
}
${
GLOB_OP_LIB
}
)
cc_test
(
test_paddle_inference_api
...
...
@@ -84,8 +90,8 @@ inference_api_test(test_paddle_inference_api_impl
ARGS test_word2vec test_image_classification
)
if
(
ANAKIN_FOUND
)
nv
_test
(
inference_anakin_test SRCS paddle_inference_api_anakin_engine_tester.cc
DEPS
${
inference_deps
}
protobuf
)
cc
_test
(
inference_anakin_test SRCS paddle_inference_api_anakin_engine_tester.cc
DEPS
${
inference_deps
}
)
endif
()
if
(
WITH_TESTING
)
...
...
paddle/contrib/inference/paddle_inference_api.h
浏览文件 @
9141bee1
...
...
@@ -113,5 +113,4 @@ struct AnakinConfig : public PaddlePredictor::Config {
// Similarly, each engine kind should map to a unique predictor implementation.
template
<
typename
ConfigT
,
PaddleEngineKind
engine
=
PaddleEngineKind
::
kNative
>
std
::
unique_ptr
<
PaddlePredictor
>
CreatePaddlePredictor
(
const
ConfigT
&
config
);
}
// namespace paddle
paddle/contrib/inference/paddle_inference_api_anakin_engine.cc
浏览文件 @
9141bee1
...
...
@@ -24,8 +24,16 @@ PaddleInferenceAnakinPredictor::PaddleInferenceAnakinPredictor(
}
bool
PaddleInferenceAnakinPredictor
::
Init
(
const
AnakinConfig
&
config
)
{
// TODO(Superjomn) Tell anakin to support return code.
engine_
.
Build
(
config
.
model_file
,
config
.
max_batch_size
);
if
(
!
(
graph_
.
load
(
config
.
model_file
)))
{
return
false
;
}
graph_
.
ResetBatchSize
(
"input_0"
,
config
.
max_batch_size
);
// optimization for graph
if
(
!
(
graph_
.
Optimize
()))
{
return
false
;
}
// construct executer
executor_
.
init
(
graph_
);
return
true
;
}
...
...
@@ -38,24 +46,30 @@ bool PaddleInferenceAnakinPredictor::Run(
<<
"'s type is not float"
;
return
false
;
}
engine_
.
SetInputFromCPU
(
input
.
name
,
static_cast
<
float
*>
(
input
.
data
.
data
),
input
.
data
.
length
);
auto
d_tensor_in_p
=
executor_
.
get_in
(
input
.
name
);
float
*
d_data_p
=
d_tensor_in_p
->
mutable_data
();
if
(
cudaMemcpy
(
d_data_p
,
static_cast
<
float
*>
(
input
.
data
.
data
),
d_tensor_in_p
->
valid_size
()
*
sizeof
(
float
),
cudaMemcpyHostToDevice
)
!=
0
)
{
LOG
(
ERROR
)
<<
"copy data from CPU to GPU error"
;
return
false
;
}
}
// TODO(Superjomn) Tell anakin to support return code.
engine_
.
Execute
();
executor_
.
prediction
();
if
(
output_data
->
empty
())
{
LOG
(
ERROR
)
<<
"At least one output should be set with tensors' names."
;
return
false
;
}
for
(
auto
&
output
:
*
output_data
)
{
auto
*
tensor
=
e
ngine_
.
GetOutputInGPU
(
output
.
name
);
auto
*
tensor
=
e
xecutor_
.
get_out
(
output
.
name
);
output
.
shape
=
tensor
->
shape
();
// Copy data from GPU -> CPU
if
(
cudaMemcpy
(
output
.
data
.
data
,
tensor
->
data
(),
tensor
->
size
(
),
tensor
->
mutable_
data
(),
tensor
->
valid_size
()
*
sizeof
(
float
),
cudaMemcpyDeviceToHost
)
!=
0
)
{
LOG
(
ERROR
)
<<
"copy data from GPU to CPU error"
;
return
false
;
...
...
@@ -64,9 +78,26 @@ bool PaddleInferenceAnakinPredictor::Run(
return
true
;
}
// TODO(Superjomn) To implement latter.
anakin
::
Net
<
anakin
::
NV
,
anakin
::
saber
::
AK_FLOAT
,
anakin
::
Precision
::
FP32
>
&
PaddleInferenceAnakinPredictor
::
get_executer
()
{
return
executor_
;
}
// the cloned new Predictor of anakin share the same net weights from original
// Predictor
std
::
unique_ptr
<
PaddlePredictor
>
PaddleInferenceAnakinPredictor
::
Clone
()
{
return
nullptr
;
VLOG
(
3
)
<<
"Anakin Predictor::clone"
;
std
::
unique_ptr
<
PaddlePredictor
>
cls
(
new
PaddleInferenceAnakinPredictor
());
// construct executer from other graph
auto
anakin_predictor_p
=
dynamic_cast
<
PaddleInferenceAnakinPredictor
*>
(
cls
.
get
());
if
(
!
anakin_predictor_p
)
{
LOG
(
ERROR
)
<<
"fail to call Init"
;
return
nullptr
;
}
anakin_predictor_p
->
get_executer
().
init
(
graph_
);
return
std
::
move
(
cls
);
}
// A factory to help create difference predictor.
...
...
@@ -74,6 +105,7 @@ template <>
std
::
unique_ptr
<
PaddlePredictor
>
CreatePaddlePredictor
<
AnakinConfig
,
PaddleEngineKind
::
kAnakin
>
(
const
AnakinConfig
&
config
)
{
VLOG
(
3
)
<<
"Anakin Predictor create."
;
std
::
unique_ptr
<
PaddlePredictor
>
x
(
new
PaddleInferenceAnakinPredictor
(
config
));
return
x
;
...
...
paddle/contrib/inference/paddle_inference_api_anakin_engine.h
浏览文件 @
9141bee1
...
...
@@ -20,32 +20,42 @@ limitations under the License. */
#pragma once
// NOTE This header file do not have namespace.
// TODO(Superjomn) Tell Anakin to provide better APIs.
#include <test/framework/net/paddle_api.h>
//#include <test/framework/net/paddle_api.h>
#include "paddle/contrib/inference/paddle_inference_api.h"
#include "framework/core/net/net.h"
#include "saber/saber_types.h"
namespace
paddle
{
class
PaddleInferenceAnakinPredictor
:
public
PaddlePredictor
{
public:
PaddleInferenceAnakinPredictor
()
{}
PaddleInferenceAnakinPredictor
(
const
AnakinConfig
&
config
);
// NOTE Unlike the native engine, the buffers of anakin engine's output_data
// should be allocated first.
// TODO(Superjomn) should unify all the behaviors of output_data accross all
// the engines.
bool
Run
(
const
std
::
vector
<
PaddleTensor
>&
inputs
,
std
::
vector
<
PaddleTensor
>*
output_data
)
override
;
std
::
unique_ptr
<
PaddlePredictor
>
Clone
()
override
;
anakin
::
Net
<
anakin
::
NV
,
anakin
::
saber
::
AK_FLOAT
,
anakin
::
Precision
::
FP32
>&
get_executer
();
~
PaddleInferenceAnakinPredictor
()
override
{};
private:
bool
Init
(
const
AnakinConfig
&
config
);
anakin
::
AnakinEngine
<
anakin
::
NV
,
anakin
::
graph
::
Graph
<
anakin
::
NV
,
anakin
::
saber
::
AK_FLOAT
,
anakin
::
Precision
::
FP32
>
engine_
;
graph_
;
anakin
::
Net
<
anakin
::
NV
,
anakin
::
saber
::
AK_FLOAT
,
anakin
::
Precision
::
FP32
>
executor_
;
AnakinConfig
config_
;
};
}
// namespace paddle
paddle/contrib/inference/paddle_inference_api_anakin_engine_tester.cc
浏览文件 @
9141bee1
...
...
@@ -12,16 +12,54 @@ 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. */
#include
"paddle/contrib/inference/paddle_inference_api.h"
#include
<glog/logging.h>
#include <gtest/gtest.h>
#include "gflags/gflags.h"
#include "paddle/contrib/inference/paddle_inference_api.h"
namespace
paddle
{
TEST
(
inference
,
anakin
)
{
AnakinConfig
GetConfig
(
)
{
AnakinConfig
config
;
config
.
model_file
=
"./mobilenet_v2.anakin.bin"
;
config
.
device
=
0
;
config
.
max_batch_size
=
1
;
return
config
;
}
auto
engine
=
TEST
(
inference
,
anakin
)
{
AnakinConfig
config
=
GetConfig
();
auto
predictor
=
CreatePaddlePredictor
<
AnakinConfig
,
PaddleEngineKind
::
kAnakin
>
(
config
);
float
data
[
1
*
3
*
224
*
224
]
=
{
1.0
f
};
PaddleBuf
buf
{.
data
=
data
,
.
length
=
sizeof
(
data
)};
PaddleTensor
tensor
{.
name
=
"input_0"
,
.
shape
=
std
::
vector
<
int
>
({
1
,
3
,
224
,
224
}),
.
data
=
buf
,
.
dtype
=
PaddleDType
::
FLOAT32
};
// For simplicity, we set all the slots with the same data.
std
::
vector
<
PaddleTensor
>
paddle_tensor_feeds
(
1
,
tensor
);
float
data_out
[
1000
];
PaddleBuf
buf_out
{.
data
=
data_out
,
.
length
=
sizeof
(
data
)};
PaddleTensor
tensor_out
{.
name
=
"prob_out"
,
.
shape
=
std
::
vector
<
int
>
({
1000
,
1
}),
.
data
=
buf_out
,
.
dtype
=
PaddleDType
::
FLOAT32
};
std
::
vector
<
PaddleTensor
>
outputs
(
1
,
tensor_out
);
ASSERT_TRUE
(
predictor
->
Run
(
paddle_tensor_feeds
,
&
outputs
));
float
*
data_o
=
static_cast
<
float
*>
(
outputs
[
0
].
data
.
data
);
for
(
size_t
j
=
0
;
j
<
1000
;
++
j
)
{
LOG
(
INFO
)
<<
"output["
<<
j
<<
"]: "
<<
data_o
[
j
];
}
}
}
// namespace paddle
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录