tensor_test.cc 11.4 KB
Newer Older
F
fengjiayi 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
  Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

#include "paddle/framework/tensor.h"
#include <gtest/gtest.h>
16
#include <string>
F
fengjiayi 已提交
17

F
fengjiayi 已提交
18 19 20
TEST(Tensor, Dims) {
  using namespace paddle::framework;
  using namespace paddle::platform;
F
fengjiayi 已提交
21
  Tensor tt;
22
  tt.Resize({2, 3, 4});
F
fengjiayi 已提交
23 24 25 26 27 28 29 30 31
  DDim dims = tt.dims();
  ASSERT_EQ(arity(dims), 3);
  for (int i = 0; i < 3; ++i) {
    EXPECT_EQ(i + 2, dims[i]);
  }
}

TEST(Tensor, DataAssert) {
  paddle::framework::Tensor src_tensor;
F
fengjiayi 已提交
32

33 34
  bool caught = false;
  try {
F
fengjiayi 已提交
35
    src_tensor.data<double>();
Y
Yu Yang 已提交
36
  } catch (paddle::platform::EnforceNotMet err) {
37
    caught = true;
F
fengjiayi 已提交
38
    std::string msg =
Z
zchen0211 已提交
39
        "holder_ should not be null\nTensor holds no memory. Call "
Y
Yan Chunwei 已提交
40
        "Tensor::mutable_data first.";
41 42 43 44 45 46
    const char* what = err.what();
    for (size_t i = 0; i < msg.length(); ++i) {
      ASSERT_EQ(what[i], msg[i]);
    }
  }
  ASSERT_TRUE(caught);
F
fengjiayi 已提交
47 48
}

F
fengjiayi 已提交
49
/* following tests are not available at present
F
fengjiayi 已提交
50
   because Memory::Alloc() and Memory::Free() have not been ready.
F
fengjiayi 已提交
51
*/
F
fengjiayi 已提交
52 53 54
TEST(Tensor, MutableData) {
  using namespace paddle::framework;
  using namespace paddle::platform;
55
  {
F
fengjiayi 已提交
56
    Tensor src_tensor;
57 58 59
    float* p1 = nullptr;
    float* p2 = nullptr;
    // initialization
F
fengjiayi 已提交
60
    p1 = src_tensor.mutable_data<float>(make_ddim({1, 2, 3}), CPUPlace());
61
    EXPECT_NE(p1, nullptr);
F
fengjiayi 已提交
62
    // set src_tensor a new dim with large size
63
    // momery is supposed to be re-allocated
F
fengjiayi 已提交
64
    p2 = src_tensor.mutable_data<float>(make_ddim({3, 4}), CPUPlace());
65 66
    EXPECT_NE(p2, nullptr);
    EXPECT_NE(p1, p2);
F
fengjiayi 已提交
67
    // set src_tensor a new dim with same size
68
    // momery block is supposed to be unchanged
F
fengjiayi 已提交
69
    p1 = src_tensor.mutable_data<float>(make_ddim({2, 2, 3}), CPUPlace());
70
    EXPECT_EQ(p1, p2);
F
fengjiayi 已提交
71
    // set src_tensor a new dim with smaller size
72
    // momery block is supposed to be unchanged
F
fengjiayi 已提交
73
    p2 = src_tensor.mutable_data<float>(make_ddim({2, 2}), CPUPlace());
74 75
    EXPECT_EQ(p1, p2);
  }
L
liaogang 已提交
76

77
#ifdef PADDLE_WITH_CUDA
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
  {
    Tensor src_tensor;
    float* p1 = nullptr;
    float* p2 = nullptr;
    // initialization
    p1 = src_tensor.mutable_data<float>(make_ddim({1, 2, 3}), GPUPlace());
    EXPECT_NE(p1, nullptr);
    // set src_tensor a new dim with large size
    // momery is supposed to be re-allocated
    p2 = src_tensor.mutable_data<float>(make_ddim({3, 4}), GPUPlace());
    EXPECT_NE(p2, nullptr);
    EXPECT_NE(p1, p2);
    // set src_tensor a new dim with same size
    // momery block is supposed to be unchanged
    p1 = src_tensor.mutable_data<float>(make_ddim({2, 2, 3}), GPUPlace());
    EXPECT_EQ(p1, p2);
    // set src_tensor a new dim with smaller size
    // momery block is supposed to be unchanged
    p2 = src_tensor.mutable_data<float>(make_ddim({2, 2}), GPUPlace());
    EXPECT_EQ(p1, p2);
  }
#endif
F
fengjiayi 已提交
100
}
F
fengjiayi 已提交
101

