提交 0596cd88 编写于 作者: T tensor-tang

refine test recurrent layer

上级 290edd8f
...@@ -222,6 +222,7 @@ TEST(Layer, RecurrentLayer) { ...@@ -222,6 +222,7 @@ TEST(Layer, RecurrentLayer) {
#define protected public #define protected public
#include "paddle/gserver/layers/GatedRecurrentLayer.h" #include "paddle/gserver/layers/GatedRecurrentLayer.h"
#include "paddle/gserver/layers/LstmLayer.h" #include "paddle/gserver/layers/LstmLayer.h"
#include "paddle/gserver/layers/RecurrentLayer.h"
template <class T> template <class T>
class TestRecurrentLayer { class TestRecurrentLayer {
public: public:
...@@ -422,6 +423,8 @@ TEST(Layer, LstmLayer) { ...@@ -422,6 +423,8 @@ TEST(Layer, LstmLayer) {
#ifdef PADDLE_WITH_MKLML #ifdef PADDLE_WITH_MKLML
#include "paddle/gserver/layers/MKLPackedRecurrentLayer.h"
LayerPtr initMKLPackedLayer(LayerConfig layerConfig, LayerPtr initMKLPackedLayer(LayerConfig layerConfig,
bool reversed, bool reversed,
int layerSize, int layerSize,
...@@ -453,7 +456,31 @@ LayerPtr initMKLPackedLayer(LayerConfig layerConfig, ...@@ -453,7 +456,31 @@ LayerPtr initMKLPackedLayer(LayerConfig layerConfig,
return testLayer; return testLayer;
} }
void checkMKLPackedLayer(LayerPtr testLayer1, LayerPtr testLayer2) { void checkMKLPackedLayer(LayerConfig layerConfig1,
LayerConfig layerConfig2,
bool reversed,
int layerSize,
int batchSize,
bool useBatch1,
bool useBatch2) {
LayerPtr dataLayer;
ParameterPtr para, bias;
if (layerConfig1.type() == "recurrent") {
dataLayer = creatDataLayer("layer_0", batchSize, layerSize, false);
para = creatParameter("para_0", 0, layerSize * layerSize, false);
bias = nullptr;
} else if (layerConfig1.type() == "gated_recurrent") {
dataLayer = creatDataLayer("layer_0", batchSize, layerSize * 3, false);
para = creatParameter("para_0", 0, layerSize * layerSize * 3, false);
bias = creatParameterBias("bias_0", 1, layerSize * 3, false);
}
LayerPtr testLayer1 = initMKLPackedLayer(
layerConfig1, reversed, layerSize, dataLayer, para, bias);
LayerPtr testLayer2 = initMKLPackedLayer(
layerConfig2, reversed, layerSize, dataLayer, para, bias);
const VectorPtr& weightGrad = const VectorPtr& weightGrad =
(testLayer1->getParameters()[0])->getBuf(PARAMETER_GRADIENT); (testLayer1->getParameters()[0])->getBuf(PARAMETER_GRADIENT);
const MatrixPtr& inputGrad = testLayer1->getPrev(0)->getOutputGrad(); const MatrixPtr& inputGrad = testLayer1->getPrev(0)->getOutputGrad();
...@@ -462,78 +489,34 @@ void checkMKLPackedLayer(LayerPtr testLayer1, LayerPtr testLayer2) { ...@@ -462,78 +489,34 @@ void checkMKLPackedLayer(LayerPtr testLayer1, LayerPtr testLayer2) {
CpuMatrix input_grad1(inputGrad->getHeight(), inputGrad->getWidth()); CpuMatrix input_grad1(inputGrad->getHeight(), inputGrad->getWidth());
CpuMatrix input_grad2(inputGrad->getHeight(), inputGrad->getWidth()); CpuMatrix input_grad2(inputGrad->getHeight(), inputGrad->getWidth());
CpuMatrix outputGrad(inputGrad->getHeight(), inputGrad->getWidth());
outputGrad.randomizeUniform();
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
FLAGS_rnn_use_batch = true; FLAGS_rnn_use_batch = useBatch1;
testLayer1->forward(PASS_GC); testLayer1->forward(PASS_GC);
testLayer1->getOutputGrad()->copyFrom(outputGrad); FLAGS_rnn_use_batch = useBatch2;
weightGrad->zero();
inputGrad->zero();
testLayer1->backward(nullptr);
wgt_grad1.copyFrom(*weightGrad);
input_grad1.copyFrom(*inputGrad);
FLAGS_rnn_use_batch = true;
testLayer2->forward(PASS_GC); testLayer2->forward(PASS_GC);
testLayer2->getOutputGrad()->copyFrom(outputGrad);
weightGrad->zero();
inputGrad->zero();
testLayer2->backward(nullptr);
wgt_grad2.copyFrom(*weightGrad); testLayer1->getOutputGrad()->randomizeUniform();
input_grad2.copyFrom(*inputGrad); testLayer2->getOutputGrad()->copyFrom(*testLayer1->getOutputGrad());
checkError(*testLayer1->getOutputValue(), *testLayer2->getOutputValue());
checkError(wgt_grad1, wgt_grad2);
checkError(input_grad1, input_grad2);
}
for (int i = 0; i < 2; i++) {
CpuMatrix outputValue(testLayer2->getOutputValue()->getHeight(),
testLayer2->getOutputValue()->getWidth());
FLAGS_rnn_use_batch = true;
testLayer2->forward(PASS_GC);
outputValue.copyFrom(*testLayer2->getOutputValue());
testLayer2->getOutputGrad()->copyFrom(outputGrad);
weightGrad->zero(); weightGrad->zero();
inputGrad->zero(); inputGrad->zero();
FLAGS_rnn_use_batch = useBatch1;
testLayer2->backward(nullptr); testLayer1->backward(nullptr);
wgt_grad1.copyFrom(*weightGrad); wgt_grad1.copyFrom(*weightGrad);
input_grad1.copyFrom(*inputGrad); input_grad1.copyFrom(*inputGrad);
FLAGS_rnn_use_batch = false;
testLayer2->getOutputValue()->zero();
testLayer2->forward(PASS_GC);
testLayer2->getOutputGrad()->copyFrom(outputGrad);
weightGrad->zero(); weightGrad->zero();
inputGrad->zero(); inputGrad->zero();
FLAGS_rnn_use_batch = useBatch2;
testLayer2->backward(nullptr); testLayer2->backward(nullptr);
wgt_grad2.copyFrom(*weightGrad); wgt_grad2.copyFrom(*weightGrad);
input_grad2.copyFrom(*inputGrad); input_grad2.copyFrom(*inputGrad);
checkError(outputValue, *testLayer2->getOutputValue()); checkError(*testLayer1->getOutputValue(), *testLayer2->getOutputValue());
checkError(wgt_grad1, wgt_grad2); checkError(wgt_grad1, wgt_grad2);
checkError(input_grad1, input_grad2); checkError(input_grad1, input_grad2);
} }
...@@ -556,20 +539,22 @@ TEST(MKLPackedLayer, RecurrentLayer) { ...@@ -556,20 +539,22 @@ TEST(MKLPackedLayer, RecurrentLayer) {
for (auto layerSize : {32, 64, 128, 256, 512}) { for (auto layerSize : {32, 64, 128, 256, 512}) {
for (auto batchSize : {1, 5, 100, 500}) { for (auto batchSize : {1, 5, 100, 500}) {
for (auto reversed : {true, false}) { for (auto reversed : {true, false}) {
LOG(INFO) << " layerSize=" << layerSize << " batchSize=" << batchSize for (auto paddle_use_batch : {true, false}) {
<< " reversed=" << reversed; for (auto MKLPacked_use_batch : {true, false}) {
LOG(INFO) << " layerSize=" << layerSize
LayerPtr dataLayer = << " batchSize=" << batchSize << " reversed=" << reversed
creatDataLayer("layer_0", batchSize, layerSize, false); << " paddle_use_batch=" << paddle_use_batch
ParameterPtr para = << " MKLPacked_use_batch=" << MKLPacked_use_batch;
creatParameter("para_0", 0, layerSize * layerSize, false);
checkMKLPackedLayer(layerConfig1,
LayerPtr testLayer1 = initMKLPackedLayer( layerConfig2,
layerConfig1, reversed, layerSize, dataLayer, para); reversed,
LayerPtr testLayer2 = initMKLPackedLayer( layerSize,
layerConfig2, reversed, layerSize, dataLayer, para); batchSize,
paddle_use_batch,
checkMKLPackedLayer(testLayer1, testLayer2); MKLPacked_use_batch);
}
}
} }
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册