Skip to content

Commit e7eeb19

Browse files
committed
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into parallel-executor-support-prefetch
2 parents 754e96a + 2d21aa7 commit e7eeb19

File tree

5 files changed

+114
-105
lines changed

5 files changed

+114
-105
lines changed

paddle/fluid/framework/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ endif()
2222

2323
cc_test(eigen_test SRCS eigen_test.cc DEPS tensor)
2424

25-
nv_test(mixed_vector_test SRCS mixed_vector_test.cu DEPS place memory device_context tensor)
25+
if(WITH_GPU)
26+
nv_test(mixed_vector_test SRCS mixed_vector_test.cc mixed_vector_test.cu DEPS place memory device_context tensor)
27+
else()
28+
cc_test(mixed_vector_test SRCS mixed_vector_test.cc DEPS place memory device_context tensor)
29+
endif()
30+
2631
cc_library(lod_tensor SRCS lod_tensor.cc DEPS ddim place tensor framework_proto recordio)
2732
cc_test(lod_tensor_test SRCS lod_tensor_test.cc DEPS lod_tensor memory)
2833
nv_test(lod_tensor_gpu_test SRCS lod_tensor_test.cu DEPS lod_tensor)

paddle/fluid/framework/mixed_vector.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <algorithm>
1818
#include <initializer_list>
19+
#include <memory>
1920
#include <vector>
2021

