Skip to content

Commit ef2f0ec

Browse files
authored
Merge pull request #5076 from luotao1/expand_test
add test_Expand and simply the gserver/tests/CMakeLists
2 parents 878dd88 + 5513f92 commit ef2f0ec

File tree

2 files changed

+176
-116
lines changed

2 files changed

+176
-116
lines changed

paddle/gserver/tests/CMakeLists.txt

Lines changed: 49 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
# gserver pacakge unittests
22

3-
if(NOT MOBILE_INFERENCE)
4-
################### test_ProtoDataProvider ############
5-
add_unittest_without_exec(test_ProtoDataProvider
6-
test_ProtoDataProvider.cpp)
7-
8-
# test_ProtoDataProvider will mkdir as same name,
9-
# so if WORKING_DIRECTORY is default directory, then
10-
# mkdir will get error.
11-
add_test(NAME test_ProtoDataProvider
12-
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/test_ProtoDataProvider
13-
WORKING_DIRECTORY ${PADDLE_SOURCE_DIR}/paddle)
14-
endif()
3+
add_simple_unittest(test_LinearChainCRF)
4+
add_simple_unittest(test_MultinomialSampler)
5+
add_simple_unittest(test_RecurrentLayer)
156

16-
################# test_LayerGrad #######################
17-
add_unittest_without_exec(test_LayerGrad
18-
test_LayerGrad.cpp
19-
LayerGradUtil.cpp)
20-
add_test(NAME test_LayerGrad
21-
COMMAND test_LayerGrad)
7+
function(gserver_test TARGET)
8+
add_unittest_without_exec(${TARGET}
9+
${TARGET}.cpp
10+
LayerGradUtil.cpp)
11+
add_test(NAME ${TARGET}
12+
COMMAND ${TARGET})
13+
endfunction()
14+
15+
gserver_test(test_LayerGrad)
16+
gserver_test(test_CRFLayerGrad)
17+
gserver_test(test_CrossEntropyOverBeamGrad)
18+
gserver_test(test_SeqSliceLayerGrad)
19+
gserver_test(test_ActivationGrad)
20+
gserver_test(test_ConvTrans)
21+
gserver_test(test_PriorBox)
22+
gserver_test(test_DetectionOutput)
23+
gserver_test(test_ConvUnify)
24+
gserver_test(test_BatchNorm)
25+
gserver_test(test_KmaxSeqScore)
26+
gserver_test(test_Expand)
2227

2328
########## test_Mkldnn layers and activations ##########
2429
if(WITH_MKLDNN)
@@ -32,89 +37,6 @@ if(WITH_MKLDNN)
3237
WORKING_DIRECTORY ${PADDLE_SOURCE_DIR}/paddle)
3338
endif()
3439

35-
################ test_CRFLayerGrad ####################
36-
add_unittest_without_exec(test_CRFLayerGrad
37-
test_CRFLayerGrad.cpp
38-
LayerGradUtil.cpp)
39-
add_test(NAME test_CRFLayerGrad
40-
COMMAND test_CRFLayerGrad)
41-
42-
################ test_CrossEntropyOverBeam ####################
43-
add_unittest_without_exec(test_CrossEntropyOverBeam
44-
test_CrossEntropyOverBeamGrad.cpp
45-
LayerGradUtil.cpp)
46-
add_test(NAME test_CrossEntropyOverBeam
47-
COMMAND test_CrossEntropyOverBeam)
48-
49-
################ test_SeqSliceLayerGrad ####################
50-
add_unittest_without_exec(test_SeqSliceLayerGrad
51-
test_SeqSliceLayerGrad.cpp
52-
LayerGradUtil.cpp)
53-
add_test(NAME test_SeqSliceLayerGrad
54-
COMMAND test_SeqSliceLayerGrad)
55-
56-
add_unittest_without_exec(test_ActivationGrad
57-
test_ActivationGrad.cpp
58-
LayerGradUtil.cpp)
59-
add_test(NAME test_ActivationGrad
60-
COMMAND test_ActivationGrad)
61-
################# test_ConvTrans #######################
62-
add_unittest_without_exec(test_ConvTrans
63-
test_ConvTrans.cpp
64-
LayerGradUtil.cpp)
65-
66-
add_test(NAME test_ConvTrans
67-
COMMAND test_ConvTrans)
68-
################# test_PriorBox #######################
69-
add_unittest_without_exec(test_PriorBox
70-
test_PriorBox.cpp
71-
LayerGradUtil.cpp)
72-
73-
add_test(NAME test_PriorBox
74-
COMMAND test_PriorBox)
75-
################# test_DetectionOutput #######################
76-
add_unittest_without_exec(test_DetectionOutput
77-
test_DetectionOutput.cpp
78-
LayerGradUtil.cpp)
79-
80-
add_test(NAME test_DetectionOutput
81-
COMMAND test_DetectionOutput)
82-
################# test_ConvUnify #######################
83-
add_unittest_without_exec(test_ConvUnify
84-
test_ConvUnify.cpp
85-
LayerGradUtil.cpp)
86-
87-
add_test(NAME test_ConvUnify
88-
COMMAND test_ConvUnify)
89-
################# test_BatchNorm #######################
90-
add_unittest_without_exec(test_BatchNorm
91-
test_BatchNorm.cpp
92-
LayerGradUtil.cpp)
93-
94-
add_test(NAME test_BatchNorm
95-
COMMAND test_BatchNorm)
96-
97-
98-
################# test_KmaxSeqScore #######################
99-
add_unittest_without_exec(test_KmaxSeqScore
100-
test_KmaxSeqScore.cpp
101-
LayerGradUtil.cpp)
102-
103-
add_test(NAME test_KmaxSeqScore
104-
COMMAND test_KmaxSeqScore)
105-
106-
if(NOT MOBILE_INFERENCE)
107-
################## test_Evaluator #######################
108-
add_unittest(test_Evaluator
109-
test_Evaluator.cpp)
110-
endif()
111-
112-
################ test_LinearChainCRF ####################
113-
add_simple_unittest(test_LinearChainCRF)
114-
115-
############## test_MultinomialSampler ###################
116-
add_simple_unittest(test_MultinomialSampler)
117-
11840
############## test_PyDataProvider ########################
11941
if(WITH_PYTHON)
12042
add_unittest_without_exec(test_PyDataProvider
@@ -125,9 +47,6 @@ if(WITH_PYTHON)
12547
WORKING_DIRECTORY ${PADDLE_SOURCE_DIR}/paddle)
12648
endif()
12749