F
fengjiayi 已提交
102
TEST(Tensor, ShareDataWith) {
F
fengjiayi 已提交
103 104 105 106 107 108 109 110
  using namespace paddle::framework;
  using namespace paddle::platform;
  {
    Tensor src_tensor;
    Tensor dst_tensor;
    // Try to share data form uninitialized tensor
    bool caught = false;
    try {
F
fengjiayi 已提交
111
      dst_tensor.ShareDataWith<float>(src_tensor);
Y
Yu Yang 已提交
112
    } catch (paddle::platform::EnforceNotMet err) {
F
fengjiayi 已提交
113
      caught = true;
F
fengjiayi 已提交
114
      std::string msg =
Z
zchen0211 已提交
115
          "holder_ should not be null\nTensor holds no memory. Call "
Y
Yan Chunwei 已提交
116
          "Tensor::mutable_data first.";
F
fengjiayi 已提交
117 118 119
      const char* what = err.what();
      for (size_t i = 0; i < msg.length(); ++i) {
        ASSERT_EQ(what[i], msg[i]);
F
fengjiayi 已提交
120 121 122 123 124
      }
    }
    ASSERT_TRUE(caught);

    src_tensor.mutable_data<int>(make_ddim({2, 3, 4}), CPUPlace());
F
fengjiayi 已提交
125
    dst_tensor.ShareDataWith<int>(src_tensor);
F
fengjiayi 已提交
126 127 128
    ASSERT_EQ(src_tensor.data<int>(), dst_tensor.data<int>());
  }

129
#ifdef PADDLE_WITH_CUDA
130 131 132 133
  {
    Tensor src_tensor;
    Tensor dst_tensor;
    src_tensor.mutable_data<int>(make_ddim({2, 3, 4}), GPUPlace());
F
fengjiayi 已提交
134
    dst_tensor.ShareDataWith<int>(src_tensor);
135 136 137
    ASSERT_EQ(src_tensor.data<int>(), dst_tensor.data<int>());
  }
#endif
F
fengjiayi 已提交
138 139 140 141 142 143 144 145
}

TEST(Tensor, Slice) {
  using namespace paddle::framework;
  using namespace paddle::platform;
  {
    Tensor src_tensor;
    src_tensor.mutable_data<int>(make_ddim({5, 3, 4}), CPUPlace());
F
fengjiayi 已提交
146
    Tensor slice_tensor = src_tensor.Slice<int>(1, 3);
F
fengjiayi 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    DDim slice_dims = slice_tensor.dims();
    ASSERT_EQ(arity(slice_dims), 3);
    EXPECT_EQ(slice_dims[0], 2);
    EXPECT_EQ(slice_dims[1], 3);
    EXPECT_EQ(slice_dims[2], 4);

    uintptr_t src_data_address =
        reinterpret_cast<uintptr_t>(src_tensor.data<int>());
    uintptr_t src_mutable_data_address = reinterpret_cast<uintptr_t>(
        src_tensor.mutable_data<int>(src_tensor.dims(), CPUPlace()));
    uintptr_t slice_data_address =
        reinterpret_cast<uintptr_t>(slice_tensor.data<int>());
    uintptr_t slice_mutable_data_address = reinterpret_cast<uintptr_t>(
        slice_tensor.mutable_data<int>(slice_tensor.dims(), CPUPlace()));
    EXPECT_EQ(src_data_address, src_mutable_data_address);
    EXPECT_EQ(slice_data_address, slice_mutable_data_address);
    EXPECT_EQ(src_data_address + 3 * 4 * 1 * sizeof(int), slice_data_address);
  }

166
#ifdef PADDLE_WITH_CUDA
167 168 169 170 171 172 173 174
  {
    Tensor src_tensor;
    src_tensor.mutable_data<double>(make_ddim({6, 9}), GPUPlace());
    Tensor slice_tensor = src_tensor.Slice<double>(2, 6);
    DDim slice_dims = slice_tensor.dims();
    ASSERT_EQ(arity(slice_dims), 2);
    EXPECT_EQ(slice_dims[0], 4);
    EXPECT_EQ(slice_dims[1], 9);
F
fengjiayi 已提交
175

176 177 178 179 180 181 182 183 184 185 186 187 188
    uintptr_t src_data_address =
        reinterpret_cast<uintptr_t>(src_tensor.data<double>());
    uintptr_t src_mutable_data_address = reinterpret_cast<uintptr_t>(
        src_tensor.mutable_data<double>(src_tensor.dims(), GPUPlace()));
    uintptr_t slice_data_address =
        reinterpret_cast<uintptr_t>(slice_tensor.data<double>());
    uintptr_t slice_mutable_data_address = reinterpret_cast<uintptr_t>(
        slice_tensor.mutable_data<double>(slice_tensor.dims(), GPUPlace()));
    EXPECT_EQ(src_data_address, src_mutable_data_address);
    EXPECT_EQ(slice_data_address, slice_mutable_data_address);
    EXPECT_EQ(src_data_address + 9 * 2 * sizeof(double), slice_data_address);
  }
#endif
F
fengjiayi 已提交
189 190
}