2122
#include "paddle/fluid/framework/tensor.h"
@@ -386,13 +387,14 @@ template <typename T>
386387
class CPUVector : public std::vector<T, std::allocator<T>> {
387388
public:
388389
CPUVector() : std::vector<T>() {}
389-
CPUVector(size_t count, const T &value = T())
390+
CPUVector(size_t count, const T &value = T()) // NOLINT
390391
: std::vector<T>(count, value) {}
391392
CPUVector(std::initializer_list<T> init) : std::vector<T>(init) {}
392-
CPUVector(const std::vector<T> &other) : std::vector<T>(other) {}
393-
explicit CPUVector(const CPUVector<T> &other) : std::vector<T>(other) {}
393+
CPUVector(const std::vector<T> &other) : std::vector<T>(other) {} // NOLINT
394+
CPUVector(const CPUVector<T> &other) : std::vector<T>(other) {}
394395
CPUVector(CPUVector<T> &&other) : std::vector<T>(std::move(other)) {}
395-
CPUVector(std::vector<T> &&other) : std::vector<T>(std::move(other)) {}
396+
CPUVector(std::vector<T> &&other) // NOLINT
397+
: std::vector<T>(std::move(other)) {}
396398
CPUVector &operator=(const CPUVector &other) {
397399
this->assign(other.begin(), other.end());
398400
return *this;
@@ -410,8 +412,6 @@ class CPUVector : public std::vector<T, std::allocator<T>> {
410412
return os;
411413
}
412414

413-
void resize(size_t size) { this->resize(size); }
414-
415415
T &operator[](size_t id) { return this->at(id); }
416416

417417
const T &operator[](size_t id) const { return this->at(id); }
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
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 <memory>
16+
17+
#include "glog/logging.h"
18+
#include "gtest/gtest.h"
19+
#include "paddle/fluid/framework/mixed_vector.h"
20+
21+
template <typename T>
22+
using vec = paddle::framework::Vector<T>;
23+
24+
TEST(mixed_vector, CPU_VECTOR) {
25+
vec<int> tmp;
26+
for (int i = 0; i < 10; ++i) {
27+
tmp.push_back(i);
28+
}
29+
ASSERT_EQ(tmp.size(), 10UL);
30+
vec<int> tmp2;
31+
tmp2 = tmp;
32+
ASSERT_EQ(tmp2.size(), 10UL);
33+
for (int i = 0; i < 10; ++i) {
34+
ASSERT_EQ(tmp2[i], i);
35+
ASSERT_EQ(tmp2[i], tmp[i]);
36+
}
37+
int cnt = 0;
38+
for (auto& t : tmp2) {
39+
ASSERT_EQ(t, cnt);
40+
++cnt;
41+
}
42+
}
43+
44+
TEST(mixed_vector, InitWithCount) {
45+
paddle::framework::Vector<int> vec(10, 10);
46+
for (int i = 0; i < 10; ++i) {
47+
ASSERT_EQ(vec[i], 10);
48+
}
49+
}
50+
51+
TEST(mixed_vector, ForEach) {
52+
vec<int> tmp;
53+
for (auto& v : tmp) {
54+
VLOG(3) << v;
55+
}
56+
}
57+
58+
TEST(mixed_vector, Reserve) {
59+
paddle::framework::Vector<int> vec;
60+
vec.reserve(1);
61+
vec.push_back(0);
62+
vec.push_back(0);
63+
vec.push_back(0);
64+
}
65+
66+
TEST(mixed_vector, Resize) {
67+
paddle::framework::Vector<int> vec;
68+
vec.resize(1);
69+
vec.push_back(0);
70+
vec.push_back(0);
71+
vec.push_back(0);
72+
}

paddle/fluid/framework/mixed_vector_test.cu

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
See the License for the specific language governing permissions and
1313
limitations under the License. */
14+
1415
#include <cuda_runtime.h>
16+
#include <memory>
1517

1618
#include "glog/logging.h"
1719
#include "gtest/gtest.h"
@@ -21,26 +23,6 @@
2123
template <typename T>
2224
using vec = paddle::framework::Vector<T>;
2325

24-
TEST(mixed_vector, CPU_VECTOR) {
25-
vec<int> tmp;
26-
for (int i = 0; i < 10; ++i) {
27-
tmp.push_back(i);
28-
}
29-
ASSERT_EQ(tmp.size(), 10UL);
30-
vec<int> tmp2;
31-
tmp2 = tmp;
32-
ASSERT_EQ(tmp2.size(), 10UL);
33-
for (int i = 0; i < 10; ++i) {
34-
ASSERT_EQ(tmp2[i], i);
35-
ASSERT_EQ(tmp2[i], tmp[i]);
36-
}
37-
int cnt = 0;
38-
for (auto& t : tmp2) {
39-
ASSERT_EQ(t, cnt);
40-
++cnt;
41-
}
42-
}
43-
4426
static __global__ void multiply_10(int* ptr) {
4527
for (int i = 0; i < 10; ++i) {
4628
ptr[i] *= 10;
@@ -91,24 +73,3 @@ TEST(mixed_vector, MultiGPU) {
9173
ASSERT_EQ(tmp[i], i * 100);
9274
}
9375
}
94-
95-
TEST(mixed_vector, InitWithCount) {
96-
paddle::framework::Vector<int> vec(10, 10);
97-
for (int i = 0; i < 10; ++i) {
98-
ASSERT_EQ(vec[i], 10);
99-
}
100-
}
101-
102-
TEST(mixed_vector, ForEach) {
103-
vec<int> tmp;
104-
for (auto& v : tmp) {
105-
}
106-
}
107-
108-
TEST(mixed_vector, Reserve) {
109-
paddle::framework::Vector<int> vec;
110-
vec.reserve(1);
111-
vec.push_back(0);
112-
vec.push_back(0);
113-
vec.push_back(0);
114-
}

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

Lines changed: 28 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -107,44 +107,24 @@ def _init_data(self, random=True):
107107
label = np.ones(shape=[32, 1], dtype='int64')
108108
return img, label
109109

110-
# simple_fc
111-
def check_simple_fc_convergence(self, use_cuda, use_reduce=False):
110+
def _compare_reduce_and_allreduce(self, model, use_cuda, random_data=True):
112111
if use_cuda and not core.is_compiled_with_cuda():
113112
return
114-
self.check_network_convergence(simple_fc_net, use_cuda=use_cuda)
115113
self.check_network_convergence(
116-
simple_fc_net, use_cuda=use_cuda, allow_op_delay=True)
117-
118-
img, label = self._init_data()
119-
114+
model, use_cuda=use_cuda, use_reduce=True)
120115
self.check_network_convergence(
121-
simple_fc_net,
122-
feed_dict={"image": img,
123-
"label": label},
124-
use_cuda=use_cuda,
125-
use_reduce=use_reduce)
116+
model, use_cuda=use_cuda, allow_op_delay=True, use_reduce=True)
126117

127-
def check_simple_fc_convergence_with_Reduce(self, use_cuda):
128-
if use_cuda and not core.is_compiled_with_cuda():
129-
return
130-
self.check_network_convergence(
131-
simple_fc_net, use_cuda=use_cuda, use_reduce=True)
132-
self.check_network_convergence(
133-
simple_fc_net,
134-
use_cuda=use_cuda,
135-
allow_op_delay=True,
136-
use_reduce=True)
137-
138-
img, label = self._init_data()
118+
img, label = self._init_data(random_data)
139119

140120
all_reduce_first_loss, all_reduce_last_loss = self.check_network_convergence(
141-
simple_fc_net,
121+
model,
142122
feed_dict={"image": img,
143123
"label": label},
144124
use_cuda=use_cuda,
145125
use_reduce=False)
146126
reduce_first_loss, reduce_last_loss = self.check_network_convergence(
147-
simple_fc_net,
127+
model,
148128
feed_dict={"image": img,
149129
"label": label},
150130
use_cuda=use_cuda,
@@ -153,7 +133,24 @@ def check_simple_fc_convergence_with_Reduce(self, use_cuda):
153133
for loss in zip(all_reduce_first_loss, reduce_first_loss):
154134
self.assertAlmostEquals(loss[0], loss[1], delta=1e-6)
155135
for loss in zip(all_reduce_last_loss, reduce_last_loss):
156-
self.assertAlmostEquals(loss[0], loss[1], delta=1e-6)
136+
self.assertAlmostEquals(loss[0], loss[1], delta=1e-4)
137+
138+
# simple_fc
139+
def check_simple_fc_convergence(self, use_cuda, use_reduce=False):
140+
if use_cuda and not core.is_compiled_with_cuda():
141+
return
142+
self.check_network_convergence(simple_fc_net, use_cuda=use_cuda)
143+
self.check_network_convergence(
144+
simple_fc_net, use_cuda=use_cuda, allow_op_delay=True)
145+
146+
img, label = self._init_data()
147+
148+
self.check_network_convergence(
149+
simple_fc_net,
150+
feed_dict={"image": img,
151+
"label": label},
152+
use_cuda=use_cuda,
153+
use_reduce=use_reduce)
157154

158155
def test_simple_fc(self):
159156
# use_cuda
@@ -162,8 +159,8 @@ def test_simple_fc(self):
162159

163160
def test_simple_fc_with_new_strategy(self):
164161
# use_cuda, use_reduce
165-
self.check_simple_fc_convergence_with_Reduce(True)
166-
self.check_simple_fc_convergence_with_Reduce(False)
162+
self._compare_reduce_and_allreduce(simple_fc_net, True)
163+
self._compare_reduce_and_allreduce(simple_fc_net, False)
167164

168165
def check_simple_fc_parallel_accuracy(self, use_cuda):
169166
if use_cuda and not core.is_compiled_with_cuda():
@@ -209,39 +206,13 @@ def check_batchnorm_fc_convergence(self, use_cuda):
209206
"label": label},
210207
use_cuda=use_cuda)
211208

