Skip to content

Commit 264e830

Browse files
committed
Fixed unittests for WITH_GPU=OFF and WITH_DISTRIBUTE=OFF build
1 parent 7c5e6d2 commit 264e830

20 files changed

+111
-27
lines changed

paddle/fluid/inference/analysis/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@ function (inference_analysis_test TARGET)
1919
set(multiValueArgs SRCS)
2020
cmake_parse_arguments(analysis_test "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
2121

22+
set(mem_opt "")
23+
if(WITH_GPU)
24+
set(mem_opt "--fraction_of_gpu_memory_to_use=0.5")
25+
endif()
2226
cc_test(${TARGET}
2327
SRCS "${analysis_test_SRCS}"
2428
DEPS analysis
25-
ARGS --inference_model_dir=${PYTHON_TESTS_DIR}/book/word2vec.inference.model --fraction_of_gpu_memory_to_use=0.5)
29+
ARGS --inference_model_dir=${PYTHON_TESTS_DIR}/book/word2vec.inference.model ${mem_opt})
2630
set_tests_properties(${TARGET} PROPERTIES DEPENDS test_word2vec)
2731
endif(WITH_TESTING)
2832
endfunction(inference_analysis_test)

paddle/fluid/pybind/pybind.cc

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -248,15 +248,11 @@ PYBIND11_PLUGIN(core) {
248248
#endif
249249
})
250250
.def("rows", [](SelectedRows &self) {
251-
#ifndef PADDLE_WITH_CUDA
252-
return self.rows();
253-
#else
254-
auto rows = self.rows();
255-
std::vector<int64_t> new_rows;
256-
new_rows.reserve(rows.size());
257-
std::copy(rows.begin(), rows.end(), std::back_inserter(new_rows));
258-
return new_rows;
259-
#endif
251+
auto rows = self.rows();
252+
std::vector<int64_t> new_rows;
253+
new_rows.reserve(rows.size());
254+
std::copy(rows.begin(), rows.end(), std::back_inserter(new_rows));
255+
return new_rows;
260256
});
261257

