Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
90d5be74
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
90d5be74
编写于
8月 06, 2017
作者:
T
tensor-tang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add mkldnn fc forward
上级
94b172a7
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
236 addition
and
12 deletion
+236
-12
paddle/gserver/layers/MkldnnFcLayer.cpp
paddle/gserver/layers/MkldnnFcLayer.cpp
+75
-3
paddle/gserver/layers/MkldnnFcLayer.h
paddle/gserver/layers/MkldnnFcLayer.h
+9
-0
paddle/gserver/layers/MkldnnLayer.cpp
paddle/gserver/layers/MkldnnLayer.cpp
+98
-0
paddle/gserver/layers/MkldnnLayer.h
paddle/gserver/layers/MkldnnLayer.h
+54
-9
未找到文件。
paddle/gserver/layers/MkldnnFcLayer.cpp
浏览文件 @
90d5be74
...
@@ -13,6 +13,7 @@ See the License for the specific language governing permissions and
...
@@ -13,6 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License. */
limitations under the License. */
#include "MkldnnFcLayer.h"
#include "MkldnnFcLayer.h"
#include "paddle/utils/Stat.h"
namespace
paddle
{
namespace
paddle
{
...
@@ -20,11 +21,82 @@ REGISTER_LAYER(mkldnn_fc, MkldnnFcLayer);
...
@@ -20,11 +21,82 @@ REGISTER_LAYER(mkldnn_fc, MkldnnFcLayer);
bool
MkldnnFcLayer
::
init
(
const
LayerMap
&
layerMap
,
bool
MkldnnFcLayer
::
init
(
const
LayerMap
&
layerMap
,
const
ParameterMap
&
parameterMap
)
{
const
ParameterMap
&
parameterMap
)
{
return
MkldnnLayer
::
init
(
layerMap
,
parameterMap
);
if
(
!
MkldnnLayer
::
init
(
layerMap
,
parameterMap
))
{
return
false
;
}
CHECK_EQ
(
inputLayers_
.
size
(),
1
)
<<
"Only support one input layer yet!"
;
CHECK_EQ
(
inputLayers_
.
size
(),
parameters_
.
size
());
CHECK
(
!
parameters_
[
0
]
->
isSparse
())
<<
"Do not support sparse yet"
;
// output size, cat not be changed
oc_
=
getSize
();
oh_
=
1
;
ow_
=
1
;
// input size can not change in FC
iLayerSize_
=
inputLayers_
[
0
]
->
getSize
();
CHECK_EQ
(
parameters_
[
0
]
->
getSize
(),
iLayerSize_
*
oc_
);
// create weight
weight_
=
std
::
unique_ptr
<
Weight
>
(
new
Weight
(
oc_
,
iLayerSize_
,
parameters_
[
0
],
0
));
// create biases
if
(
biasParameter_
.
get
()
!=
NULL
)
{
biases_
=
std
::
unique_ptr
<
Weight
>
(
new
Weight
(
1
,
oc_
,
biasParameter_
));
}
return
true
;
}
void
MkldnnFcLayer
::
reshape
()
{
const
Argument
&
input
=
getInput
(
0
);
int
batchSize
=
input
.
getBatchSize
();
if
(
bs_
==
batchSize
)
{
return
;
}
bs_
=
batchSize
;
ih_
=
input
.
getFrameHeight
();
iw_
=
input
.
getFrameWidth
();
if
(
ih_
==
0
)
{
ih_
=
1
;
}
if
(
iw_
==
0
)
{
iw_
=
1
;
}
CHECK_EQ
(
iLayerSize_
,
inputLayers_
[
0
]
->
getSize
());
ic_
=
iLayerSize_
/
(
ih_
*
iw_
);
CHECK_EQ
(
size_t
(
ic_
*
ih_
*
iw_
),
iLayerSize_
)
<<
"not divisible"
;
CHECK_EQ
(
size_t
(
oc_
),
getSize
());
// reset output
output_
.
setFrameHeight
(
oh_
);
output_
.
setFrameWidth
(
ow_
);
resetOutput
(
bs_
,
oc_
);
}
}
void
MkldnnFcLayer
::
forward
(
PassType
passType
)
{}
void
MkldnnFcLayer
::
forward
(
PassType
passType
)
{
Layer
::
forward
(
passType
);
reshape
();
void
MkldnnFcLayer
::
backward
(
const
UpdateCallback
&
callback
)
{}
{
REGISTER_TIMER_INFO
(
"mkldnn_FwdTimer"
,
getName
().
c_str
());
real
*
input
=
getInputValue
(
0
)
->
getData
();
real
*
output
=
getOutputValue
()
->
getData
();
real
*
wgt
=
weight_
->
getW
()
->
getData
();
bool
hasBias
=
biases_
&&
biases_
->
getW
();
real
*
bias
=
hasBias
?
biases_
->
getW
()
->
getData
()
:
NULL
;
mkldnnForwardFC
(
bs_
,
ic_
,
ih_
,
iw_
,
input
,
oc_
,
output
,
wgt
,
bias
);
}
/* activation */
{
REGISTER_TIMER_INFO
(
"FwActTimer"
,
getName
().
c_str
());
forwardActivation
();
}
}
void
MkldnnFcLayer
::
backward
(
const
UpdateCallback
&
callback
)
{
;
// bool hasBias = biases_ && biases_->getWGrad();
}
}
// namespace paddle
}
// namespace paddle
paddle/gserver/layers/MkldnnFcLayer.h
浏览文件 @
90d5be74
...
@@ -26,6 +26,13 @@ namespace paddle {
...
@@ -26,6 +26,13 @@ namespace paddle {
*/
*/
class
MkldnnFcLayer
:
public
MkldnnLayer
{
class
MkldnnFcLayer
:
public
MkldnnLayer
{
protected:
protected:
// input layer size, can not be change after init
size_t
iLayerSize_
;
// == ic * ih * iw
// fc weight and bias
std
::
unique_ptr
<
Weight
>
weight_
;
std
::
unique_ptr
<
Weight
>
biases_
;
public:
public:
explicit
MkldnnFcLayer
(
const
LayerConfig
&
config
)
:
MkldnnLayer
(
config
)
{}
explicit
MkldnnFcLayer
(
const
LayerConfig
&
config
)
:
MkldnnLayer
(
config
)
{}
...
@@ -34,6 +41,8 @@ public:
...
@@ -34,6 +41,8 @@ public:
bool
init
(
const
LayerMap
&
layerMap
,
bool
init
(
const
LayerMap
&
layerMap
,
const
ParameterMap
&
parameterMap
)
override
;
const
ParameterMap
&
parameterMap
)
override
;
void
reshape
();
void
forward
(
PassType
passType
)
override
;
void
forward
(
PassType
passType
)
override
;
void
backward
(
const
UpdateCallback
&
callback
)
override
;
void
backward
(
const
UpdateCallback
&
callback
)
override
;
...
...
paddle/gserver/layers/MkldnnLayer.cpp
0 → 100644
浏览文件 @
90d5be74
/* Copyright (c) 2017 PaddlePaddle Authors. All Rights Reserve.
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. */
#include "MkldnnLayer.h"
// using namespace mkldnn; // NOLINT
using
mem
=
mkldnn
::
memory
;
// NOLINT
typedef
mem
::
format
format
;
typedef
mkldnn
::
inner_product_forward
fc_fwd
;
typedef
mkldnn
::
inner_product_backward_weights
fc_bwdWgt
;
typedef
mkldnn
::
inner_product_backward_data
fc_bwdData
;
namespace
paddle
{
bool
MkldnnLayer
::
init
(
const
LayerMap
&
layerMap
,
const
ParameterMap
&
parameterMap
)
{
CHECK
(
FLAGS_use_mkldnn
)
<<
"MkldnnLayers only support use_mkldnn."
<<
"Please set WITH_MKLDNN=ON"
;
// TODO(TJ): deivecId
return
Layer
::
init
(
layerMap
,
parameterMap
);
}
void
MkldnnLayer
::
resetForwardFC
(
int
bs
,
int
ic
,
int
ih
,
int
iw
,
real
*
botData
,
int
oc
,
real
*
topData
,
real
*
wgtData
,
real
*
biasData
)
{
bool
hasSpatial
=
ih
==
1
&&
iw
==
1
?
false
:
true
;
engine_
=
CpuEngine
::
Instance
().
getEngine
();
mem
::
desc
botMD
=
hasSpatial
?
createMD
({
bs
,
ic
,
ih
,
iw
},
format
::
nchw
)
:
createMD
({
bs
,
ic
},
format
::
nc
);
mem
::
desc
wgtMD
=
hasSpatial
?
createMD
({
oc
,
ic
,
ih
,
iw
},
format
::
oihw
)
:
createMD
({
oc
,
ic
},
format
::
oi
);
mem
::
desc
biasMD
=
biasData
!=
NULL
?
createMD
({
oc
},
format
::
x
)
:
createMD
({},
format
::
format_undef
);
mem
::
desc
topMD
=
createMD
({
bs
,
oc
},
format
::
nc
);
mkldnn
::
prop_kind
pk
=
mkldnn
::
prop_kind
::
forward
;
fc_fwd
::
desc
fwdDesc
=
biasData
!=
NULL
?
fc_fwd
::
desc
(
pk
,
botMD
,
wgtMD
,
biasMD
,
topMD
)
:
fc_fwd
::
desc
(
pk
,
botMD
,
wgtMD
,
topMD
);
fc_fwd
::
primitive_desc
fwdPD
=
fc_fwd
::
primitive_desc
(
fwdDesc
,
engine_
);
mem
bot
=
mem
(
mem
::
primitive_desc
(
botMD
,
engine_
),
botData
);
mem
wgt
=
mem
(
mem
::
primitive_desc
(
wgtMD
,
engine_
),
wgtData
);
mem
top
=
mem
(
mem
::
primitive_desc
(
topMD
,
engine_
),
topData
);
if
(
biasData
!=
NULL
)
{
mem
bias
=
mem
(
mem
::
primitive_desc
(
biasMD
,
engine_
),
biasData
);
fwd_
.
reset
(
new
fc_fwd
(
fwdPD
,
bot
,
wgt
,
bias
,
top
));
}
else
{
fwd_
.
reset
(
new
fc_fwd
(
fwdPD
,
bot
,
wgt
,
top
));
}
pipelineFwd_
.
clear
();
pipelineFwd_
.
push_back
(
*
fwd_
);
}
void
MkldnnLayer
::
mkldnnForwardFC
(
int
bs
,
int
ic
,
int
ih
,
int
iw
,
real
*
botData
,
int
oc
,
real
*
topData
,
real
*
wgtData
,
real
*
biasData
)
{
// if input size changed, reset it
resetForwardFC
(
bs
,
ic
,
ih
,
iw
,
botData
,
oc
,
topData
,
wgtData
,
biasData
);
// just forward
// update botdata
stream_
->
submit
(
pipelineFwd_
);
}
mem
::
desc
MkldnnLayer
::
createMD
(
mem
::
dims
dims
,
mem
::
format
fmt
,
mem
::
data_type
type
)
{
// TODO(TJ): isFmtSuppoted(fmt)
return
mem
::
desc
(
dims
,
type
,
fmt
);
}
}
// namespace paddle
paddle/gserver/layers/MkldnnLayer.h
浏览文件 @
90d5be74
...
@@ -29,20 +29,65 @@ typedef std::shared_ptr<MkldnnLayer> MkldnnLayerPtr;
...
@@ -29,20 +29,65 @@ typedef std::shared_ptr<MkldnnLayer> MkldnnLayerPtr;
*
*
*/
*/
class
MkldnnLayer
:
public
Layer
{
class
MkldnnLayer
:
public
Layer
{
protected:
// batch size
int
bs_
;
// input image channel, height and width
int
ic_
,
ih_
,
iw_
;
// output image channel, height and width
int
oc_
,
oh_
,
ow_
;
// mkldnn engine, stream and primivtives
mkldnn
::
engine
engine_
;
std
::
shared_ptr
<
MkldnnStream
>
stream_
;
std
::
shared_ptr
<
mkldnn
::
primitive
>
fwd_
;
std
::
vector
<
mkldnn
::
primitive
>
pipelineFwd_
;
std
::
vector
<
mkldnn
::
primitive
>
pipelineBwd_
;
public:
public:
explicit
MkldnnLayer
(
const
LayerConfig
&
config
)
:
Layer
(
config
)
{}
explicit
MkldnnLayer
(
const
LayerConfig
&
config
)
:
Layer
(
config
),
bs_
(
0
),
ic_
(
0
),
ih_
(
0
),
iw_
(
0
),
oc_
(
0
),
oh_
(
0
),
ow_
(
0
),
engine_
(
mkldnn
::
engine
::
cpu
,
0
),
stream_
(
nullptr
)
{}
~
MkldnnLayer
()
{}
~
MkldnnLayer
()
{}
virtual
bool
init
(
const
LayerMap
&
layerMap
,
virtual
bool
init
(
const
LayerMap
&
layerMap
,
const
ParameterMap
&
parameterMap
);
const
ParameterMap
&
parameterMap
)
{
CHECK
(
FLAGS_use_mkldnn
)
<<
"MkldnnLayers only support use_mkldnn."
void
resetForwardFC
(
int
bs
,
<<
"Please set WITH_MKLDNN=ON"
;
int
ic
,
// TODO(TJ): deivecId
int
ih
,
return
Layer
::
init
(
layerMap
,
parameterMap
);
int
iw
,
}
real
*
botData
,
int
oc
,
real
*
topData
,
real
*
wgtData
,
real
*
biasData
);
void
mkldnnForwardFC
(
int
bs
,
int
ic
,
int
ih
,
int
iw
,
real
*
botData
,
int
oc
,
real
*
topData
,
real
*
wgtData
,
real
*
biasData
);
void
resetOutput
(
size_t
height
,
size_t
width
)
{
;
}
// TODO(TJ): move to MkldnnMatrix
// create memory desc
inline
mkldnn
::
memory
::
desc
createMD
(
mkldnn
::
memory
::
dims
dims
,
mkldnn
::
memory
::
format
fmt
,
mkldnn
::
memory
::
data_type
type
=
mkldnn
::
memory
::
data_type
::
f32
);
};
};
}
// namespace paddle
}
// namespace paddle
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录