Skip to content

Commit 5022b14

Browse files
committed
fix mixed tensor compile and add cpu unit test
1 parent e011e34 commit 5022b14

File tree

3 files changed

+51
-26
lines changed

3 files changed

+51
-26
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 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: 5 additions & 5 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+
explicit CPUVector(size_t count, const T &value = T())
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 std::vector<T> &other) : std::vector<T>(other) {}
393394
explicit 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+
explicit CPUVector(std::vector<T> &&other)
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); }

paddle/fluid/framework/mixed_vector_test.cu renamed to paddle/fluid/framework/mixed_vector_test.cc

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,15 @@
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+
15+
#ifdef PADDLE_WITH_CUDA
16+
1417
#include <cuda_runtime.h>
1518

19+
#endif
20+
21+
#include <memory>
22+
1623
#include "glog/logging.h"
1724
#include "gtest/gtest.h"
1825
#include "paddle/fluid/framework/mixed_vector.h"
@@ -41,6 +48,38 @@ TEST(mixed_vector, CPU_VECTOR) {
4148
}
4249
}
4350

51+
TEST(mixed_vector, InitWithCount) {
52+
paddle::framework::Vector<int> vec(10, 10);
53+
for (int i = 0; i < 10; ++i) {
54+
ASSERT_EQ(vec[i], 10);
55+
}
56+
}
57+
58+
TEST(mixed_vector, ForEach) {
59+
vec<int> tmp;
60+
for (auto& v : tmp) {
61+
VLOG(3) << v;
62+
}
63+
}
64+
65+
TEST(mixed_vector, Reserve) {
66+
paddle::framework::Vector<int> vec;
67+
vec.reserve(1);
68+
vec.push_back(0);
69+
vec.push_back(0);
70+
vec.push_back(0);
71+
}
72+
73+
TEST(mixed_vector, Resize) {
74+
paddle::framework::Vector<int> vec;
75+
vec.resize(1);
76+
vec.push_back(0);
77+
vec.push_back(0);
78+
vec.push_back(0);
79+
}
80+
81+
#ifdef PADDLE_WITH_CUDA
82+
4483
static __global__ void multiply_10(int* ptr) {
4584
for (int i = 0; i < 10; ++i) {
4685
ptr[i] *= 10;
@@ -92,23 +131,4 @@ TEST(mixed_vector, MultiGPU) {
92131
}
93132
}
94133

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-
}
134+
#endif

0 commit comments

Comments
 (0)