262258
py::class_<Variable>(m, "Variable", R"DOC(Variable Class.

paddle/fluid/pybind/recordio.cc

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ class RecordIOWriter {
3030
public:
3131
RecordIOWriter(const std::string& filename, recordio::Compressor compressor,
3232
size_t max_num_record)
33-
: stream_(filename), writer_(&stream_, compressor, max_num_record) {}
33+
: closed_(false),
34+
stream_(filename),
35+
writer_(&stream_, compressor, max_num_record) {}
3436

3537
void AppendTensor(const framework::LoDTensor& tensor) {
3638
tensors_.push_back(tensor);
@@ -47,9 +49,17 @@ class RecordIOWriter {
4749
PADDLE_ENFORCE(tensors_.empty());
4850
writer_.Flush();
4951
stream_.close();
52+
closed_ = true;
53+
}
54+
55+
~RecordIOWriter() {
56+
if (!closed_) {
57+
Close();
58+
}
5059
}
5160

5261
private:
62+
bool closed_;
5363
std::vector<framework::LoDTensor> tensors_;
5464
std::ofstream stream_;
5565
recordio::Writer writer_;

python/paddle/dataset/mnist.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,14 @@ def reader():
6868
for i in xrange(buffer_size):
6969
yield images[i, :], int(labels[i])
7070
finally:
71-
m.terminate()
72-
l.terminate()
71+
try:
72+
m.terminate()
73+
except:
74+
pass
75+
try:
76+
l.terminate()
77+
except:
78+
pass
7379

7480
return reader
7581

python/paddle/fluid/tests/book/high-level-api/recognize_digits/test_recognize_digits_conv.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from __future__ import print_function
1515
import argparse
1616
import paddle.fluid as fluid
17+
import paddle.fluid.core as core
1718
import paddle
1819
import sys
1920
import numpy
@@ -134,4 +135,4 @@ def main(use_cuda):
134135

135136
if __name__ == '__main__':
136137
# for use_cuda in (False, True):
137-
main(use_cuda=True)
138+
main(use_cuda=core.is_compiled_with_cuda())

python/paddle/fluid/tests/book/test_recognize_digits.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414
from __future__ import print_function
1515

16+
import paddle.fluid.core as core
1617
import math
1718
import os
1819
import sys
@@ -257,6 +258,8 @@ def __impl__(self):
257258

258259
def inject_all_tests():
259260
for use_cuda in (False, True):
261+
if use_cuda and not core.is_compiled_with_cuda():
262+
continue
260263
for parallel in (False, True):
261264
for nn_type in ('mlp', 'conv'):
262265
inject_test_method(use_cuda, parallel, nn_type, True)

python/paddle/fluid/tests/book/test_word2vec.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def __impl__(*args, **kwargs):
245245
is_sparse=is_sparse,
246246
is_parallel=is_parallel)
247247

248-
if use_cuda and is_sparse:
248+
if (not fluid.core.is_compiled_with_cuda() or use_cuda) and is_sparse:
249249
fn = __impl__
250250
else:
251251
# skip the other test when on CI server

python/paddle/fluid/tests/unittests/CMakeLists.txt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ endif(NOT WITH_MKLDNN)
1212

1313
if(NOT WITH_DISTRIBUTE)
1414
list(REMOVE_ITEM TEST_OPS test_recv_op)
15+
list(REMOVE_ITEM TEST_OPS test_dist_transpiler)
16+
list(REMOVE_ITEM TEST_OPS test_simple_dist_transpiler)
17+
list(REMOVE_ITEM TEST_OPS test_listen_and_serv_op)
18+
LIST(REMOVE_ITEM TEST_OPS test_dist_mnist)
19+
LIST(REMOVE_ITEM TEST_OPS test_dist_word2vec)
1520
endif(NOT WITH_DISTRIBUTE)
1621

1722
list(REMOVE_ITEM TEST_OPS test_seq_concat_op) # FIXME(helin): https://github.com/PaddlePaddle/Paddle/issues/8290
@@ -47,9 +52,11 @@ foreach(TEST_OP ${TEST_OPS})
4752
py_test_modules(${TEST_OP} MODULES ${TEST_OP})
4853
endforeach(TEST_OP)
4954
py_test_modules(test_warpctc_op MODULES test_warpctc_op ENVS FLAGS_warpctc_dir=${WARPCTC_LIB_DIR} SERIAL)
50-
py_test_modules(test_dist_train MODULES test_dist_train SERIAL)
55+
if(WITH_DISTRIBUTE)
56+
py_test_modules(test_dist_train MODULES test_dist_train SERIAL)
57+
set_tests_properties(test_listen_and_serv_op PROPERTIES TIMEOUT 20)
58+
set_tests_properties(test_dist_mnist PROPERTIES TIMEOUT 180)
59+
set_tests_properties(test_dist_word2vec PROPERTIES TIMEOUT 180)
60+
endif()
5161
py_test_modules(test_parallel_executor_crf MODULES test_parallel_executor_crf SERIAL)
5262
py_test_modules(test_parallel_executor_fetch_feed MODULES test_parallel_executor_fetch_feed SERIAL)
53-
set_tests_properties(test_listen_and_serv_op PROPERTIES TIMEOUT 20)
54-
set_tests_properties(test_dist_mnist PROPERTIES TIMEOUT 180)
55-
set_tests_properties(test_dist_word2vec PROPERTIES TIMEOUT 180)

python/paddle/fluid/tests/unittests/test_beam_search_decode_op.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ def test_get_set(self):
100100
np.array_equal(np.array(sentence_scores), expected_data))
101101

102102

103+
@unittest.skipIf(not core.is_compiled_with_cuda(),
104+
"core is not compiled with CUDA")
103105
class TestBeamSearchDecodeOpGPU(TestBeamSearchDecodeOp):
104106
def setUp(self):
105107
self.scope = core.Scope()

python/paddle/fluid/tests/unittests/test_conv2d_transpose_op.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,16 @@ def init_test_case(self):
191191

192192

193193
# ------------ test_cudnn ------------
194+
@unittest.skipIf(not core.is_compiled_with_cuda(),
195+
"core is not compiled with CUDA")
194196
class TestCUDNN(TestConv2dTransposeOp):
195197
def init_op_type(self):
196198
self.use_cudnn = True
197199
self.op_type = "conv2d_transpose"
198200

199201

202+
@unittest.skipIf(not core.is_compiled_with_cuda(),
203+
"core is not compiled with CUDA")
200204
class TestCUDNNWithPad(TestWithPad):
201205
def init_test_case(self):
202206
self.pad = [1, 1]
@@ -212,6 +216,8 @@ def init_op_type(self):
212216
self.op_type = "conv2d_transpose"
213217

214218

219+
@unittest.skipIf(not core.is_compiled_with_cuda(),
220+
"core is not compiled with CUDA")
215221
class TestCUDNNWithStride(TestWithStride):
216222
def init_test_case(self):
217223
self.pad = [1, 1]
@@ -227,6 +233,8 @@ def init_op_type(self):
227233
self.op_type = "conv2d_transpose"
228234

229235

236+
@unittest.skipIf(not core.is_compiled_with_cuda(),
237+
"core is not compiled with CUDA")
230238
class TestCUDNNWithGroups(TestWithGroups):
231239
def init_test_case(self):
232240
self.pad = [1, 1]

0 commit comments

Comments
 (0)