Skip to content

Commit 2d21aa7

Browse files
authored
Merge pull request #12331 from jacquesqiao/fix-mixed-tensor
fix mixed tensor compile and add cpu unit test
2 parents a4c5223 + 65e5aeb commit 2d21aa7

File tree

4 files changed

+86
-48
lines changed

4 files changed

+86
-48
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-
}

0 commit comments

Comments
 (0)