Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wmsofts
Paddle
提交
55bee85e
P
Paddle
项目概览
wmsofts
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
0
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看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
55bee85e
编写于
11月 21, 2017
作者:
T
Tao Luo
提交者:
GitHub
11月 21, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #5779 from tensor-tang/refine
refine MKLDNNLayer
上级
3c4d4065
c961fbf0
变更
14
隐藏空白更改
内联
并排
Showing
14 changed file
with
269 addition
and
423 deletion
+269
-423
paddle/gserver/layers/MKLDNNAddtoLayer.cpp
paddle/gserver/layers/MKLDNNAddtoLayer.cpp
+20
-23
paddle/gserver/layers/MKLDNNAddtoLayer.h
paddle/gserver/layers/MKLDNNAddtoLayer.h
+3
-46
paddle/gserver/layers/MKLDNNBatchNormLayer.cpp
paddle/gserver/layers/MKLDNNBatchNormLayer.cpp
+17
-19
paddle/gserver/layers/MKLDNNBatchNormLayer.h
paddle/gserver/layers/MKLDNNBatchNormLayer.h
+4
-18
paddle/gserver/layers/MKLDNNConcatLayer.cpp
paddle/gserver/layers/MKLDNNConcatLayer.cpp
+14
-31
paddle/gserver/layers/MKLDNNConcatLayer.h
paddle/gserver/layers/MKLDNNConcatLayer.h
+9
-42
paddle/gserver/layers/MKLDNNConvLayer.cpp
paddle/gserver/layers/MKLDNNConvLayer.cpp
+18
-21
paddle/gserver/layers/MKLDNNConvLayer.h
paddle/gserver/layers/MKLDNNConvLayer.h
+3
-29
paddle/gserver/layers/MKLDNNFcLayer.cpp
paddle/gserver/layers/MKLDNNFcLayer.cpp
+25
-27
paddle/gserver/layers/MKLDNNFcLayer.h
paddle/gserver/layers/MKLDNNFcLayer.h
+3
-19
paddle/gserver/layers/MKLDNNLayer.cpp
paddle/gserver/layers/MKLDNNLayer.cpp
+49
-64
paddle/gserver/layers/MKLDNNLayer.h
paddle/gserver/layers/MKLDNNLayer.h
+88
-50
paddle/gserver/layers/MKLDNNPoolLayer.cpp
paddle/gserver/layers/MKLDNNPoolLayer.cpp
+13
-16
paddle/gserver/layers/MKLDNNPoolLayer.h
paddle/gserver/layers/MKLDNNPoolLayer.h
+3
-18
未找到文件。
paddle/gserver/layers/MKLDNNAddtoLayer.cpp
浏览文件 @
55bee85e
...
...
@@ -38,12 +38,13 @@ bool MKLDNNAddtoLayer::init(const LayerMap& layerMap,
}
void
MKLDNNAddtoLayer
::
reshape
(
int
&
bs
,
int
&
ic
,
int
&
ih
,
int
&
iw
,
int
oc
,
int
&
oh
,
int
&
ow
)
{
int
&
bs
,
int
&
ic
,
int
&
ih
,
int
&
iw
,
int
&
oc
,
int
&
oh
,
int
&
ow
)
{
CHECK_EQ
(
layerSize_
,
getSize
())
<<
"this layer size can not be changed"
;
reshapeInput
(
bs
,
ih
,
iw
);
ic
=
inputLayers_
[
0
]
->
getSize
()
/
ih
/
iw
;
CHECK_EQ
((
size_t
)
ic
*
ih
*
iw
,
inputLayers_
[
0
]
->
getSize
());
CHECK_EQ
(
inputElemenCnt_
,
(
size_t
)
bs
*
ic
*
ih
*
iw
);
CHECK_EQ
(
inputLayers_
[
0
]
->
getOutputValue
()
->
getElementCnt
(),
(
size_t
)
bs
*
ic
*
ih
*
iw
);
for
(
size_t
i
=
0
;
i
<
inputLayers_
.
size
();
i
++
)
{
CHECK_EQ
(
int64_t
(
bs
),
inputLayers_
[
i
]
->
getOutput
().
getBatchSize
());
CHECK_EQ
(
layerSize_
,
inputLayers_
[
i
]
->
getSize
());
...
...
@@ -57,47 +58,43 @@ void MKLDNNAddtoLayer::reshape(
}
void
MKLDNNAddtoLayer
::
resetFwd
(
std
::
vector
<
primitive
>&
pipeline
,
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
out
)
{
resetFwdBuffers
(
inVals_
,
bias
,
out
);
in
=
inVals_
[
0
];
resetFwdBuffers
(
inputs
,
biasVal_
,
out
);
std
::
shared_ptr
<
sum
::
primitive_desc
>
fwdPD
;
std
::
shared_ptr
<
sum
::
primitive_desc
>
biasPD
;
resetFwdPD
(
fwdPD
,
biasPD
,
in
Vals_
,
bias
,
out
);
resetFwdPD
(
fwdPD
,
biasPD
,
in
puts
,
biasVal_
,
out
);
resetFwdPipeline
(
pipeline
,
fwdPD
,
biasPD
,
in
Vals_
,
bias
,
out
);
resetFwdPipeline
(
pipeline
,
fwdPD
,
biasPD
,
in
puts
,
biasVal_
,
out
);
}
void
MKLDNNAddtoLayer
::
resetBwd
(
std
::
vector
<
primitive
>&
pipeline
,
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
out
)
{
resetBwdBuffers
(
inGrads_
,
bias
,
out
);
in
=
inGrads_
[
0
];
resetBwdBuffers
(
inputs
,
biasGrad_
,
out
);
// backward only need share output grad to input grad
for
(
size_t
i
=
0
;
i
<
in
Grads_
.
size
();
i
++
)
{
if
(
in
Grads_
[
i
]
!=
nullptr
)
{
in
Grads_
[
i
]
=
out
;
inputLayers_
[
i
]
->
getOutputGrad
()
->
setData
(
in
Grads_
[
i
]
->
getData
());
for
(
size_t
i
=
0
;
i
<
in
puts
.
size
();
i
++
)
{
if
(
in
puts
[
i
]
!=
nullptr
)
{
in
puts
[
i
]
=
out
;
inputLayers_
[
i
]
->
getOutputGrad
()
->
setData
(
in
puts
[
i
]
->
getData
());
}
}
// backward bias
bwdBias_
=
nullptr
;
if
(
bias
)
{
if
(
bias
Grad_
)
{
std
::
vector
<
float
>
scales
(
bs_
,
1.0
);
std
::
vector
<
memory
::
primitive_desc
>
srcPDs
(
bs_
,
bias
->
getPrimitiveDesc
());
auto
biasPD
=
sum
::
primitive_desc
(
bias
->
getMemoryDesc
(),
scales
,
srcPDs
);
std
::
vector
<
memory
::
primitive_desc
>
srcPDs
(
bs_
,
biasGrad_
->
getPrimitiveDesc
());
auto
biasPD
=
sum
::
primitive_desc
(
biasGrad_
->
getMemoryDesc
(),
scales
,
srcPDs
);
std
::
vector
<
primitive
::
at
>
srcs
;
for
(
size_t
i
=
0
;
i
<
grads_
.
size
();
++
i
)
{
srcs
.
push_back
(
*
(
grads_
[
i
]));
}
bwdBias_
.
reset
(
new
sum
(
biasPD
,
srcs
,
*
bias
));
bwdBias_
.
reset
(
new
sum
(
biasPD
,
srcs
,
*
bias
Grad_
));
pipeline
.
push_back
(
*
bwdBias_
);
}
}
...
...
@@ -208,7 +205,7 @@ void MKLDNNAddtoLayer::resetBwdBuffers(std::vector<MKLDNNMatrixPtr>& inputs,
inputs
.
resize
(
inputLayers_
.
size
());
for
(
size_t
i
=
0
;
i
<
inputs
.
size
();
i
++
)
{
resetInGrad
(
inputs
[
i
],
inVal
_
->
getPrimitiveDesc
(),
i
);
resetInGrad
(
inputs
[
i
],
inVal
s_
[
i
]
->
getPrimitiveDesc
(),
i
);
CHECK_PRIMITIVE_DESC_EQ
(
inputs
[
i
],
out
->
getPrimitiveDesc
());
}
...
...
paddle/gserver/layers/MKLDNNAddtoLayer.h
浏览文件 @
55bee85e
...
...
@@ -26,9 +26,6 @@ namespace paddle {
*/
class
MKLDNNAddtoLayer
:
public
MKLDNNLayer
{
protected:
std
::
vector
<
MKLDNNMatrixPtr
>
inVals_
;
std
::
vector
<
MKLDNNMatrixPtr
>
inGrads_
;
// layer size == ic * ih * iw == oc * oh *ow, and can not be changed
size_t
layerSize_
;
...
...
@@ -50,52 +47,19 @@ public:
const
ParameterMap
&
parameterMap
)
override
;
void
reshape
(
int
&
bs
,
int
&
ic
,
int
&
ih
,
int
&
iw
,
int
oc
,
int
&
oh
,
int
&
ow
)
override
;
int
&
bs
,
int
&
ic
,
int
&
ih
,
int
&
iw
,
int
&
oc
,
int
&
oh
,
int
&
ow
)
override
;
void
resetFwd
(
std
::
vector
<
mkldnn
::
primitive
>&
pipeline
,
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
out
)
override
;
void
resetBwd
(
std
::
vector
<
mkldnn
::
primitive
>&
pipeline
,
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
out
)
override
;
void
updateWeights
(
const
UpdateCallback
&
callback
)
override
;
void
printValueFormat
()
override
{
for
(
size_t
i
=
0
;
i
<
inVals_
.
size
();
++
i
)
{
VLOG
(
MKLDNN_FMTS
)
<<
i
<<
" input: "
<<
inVals_
[
i
]
->
getFormat
()
<<
" >>>"
;
}
if
(
outVal_
)
{
VLOG
(
MKLDNN_FMTS
)
<<
outVal_
->
getFormat
()
<<
" >>> "
;
}
if
(
extOutVal_
)
{
VLOG
(
MKLDNN_FMTS
)
<<
extOutVal_
->
getFormat
();
}
}
void
printGradFormat
()
override
{
if
(
extOutGrad_
)
{
VLOG
(
MKLDNN_FMTS
)
<<
extOutGrad_
->
getFormat
();
}
if
(
outGrad_
)
{
VLOG
(
MKLDNN_FMTS
)
<<
outGrad_
->
getFormat
()
<<
" <<< "
;
}
for
(
size_t
i
=
0
;
i
<
inGrads_
.
size
();
++
i
)
{
VLOG
(
MKLDNN_FMTS
)
<<
i
<<
" input: "
<<
inGrads_
[
i
]
->
getFormat
()
<<
"<<<"
;
}
}
protected:
/**
* Forward functions: reset buffers(inputs, output, bias),
* reset primitive descriptor,
* reset pipeline.
*/
void
resetFwdBuffers
(
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
bias
,
MKLDNNMatrixPtr
&
out
);
...
...
@@ -110,17 +74,10 @@ protected:
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
bias
,
MKLDNNMatrixPtr
&
out
);
/**
* Backward functions: reset buffers(inputs, output, bias)
*/
void
resetBwdBuffers
(
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
bias
,
MKLDNNMatrixPtr
&
out
);
/**
* prepare for bias
*/
void
prepareBias
(
MKLDNNMatrixPtr
&
bias
,
const
MatrixPtr
&
biasMat
,
const
MKLDNNMatrixPtr
&
out
,
...
...
paddle/gserver/layers/MKLDNNBatchNormLayer.cpp
浏览文件 @
55bee85e
...
...
@@ -116,21 +116,20 @@ void MKLDNNBatchNormLayer::calMovingMeanAndVar() {
}
void
MKLDNNBatchNormLayer
::
reshape
(
int
&
bs
,
int
&
ic
,
int
&
ih
,
int
&
iw
,
int
oc
,
int
&
oh
,
int
&
ow
)
{
int
&
bs
,
int
&
ic
,
int
&
ih
,
int
&
iw
,
int
&
oc
,
int
&
oh
,
int
&
ow
)
{
reshapeInput
(
bs
,
ih
,
iw
);
oh
=
ih
;
ow
=
iw
;
// ic_ and oc can not be changed
CHECK_EQ
(
inputElemenCnt_
/
bs
/
ih
/
iw
,
(
size_t
)
ic
)
CHECK_EQ
((
size_t
)
ic
,
inputLayers_
[
0
]
->
getOutputValue
()
->
getElementCnt
()
/
bs
/
ih
/
iw
)
<<
"Input channel can not be changed"
;
reshapeOutput
(
oh
,
ow
);
resizeOutput
(
bs
,
oc
*
oh
*
ow
);
}
void
MKLDNNBatchNormLayer
::
resetFwd
(
std
::
vector
<
primitive
>&
pipeline
,
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
out
)
{
// In training phase, it will always calculate mean and var,
// so useGlobalStats must be false.
...
...
@@ -140,25 +139,23 @@ void MKLDNNBatchNormLayer::resetFwd(std::vector<primitive>& pipeline,
useGlobalStats_
=
false
;
}
resetFwdBuffers
(
in
,
wgt
,
out
);
resetFwdBuffers
(
in
puts
[
0
],
wgtVal_
,
out
);
resetFwdPD
(
fwdPD_
,
in
,
wgt
,
out
);
resetFwdPD
(
fwdPD_
,
in
puts
[
0
],
wgtVal_
,
out
);
resetFwdPipeline
(
pipeline
,
fwdPD_
,
in
,
wgt
,
out
);
resetFwdPipeline
(
pipeline
,
fwdPD_
,
in
puts
[
0
],
wgtVal_
,
out
);
}
void
MKLDNNBatchNormLayer
::
resetBwd
(
std
::
vector
<
primitive
>&
pipeline
,
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
out
)
{
std
::
shared_ptr
<
bn_bwd
::
primitive_desc
>
pd
;
resetBwdBuffers
(
in
,
wgt
,
out
);
resetBwdBuffers
(
in
puts
[
0
],
wgtGrad_
,
out
);
resetBwdPD
(
pd
,
in
,
wgt
,
out
);
resetBwdPD
(
pd
,
in
puts
[
0
],
wgtGrad_
,
out
);
resetBwdPipeline
(
pipeline
,
pd
,
in
,
wgt
,
out
);
resetBwdPipeline
(
pipeline
,
pd
,
in
puts
[
0
],
wgtGrad_
,
out
);
}
void
MKLDNNBatchNormLayer
::
forward
(
PassType
passType
)
{
...
...
@@ -260,9 +257,9 @@ void MKLDNNBatchNormLayer::resetFwdPipeline(
void
MKLDNNBatchNormLayer
::
resetBwdBuffers
(
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
out
)
{
CHECK
(
inVal
_
&&
outVal_
);
CHECK
(
inVal
s_
[
0
]
&&
outVal_
);
resetOutGrad
(
out
,
outVal_
->
getPrimitiveDesc
());
resetInGrad
(
in
,
inVal
_
->
getPrimitiveDesc
());
resetInGrad
(
in
,
inVal
s_
[
0
]
->
getPrimitiveDesc
());
if
(
gradScaleShift_
)
{
CHECK
(
wgtVal_
);
resetWithMatrix
(
wgt
,
gradScaleShift_
,
wgtVal_
->
getPrimitiveDesc
());
...
...
@@ -297,11 +294,12 @@ void MKLDNNBatchNormLayer::resetBwdPipeline(
if
(
pd
==
nullptr
)
{
return
;
}
CHECK
(
inVal
_
);
CHECK
(
inVal
s_
[
0
]
);
bwdData_
.
reset
(
wgt
&&
wgtVal_
?
new
bn_bwd
(
*
pd
,
*
inVal_
,
*
mean_
,
*
var_
,
*
out
,
*
wgtVal_
,
*
in
,
*
wgt
)
:
new
bn_bwd
(
*
pd
,
*
inVal_
,
*
mean_
,
*
var_
,
*
out
,
*
in
));
?
new
bn_bwd
(
*
pd
,
*
inVals_
[
0
],
*
mean_
,
*
var_
,
*
out
,
*
wgtVal_
,
*
in
,
*
wgt
)
:
new
bn_bwd
(
*
pd
,
*
inVals_
[
0
],
*
mean_
,
*
var_
,
*
out
,
*
in
));
pipeline
.
push_back
(
*
bwdData_
);
}
...
...
paddle/gserver/layers/MKLDNNBatchNormLayer.h
浏览文件 @
55bee85e
...
...
@@ -73,18 +73,14 @@ public:
void
forward
(
PassType
passType
)
override
;
void
reshape
(
int
&
bs
,
int
&
ic
,
int
&
ih
,
int
&
iw
,
int
oc
,
int
&
oh
,
int
&
ow
)
override
;
int
&
bs
,
int
&
ic
,
int
&
ih
,
int
&
iw
,
int
&
oc
,
int
&
oh
,
int
&
ow
)
override
;
void
resetFwd
(
std
::
vector
<
mkldnn
::
primitive
>&
pipeline
,
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
out
)
override
;
void
resetBwd
(
std
::
vector
<
mkldnn
::
primitive
>&
pipeline
,
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
out
)
override
;
void
updateWeights
(
const
UpdateCallback
&
callback
)
override
;
...
...
@@ -98,11 +94,7 @@ protected:
* moving = moving * AvgFraction + local * (1 - AvgFraction)
*/
void
calMovingMeanAndVar
();
/**
* Forward functions: reset buffers(input, weight, output),
* reset primitive descriptor,
* reset pipeline.
*/
void
resetFwdBuffers
(
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
out
);
...
...
@@ -115,12 +107,6 @@ protected:
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
out
);
/**
* Backward functions: reset buffers(input, weight, output),
* reset primitive descriptor,
* reset pipeline.
*/
void
resetBwdBuffers
(
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
out
);
...
...
paddle/gserver/layers/MKLDNNConcatLayer.cpp
浏览文件 @
55bee85e
...
...
@@ -32,17 +32,16 @@ bool MKLDNNConcatLayer::init(const LayerMap& layerMap,
}
void
MKLDNNConcatLayer
::
reshape
(
int
&
bs
,
int
&
ic
,
int
&
ih
,
int
&
iw
,
int
oc
,
int
&
oh
,
int
&
ow
)
{
int
&
bs
,
int
&
ic
,
int
&
ih
,
int
&
iw
,
int
&
oc
,
int
&
oh
,
int
&
ow
)
{
reshapeInput
(
bs
,
ih
,
iw
);
ic
=
inputLayers_
[
0
]
->
getSize
()
/
ih
/
iw
;
CHECK_EQ
((
size_t
)
ic
*
ih
*
iw
,
inputLayers_
[
0
]
->
getSize
());
CHECK_EQ
(
inputElemenCnt_
,
(
size_t
)
bs
*
ic
*
ih
*
iw
);
CHECK_EQ
(
inputLayers_
[
0
]
->
getOutputValue
()
->
getElementCnt
(),
(
size_t
)
bs
*
ic
*
ih
*
iw
);
CHECK_GT
(
inputLayers_
.
size
(),
1UL
);
channels_
.
resize
(
inputLayers_
.
size
());
channels_
[
0
]
=
ic
;
// need change the output channel, so use oc_ instead
// TODO(TJ): change API, use &oc
oc_
=
ic
;
oc
=
ic
;
for
(
size_t
i
=
1
;
i
<
inputLayers_
.
size
();
i
++
)
{
int
batchsize
,
height
,
witdh
;
reshapeInput
(
batchsize
,
height
,
witdh
,
i
);
...
...
@@ -52,37 +51,31 @@ void MKLDNNConcatLayer::reshape(
channels_
[
i
]
=
inputLayers_
[
i
]
->
getSize
()
/
height
/
witdh
;
CHECK_EQ
((
size_t
)
channels_
[
i
]
*
height
*
witdh
,
inputLayers_
[
i
]
->
getSize
());
oc
_
+=
channels_
[
i
];
oc
+=
channels_
[
i
];
}
oh
=
ih
;
ow
=
iw
;
reshapeOutput
(
oh
,
ow
);
resizeOutput
(
bs
,
oc
_
*
oh
*
ow
);
resizeOutput
(
bs
,
oc
*
oh
*
ow
);
}
void
MKLDNNConcatLayer
::
resetFwd
(
std
::
vector
<
primitive
>&
pipeline
,
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
out
)
{
resetFwdBuffers
(
inVals_
,
out
);
in
=
inVals_
[
0
];
resetFwdBuffers
(
inputs
,
out
);
std
::
shared_ptr
<
concat
::
primitive_desc
>
fwdPD
;
resetFwdPD
(
fwdPD
,
in
Vals_
,
out
);
resetFwdPD
(
fwdPD
,
in
puts
,
out
);
resetFwdPipeline
(
pipeline
,
fwdPD
,
in
Vals_
,
out
);
resetFwdPipeline
(
pipeline
,
fwdPD
,
in
puts
,
out
);
}
void
MKLDNNConcatLayer
::
resetBwd
(
std
::
vector
<
primitive
>&
pipeline
,
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
out
)
{
resetBwdBuffers
(
inGrads_
,
out
);
in
=
inGrads_
[
0
];
resetBwdBuffers
(
inputs
,
out
);
resetBwdPipeline
(
pipeline
,
bwds_
,
in
Grads_
,
out
);
resetBwdPipeline
(
pipeline
,
bwds_
,
in
puts
,
out
);
}
void
MKLDNNConcatLayer
::
resetFwdBuffers
(
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
...
...
@@ -90,10 +83,7 @@ void MKLDNNConcatLayer::resetFwdBuffers(std::vector<MKLDNNMatrixPtr>& inputs,
inputs
.
resize
(
inputLayers_
.
size
());
bool
has8c
=
false
,
has16c
=
false
,
hasnc
=
false
;
for
(
size_t
i
=
0
;
i
<
inputs
.
size
();
i
++
)
{
// resetInValue will use ic_ so temporary change as current input's channel
// TODO(TJ): change ic_ as vector then can remove channels_
ic_
=
channels_
[
i
];
resetInValue
(
inputs
[
i
],
nullptr
,
i
);
resetInValue
(
inputs
[
i
],
nullptr
,
i
,
channels_
[
i
]);
CHECK
(
inputs
[
i
]);
auto
dm
=
inputs
[
i
]
->
getDims
();
// inputs format can be different, but ndims must equal
...
...
@@ -114,8 +104,6 @@ void MKLDNNConcatLayer::resetFwdBuffers(std::vector<MKLDNNMatrixPtr>& inputs,
has16c
=
true
;
}
}
// change back, ic_ always save the input 0 size
ic_
=
channels_
[
0
];
format
outFmt
;
if
(
has16c
&&
oc_
%
16
==
0
)
{
...
...
@@ -168,14 +156,9 @@ void MKLDNNConcatLayer::resetBwdBuffers(std::vector<MKLDNNMatrixPtr>& inputs,
inputs
.
resize
(
inputLayers_
.
size
());
for
(
size_t
i
=
0
;
i
<
inputs
.
size
();
i
++
)
{
CHECK
(
inVals_
[
i
]);
// resetInGrad will use inVal_
// TODO(TJ): change move inVals_ to MKLDNNLayer ans remove inVal_
inVal_
=
inVals_
[
i
];
resetInGrad
(
inputs
[
i
],
inVals_
[
i
]
->
getPrimitiveDesc
(),
i
);
CHECK_PRIMITIVE_DESC_EQ
(
inputs
[
i
],
inVals_
[
i
]
->
getPrimitiveDesc
());
}
// change back, inVal_ always save the input 0
inVal_
=
inVals_
[
0
];
}
void
MKLDNNConcatLayer
::
resetBwdPipeline
(
...
...
paddle/gserver/layers/MKLDNNConcatLayer.h
浏览文件 @
55bee85e
...
...
@@ -26,8 +26,6 @@ namespace paddle {
*/
class
MKLDNNConcatLayer
:
public
MKLDNNLayer
{
protected:
std
::
vector
<
MKLDNNMatrixPtr
>
inVals_
;
std
::
vector
<
MKLDNNMatrixPtr
>
inGrads_
;
std
::
vector
<
std
::
shared_ptr
<
mkldnn
::
primitive
>>
bwds_
;
// input channel numbers
std
::
vector
<
int
>
channels_
;
...
...
@@ -47,18 +45,14 @@ public:
const
ParameterMap
&
parameterMap
)
override
;
void
reshape
(
int
&
bs
,
int
&
ic
,
int
&
ih
,
int
&
iw
,
int
oc
,
int
&
oh
,
int
&
ow
)
override
;
int
&
bs
,
int
&
ic
,
int
&
ih
,
int
&
iw
,
int
&
oc
,
int
&
oh
,
int
&
ow
)
override
;
void
resetFwd
(
std
::
vector
<
mkldnn
::
primitive
>&
pipeline
,
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
out
)
override
;
void
resetBwd
(
std
::
vector
<
mkldnn
::
primitive
>&
pipeline
,
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
out
)
override
;
void
printSizeInfo
()
override
{
...
...
@@ -72,38 +66,16 @@ public:
<<
", "
<<
ow_
;
}
void
printValueFormat
()
override
{
for
(
size_t
i
=
0
;
i
<
inVals_
.
size
();
++
i
)
{
VLOG
(
MKLDNN_FMTS
)
<<
"Input "
<<
i
<<
", "
<<
inputLayers_
[
i
]
->
getName
()
<<
": "
<<
inVals_
[
i
]
->
getFormat
()
<<
" >>>"
;
}
if
(
outVal_
)
{
VLOG
(
MKLDNN_FMTS
)
<<
outVal_
->
getFormat
()
<<
" >>> "
;
}
if
(
extOutVal_
)
{
VLOG
(
MKLDNN_FMTS
)
<<
extOutVal_
->
getFormat
();
}
}
void
printGradFormat
()
override
{
if
(
extOutGrad_
)
{
VLOG
(
MKLDNN_FMTS
)
<<
extOutGrad_
->
getFormat
();
}
if
(
outGrad_
)
{
VLOG
(
MKLDNN_FMTS
)
<<
outGrad_
->
getFormat
()
<<
" <<< "
;
}
for
(
size_t
i
=
0
;
i
<
inGrads_
.
size
();
++
i
)
{
VLOG
(
MKLDNN_FMTS
)
<<
"Input "
<<
i
<<
", "
<<
inputLayers_
[
i
]
->
getName
()
<<
": "
<<
inGrads_
[
i
]
->
getFormat
()
<<
"<<<"
;
size_t
keepCondition
()
{
// reset when the total element size of all inputs changed
size_t
totalSize
=
inputLayers_
[
0
]
->
getOutputValue
()
->
getElementCnt
();
for
(
size_t
i
=
1
;
i
<
inputLayers_
.
size
();
++
i
)
{
totalSize
+=
inputLayers_
[
i
]
->
getOutputValue
()
->
getElementCnt
();
}
return
totalSize
;
}
protected:
/**
* Forward functions: reset buffers(inputs, output, bias),
* reset primitive descriptor,
* reset pipeline.
*/
void
resetFwdBuffers
(
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
out
);
void
resetFwdPD
(
std
::
shared_ptr
<
mkldnn
::
concat
::
primitive_desc
>&
pd
,
...
...
@@ -113,11 +85,6 @@ protected:
std
::
shared_ptr
<
mkldnn
::
concat
::
primitive_desc
>&
pd
,
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
out
);
/**
* Backward functions: reset buffers(inputs, output, bias)
* reset primitives and pipeline
*/
void
resetBwdBuffers
(
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
out
);
void
resetBwdPipeline
(
std
::
vector
<
mkldnn
::
primitive
>&
pipeline
,
...
...
paddle/gserver/layers/MKLDNNConvLayer.cpp
浏览文件 @
55bee85e
...
...
@@ -90,7 +90,7 @@ void MKLDNNConvLayer::convertWeightsToPaddle() {
}
void
MKLDNNConvLayer
::
reshape
(
int
&
bs
,
int
&
ic
,
int
&
ih
,
int
&
iw
,
int
oc
,
int
&
oh
,
int
&
ow
)
{
int
&
bs
,
int
&
ic
,
int
&
ih
,
int
&
iw
,
int
&
oc
,
int
&
oh
,
int
&
ow
)
{
reshapeInput
(
bs
,
ih
,
iw
);
// cal output sizes
...
...
@@ -105,21 +105,17 @@ void MKLDNNConvLayer::reshape(
}
void
MKLDNNConvLayer
::
resetFwd
(
std
::
vector
<
primitive
>&
pipeline
,
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
out
)
{
resetFwdPD
(
fwdPD_
);
resetFwdBuffers
(
fwdPD_
,
in
,
wgt
,
bias
,
out
);
resetFwdBuffers
(
fwdPD_
,
in
puts
[
0
],
wgtVal_
,
biasVal_
,
out
);
resetFwdPipeline
(
pipeline
,
fwdPD_
,
in
,
wgt
,
bias
,
out
);
resetFwdPipeline
(
pipeline
,
fwdPD_
,
in
puts
[
0
],
wgtVal_
,
biasVal_
,
out
);
}
void
MKLDNNConvLayer
::
resetBwd
(
std
::
vector
<
primitive
>&
pipeline
,
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
out
)
{
std
::
shared_ptr
<
conv_bwdWgt
::
primitive_desc
>
bwdWgtPD
;
std
::
shared_ptr
<
conv_bwdData
::
primitive_desc
>
bwdDataPD
;
...
...
@@ -128,9 +124,10 @@ void MKLDNNConvLayer::resetBwd(std::vector<primitive>& pipeline,
resetBwdDataPD
(
bwdDataPD
);
resetBwdBuffers
(
bwdWgtPD
,
bwdDataPD
,
in
,
wgt
,
bias
,
out
);
resetBwdBuffers
(
bwdWgtPD
,
bwdDataPD
,
in
puts
[
0
],
wgtGrad_
,
biasGrad_
,
out
);
resetBwdPipeline
(
pipeline
,
bwdWgtPD
,
bwdDataPD
,
in
,
wgt
,
bias
,
out
);
resetBwdPipeline
(
pipeline
,
bwdWgtPD
,
bwdDataPD
,
inputs
[
0
],
wgtGrad_
,
biasGrad_
,
out
);
}
void
MKLDNNConvLayer
::
updateWeights
(
const
UpdateCallback
&
callback
)
{
...
...
@@ -236,14 +233,14 @@ void MKLDNNConvLayer::resetBwdWgtPD(
loadConvSettings
(
wgtDims
,
biasDims
,
strides
,
dilations
,
padL
,
padR
);
// create backward weight using input, output and weight value memory desc
CHECK
(
inVal
_
)
<<
"Should have internal input value"
;
CHECK
(
inVal
s_
[
0
]
)
<<
"Should have internal input value"
;
CHECK
(
outVal_
)
<<
"Should have internal output value"
;
CHECK
(
wgtVal_
)
<<
"Should have weight value"
;
algorithm
algo
=
algorithm
::
convolution_direct
;
padding_kind
padKind
=
padding_kind
::
zero
;
auto
bwdWgtDesc
=
biasVal_
!=
nullptr
?
conv_bwdWgt
::
desc
(
algo
,
inVal
_
->
getMemoryDesc
(),
inVal
s_
[
0
]
->
getMemoryDesc
(),
wgtVal_
->
getMemoryDesc
(),
biasVal_
->
getMemoryDesc
(),
outVal_
->
getMemoryDesc
(),
...
...
@@ -252,7 +249,7 @@ void MKLDNNConvLayer::resetBwdWgtPD(
padR
,
padKind
)
:
conv_bwdWgt
::
desc
(
algo
,
inVal
_
->
getMemoryDesc
(),
inVal
s_
[
0
]
->
getMemoryDesc
(),
wgtVal_
->
getMemoryDesc
(),
outVal_
->
getMemoryDesc
(),
strides
,
...
...
@@ -260,7 +257,7 @@ void MKLDNNConvLayer::resetBwdWgtPD(
padR
,
padKind
);
pd
.
reset
(
new
conv_bwdWgt
::
primitive_desc
(
bwdWgtDesc
,
engine_
,
*
fwdPD_
));
CHECK_PRIMITIVE_DESC_EQ
(
inVal
_
,
pd
->
src_primitive_desc
());
CHECK_PRIMITIVE_DESC_EQ
(
inVal
s_
[
0
]
,
pd
->
src_primitive_desc
());
CHECK_PRIMITIVE_DESC_EQ
(
outVal_
,
pd
->
diff_dst_primitive_desc
(),
...
...
@@ -280,12 +277,12 @@ void MKLDNNConvLayer::resetBwdDataPD(
memory
::
dims
wgtDims
,
biasDims
,
strides
,
dilations
,
padL
,
padR
;
loadConvSettings
(
wgtDims
,
biasDims
,
strides
,
dilations
,
padL
,
padR
);
CHECK
(
inVal
_
)
<<
"Should have internal input value"
;
CHECK
(
inVal
s_
[
0
]
)
<<
"Should have internal input value"
;
CHECK
(
outVal_
)
<<
"Should have internal output value"
;
// create backward data using input and output value memory desc
// but using weight memory desc with any format
auto
bwdDataDesc
=
conv_bwdData
::
desc
(
algorithm
::
convolution_direct
,
inVal
_
->
getMemoryDesc
(),
inVal
s_
[
0
]
->
getMemoryDesc
(),
MKLDNNMatrix
::
createMemoryDesc
(
wgtDims
),
outVal_
->
getMemoryDesc
(),
strides
,
...
...
@@ -294,7 +291,7 @@ void MKLDNNConvLayer::resetBwdDataPD(
padding_kind
::
zero
);
pd
.
reset
(
new
conv_bwdData
::
primitive_desc
(
bwdDataDesc
,
engine_
,
*
fwdPD_
));
CHECK_PRIMITIVE_DESC_EQ
(
inVal
_
,
inVal
s_
[
0
]
,
pd
->
diff_src_primitive_desc
(),
"primitive desc of in value and grad should be equal"
);
CHECK_PRIMITIVE_DESC_EQ
(
...
...
@@ -346,12 +343,12 @@ void MKLDNNConvLayer::resetBwdPipeline(
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
MKLDNNMatrixPtr
&
out
)
{
CHECK
(
inVal
_
);
CHECK
(
inVal
s_
[
0
]
);
// add bwdWgt handle
if
(
bias
)
{
bwdWgt_
.
reset
(
new
conv_bwdWgt
(
*
wgtPD
,
*
inVal
_
,
*
out
,
*
wgt
,
*
bias
));
bwdWgt_
.
reset
(
new
conv_bwdWgt
(
*
wgtPD
,
*
inVal
s_
[
0
]
,
*
out
,
*
wgt
,
*
bias
));
}
else
{
bwdWgt_
.
reset
(
new
conv_bwdWgt
(
*
wgtPD
,
*
inVal
_
,
*
out
,
*
wgt
));
bwdWgt_
.
reset
(
new
conv_bwdWgt
(
*
wgtPD
,
*
inVal
s_
[
0
]
,
*
out
,
*
wgt
));
}
pipeline
.
push_back
(
*
bwdWgt_
);
...
...
paddle/gserver/layers/MKLDNNConvLayer.h
浏览文件 @
55bee85e
...
...
@@ -69,18 +69,14 @@ public:
const
ParameterMap
&
parameterMap
)
override
;
void
reshape
(
int
&
bs
,
int
&
ic
,
int
&
ih
,
int
&
iw
,
int
oc
,
int
&
oh
,
int
&
ow
)
override
;
int
&
bs
,
int
&
ic
,
int
&
ih
,
int
&
iw
,
int
&
oc
,
int
&
oh
,
int
&
ow
)
override
;
void
resetFwd
(
std
::
vector
<
mkldnn
::
primitive
>&
pipeline
,
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
out
)
override
;
void
resetBwd
(
std
::
vector
<
mkldnn
::
primitive
>&
pipeline
,
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
out
)
override
;
void
updateWeights
(
const
UpdateCallback
&
callback
)
override
;
...
...
@@ -107,48 +103,26 @@ protected:
mkldnn
::
memory
::
dims
&
padL
,
mkldnn
::
memory
::
dims
&
padR
);
/**
* reset the forward primitive descriptor.
*/
void
resetFwdPD
(
std
::
shared_ptr
<
conv_fwd
::
primitive_desc
>&
pd
);
/**
* reset the MKLDNNMatrix buffers used in forward.
*/
void
resetFwdBuffers
(
std
::
shared_ptr
<
conv_fwd
::
primitive_desc
>&
pd
,
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
MKLDNNMatrixPtr
&
out
);
/**
* reset the forward pipeline.
*/
void
resetFwdPipeline
(
std
::
vector
<
mkldnn
::
primitive
>&
pipeline
,
std
::
shared_ptr
<
conv_fwd
::
primitive_desc
>&
pd
,
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
MKLDNNMatrixPtr
&
out
);
/**
* reset the backward weight primitive descriptor.
*/
void
resetBwdWgtPD
(
std
::
shared_ptr
<
conv_bwdWgt
::
primitive_desc
>&
pd
);
/**
* reset the backward data primitive descriptor.
*/
void
resetBwdDataPD
(
std
::
shared_ptr
<
conv_bwdData
::
primitive_desc
>&
pd
);
/**
* reset the MKLDNNMatrix buffers used in backward.
*/
void
resetBwdBuffers
(
std
::
shared_ptr
<
conv_bwdWgt
::
primitive_desc
>&
wgtPD
,
std
::
shared_ptr
<
conv_bwdData
::
primitive_desc
>&
dataPD
,
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
MKLDNNMatrixPtr
&
out
);
/**
* reset the backward pipeline.
*/
void
resetBwdPipeline
(
std
::
vector
<
mkldnn
::
primitive
>&
pipeline
,
std
::
shared_ptr
<
conv_bwdWgt
::
primitive_desc
>&
wgtPD
,
std
::
shared_ptr
<
conv_bwdData
::
primitive_desc
>&
dataPD
,
...
...
paddle/gserver/layers/MKLDNNFcLayer.cpp
浏览文件 @
55bee85e
...
...
@@ -74,7 +74,7 @@ void MKLDNNFcLayer::convertWeightsToPaddle() {
}
void
MKLDNNFcLayer
::
reshape
(
int
&
bs
,
int
&
ic
,
int
&
ih
,
int
&
iw
,
int
oc
,
int
&
oh
,
int
&
ow
)
{
int
&
bs
,
int
&
ic
,
int
&
ih
,
int
&
iw
,
int
&
oc
,
int
&
oh
,
int
&
ow
)
{
reshapeInput
(
bs
,
ih
,
iw
);
CHECK_EQ
(
iLayerSize_
,
inputLayers_
[
0
]
->
getSize
());
...
...
@@ -87,32 +87,29 @@ void MKLDNNFcLayer::reshape(
}
void
MKLDNNFcLayer
::
resetFwd
(
std
::
vector
<
primitive
>&
pipeline
,
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
out
)
{
resetFwdBuffers
(
in
,
wgt
,
bias
,
out
);
resetFwdBuffers
(
in
puts
[
0
],
wgtVal_
,
biasVal_
,
out
);
resetFwdPD
(
fwdPD_
,
in
,
wgt
,
bias
,
out
);
resetFwdPD
(
fwdPD_
,
in
puts
[
0
],
wgtVal_
,
biasVal_
,
out
);
resetFwdPipeline
(
pipeline
,
fwdPD_
,
in
,
wgt
,
bias
,
out
);
resetFwdPipeline
(
pipeline
,
fwdPD_
,
in
puts
[
0
],
wgtVal_
,
biasVal_
,
out
);
}
void
MKLDNNFcLayer
::
resetBwd
(
std
::
vector
<
primitive
>&
pipeline
,
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
out
)
{
std
::
shared_ptr
<
fc_bwdWgt
::
primitive_desc
>
bwdWgtPD
;
std
::
shared_ptr
<
fc_bwdData
::
primitive_desc
>
bwdDataPD
;
resetBwdBuffers
(
in
,
wgt
,
bias
,
out
);
resetBwdBuffers
(
in
puts
[
0
],
wgtGrad_
,
biasGrad_
,
out
);
resetBwdWgtPD
(
bwdWgtPD
,
wgt
,
bias
,
out
);
resetBwdWgtPD
(
bwdWgtPD
,
wgt
Grad_
,
biasGrad_
,
out
);
resetBwdDataPD
(
bwdDataPD
,
in
,
out
);
resetBwdDataPD
(
bwdDataPD
,
in
puts
[
0
]
,
out
);
resetBwdPipeline
(
pipeline
,
bwdWgtPD
,
bwdDataPD
,
in
,
wgt
,
bias
,
out
);
resetBwdPipeline
(
pipeline
,
bwdWgtPD
,
bwdDataPD
,
inputs
[
0
],
wgtGrad_
,
biasGrad_
,
out
);
}
void
MKLDNNFcLayer
::
updateWeights
(
const
UpdateCallback
&
callback
)
{
...
...
@@ -193,9 +190,9 @@ void MKLDNNFcLayer::resetBwdBuffers(MKLDNNMatrixPtr& in,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
MKLDNNMatrixPtr
&
out
)
{
CHECK
(
inVal
_
&&
outVal_
);
CHECK
(
inVal
s_
[
0
]
&&
outVal_
);
resetOutGrad
(
out
,
outVal_
->
getPrimitiveDesc
());
resetInGrad
(
in
,
inVal
_
->
getPrimitiveDesc
());
resetInGrad
(
in
,
inVal
s_
[
0
]
->
getPrimitiveDesc
());
CHECK
(
wgtVal_
);
resetWithMatrix
(
wgt
,
weight_
->
getWGrad
(),
wgtVal_
->
getPrimitiveDesc
());
...
...
@@ -212,14 +209,15 @@ void MKLDNNFcLayer::resetBwdWgtPD(
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
MKLDNNMatrixPtr
&
out
)
{
CHECK
(
inVal_
);
fc_bwdWgt
::
desc
bwdWgtDesc
=
bias
?
fc_bwdWgt
::
desc
(
inVal_
->
getMemoryDesc
(),
wgt
->
getMemoryDesc
(),
bias
->
getMemoryDesc
(),
out
->
getMemoryDesc
())
:
fc_bwdWgt
::
desc
(
inVal_
->
getMemoryDesc
(),
wgt
->
getMemoryDesc
(),
out
->
getMemoryDesc
());
CHECK
(
inVals_
[
0
]);
fc_bwdWgt
::
desc
bwdWgtDesc
=
bias
?
fc_bwdWgt
::
desc
(
inVals_
[
0
]
->
getMemoryDesc
(),
wgt
->
getMemoryDesc
(),
bias
->
getMemoryDesc
(),
out
->
getMemoryDesc
())
:
fc_bwdWgt
::
desc
(
inVals_
[
0
]
->
getMemoryDesc
(),
wgt
->
getMemoryDesc
(),
out
->
getMemoryDesc
());
pd
.
reset
(
new
fc_bwdWgt
::
primitive_desc
(
bwdWgtDesc
,
engine_
,
*
fwdPD_
));
}
...
...
@@ -245,11 +243,11 @@ void MKLDNNFcLayer::resetBwdPipeline(
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
MKLDNNMatrixPtr
&
out
)
{
CHECK
(
inVal
_
);
CHECK
(
inVal
s_
[
0
]
);
if
(
bias
)
{
bwdWgt_
.
reset
(
new
fc_bwdWgt
(
*
bwdWgtPD
,
*
inVal
_
,
*
out
,
*
wgt
,
*
bias
));
bwdWgt_
.
reset
(
new
fc_bwdWgt
(
*
bwdWgtPD
,
*
inVal
s_
[
0
]
,
*
out
,
*
wgt
,
*
bias
));
}
else
{
bwdWgt_
.
reset
(
new
fc_bwdWgt
(
*
bwdWgtPD
,
*
inVal
_
,
*
out
,
*
wgt
));
bwdWgt_
.
reset
(
new
fc_bwdWgt
(
*
bwdWgtPD
,
*
inVal
s_
[
0
]
,
*
out
,
*
wgt
));
}
pipeline
.
push_back
(
*
bwdWgt_
);
...
...
paddle/gserver/layers/MKLDNNFcLayer.h
浏览文件 @
55bee85e
...
...
@@ -52,18 +52,14 @@ public:
const
ParameterMap
&
parameterMap
)
override
;
void
reshape
(
int
&
bs
,
int
&
ic
,
int
&
ih
,
int
&
iw
,
int
oc
,
int
&
oh
,
int
&
ow
)
override
;
int
&
bs
,
int
&
ic
,
int
&
ih
,
int
&
iw
,
int
&
oc
,
int
&
oh
,
int
&
ow
)
override
;
void
resetFwd
(
std
::
vector
<
mkldnn
::
primitive
>&
pipeline
,
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
out
)
override
;
void
resetBwd
(
std
::
vector
<
mkldnn
::
primitive
>&
pipeline
,
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
out
)
override
;
void
updateWeights
(
const
UpdateCallback
&
callback
)
override
;
...
...
@@ -73,11 +69,6 @@ public:
void
convertWeightsToPaddle
()
override
;
protected:
/**
* Forward functions: reset buffers(input, output, weight and bias),
* reset primitive descriptor,
* reset pipeline.
*/
void
resetFwdBuffers
(
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
...
...
@@ -93,13 +84,6 @@ protected:
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
MKLDNNMatrixPtr
&
out
);
/**
* Backward functions: reset buffers(input, output, weight and bias),
* reset primitive descriptor for backward weight,
* reset primitive descriptor for backward data,
* reset pipeline.
*/
void
resetBwdBuffers
(
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
...
...
paddle/gserver/layers/MKLDNNLayer.cpp
浏览文件 @
55bee85e
...
...
@@ -48,31 +48,20 @@ void MKLDNNLayer::forward(PassType passType) {
REGISTER_TIMER_INFO
(
"mkldnn_FwdTimer"
,
getName
().
c_str
());
CHECK
(
!
inputLayers_
.
empty
());
copySeqInfoToOutputs
();
size_t
elemenCnt
=
inputLayers_
[
0
]
->
getOutputValue
()
->
getElementCnt
();
if
(
inputElemenCnt_
!=
elemenCnt
)
{
if
(
condition_
!=
keepCondition
())
{
VLOG
(
MKLDNN_BASE
)
<<
getName
()
<<
" reset mkldnn forward"
;
// reset when input total sizes changed, not only the batchsize
inputElemenCnt_
=
elemenCnt
;
pipelineFwd_
.
clear
();
condition_
=
keepCondition
();
reshape
(
bs_
,
ic_
,
ih_
,
iw_
,
oc_
,
oh_
,
ow_
);
// all cpu device output grad or value share output's
printSizeInfo
();
// the output_.value and output_.grad are shared with CPU device
shareCPUDevice
();
resetFwd
(
pipelineFwd_
,
inVal_
,
wgtVal_
,
biasVal_
,
outVal_
);
// MKLDNNLayer output value should be MKLDNNMatrix
// so external output value is necessary.
// Then external input value is not necessary,
// since input may be mkldnn internal buffer.
CHECK
(
extOutVal_
)
<<
"external output value is necessary"
;
output_
.
value
=
std
::
dynamic_pointer_cast
<
Matrix
>
(
extOutVal_
);
CHECK
(
inVal_
&&
outVal_
)
<<
"internal memories are necessary"
;
if
(
cvtInVal_
)
{
pipelineFwd_
.
insert
(
pipelineFwd_
.
begin
(),
*
cvtInVal_
);
}
if
(
cvtOutVal_
)
{
pipelineFwd_
.
push_back
(
*
cvtOutVal_
);
}
pipelineFwd_
.
clear
();
inVals_
.
resize
(
inputLayers_
.
size
(),
nullptr
);
extInVals_
.
resize
(
inputLayers_
.
size
(),
nullptr
);
cvtInVals_
.
resize
(
inputLayers_
.
size
(),
nullptr
);
resetFwd
(
pipelineFwd_
,
inVals_
,
outVal_
);
prepareValueConversions
(
pipelineFwd_
);
convertWeightsFromPaddle
();
printSizeInfo
();
printValueFormat
();
needResetBwd_
=
true
;
}
...
...
@@ -80,8 +69,8 @@ void MKLDNNLayer::forward(PassType passType) {
if
(
inputLayers_
[
0
]
->
getType
()
==
"data"
&&
inputLayers_
.
size
()
==
1
)
{
// Update input value data when input layer is "data" type,
// since the input value data address might be changed.
CHECK
(
extInVal
_
);
extInVal
_
->
setData
(
getInputValue
(
0
,
CPU_DEVICE
)
->
getData
());
CHECK
(
extInVal
s_
[
0
]
);
extInVal
s_
[
0
]
->
setData
(
getInputValue
(
0
,
CPU_DEVICE
)
->
getData
());
}
if
(
!
outputOnlyMKLDNN_
)
{
...
...
@@ -99,22 +88,13 @@ void MKLDNNLayer::backward(const UpdateCallback& callback) {
if
(
needResetBwd_
)
{
VLOG
(
MKLDNN_BASE
)
<<
getName
()
<<
" reset mkldnn backward"
;
pipelineBwd_
.
clear
();
inGrads_
.
resize
(
inputLayers_
.
size
(),
nullptr
);
extInGrads_
.
resize
(
inputLayers_
.
size
(),
nullptr
);
cvtInGrads_
.
resize
(
inputLayers_
.
size
(),
nullptr
);
pipelineMergeGrad_
.
clear
();
mergeGrad_
=
nullptr
;
resetBwd
(
pipelineBwd_
,
inGrad_
,
wgtGrad_
,
biasGrad_
,
outGrad_
);
// external output grad is not necessary
// since output may be mkldnn internal buffer or merge them directly.
CHECK
(
outGrad_
)
<<
"internal output grad is necessary"
;
if
(
extOutGrad_
)
{
CHECK_EQ
(
extOutGrad_
->
getData
(),
output_
.
grad
->
getData
())
<<
"the external buffer should share the same data with output_.grad"
;
}
if
(
cvtOutGrad_
)
{
pipelineBwd_
.
insert
(
pipelineBwd_
.
begin
(),
*
cvtOutGrad_
);
}
if
(
cvtInGrad_
)
{
pipelineBwd_
.
push_back
(
*
cvtInGrad_
);
}
resetBwd
(
pipelineBwd_
,
inGrads_
,
outGrad_
);
prepareGradConversions
(
pipelineBwd_
);
printGradFormat
();
needResetBwd_
=
false
;
}
...
...
@@ -141,8 +121,8 @@ void MKLDNNLayer::backward(const UpdateCallback& callback) {
void
MKLDNNLayer
::
reshapeInput
(
int
&
batchsize
,
int
&
height
,
int
&
width
,
size_t
i
nputI
dx
)
{
const
Argument
&
input
=
inputLayers_
[
i
nputI
dx
]
->
getOutput
();
size_t
idx
)
{
const
Argument
&
input
=
inputLayers_
[
idx
]
->
getOutput
();
batchsize
=
input
.
getBatchSize
();
int
h
=
input
.
getFrameHeight
();
int
w
=
input
.
getFrameWidth
();
...
...
@@ -176,27 +156,30 @@ void MKLDNNLayer::resetWithMatrix(MKLDNNMatrixPtr& dnn,
void
MKLDNNLayer
::
resetInValue
(
MKLDNNMatrixPtr
&
in
,
const
std
::
shared_ptr
<
memory
::
primitive_desc
>&
intPD
,
size_t
inputIdx
)
{
cvtInVal_
=
nullptr
;
extInVal_
=
nullptr
;
size_t
idx
,
int
inputChannel
)
{
cvtInVals_
[
idx
]
=
nullptr
;
extInVals_
[
idx
]
=
nullptr
;
in
=
nullptr
;
CHECK_GT
(
bs_
*
ic_
*
ih_
*
iw_
,
0
);
inputChannel
=
inputChannel
==
0
?
ic_
:
inputChannel
;
CHECK_GT
(
bs_
*
inputChannel
*
ih_
*
iw_
,
0
);
auto
extPD
=
MKLDNNMatrix
::
createPrimitiveDesc
(
{
bs_
,
ic_
,
ih_
,
iw_
},
format
::
nchw
,
engine_
);
const
MatrixPtr
&
inMat
=
inputLayers_
[
inputIdx
]
->
getOutputValue
();
extInVal_
=
std
::
dynamic_pointer_cast
<
MKLDNNMatrix
>
(
inMat
);
CHECK_EQ
(
inputIsOnlyMKLDNN
(),
extInVal_
!=
nullptr
);
if
(
extInVal_
==
nullptr
||
extInVal_
->
getFormat
()
==
format
::
nc
)
{
extInVal_
=
MKLDNNMatrix
::
create
(
extPD
,
inMat
);
{
bs_
,
inputChannel
,
ih_
,
iw_
},
format
::
nchw
,
engine_
);
const
MatrixPtr
&
inMat
=
inputLayers_
[
idx
]
->
getOutputValue
();
extInVals_
[
idx
]
=
std
::
dynamic_pointer_cast
<
MKLDNNMatrix
>
(
inMat
);
CHECK_EQ
(
inputIsOnlyMKLDNN
(),
extInVals_
[
idx
]
!=
nullptr
);
if
(
extInVals_
[
idx
]
==
nullptr
||
extInVals_
[
idx
]
->
getFormat
()
==
format
::
nc
)
{
extInVals_
[
idx
]
=
MKLDNNMatrix
::
create
(
extPD
,
inMat
);
}
in
=
extInVal
_
;
in
=
extInVal
s_
[
idx
]
;
if
(
nullptr
==
intPD
||
in
->
getPrimitiveDesc
()
==
*
intPD
)
{
return
;
}
// need create reorder
in
=
MKLDNNMatrix
::
create
(
*
intPD
);
cvtInVal
_
=
MKLDNNMatrix
::
createReorder
(
extInVal_
,
in
);
CHECK
(
cvtInVal
_
)
<<
"should not be emptry"
;
cvtInVal
s_
[
idx
]
=
MKLDNNMatrix
::
createReorder
(
extInVals_
[
idx
]
,
in
);
CHECK
(
cvtInVal
s_
[
idx
]
)
<<
"should not be emptry"
;
}
void
MKLDNNLayer
::
resetOutValue
(
MKLDNNMatrixPtr
&
out
,
...
...
@@ -218,11 +201,11 @@ void MKLDNNLayer::resetOutValue(MKLDNNMatrixPtr& out,
void
MKLDNNLayer
::
resetInGrad
(
MKLDNNMatrixPtr
&
in
,
memory
::
primitive_desc
intPD
,
size_t
i
nputI
dx
)
{
cvtInGrad
_
=
nullptr
;
extInGrad
_
=
nullptr
;
size_t
idx
)
{
cvtInGrad
s_
[
idx
]
=
nullptr
;
extInGrad
s_
[
idx
]
=
nullptr
;
in
=
nullptr
;
LayerPtr
&
input
=
inputLayers_
[
i
nputI
dx
];
LayerPtr
&
input
=
inputLayers_
[
idx
];
if
(
input
->
getOutputGrad
()
==
nullptr
)
{
// no need input grad
return
;
...
...
@@ -237,23 +220,25 @@ void MKLDNNLayer::resetInGrad(MKLDNNMatrixPtr& in,
in
=
MKLDNNMatrix
::
create
(
intPD
,
inMat
);
Argument
&
arg
=
input
->
getOutput
(
this
->
getName
());
arg
.
grad
=
std
::
dynamic_pointer_cast
<
Matrix
>
(
in
);
CHECK_PRIMITIVE_DESC_EQ
(
inVal
_
,
intPD
);
CHECK_PRIMITIVE_DESC_EQ
(
inVal
s_
[
idx
]
,
intPD
);
if
(
inputIsOnlyMKLDNN
())
{
return
;
}
extInGrad
_
=
in
;
if
(
isPaddleFormat
(
extInGrad
_
->
getFormat
()))
{
extInGrad
s_
[
idx
]
=
in
;
if
(
isPaddleFormat
(
extInGrad
s_
[
idx
]
->
getFormat
()))
{
return
;
}
// need create reorder
CHECK
(
extInVal_
!=
nullptr
&&
isPaddleFormat
(
extInVal_
->
getFormat
()))
CHECK
(
extInVals_
[
idx
]
!=
nullptr
&&
isPaddleFormat
(
extInVals_
[
idx
]
->
getFormat
()))
<<
"should have external input value and the format must be nchw(nc)"
;
extInGrad_
=
MKLDNNMatrix
::
create
(
extInVal_
->
getPrimitiveDesc
(),
inMat
);
CHECK_PRIMITIVE_DESC_EQ
(
inVal_
,
intPD
);
extInGrads_
[
idx
]
=
MKLDNNMatrix
::
create
(
extInVals_
[
idx
]
->
getPrimitiveDesc
(),
inMat
);
CHECK_PRIMITIVE_DESC_EQ
(
inVals_
[
idx
],
intPD
);
in
=
MKLDNNMatrix
::
create
(
intPD
);
cvtInGrad
_
=
MKLDNNMatrix
::
createReorder
(
in
,
extInGrad_
);
CHECK
(
cvtInGrad
_
);
cvtInGrad
s_
[
idx
]
=
MKLDNNMatrix
::
createReorder
(
in
,
extInGrads_
[
idx
]
);
CHECK
(
cvtInGrad
s_
[
idx
]
);
}
void
MKLDNNLayer
::
resetOutGrad
(
MKLDNNMatrixPtr
&
out
,
...
...
paddle/gserver/layers/MKLDNNLayer.h
浏览文件 @
55bee85e
...
...
@@ -34,15 +34,16 @@ typedef std::shared_ptr<MKLDNNLayer> MKLDNNLayerPtr;
*/
class
MKLDNNLayer
:
public
Layer
{
protected:
// input value element count
size_t
inputElemenCnt_
;
// batch size
int
bs_
;
// they sizes are always from the first input layer
// input image channel, height and width
int
ic_
,
ih_
,
iw_
;
// output image channel, height and width
int
oc_
,
oh_
,
ow_
;
// the condition that forward need be reset
size_t
condition_
;
// backward also need reset after reset forward handle
bool
needResetBwd_
;
...
...
@@ -67,18 +68,18 @@ protected:
* When all layers are mkldnn layers, they could save internal data.
*/
// below MKLDNNMatrix buffers are all internal buffers
MKLDNNMatrixPtr
inVal
_
;
MKLDNNMatrixPtr
inGrad
_
;
std
::
vector
<
MKLDNNMatrixPtr
>
inVals
_
;
std
::
vector
<
MKLDNNMatrixPtr
>
inGrads
_
;
MKLDNNMatrixPtr
outVal_
;
MKLDNNMatrixPtr
outGrad_
;
// below are external value and grad
MKLDNNMatrixPtr
extInVal
_
;
MKLDNNMatrixPtr
extInGrad
_
;
std
::
vector
<
MKLDNNMatrixPtr
>
extInVals
_
;
std
::
vector
<
MKLDNNMatrixPtr
>
extInGrads
_
;
MKLDNNMatrixPtr
extOutVal_
;
MKLDNNMatrixPtr
extOutGrad_
;
// convert handle between external and internal buffers
std
::
shared_ptr
<
mkldnn
::
reorder
>
cvtInVal
_
;
std
::
shared_ptr
<
mkldnn
::
reorder
>
cvtInGrad
_
;
std
::
vector
<
std
::
shared_ptr
<
mkldnn
::
reorder
>>
cvtInVals
_
;
std
::
vector
<
std
::
shared_ptr
<
mkldnn
::
reorder
>>
cvtInGrads
_
;
std
::
shared_ptr
<
mkldnn
::
reorder
>
cvtOutVal_
;
std
::
shared_ptr
<
mkldnn
::
reorder
>
cvtOutGrad_
;
...
...
@@ -102,14 +103,7 @@ protected:
public:
explicit
MKLDNNLayer
(
const
LayerConfig
&
config
)
:
Layer
(
config
),
inputElemenCnt_
(
0
),
bs_
(
0
),
ic_
(
0
),
ih_
(
0
),
iw_
(
0
),
oc_
(
0
),
oh_
(
0
),
ow_
(
0
),
condition_
(
0
),
needResetBwd_
(
true
),
outputOnlyMKLDNN_
(
false
),
engine_
(
mkldnn
::
engine
::
cpu
,
0
),
...
...
@@ -125,31 +119,28 @@ public:
virtual
void
backward
(
const
UpdateCallback
&
callback
);
/**
* reshape the input image sizes
* and reset output image and buffer size
* output channel can not be changed
* reshape the input and output channels and image sizes
* and reset output buffer size
*/
virtual
void
reshape
(
int
&
bs
,
int
&
ic
,
int
&
ih
,
int
&
iw
,
int
oc
,
int
&
oh
,
int
&
ow
)
=
0
;
int
&
bs
,
int
&
ic
,
int
&
ih
,
int
&
iw
,
int
&
oc
,
int
&
oh
,
int
&
ow
)
=
0
;
/**
* reset the mkldnn forward primitve and memories
* only would be called when input size changes
* weight and bias buffers should be coverd by child class itself
*/
virtual
void
resetFwd
(
std
::
vector
<
mkldnn
::
primitive
>&
pipeline
,
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
out
)
=
0
;
/**
* reset the mkldnn backward primitve and memories
* only would be called when needed
* weight and bias buffers should be coverd by child class itself
*/
virtual
void
resetBwd
(
std
::
vector
<
mkldnn
::
primitive
>&
pipeline
,
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
out
)
=
0
;
/**
...
...
@@ -175,13 +166,19 @@ public:
void
addOutputArgument
(
int
deviceId
)
{
Layer
::
addOutputArgument
(
deviceId
);
}
protected:
/**
* Some layers may have different condition to reset the forward.
* The function returns the condition that do not need reset forward.
*/
inline
virtual
size_t
keepCondition
()
{
// reset when the first input element size changed, not only the batchsize
return
inputLayers_
[
0
]
->
getOutputValue
()
->
getElementCnt
();
}
/**
* reshape the input image sizes and input batchsize
*/
void
reshapeInput
(
int
&
batchsize
,
int
&
height
,
int
&
width
,
size_t
inputIdx
=
0
);
void
reshapeInput
(
int
&
batchsize
,
int
&
height
,
int
&
width
,
size_t
idx
=
0
);
/**
* reshape output image sizes
...
...
@@ -199,11 +196,13 @@ protected:
/**
* reset input value from input MKLDNNMatrix and internal primitive desc.
* reset both internal and external buffer and create reorder if necessary.
* input channel may be different in concat.
*/
void
resetInValue
(
MKLDNNMatrixPtr
&
in
,
const
std
::
shared_ptr
<
mkldnn
::
memory
::
primitive_desc
>&
intPD
=
nullptr
,
size_t
inputIdx
=
0
);
size_t
idx
=
0
,
int
inputChannel
=
0
);
/**
* reset output value from internal primitive desc.
...
...
@@ -218,7 +217,7 @@ protected:
*/
void
resetInGrad
(
MKLDNNMatrixPtr
&
in
,
mkldnn
::
memory
::
primitive_desc
intPD
,
size_t
i
nputI
dx
=
0
);
size_t
idx
=
0
);
/**
* reset output grad from internal primitive desc.
...
...
@@ -296,17 +295,19 @@ protected:
* print the mkldnn memory format of value
*/
virtual
void
printValueFormat
()
{
if
(
extInVal_
)
{
VLOG
(
MKLDNN_FMTS
)
<<
extInVal_
->
getFormat
()
<<
" >>> "
;
}
if
(
inVal_
)
{
VLOG
(
MKLDNN_FMTS
)
<<
inVal_
->
getFormat
()
<<
" >>>"
;
for
(
size_t
i
=
0
;
i
<
inVals_
.
size
();
++
i
)
{
if
(
!
inVals_
[
i
])
{
continue
;
}
VLOG
(
MKLDNN_FMTS
)
<<
"Input "
<<
i
<<
", "
<<
inputLayers_
[
i
]
->
getName
()
<<
": "
<<
(
extInVals_
[
i
]
?
extInVals_
[
i
]
->
getFormat
()
:
inVals_
[
i
]
->
getFormat
())
<<
" >>> "
<<
inVals_
[
i
]
->
getFormat
()
<<
" >>>"
;
}
if
(
outVal_
)
{
VLOG
(
MKLDNN_FMTS
)
<<
outVal_
->
getFormat
()
<<
" >>> "
;
}
if
(
extOutVal_
)
{
VLOG
(
MKLDNN_FMTS
)
<<
extOutVal_
->
getFormat
();
VLOG
(
MKLDNN_FMTS
)
<<
outVal_
->
getFormat
()
<<
" >>> "
<<
(
extOutVal_
?
extOutVal_
->
getFormat
()
:
outVal_
->
getFormat
());
}
if
(
wgtVal_
)
{
VLOG
(
MKLDNN_FMTS
)
<<
"Weight value format: "
<<
wgtVal_
->
getFormat
();
...
...
@@ -320,17 +321,19 @@ protected:
* print the mkldnn memory format of grad
*/
virtual
void
printGradFormat
()
{
if
(
extOutGrad_
)
{
VLOG
(
MKLDNN_FMTS
)
<<
extOutGrad_
->
getFormat
();
}
if
(
outGrad_
)
{
VLOG
(
MKLDNN_FMTS
)
<<
outGrad_
->
getFormat
()
<<
" <<< "
;
VLOG
(
MKLDNN_FMTS
)
<<
outGrad_
->
getFormat
()
<<
" <<< "
<<
(
extOutGrad_
?
extOutGrad_
->
getFormat
()
:
outGrad_
->
getFormat
());
}
if
(
inGrad_
)
{
VLOG
(
MKLDNN_FMTS
)
<<
inGrad_
->
getFormat
()
<<
" <<<"
;
}
if
(
extInGrad_
)
{
VLOG
(
MKLDNN_FMTS
)
<<
extInGrad_
->
getFormat
()
<<
" <<< "
;
for
(
size_t
i
=
0
;
i
<
inGrads_
.
size
();
++
i
)
{
if
(
!
inGrads_
[
i
])
{
continue
;
}
VLOG
(
MKLDNN_FMTS
)
<<
"Input "
<<
i
<<
", "
<<
inputLayers_
[
i
]
->
getName
()
<<
": "
<<
(
extInGrads_
[
i
]
?
extInGrads_
[
i
]
->
getFormat
()
:
inGrads_
[
i
]
->
getFormat
())
<<
" <<< "
<<
inGrads_
[
i
]
->
getFormat
()
<<
" <<<"
;
}
if
(
wgtGrad_
)
{
VLOG
(
MKLDNN_FMTS
)
<<
"Weight grad format: "
<<
wgtGrad_
->
getFormat
();
...
...
@@ -437,6 +440,41 @@ private:
outputOtherDevice_
[
i
].
cpuSequenceDims
=
output_
.
cpuSequenceDims
;
}
}
void
prepareValueConversions
(
std
::
vector
<
mkldnn
::
primitive
>&
pipeline
)
{
// MKLDNNLayer output value should be MKLDNNMatrix
// so external output value is necessary.
// Then external input value is not necessary,
// since input may be mkldnn internal buffer.
CHECK
(
extOutVal_
)
<<
"external output value is necessary"
;
output_
.
value
=
std
::
dynamic_pointer_cast
<
Matrix
>
(
extOutVal_
);
CHECK
(
inVals_
[
0
]
&&
outVal_
)
<<
"internal memories are necessary"
;
for
(
size_t
i
=
0
;
i
<
cvtInVals_
.
size
();
++
i
)
{
if
(
cvtInVals_
[
i
])
{
pipeline
.
insert
(
pipeline
.
begin
(),
*
cvtInVals_
[
i
]);
}
}
if
(
cvtOutVal_
)
{
pipeline
.
push_back
(
*
cvtOutVal_
);
}
}
void
prepareGradConversions
(
std
::
vector
<
mkldnn
::
primitive
>&
pipeline
)
{
// external output grad is not necessary
// since output may be mkldnn internal buffer or merge them directly.
CHECK
(
outGrad_
)
<<
"internal output grad is necessary"
;
if
(
extOutGrad_
)
{
CHECK_EQ
(
extOutGrad_
->
getData
(),
output_
.
grad
->
getData
())
<<
"the external buffer should share the same data with output_.grad"
;
}
if
(
cvtOutGrad_
)
{
pipeline
.
insert
(
pipeline
.
begin
(),
*
cvtOutGrad_
);
}
for
(
size_t
i
=
0
;
i
<
cvtInGrads_
.
size
();
++
i
)
{
if
(
cvtInGrads_
[
i
])
{
pipeline
.
push_back
(
*
cvtInGrads_
[
i
]);
}
}
}
};
}
// namespace paddle
paddle/gserver/layers/MKLDNNPoolLayer.cpp
浏览文件 @
55bee85e
...
...
@@ -58,10 +58,11 @@ bool MKLDNNPoolLayer::init(const LayerMap& layerMap,
}
void
MKLDNNPoolLayer
::
reshape
(
int
&
bs
,
int
&
ic
,
int
&
ih
,
int
&
iw
,
int
oc
,
int
&
oh
,
int
&
ow
)
{
int
&
bs
,
int
&
ic
,
int
&
ih
,
int
&
iw
,
int
&
oc
,
int
&
oh
,
int
&
ow
)
{
reshapeInput
(
bs
,
ih
,
iw
);
// ic_ and oc can not be changed
CHECK_EQ
(
inputElemenCnt_
/
bs
/
ih
/
iw
,
(
size_t
)
ic
)
CHECK_EQ
((
size_t
)
ic
,
inputLayers_
[
0
]
->
getOutputValue
()
->
getElementCnt
()
/
bs
/
ih
/
iw
)
<<
"Input channel can not be changed"
;
// cal output sizes
...
...
@@ -74,29 +75,25 @@ void MKLDNNPoolLayer::reshape(
}
void
MKLDNNPoolLayer
::
resetFwd
(
std
::
vector
<
primitive
>&
pipeline
,
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
out
)
{
resetFwdBuffers
(
in
,
out
);
resetFwdBuffers
(
in
puts
[
0
]
,
out
);
resetFwdPD
(
fwdPD_
,
in
,
out
);
resetFwdPD
(
fwdPD_
,
in
puts
[
0
]
,
out
);
resetFwdPipeline
(
pipeline
,
fwdPD_
,
in
,
out
);
resetFwdPipeline
(
pipeline
,
fwdPD_
,
in
puts
[
0
]
,
out
);
}
void
MKLDNNPoolLayer
::
resetBwd
(
std
::
vector
<
primitive
>&
pipeline
,
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
out
)
{
std
::
shared_ptr
<
pool_bwd
::
primitive_desc
>
pd
;
resetBwdBuffers
(
in
,
out
);
resetBwdBuffers
(
in
puts
[
0
]
,
out
);
resetBwdPD
(
pd
,
in
,
out
);
resetBwdPD
(
pd
,
in
puts
[
0
]
,
out
);
resetBwdPipeline
(
pipeline
,
pd
,
in
,
out
);
resetBwdPipeline
(
pipeline
,
pd
,
in
puts
[
0
]
,
out
);
}
void
MKLDNNPoolLayer
::
resetFwdBuffers
(
MKLDNNMatrixPtr
&
in
,
...
...
@@ -151,9 +148,9 @@ void MKLDNNPoolLayer::resetFwdPipeline(
void
MKLDNNPoolLayer
::
resetBwdBuffers
(
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
out
)
{
CHECK
(
inVal
_
&&
outVal_
);
CHECK
(
inVal
s_
[
0
]
&&
outVal_
);
resetOutGrad
(
out
,
outVal_
->
getPrimitiveDesc
());
resetInGrad
(
in
,
inVal
_
->
getPrimitiveDesc
());
resetInGrad
(
in
,
inVal
s_
[
0
]
->
getPrimitiveDesc
());
}
void
MKLDNNPoolLayer
::
resetBwdPD
(
std
::
shared_ptr
<
pool_bwd
::
primitive_desc
>&
pd
,
...
...
paddle/gserver/layers/MKLDNNPoolLayer.h
浏览文件 @
55bee85e
...
...
@@ -53,18 +53,14 @@ public:
const
ParameterMap
&
parameterMap
)
override
;
void
reshape
(
int
&
bs
,
int
&
ic
,
int
&
ih
,
int
&
iw
,
int
oc
,
int
&
oh
,
int
&
ow
)
override
;
int
&
bs
,
int
&
ic
,
int
&
ih
,
int
&
iw
,
int
&
oc
,
int
&
oh
,
int
&
ow
)
override
;
void
resetFwd
(
std
::
vector
<
mkldnn
::
primitive
>&
pipeline
,
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
out
)
override
;
void
resetBwd
(
std
::
vector
<
mkldnn
::
primitive
>&
pipeline
,
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
out
)
override
;
void
printSizeInfo
()
override
{
...
...
@@ -75,11 +71,6 @@ public:
}
protected:
/**
* Forward functions: reset buffers(input, output),
* reset primitive descriptor,
* reset pipeline.
*/
void
resetFwdBuffers
(
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
out
);
void
resetFwdPD
(
std
::
shared_ptr
<
pool_fwd
::
primitive_desc
>&
pd
,
MKLDNNMatrixPtr
in
,
...
...
@@ -88,12 +79,6 @@ protected:
std
::
shared_ptr
<
pool_fwd
::
primitive_desc
>&
pd
,
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
out
);
/**
* Backward functions: reset buffers(input, output),
* reset primitive descriptor,
* reset pipeline.
*/
void
resetBwdBuffers
(
MKLDNNMatrixPtr
&
in
,
MKLDNNMatrixPtr
&
out
);
void
resetBwdPD
(
std
::
shared_ptr
<
pool_bwd
::
primitive_desc
>&
pd
,
MKLDNNMatrixPtr
&
in
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录