Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
f3bb7b99
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看板
提交
f3bb7b99
编写于
9月 11, 2017
作者:
T
tensor-tang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refine MKLDNNTester add UpdateCallback for test
上级
94ea8ee0
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
51 addition
and
38 deletion
+51
-38
paddle/gserver/tests/MKLDNNTester.cpp
paddle/gserver/tests/MKLDNNTester.cpp
+44
-33
paddle/gserver/tests/MKLDNNTester.h
paddle/gserver/tests/MKLDNNTester.h
+7
-5
未找到文件。
paddle/gserver/tests/MKLDNNTester.cpp
浏览文件 @
f3bb7b99
...
@@ -63,8 +63,12 @@ void MKLDNNTester::reset(const TestConfig& dnn,
...
@@ -63,8 +63,12 @@ void MKLDNNTester::reset(const TestConfig& dnn,
initTestLayer
(
initTestLayer
(
configs_
[
i
],
&
(
layerMaps_
[
i
]),
&
(
parameters_
[
i
]),
&
(
testLayers_
[
i
]));
configs_
[
i
],
&
(
layerMaps_
[
i
]),
&
(
parameters_
[
i
]),
&
(
testLayers_
[
i
]));
}
}
dnnLayer_
=
testLayers_
[
DNN
];
refLayer_
=
testLayers_
[
REF
];
refLayer_
=
testLayers_
[
REF
];
dnnLayer_
=
std
::
dynamic_pointer_cast
<
MKLDNNLayer
>
(
testLayers_
[
DNN
]);
CHECK
(
dnnLayer_
);
// for comparison with Paddle reference results,
// need manually add cpu device output for test
dnnLayer_
->
addOutputArgument
(
-
1
);
EXPECT_EQ
(
dataLayers_
[
DNN
].
size
(),
dataLayers_
[
REF
].
size
());
EXPECT_EQ
(
dataLayers_
[
DNN
].
size
(),
dataLayers_
[
REF
].
size
());
EXPECT_EQ
(
parameters_
[
DNN
].
size
(),
parameters_
[
REF
].
size
());
EXPECT_EQ
(
parameters_
[
DNN
].
size
(),
parameters_
[
REF
].
size
());
...
@@ -109,20 +113,21 @@ void MKLDNNTester::randomBotDatas() {
...
@@ -109,20 +113,21 @@ void MKLDNNTester::randomBotDatas() {
void
MKLDNNTester
::
randomTopDiffs
()
{
void
MKLDNNTester
::
randomTopDiffs
()
{
refLayer_
->
getOutputGrad
()
->
randomizeUniform
();
refLayer_
->
getOutputGrad
()
->
randomizeUniform
();
dnnLayer_
->
getOutput
Grad
()
->
copyFrom
(
*
(
refLayer_
->
getOutputGrad
()));
dnnLayer_
->
getOutput
(
-
1
).
grad
->
copyFrom
(
*
(
refLayer_
->
getOutputGrad
()));
VLOG
(
lvl_
)
<<
"Random
dom
Backward Input, TopDiff: "
;
VLOG
(
lvl_
)
<<
"Random Backward Input, TopDiff: "
;
printMatrix
(
refLayer_
->
getOutputGrad
());
printMatrix
(
refLayer_
->
getOutputGrad
());
}
}
void
MKLDNNTester
::
checkForward
()
{
void
MKLDNNTester
::
checkForward
()
{
printTopDatas
();
double
delta
=
compareMatrix
(
testLayers_
[
DNN
]
->
getOutputValue
(),
testLayers_
[
REF
]
->
getOutputValue
());
VLOG
(
MKLDNN_ALL
)
<<
"Check Forward"
;
VLOG
(
MKLDNN_ALL
)
<<
"Check Forward"
;
printTopDatas
();
double
delta
=
compareMatrix
(
dnnLayer_
->
getOutput
(
-
1
).
value
,
refLayer_
->
getOutputValue
());
EXPECT_LE
(
fabs
(
delta
),
eps_
);
EXPECT_LE
(
fabs
(
delta
),
eps_
);
}
}
void
MKLDNNTester
::
checkBackwardData
()
{
void
MKLDNNTester
::
checkBackwardData
()
{
VLOG
(
MKLDNN_ALL
)
<<
"Check Backward Data"
;
// TODO(TJ): uncomment me when batch norm ready
// TODO(TJ): uncomment me when batch norm ready
// const bool isBN = dnnLayer_->getType() == "mkldnn_batch_norm";
// const bool isBN = dnnLayer_->getType() == "mkldnn_batch_norm";
for
(
size_t
i
=
0
;
i
<
dataLayers_
[
DNN
].
size
();
++
i
)
{
for
(
size_t
i
=
0
;
i
<
dataLayers_
[
DNN
].
size
();
++
i
)
{
...
@@ -144,14 +149,12 @@ void MKLDNNTester::checkBackwardData() {
...
@@ -144,14 +149,12 @@ void MKLDNNTester::checkBackwardData() {
}
}
void
MKLDNNTester
::
checkBackwardWgts
()
{
void
MKLDNNTester
::
checkBackwardWgts
()
{
VLOG
(
MKLDNN_ALL
)
<<
"Check Backward Weight"
;
CHECK_EQ
(
parameters_
[
DNN
].
size
(),
parameters_
[
REF
].
size
());
CHECK_EQ
(
parameters_
[
DNN
].
size
(),
parameters_
[
REF
].
size
());
vector
<
VectorPtr
>
dnnWgts
;
// used to temply save mkldnn weights
vector
<
VectorPtr
>
dnnWgts
;
// used to temply save mkldnn weights
saveWgt
(
parameters_
[
DNN
],
dnnWgts
);
saveWgt
(
parameters_
[
DNN
],
dnnWgts
);
const
MKLDNNLayerPtr
dnnlayer
=
dnnLayer_
->
convertWeightsToPaddle
();
std
::
dynamic_pointer_cast
<
MKLDNNLayer
>
(
dnnLayer_
);
CHECK
(
dnnlayer
);
dnnlayer
->
convertWeightsToPaddle
();
for
(
size_t
i
=
0
;
i
<
parameters_
[
DNN
].
size
();
++
i
)
{
for
(
size_t
i
=
0
;
i
<
parameters_
[
DNN
].
size
();
++
i
)
{
const
VectorPtr
&
dnn
=
parameters_
[
DNN
][
i
]
->
getBuf
(
PARAMETER_VALUE
);
const
VectorPtr
&
dnn
=
parameters_
[
DNN
][
i
]
->
getBuf
(
PARAMETER_VALUE
);
const
VectorPtr
&
ref
=
parameters_
[
REF
][
i
]
->
getBuf
(
PARAMETER_VALUE
);
const
VectorPtr
&
ref
=
parameters_
[
REF
][
i
]
->
getBuf
(
PARAMETER_VALUE
);
...
@@ -189,38 +192,38 @@ void MKLDNNTester::restoreWgt(const vector<VectorPtr>& from,
...
@@ -189,38 +192,38 @@ void MKLDNNTester::restoreWgt(const vector<VectorPtr>& from,
}
}
// clear parameters grad
// clear parameters grad
void
MKLDNNTester
::
clearWgtDiffs
()
{
void
MKLDNNTester
::
clearWgtDiffs
(
size_t
id
)
{
CHECK_LE
(
id
,
parameters_
.
size
());
for
(
size_t
n
=
0
;
n
<
parameters_
.
size
();
++
n
)
{
for
(
size_t
n
=
0
;
n
<
parameters_
.
size
();
++
n
)
{
for
(
size_t
i
=
0
;
i
<
parameters_
[
n
].
size
();
++
i
)
{
if
(
id
==
n
||
id
==
parameters_
.
size
())
{
const
VectorPtr
&
grad
=
parameters_
[
n
][
i
]
->
getBuf
(
PARAMETER_GRADIENT
);
for
(
size_t
i
=
0
;
i
<
parameters_
[
n
].
size
();
++
i
)
{
if
(
grad
)
{
const
VectorPtr
&
grad
=
parameters_
[
n
][
i
]
->
getBuf
(
PARAMETER_GRADIENT
);
grad
->
zeroMem
();
if
(
grad
)
{
grad
->
zeroMem
();
}
}
}
}
}
}
}
}
}
void
MKLDNNTester
::
clearBotDiffs
()
{
void
MKLDNNTester
::
clearBotDiffs
(
size_t
id
)
{
// dnn and ref
CHECK_LE
(
id
,
dataLayers_
.
size
());
for
(
size_t
n
=
0
;
n
<
dataLayers_
.
size
();
++
n
)
{
for
(
size_t
n
=
0
;
n
<
dataLayers_
.
size
();
++
n
)
{
// all inputs layers
if
(
id
==
n
||
id
==
dataLayers_
.
size
())
{
for
(
size_t
i
=
0
;
i
<
dataLayers_
[
n
].
size
();
++
i
)
{
// clear inputs layers of this specific layer
dataLayers_
[
n
][
i
]
->
getOutputGrad
()
->
zeroMem
();
for
(
size_t
i
=
0
;
i
<
dataLayers_
[
n
].
size
();
++
i
)
{
dataLayers_
[
n
][
i
]
->
getOutputGrad
()
->
zeroMem
();
}
}
}
}
}
}
}
void
MKLDNNTester
::
clearBotDiffs
(
int
n
)
{
void
MKLDNNTester
::
clearTopDatas
(
size_t
id
)
{
CHECK_LT
(
n
,
NUM
);
CHECK_LE
(
id
,
testLayers_
.
size
());
// all inputs layers
for
(
size_t
i
=
0
;
i
<
dataLayers_
[
n
].
size
();
++
i
)
{
dataLayers_
[
n
][
i
]
->
getOutputGrad
()
->
zeroMem
();
}
}
void
MKLDNNTester
::
clearTopDatas
()
{
for
(
size_t
i
=
0
;
i
<
testLayers_
.
size
();
++
i
)
{
for
(
size_t
i
=
0
;
i
<
testLayers_
.
size
();
++
i
)
{
testLayers_
[
i
]
->
getOutputValue
()
->
zeroMem
();
if
(
id
==
i
||
id
==
testLayers_
.
size
())
{
testLayers_
[
i
]
->
getOutputValue
()
->
zeroMem
();
}
}
}
}
}
...
@@ -300,16 +303,24 @@ void MKLDNNTester::runOnce() {
...
@@ -300,16 +303,24 @@ void MKLDNNTester::runOnce() {
checkForward
();
checkForward
();
// test backward
// test backward
// simple updater
UpdateCallback
updateCallback
=
[](
Parameter
*
para
)
{
auto
&
grad
=
para
->
getBuf
(
PARAMETER_GRADIENT
);
auto
&
value
=
para
->
getBuf
(
PARAMETER_VALUE
);
real
lr
=
1e-3
;
value
->
add
(
*
grad
,
lr
);
};
randomTopDiffs
();
randomTopDiffs
();
dnnLayer_
->
backward
(
nullptr
);
dnnLayer_
->
backward
(
updateCallback
);
refLayer_
->
backward
(
nullptr
);
refLayer_
->
backward
(
updateCallback
);
checkBackwardData
();
checkBackwardData
();
checkBackwardWgts
();
checkBackwardWgts
();
// clear buffers
// clear buffers
// ref code will addto the diff, dnn code will writeto it
// ref code will addto the diff, dnn code will writeto it
// and clearTopDatas(
) and clearWgtDiffs() should be coverd by test
layers
// and clearTopDatas(
REF) should be coverd by ref
layers
clearBotDiffs
(
REF
);
clearBotDiffs
(
REF
);
clearWgtDiffs
(
REF
);
}
}
void
MKLDNNTester
::
run
(
const
TestConfig
&
dnn
,
void
MKLDNNTester
::
run
(
const
TestConfig
&
dnn
,
...
...
paddle/gserver/tests/MKLDNNTester.h
浏览文件 @
f3bb7b99
...
@@ -18,6 +18,7 @@ limitations under the License. */
...
@@ -18,6 +18,7 @@ limitations under the License. */
#include <vector>
#include <vector>
#include "LayerGradUtil.h"
#include "LayerGradUtil.h"
#include "paddle/gserver/layers/MKLDNNBase.h"
#include "paddle/gserver/layers/MKLDNNBase.h"
#include "paddle/gserver/layers/MKLDNNLayer.h"
namespace
paddle
{
namespace
paddle
{
...
@@ -40,7 +41,8 @@ protected:
...
@@ -40,7 +41,8 @@ protected:
vector
<
LayerMap
>
layerMaps_
;
vector
<
LayerMap
>
layerMaps_
;
vector
<
vector
<
ParameterPtr
>>
parameters_
;
vector
<
vector
<
ParameterPtr
>>
parameters_
;
vector
<
LayerPtr
>
testLayers_
;
vector
<
LayerPtr
>
testLayers_
;
LayerPtr
dnnLayer_
,
refLayer_
;
LayerPtr
refLayer_
;
MKLDNNLayerPtr
dnnLayer_
;
/// run some iterations, all the result should pass
/// run some iterations, all the result should pass
size_t
iter_
;
size_t
iter_
;
...
@@ -88,10 +90,10 @@ private:
...
@@ -88,10 +90,10 @@ private:
void
checkBackwardData
();
void
checkBackwardData
();
void
checkBackwardWgts
();
void
checkBackwardWgts
();
void
clearWgtDiffs
();
// clear specific layer, clear all when id equals NUM
void
clear
BotDiffs
(
);
void
clear
WgtDiffs
(
size_t
id
=
NUM
);
void
clearBotDiffs
(
int
n
);
// clear specific layer
void
clearBotDiffs
(
size_t
id
=
NUM
);
void
clearTopDatas
();
void
clearTopDatas
(
size_t
id
=
NUM
);
void
printTopDatas
();
void
printTopDatas
();
void
printMatrix
(
const
MatrixPtr
&
m
);
void
printMatrix
(
const
MatrixPtr
&
m
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录