191 192 193
TEST(Tensor, CopyFrom) {
  using namespace paddle::framework;
  using namespace paddle::platform;
L
liaogang 已提交
194 195 196
  {
    Tensor src_tensor;
    Tensor dst_tensor;
197

L
liaogang 已提交
198
    int* src_ptr = src_tensor.mutable_data<int>(make_ddim({3, 3}), CPUPlace());
199

L
liaogang 已提交
200 201
    int arr[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    memcpy(src_ptr, arr, 9 * sizeof(int));
202

Q
qijun 已提交
203 204
    auto cpu_place = new paddle::platform::CPUPlace();
    dst_tensor.CopyFrom<int>(src_tensor, *cpu_place);
L
liaogang 已提交
205 206 207 208 209 210 211 212

    const int* dst_ptr = dst_tensor.data<int>();
    ASSERT_NE(src_ptr, dst_ptr);
    for (size_t i = 0; i < 9; ++i) {
      EXPECT_EQ(src_ptr[i], dst_ptr[i]);
    }

    Tensor slice_tensor = src_tensor.Slice<int>(1, 2);
Q
qijun 已提交
213
    dst_tensor.CopyFrom<int>(slice_tensor, *cpu_place);
L
liaogang 已提交
214 215 216 217 218 219
    const int* slice_ptr = slice_tensor.data<int>();
    dst_ptr = dst_tensor.data<int>();
    ASSERT_NE(dst_ptr, slice_ptr);
    for (size_t i = 0; i < 3; ++i) {
      EXPECT_EQ(dst_ptr[i], slice_ptr[i]);
    }
220
  }
221
#ifdef PADDLE_WITH_CUDA
L
liaogang 已提交
222 223 224 225 226 227 228 229 230 231 232
  {
    Tensor src_tensor;
    Tensor gpu_tensor;
    Tensor dst_tensor;

    int* src_ptr = src_tensor.mutable_data<int>(make_ddim({3, 3}), CPUPlace());

    int arr[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    memcpy(src_ptr, arr, 9 * sizeof(int));

    // CPU Tensor to GPU Tensor
Q
qijun 已提交
233 234
    auto gpu_place = new paddle::platform::GPUPlace(0);
    gpu_tensor.CopyFrom<int>(src_tensor, *gpu_place);
L
liaogang 已提交
235 236

    // GPU Tensor to CPU Tensor
Q
qijun 已提交
237 238
    auto cpu_place = new paddle::platform::CPUPlace();
    dst_tensor.CopyFrom<int>(gpu_tensor, *cpu_place);
L
liaogang 已提交
239 240 241 242 243 244 245 246 247 248 249

    // Compare Tensors
    const int* dst_ptr = dst_tensor.data<int>();
    ASSERT_NE(src_ptr, dst_ptr);
    for (size_t i = 0; i < 9; ++i) {
      EXPECT_EQ(src_ptr[i], dst_ptr[i]);
    }

    Tensor slice_tensor = src_tensor.Slice<int>(1, 2);

    // CPU Slice Tensor to GPU Tensor
Q
qijun 已提交
250
    gpu_tensor.CopyFrom<int>(slice_tensor, *gpu_place);
251

L
liaogang 已提交
252
    // GPU Tensor to CPU Tensor
Q
qijun 已提交
253
    dst_tensor.CopyFrom<int>(gpu_tensor, *cpu_place);
L
liaogang 已提交
254 255 256 257 258 259 260 261

    // Compare Slice Tensors
    const int* slice_ptr = slice_tensor.data<int>();
    dst_ptr = dst_tensor.data<int>();
    ASSERT_NE(dst_ptr, slice_ptr);
    for (size_t i = 0; i < 3; ++i) {
      EXPECT_EQ(dst_ptr[i], slice_ptr[i]);
    }
262
  }
L
liaogang 已提交
263
#endif
F
fengjiayi 已提交
264
}
F
WIP  
fengjiayi 已提交
265

266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
TEST(Tensor, CopyFromVector) {
  using namespace paddle::framework;
  using namespace paddle::platform;
  {
    std::vector<int> src_vec = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    Tensor cpu_tensor;

    // Copy to CPU Tensor
    cpu_tensor.Resize(make_ddim({3, 3}));
    auto cpu_place = new paddle::platform::CPUPlace();
    cpu_tensor.CopyFromVector<int>(src_vec, *cpu_place);

    // Compare Tensors
    const int* cpu_ptr = cpu_tensor.data<int>();
    const int* src_ptr = src_vec.data();
    ASSERT_NE(src_ptr, cpu_ptr);
    for (size_t i = 0; i < 9; ++i) {
      EXPECT_EQ(src_ptr[i], cpu_ptr[i]);
    }

    src_vec.erase(src_vec.begin(), src_vec.begin() + 5);
    cpu_tensor.Resize(make_ddim({2, 2}));
    cpu_tensor.CopyFromVector<int>(src_vec, *cpu_place);
    cpu_ptr = cpu_tensor.data<int>();
    src_ptr = src_vec.data();
    ASSERT_NE(src_ptr, cpu_ptr);
    for (size_t i = 0; i < 5; ++i) {
      EXPECT_EQ(src_ptr[i], cpu_ptr[i]);
    }

    delete cpu_place;
  }

#ifdef PADDLE_WITH_CUDA
  {
    std::vector<int> src_vec = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    Tensor cpu_tensor;
    Tensor gpu_tensor;
    Tensor dst_tensor;

    // Copy to CPU Tensor
    cpu_tensor.Resize(make_ddim({3, 3}));
    auto cpu_place = new paddle::platform::CPUPlace();
    cpu_tensor.CopyFromVector<int>(src_vec, *cpu_place);

    // Copy to GPUTensor
    gpu_tensor.Resize(make_ddim({3, 3}));
    auto gpu_place = new paddle::platform::GPUPlace();
    gpu_tensor.CopyFromVector<int>(src_vec, *gpu_place);
    // Copy from GPU to CPU tensor for comparison
    dst_tensor.CopyFrom<int>(gpu_tensor, *cpu_place);

    // Compare Tensors
    const int* src_ptr = src_vec.data();
    const int* cpu_ptr = cpu_tensor.data<int>();
    const int* dst_ptr = dst_tensor.data<int>();
    ASSERT_NE(src_ptr, cpu_ptr);
    ASSERT_NE(src_ptr, dst_ptr);
    for (size_t i = 0; i < 9; ++i) {
      EXPECT_EQ(src_ptr[i], cpu_ptr[i]);
      EXPECT_EQ(src_ptr[i], dst_ptr[i]);
    }

    src_vec.erase(src_vec.begin(), src_vec.begin() + 5);

    cpu_tensor.Resize(make_ddim({2, 2}));
    cpu_tensor.CopyFromVector<int>(src_vec, *cpu_place);
    gpu_tensor.Resize(make_ddim({2, 2}));
    gpu_tensor.CopyFromVector<int>(src_vec, *gpu_place);
    dst_tensor.CopyFrom<int>(gpu_tensor, *cpu_place);

    src_ptr = src_vec.data();
    cpu_ptr = cpu_tensor.data<int>();
    dst_ptr = dst_tensor.data<int>();
    ASSERT_NE(src_ptr, cpu_ptr);
    ASSERT_NE(src_ptr, dst_ptr);
    for (size_t i = 0; i < 5; ++i) {
      EXPECT_EQ(src_ptr[i], cpu_ptr[i]);
      EXPECT_EQ(src_ptr[i], dst_ptr[i]);
    }

    delete cpu_place;
    delete gpu_place;
  }
#endif
}

F
fengjiayi 已提交
353
TEST(Tensor, ReshapeToMatrix) {
F
WIP  
fengjiayi 已提交
354 355 356
  using namespace paddle::framework;
  using namespace paddle::platform;
  Tensor src;
F
fengjiayi 已提交
357
  int* src_ptr = src.mutable_data<int>({2, 3, 4, 9}, CPUPlace());
F
WIP  
fengjiayi 已提交
358 359 360
  for (int i = 0; i < 2 * 3 * 4 * 9; ++i) {
    src_ptr[i] = i;
  }
F
fengjiayi 已提交
361
  Tensor res = ReshapeToMatrix<int>(src, 2);
F
WIP  
fengjiayi 已提交
362 363
  ASSERT_EQ(res.dims()[0], 2 * 3);
  ASSERT_EQ(res.dims()[1], 4 * 9);
Z
zchen0211 已提交
364
}