Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
93e22e7b
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
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看板
提交
93e22e7b
编写于
11月 07, 2017
作者:
T
tensor-tang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
enable bias for mkldnn_addto
上级
6f43c936
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
99 addition
and
15 deletion
+99
-15
paddle/gserver/layers/MKLDNNAddtoLayer.cpp
paddle/gserver/layers/MKLDNNAddtoLayer.cpp
+76
-7
paddle/gserver/layers/MKLDNNAddtoLayer.h
paddle/gserver/layers/MKLDNNAddtoLayer.h
+21
-1
paddle/gserver/tests/test_MKLDNN.cpp
paddle/gserver/tests/test_MKLDNN.cpp
+2
-7
未找到文件。
paddle/gserver/layers/MKLDNNAddtoLayer.cpp
浏览文件 @
93e22e7b
...
...
@@ -62,16 +62,14 @@ void MKLDNNAddtoLayer::resetFwd(std::vector<primitive>& pipeline,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
MKLDNNMatrixPtr
&
out
)
{
if
(
biases_
)
{
LOG
(
FATAL
)
<<
"not implemented yet"
;
}
resetFwdBuffers
(
inVals_
,
out
);
resetFwdBuffers
(
inVals_
,
bias
,
out
);
in
=
inVals_
[
0
];
std
::
shared_ptr
<
sum
::
primitive_desc
>
fwdPD
;
resetFwdPD
(
fwdPD
,
inVals_
,
out
);
std
::
shared_ptr
<
sum
::
primitive_desc
>
biasPD
;
resetFwdPD
(
fwdPD
,
biasPD
,
inVals_
,
bias
,
out
);
resetFwdPipeline
(
pipeline
,
fwdPD
,
inVals_
,
out
);
resetFwdPipeline
(
pipeline
,
fwdPD
,
biasPD
,
inVals_
,
bias
,
out
);
}
void
MKLDNNAddtoLayer
::
resetBwd
(
std
::
vector
<
primitive
>&
pipeline
,
...
...
@@ -79,7 +77,7 @@ void MKLDNNAddtoLayer::resetBwd(std::vector<primitive>& pipeline,
MKLDNNMatrixPtr
&
wgt
,
MKLDNNMatrixPtr
&
bias
,
MKLDNNMatrixPtr
&
out
)
{
resetBwdBuffers
(
inGrads_
,
out
);
resetBwdBuffers
(
inGrads_
,
bias
,
out
);
in
=
inGrads_
[
0
];
// backward only need share output grad to input grad
...
...
@@ -89,6 +87,20 @@ void MKLDNNAddtoLayer::resetBwd(std::vector<primitive>& pipeline,
inputLayers_
[
i
]
->
getOutputGrad
()
->
setData
(
inGrads_
[
i
]
->
getData
());
}
}
// backward bias
bwdBias_
=
nullptr
;
if
(
bias
)
{
std
::
vector
<
double
>
scales
(
bs_
,
1.0
);
std
::
vector
<
memory
::
primitive_desc
>
srcPDs
(
bs_
,
bias
->
getPrimitiveDesc
());
auto
biasPD
=
sum
::
primitive_desc
(
bias
->
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
));
pipeline
.
push_back
(
*
bwdBias_
);
}
}
void
MKLDNNAddtoLayer
::
updateWeights
(
const
UpdateCallback
&
callback
)
{
...
...
@@ -97,7 +109,25 @@ void MKLDNNAddtoLayer::updateWeights(const UpdateCallback& callback) {
}
}
void
MKLDNNAddtoLayer
::
prepareBias
(
MKLDNNMatrixPtr
&
bias
,
const
MatrixPtr
&
biasMat
,
const
MKLDNNMatrixPtr
&
out
,
std
::
vector
<
MKLDNNMatrixPtr
>&
outs
)
{
auto
pd
=
MKLDNNMatrix
::
createPrimitiveDesc
(
{(
int
)
layerSize_
},
memory
::
format
::
x
,
engine_
);
bias
=
MKLDNNMatrix
::
create
(
pd
,
biasMat
);
outs
.
clear
();
real
*
data
=
out
->
getData
();
CHECK_EQ
(
bs_
*
layerSize_
,
out
->
getElementCnt
());
for
(
int
i
=
0
;
i
<
bs_
;
++
i
)
{
MatrixPtr
tmp
=
Matrix
::
create
(
data
+
i
*
layerSize_
,
1
,
layerSize_
,
false
,
false
);
outs
.
push_back
(
MKLDNNMatrix
::
create
(
bias
->
getPrimitiveDesc
(),
tmp
));
}
}
void
MKLDNNAddtoLayer
::
resetFwdBuffers
(
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
bias
,
MKLDNNMatrixPtr
&
out
)
{
inputs
.
resize
(
inputLayers_
.
size
());
for
(
size_t
i
=
0
;
i
<
inputs
.
size
();
i
++
)
{
...
...
@@ -110,10 +140,18 @@ void MKLDNNAddtoLayer::resetFwdBuffers(std::vector<MKLDNNMatrixPtr>& inputs,
}
resetOutValue
(
out
,
inputs
[
0
]
->
getPrimitiveDesc
());
if
(
biases_
&&
biases_
->
getW
())
{
prepareBias
(
bias
,
biases_
->
getW
(),
out
,
vals_
);
}
else
{
bias
=
nullptr
;
}
}
void
MKLDNNAddtoLayer
::
resetFwdPD
(
std
::
shared_ptr
<
sum
::
primitive_desc
>&
pd
,
std
::
shared_ptr
<
sum
::
primitive_desc
>&
biasPD
,
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
bias
,
MKLDNNMatrixPtr
out
)
{
std
::
vector
<
double
>
scales
(
inputs
.
size
(),
1.0
);
std
::
vector
<
memory
::
primitive_desc
>
srcPDs
;
...
...
@@ -123,12 +161,23 @@ void MKLDNNAddtoLayer::resetFwdPD(std::shared_ptr<sum::primitive_desc>& pd,
CHECK
(
out
);
pd
.
reset
(
new
sum
::
primitive_desc
(
out
->
getMemoryDesc
(),
scales
,
srcPDs
));
CHECK_PRIMITIVE_DESC_EQ
(
out
,
pd
->
dst_primitive_desc
());
biasPD
=
nullptr
;
if
(
bias
)
{
std
::
vector
<
double
>
scales
(
2
,
1.0
);
std
::
vector
<
memory
::
primitive_desc
>
srcPDs
(
2
,
bias
->
getPrimitiveDesc
());
biasPD
.
reset
(
new
sum
::
primitive_desc
(
bias
->
getMemoryDesc
(),
scales
,
srcPDs
));
CHECK_PRIMITIVE_DESC_EQ
(
bias
,
biasPD
->
dst_primitive_desc
());
}
}
void
MKLDNNAddtoLayer
::
resetFwdPipeline
(
std
::
vector
<
primitive
>&
pipeline
,
std
::
shared_ptr
<
sum
::
primitive_desc
>&
pd
,
std
::
shared_ptr
<
sum
::
primitive_desc
>&
biasPD
,
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
bias
,
MKLDNNMatrixPtr
&
out
)
{
std
::
vector
<
primitive
::
at
>
srcs
;
for
(
size_t
i
=
0
;
i
<
inputs
.
size
();
i
++
)
{
...
...
@@ -136,9 +185,23 @@ void MKLDNNAddtoLayer::resetFwdPipeline(
}
fwd_
.
reset
(
new
sum
(
*
pd
,
srcs
,
*
out
));
pipeline
.
push_back
(
*
fwd_
);
fwdBias_
.
clear
();
if
(
biasPD
==
nullptr
||
bias
==
nullptr
)
{
return
;
}
fwdBias_
.
resize
(
vals_
.
size
());
for
(
size_t
i
=
0
;
i
<
vals_
.
size
();
++
i
)
{
std
::
vector
<
primitive
::
at
>
srcs
;
srcs
.
push_back
(
*
(
vals_
[
i
]));
srcs
.
push_back
(
*
bias
);
fwdBias_
[
i
].
reset
(
new
sum
(
*
biasPD
,
srcs
,
*
vals_
[
i
]));
pipeline
.
push_back
(
*
fwdBias_
[
i
]);
}
}
void
MKLDNNAddtoLayer
::
resetBwdBuffers
(
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
bias
,
MKLDNNMatrixPtr
&
out
)
{
CHECK
(
outVal_
);
resetOutGrad
(
out
,
outVal_
->
getPrimitiveDesc
());
...
...
@@ -149,6 +212,12 @@ void MKLDNNAddtoLayer::resetBwdBuffers(std::vector<MKLDNNMatrixPtr>& inputs,
resetInGrad
(
inputs
[
i
],
inVal_
->
getPrimitiveDesc
(),
i
);
CHECK_PRIMITIVE_DESC_EQ
(
inputs
[
i
],
out
->
getPrimitiveDesc
());
}
if
(
biases_
&&
biases_
->
getWGrad
())
{
prepareBias
(
bias
,
biases_
->
getWGrad
(),
out
,
grads_
);
}
else
{
bias
=
nullptr
;
}
}
}
// namespace paddle
paddle/gserver/layers/MKLDNNAddtoLayer.h
浏览文件 @
93e22e7b
...
...
@@ -32,9 +32,15 @@ protected:
// layer size == ic * ih * iw == oc * oh *ow, and can not be changed
size_t
layerSize_
;
// TODO(TJ): this part has not been optimized by MKL-DNN
std
::
unique_ptr
<
Weight
>
biases_
;
// buffers for adding bias
std
::
vector
<
MKLDNNMatrixPtr
>
vals_
;
std
::
vector
<
MKLDNNMatrixPtr
>
grads_
;
// primitives for adding bias
std
::
vector
<
std
::
shared_ptr
<
mkldnn
::
primitive
>>
fwdBias_
;
std
::
shared_ptr
<
mkldnn
::
primitive
>
bwdBias_
;
public:
explicit
MKLDNNAddtoLayer
(
const
LayerConfig
&
config
)
:
MKLDNNLayer
(
config
)
{}
...
...
@@ -91,20 +97,34 @@ protected:
* reset pipeline.
*/
void
resetFwdBuffers
(
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
&
bias
,
MKLDNNMatrixPtr
&
out
);
void
resetFwdPD
(
std
::
shared_ptr
<
mkldnn
::
sum
::
primitive_desc
>&
pd
,
std
::
shared_ptr
<
mkldnn
::
sum
::
primitive_desc
>&
biasPD
,
std
::
vector
<
MKLDNNMatrixPtr
>&
inputs
,
MKLDNNMatrixPtr
bias
,
MKLDNNMatrixPtr
out
);
void
resetFwdPipeline
(
std
::
vector
<
mkldnn
::
primitive
>&
pipeline
,
std
::
shared_ptr
<
mkldnn
::
sum
::
primitive_desc
>&
pd
,
std
::
shared_ptr
<
mkldnn
::
sum
::
primitive_desc
>&
biasPD
,
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
,
std
::
vector
<
MKLDNNMatrixPtr
>&
outs
);
};
}
// namespace paddle
paddle/gserver/tests/test_MKLDNN.cpp
浏览文件 @
93e22e7b
...
...
@@ -300,13 +300,8 @@ void testAddtoLayer(const testImageDesc& pm, const size_t nInputs) {
TestConfig
dnnConfig
;
getAddtoConfig
(
dnnConfig
,
pm
,
nInputs
);
dnnConfig
.
layerConfig
.
set_type
(
"mkldnn_addto"
);
// TODO(TJ): test with bias
for
(
auto
withBias
:
{
false
})
{
if
(
withBias
)
{
dnnConfig
.
biasSize
=
pm
.
ic
*
pm
.
ih
*
pm
.
iw
;
}
else
{
dnnConfig
.
biasSize
=
0
;
}
for
(
auto
withBias
:
{
false
,
true
})
{
dnnConfig
.
biasSize
=
withBias
?
pm
.
ic
*
pm
.
ih
*
pm
.
iw
:
0
;
RUN_MKLDNN_TEST_LAYER
(
dnnConfig
,
"addto"
,
pm
)
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录