212-
def check_batchnorm_fc_convergence_use_reduce(self, use_cuda):
213-
if use_cuda and not core.is_compiled_with_cuda():
214-
return
215-
self.check_network_convergence(
216-
fc_with_batchnorm, use_cuda=use_cuda, use_reduce=True)
217-
218-
img, label = self._init_data()
219-
220-
all_reduce_first_loss, all_reduce_last_loss = self.check_network_convergence(
221-
fc_with_batchnorm,
222-
feed_dict={"image": img,
223-
"label": label},
224-
use_cuda=use_cuda,
225-
use_reduce=False)
226-
reduce_first_loss, reduce_last_loss = self.check_network_convergence(
227-
fc_with_batchnorm,
228-
feed_dict={"image": img,
229-
"label": label},
230-
use_cuda=use_cuda,
231-
use_reduce=True)
232-
233-
for loss in zip(all_reduce_first_loss, reduce_first_loss):
234-
self.assertAlmostEquals(loss[0], loss[1], delta=1e-6)
235-
for loss in zip(all_reduce_last_loss, reduce_last_loss):
236-
self.assertAlmostEquals(loss[0], loss[1], delta=1e-4)
237-
238209
def test_batchnorm_fc(self):
239210
self.check_batchnorm_fc_convergence(True)
240211
self.check_batchnorm_fc_convergence(False)
241212

242213
def test_batchnorm_fc_with_new_strategy(self):
243-
self.check_batchnorm_fc_convergence_use_reduce(True)
244-
self.check_batchnorm_fc_convergence_use_reduce(False)
214+
self._compare_reduce_and_allreduce(fc_with_batchnorm, True)
215+
self._compare_reduce_and_allreduce(fc_with_batchnorm, False)
245216

246217

247218
if __name__ == '__main__':

0 commit comments

Comments
 (0)