128-
############### test_RecurrentLayer #######################
129-
add_simple_unittest(test_RecurrentLayer)
130-
13150
############### test_WarpCTCLayer #######################
13251
if(NOT WITH_DOUBLE)
13352
add_unittest_without_exec(test_WarpCTCLayer
@@ -139,19 +58,33 @@ if(NOT WITH_DOUBLE)
13958
endif()
14059

14160
if(NOT MOBILE_INFERENCE)
142-
############### test_RecurrentGradientMachine ###############
143-
# TODO(yuyang18): There is some bug in test_RecurrentGradientMachine
144-
# I will fix it.
145-
add_unittest_without_exec(test_RecurrentGradientMachine
146-
test_RecurrentGradientMachine.cpp)
147-
add_test(NAME test_RecurrentGradientMachine
148-
COMMAND .set_python_path.sh -d
149-
${PADDLE_SOURCE_DIR}/python:${PADDLE_SOURCE_DIR}/paddle/gserver/tests
150-
${CMAKE_CURRENT_BINARY_DIR}/test_RecurrentGradientMachine
151-
WORKING_DIRECTORY ${PADDLE_SOURCE_DIR}/paddle)
152-
endif()
61+
################### test_ProtoDataProvider ############
62+
add_unittest_without_exec(test_ProtoDataProvider
63+
test_ProtoDataProvider.cpp)
15364

154-
if(NOT MOBILE_INFERENCE)
65+
# test_ProtoDataProvider will mkdir as same name,
66+
# so if WORKING_DIRECTORY is default directory, then
67+
# mkdir will get error.
68+
add_test(NAME test_ProtoDataProvider
69+
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/test_ProtoDataProvider
70+
WORKING_DIRECTORY ${PADDLE_SOURCE_DIR}/paddle)
71+
72+
################## test_Evaluator #######################
73+
add_unittest(test_Evaluator
74+
test_Evaluator.cpp)
75+
76+
############### test_RecurrentGradientMachine ###############
77+
# TODO(yuyang18): There is some bug in test_RecurrentGradientMachine
78+
# I will fix it.
79+
add_unittest_without_exec(test_RecurrentGradientMachine
80+
test_RecurrentGradientMachine.cpp)
81+
add_test(NAME test_RecurrentGradientMachine
82+
COMMAND .set_python_path.sh -d
83+
${PADDLE_SOURCE_DIR}/python:${PADDLE_SOURCE_DIR}/paddle/gserver/tests
84+
${CMAKE_CURRENT_BINARY_DIR}/test_RecurrentGradientMachine
85+
WORKING_DIRECTORY ${PADDLE_SOURCE_DIR}/paddle)
86+
87+
############### test_NetworkCompare ###############
15588
add_unittest_without_exec(test_NetworkCompare
15689
test_NetworkCompare.cpp)
15790
if(WITH_GPU)

paddle/gserver/tests/test_Expand.cpp

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#include <gtest/gtest.h>
16+
#include <string>
17+
#include <vector>
18+
19+
#include "LayerGradUtil.h"
20+
#include "paddle/testing/TestUtil.h"
21+
22+
using namespace paddle; // NOLINT
23+
using namespace std; // NOLINT
24+
25+
// Do one forward pass of expand layer and check to see if its output
26+
// matches the given result.(Test onlyCPU currently.)
27+
void doOneExpandTest(string trans_type,
28+
bool hasSubseq,
29+
bool useGpu,
30+
Argument& input1,
31+
Argument& input2,
32+
Argument& result) {
33+
FLAGS_use_gpu = false;
34+
// Setting up the expand layer
35+
TestConfig config;
36+
config.layerConfig.set_type("expand");
37+
38+
auto inputType1 =
39+
trans_type == "non-seq" ? INPUT_DENSE_DIM_DATA : INPUT_SEQUENCE_DATA;
40+
config.inputDefs.push_back({inputType1, "layer0", 1, 0});
41+
auto inputType2 =
42+
hasSubseq ? INPUT_HASSUB_SEQUENCE_DATA : INPUT_SEQUENCE_DATA;
43+
44+
config.inputDefs.push_back({inputType2, "layer1", 1, 0});
45+
config.layerConfig.add_inputs();
46+
config.layerConfig.add_inputs();
47+
config.layerConfig.set_trans_type(trans_type);
48+
49+
// data layer initialize
50+
std::vector<DataLayerPtr> dataLayers;
51+
LayerMap layerMap;
52+
vector<Argument> datas;
53+
initDataLayer(
54+
config, &dataLayers, &datas, &layerMap, "expand", 1, false, useGpu);
55+
dataLayers[0]->getOutput() = input1;
56+
dataLayers[1]->getOutput() = input2;
57+
58+
// test layer initialize
59+
std::vector<ParameterPtr> parameters;
60+
LayerPtr expandLayer;
61+
initTestLayer(config, &layerMap, &parameters, &expandLayer);
62+
expandLayer->forward(PASS_GC);
63+
checkMatrixEqual(expandLayer->getOutputValue(), result.value);
64+
}
65+
66+
TEST(Layer, ExpandLayerFwd) {
67+
bool useGpu = false;
68+
69+
// Assume batch_size =3 in all cases.
70+
71+
// CPU case 1. non-seq expand to seq
72+
// input1 = 1,2,3
73+
// input2 = [4,5],[6],[7,8,9]
74+
// result = [1,1],[2],[3,3,3]
75+
Argument input1, input2, result;
76+
input1.value = Matrix::create(3, 1, false, useGpu);
77+
real input1Data[] = {1, 2, 3};
78+
input1.value->setData(input1Data);
79+
80+
input2.value = Matrix::create(6, 1, false, useGpu);
81+
real input2Data[] = {4, 5, 6, 7, 8, 9};
82+
input2.value->setData(input2Data);
83+
input2.sequenceStartPositions = ICpuGpuVector::create(4, useGpu);
84+
int input2Seq[] = {0, 2, 3, 6};
85+
input2.sequenceStartPositions->copyFrom(input2Seq, 4, useGpu);
86+
87+
result.value = Matrix::create(6, 1, false, useGpu);
88+
real resultData[] = {1, 1, 2, 3, 3, 3};
89+
result.value->setData(resultData);
90+
91+
doOneExpandTest("non-seq", false, useGpu, input1, input2, result);
92+
93+
// CPU case 2. non-seq expand to sub-seq
94+
// NOTE: input1.batch_size == input2.sequencelength in this case.
95+
// i.e, input1 expands by input2.sequence
96+
// input1 = 1,2,3
97+
// input2 = [[4,5]],[[6]],[[7],[8,9]]
98+
// result = [[1,1]],[[2]],[[3],[3,3]]
99+
input2.subSequenceStartPositions = ICpuGpuVector::create(5, useGpu);
100+
int input2SubSeq[] = {0, 2, 3, 4, 6};
101+
input2.subSequenceStartPositions->copyFrom(input2SubSeq, 5, useGpu);
102+
103+
doOneExpandTest("non-seq", true, useGpu, input1, input2, result);
104+
105+
// CPU case 3. seq expand to sub-seq
106+
// input1 = [1,2],[3],[4]
107+
// input2 = [[4,5]],[[6]],[[7],[8,9]]
108+
// result = [[1,1]],[[2]],[[3],[4,4]]
109+
Matrix::resizeOrCreate(input1.value, 4, 1, false, useGpu);
110+
real input1Data_case3[] = {1, 2, 3, 4};
111+
input1.value->setData(input1Data_case3);
112+
113+
input1.sequenceStartPositions = ICpuGpuVector::create(4, useGpu);
114+
int input1Seq[] = {0, 2, 3, 4};
115+
input1.sequenceStartPositions->copyFrom(input1Seq, 4, useGpu);
116+
117+
real resultData_case3[] = {1, 1, 2, 3, 4, 4};
118+
result.value->setData(resultData_case3);
119+
120+
doOneExpandTest("seq", true, useGpu, input1, input2, result);
121+
}
122+
123+
int main(int argc, char** argv) {
124+
testing::InitGoogleTest(&argc, argv);
125+
initMain(argc, argv);
126+
return RUN_ALL_TESTS();
127+
}

0 commit comments

Comments
 (0)