tensor_util_test.cc 16.7 KB
Newer Older
1
//  Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2 3 4 5 6 7 8 9 10 11 12 13
//
// 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.
D
dzhwinter 已提交
14

Y
Yi Wang 已提交
15
#include "paddle/fluid/framework/tensor_util.h"
W
wanghuancoder 已提交
16

D
dzhwinter 已提交
17
#include <gtest/gtest.h>
18

Y
Yang Yu 已提交
19
#include <cmath>
D
dzhwinter 已提交
20 21 22

namespace paddle {
namespace framework {
D
dzhwinter 已提交
23

Y
Yi Wang 已提交
24
TEST(TensorCopy, Tensor) {
D
dzhwinter 已提交
25 26
  Tensor src_tensor;
  Tensor dst_tensor;
L
Leo Chen 已提交
27
  phi::CPUContext cpu_ctx((platform::CPUPlace()));
D
dzhwinter 已提交
28

29
  int* src_ptr = src_tensor.mutable_data<int>(phi::make_ddim({3, 3}),
30
                                              platform::CPUPlace());
D
dzhwinter 已提交
31 32 33

  int arr[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
  memcpy(src_ptr, arr, 9 * sizeof(int));
D
dzhwinter 已提交
34
  src_tensor.set_layout(DataLayout::kAnyLayout);
D
dzhwinter 已提交
35 36

  auto cpu_place = new platform::CPUPlace();
Y
Yi Wang 已提交
37
  TensorCopy(src_tensor, *cpu_place, &dst_tensor);
D
dzhwinter 已提交
38 39

  const int* dst_ptr = dst_tensor.data<int>();
40
  EXPECT_NE(src_ptr, dst_ptr);
D
dzhwinter 已提交
41 42 43 44
  for (size_t i = 0; i < 9; ++i) {
    EXPECT_EQ(src_ptr[i], dst_ptr[i]);
  }

M
minqiyang 已提交
45 46 47 48 49
  TensorCopy(dst_tensor, *cpu_place, &dst_tensor);
  for (size_t i = 0; i < 9; ++i) {
    EXPECT_EQ(src_ptr[i], dst_ptr[i]);
  }

D
dzhwinter 已提交
50 51
  EXPECT_TRUE(dst_tensor.layout() == src_tensor.layout());

D
dzhwinter 已提交
52
  Tensor slice_tensor = src_tensor.Slice(1, 2);
Y
Yi Wang 已提交
53
  TensorCopy(slice_tensor, *cpu_place, &dst_tensor);
D
dzhwinter 已提交
54 55
  const int* slice_ptr = slice_tensor.data<int>();
  dst_ptr = dst_tensor.data<int>();
56
  EXPECT_NE(dst_ptr, slice_ptr);
D
dzhwinter 已提交
57 58 59
  for (size_t i = 0; i < 3; ++i) {
    EXPECT_EQ(dst_ptr[i], slice_ptr[i]);
  }
D
dzhwinter 已提交
60 61
  EXPECT_TRUE(dst_tensor.layout() == src_tensor.layout());

62
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
D
dzhwinter 已提交
63 64 65 66 67
  {
    Tensor src_tensor;
    Tensor gpu_tensor;
    Tensor dst_tensor;

68
    int* src_ptr = src_tensor.mutable_data<int>(phi::make_ddim({3, 3}),
69
                                                platform::CPUPlace());
D
dzhwinter 已提交
70 71 72 73 74

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

    // CPU Tensor to GPU Tensor
D
dzhwinter 已提交
75
    auto gpu_place = new platform::CUDAPlace(0);
L
Leo Chen 已提交
76
    phi::GPUContext gpu_ctx(*gpu_place);
W
Wilber 已提交
77 78 79 80
    gpu_ctx.SetAllocator(paddle::memory::allocation::AllocatorFacade::Instance()
                             .GetAllocator(*gpu_place, gpu_ctx.stream())
                             .get());
    gpu_ctx.PartialInitWithAllocator();
Y
Yi Wang 已提交
81
    TensorCopy(src_tensor, *gpu_place, gpu_ctx, &gpu_tensor);
D
dzhwinter 已提交
82 83 84

    // GPU Tensor to CPU Tensor
    auto cpu_place = new platform::CPUPlace();
Y
Yi Wang 已提交
85
    TensorCopy(gpu_tensor, *cpu_place, gpu_ctx, &dst_tensor);
D
dzhwinter 已提交
86 87 88 89

    // Sync before Compare Tensors
    gpu_ctx.Wait();
    const int* dst_ptr = dst_tensor.data<int>();
90
    EXPECT_NE(src_ptr, dst_ptr);
D
dzhwinter 已提交
91 92 93 94
    for (size_t i = 0; i < 9; ++i) {
      EXPECT_EQ(src_ptr[i], dst_ptr[i]);
    }

M
minqiyang 已提交
95 96 97 98 99 100 101 102 103
    // Copy the same tensor
    TensorCopy(gpu_tensor, *gpu_place, gpu_ctx, &gpu_tensor);
    gpu_ctx.Wait();
    const int* dst_ptr_tmp = dst_tensor.data<int>();
    EXPECT_NE(src_ptr, dst_ptr_tmp);
    for (size_t i = 0; i < 9; ++i) {
      EXPECT_EQ(src_ptr[i], dst_ptr_tmp[i]);
    }

D
dzhwinter 已提交
104 105 106
    Tensor slice_tensor = src_tensor.Slice(1, 2);

    // CPU Slice Tensor to GPU Tensor
Y
Yi Wang 已提交
107
    TensorCopy(slice_tensor, *gpu_place, gpu_ctx, &gpu_tensor);
D
dzhwinter 已提交
108 109

    // GPU Tensor to CPU Tensor
Y
Yi Wang 已提交
110
    TensorCopy(gpu_tensor, *cpu_place, gpu_ctx, &dst_tensor);
D
dzhwinter 已提交
111 112 113 114 115

    // Sync before Compare Slice Tensors
    gpu_ctx.Wait();
    const int* slice_ptr = slice_tensor.data<int>();
    dst_ptr = dst_tensor.data<int>();
116
    EXPECT_NE(dst_ptr, slice_ptr);
D
dzhwinter 已提交
117 118 119
    for (size_t i = 0; i < 3; ++i) {
      EXPECT_EQ(dst_ptr[i], slice_ptr[i]);
    }
D
dzhwinter 已提交
120 121

    EXPECT_TRUE(dst_tensor.layout() == src_tensor.layout());
D
dzhwinter 已提交
122 123 124 125
  }
#endif
}

Y
Yi Wang 已提交
126
TEST(TensorFromVector, Tensor) {
D
dzhwinter 已提交
127 128
  {
    std::vector<int> src_vec = {1, 2, 3, 4, 5, 6, 7, 8, 9};
129
    paddle::framework::Tensor cpu_tensor;
D
dzhwinter 已提交
130 131

    // Copy to CPU Tensor
132
    cpu_tensor.Resize(phi::make_ddim({3, 3}));
D
dzhwinter 已提交
133
    auto cpu_place = new paddle::platform::CPUPlace();
134
    paddle::framework::TensorFromVector<int>(src_vec, &cpu_tensor);
D
dzhwinter 已提交
135 136 137 138

    // Compare Tensors
    const int* cpu_ptr = cpu_tensor.data<int>();
    const int* src_ptr = src_vec.data();
139
    EXPECT_NE(src_ptr, cpu_ptr);
D
dzhwinter 已提交
140 141 142 143 144
    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);
145
    cpu_tensor.Resize(phi::make_ddim({2, 2}));
146
    paddle::framework::TensorFromVector<int>(src_vec, &cpu_tensor);
D
dzhwinter 已提交
147 148
    cpu_ptr = cpu_tensor.data<int>();
    src_ptr = src_vec.data();
149
    EXPECT_NE(src_ptr, cpu_ptr);
D
dzhwinter 已提交
150 151 152 153 154 155 156
    for (size_t i = 0; i < 5; ++i) {
      EXPECT_EQ(src_ptr[i], cpu_ptr[i]);
    }

    delete cpu_place;
  }

157
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
D
dzhwinter 已提交
158 159
  {
    std::vector<int> src_vec = {1, 2, 3, 4, 5, 6, 7, 8, 9};
160 161 162
    paddle::framework::Tensor cpu_tensor;
    paddle::framework::Tensor gpu_tensor;
    paddle::framework::Tensor dst_tensor;
D
dzhwinter 已提交
163 164

    // Copy to CPU Tensor
165
    cpu_tensor.Resize(phi::make_ddim({3, 3}));
D
dzhwinter 已提交
166
    auto cpu_place = new paddle::platform::CPUPlace();
L
Leo Chen 已提交
167
    phi::CPUContext cpu_ctx(*cpu_place);
168
    paddle::framework::TensorFromVector<int>(src_vec, cpu_ctx, &cpu_tensor);
D
dzhwinter 已提交
169 170

    // Copy to GPUTensor
171
    gpu_tensor.Resize(phi::make_ddim({3, 3}));
D
dzhwinter 已提交
172
    auto gpu_place = new paddle::platform::CUDAPlace();
L
Leo Chen 已提交
173
    phi::GPUContext gpu_ctx(*gpu_place);
W
Wilber 已提交
174 175 176 177
    gpu_ctx.SetAllocator(paddle::memory::allocation::AllocatorFacade::Instance()
                             .GetAllocator(*gpu_place, gpu_ctx.stream())
                             .get());
    gpu_ctx.PartialInitWithAllocator();
178
    paddle::framework::TensorFromVector<int>(src_vec, gpu_ctx, &gpu_tensor);
D
dzhwinter 已提交
179
    // Copy from GPU to CPU tensor for comparison
180
    paddle::framework::TensorCopy(gpu_tensor, *cpu_place, gpu_ctx, &dst_tensor);
D
dzhwinter 已提交
181 182 183 184 185 186

    // Sync before Compare Tensors
    gpu_ctx.Wait();
    const int* src_ptr = src_vec.data();
    const int* cpu_ptr = cpu_tensor.data<int>();
    const int* dst_ptr = dst_tensor.data<int>();
187 188
    EXPECT_NE(src_ptr, cpu_ptr);
    EXPECT_NE(src_ptr, dst_ptr);
D
dzhwinter 已提交
189 190 191 192 193 194 195
    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);

196
    cpu_tensor.Resize(phi::make_ddim({2, 2}));
197
    paddle::framework::TensorFromVector<int>(src_vec, cpu_ctx, &cpu_tensor);
198
    gpu_tensor.Resize(phi::make_ddim({2, 2}));
199 200
    paddle::framework::TensorFromVector<int>(src_vec, gpu_ctx, &gpu_tensor);
    paddle::framework::TensorCopy(gpu_tensor, *cpu_place, gpu_ctx, &dst_tensor);
D
dzhwinter 已提交
201 202 203 204 205 206

    // Sync before Compare Tensors
    gpu_ctx.Wait();
    src_ptr = src_vec.data();
    cpu_ptr = cpu_tensor.data<int>();
    dst_ptr = dst_tensor.data<int>();
207 208
    EXPECT_NE(src_ptr, cpu_ptr);
    EXPECT_NE(src_ptr, dst_ptr);
D
dzhwinter 已提交
209 210 211 212 213 214 215 216 217 218 219
    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
}

Y
Yi Wang 已提交
220
TEST(TensorToVector, Tensor) {
D
dzhwinter 已提交
221
  {
222 223
    paddle::framework::Tensor src;
    int* src_ptr = src.mutable_data<int>({3, 3}, paddle::platform::CPUPlace());
D
dzhwinter 已提交
224 225 226 227
    for (int i = 0; i < 3 * 3; ++i) {
      src_ptr[i] = i;
    }

228
    paddle::platform::CPUPlace place;
D
dzhwinter 已提交
229
    std::vector<int> dst;
230
    paddle::framework::TensorToVector<int>(src, &dst);
D
dzhwinter 已提交
231 232 233 234 235

    for (int i = 0; i < 3 * 3; ++i) {
      EXPECT_EQ(src_ptr[i], dst[i]);
    }
  }
236
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
D
dzhwinter 已提交
237 238
  {
    std::vector<int> src_vec = {1, 2, 3, 4, 5, 6, 7, 8, 9};
239 240
    paddle::framework::Tensor gpu_tensor;
    paddle::platform::CUDAPlace place;
L
Leo Chen 已提交
241
    phi::GPUContext gpu_ctx(place);
W
Wilber 已提交
242 243 244 245
    gpu_ctx.SetAllocator(paddle::memory::allocation::AllocatorFacade::Instance()
                             .GetAllocator(place, gpu_ctx.stream())
                             .get());
    gpu_ctx.PartialInitWithAllocator();
246
    paddle::framework::TensorFromVector<int>(src_vec, gpu_ctx, &gpu_tensor);
D
dzhwinter 已提交
247 248

    std::vector<int> dst;
249
    paddle::framework::TensorToVector<int>(gpu_tensor, gpu_ctx, &dst);
D
dzhwinter 已提交
250 251 252 253 254 255 256 257

    for (int i = 0; i < 3 * 3; ++i) {
      EXPECT_EQ(src_vec[i], dst[i]);
    }
  }
#endif
}

L
Leo Chen 已提交
258 259 260 261 262
TEST(TensorToVector, Tensor_bool){{paddle::framework::Tensor src;
bool* src_ptr = src.mutable_data<bool>({3, 3}, paddle::platform::CPUPlace());
for (int i = 0; i < 3 * 3; ++i) {
  src_ptr[i] = static_cast<bool>(i % 2);
}
263

L
Leo Chen 已提交
264 265 266
paddle::platform::CPUPlace place;
std::vector<bool> dst;
paddle::framework::TensorToVector<bool>(src, &dst);
267

L
Leo Chen 已提交
268 269
for (int i = 0; i < 3 * 3; ++i) {
  EXPECT_EQ(src_ptr[i], dst[i]);
270
}
L
Leo Chen 已提交
271
}  // namespace framework
L
Leo Chen 已提交
272

273
#ifdef PADDLE_WITH_CUDA
274 275
{
  std::vector<bool> src_vec = {
276 277 278 279 280 281 282 283 284
      false,
      true,
      false,
      true,
      false,
      true,
      false,
      true,
      false,
285 286 287
  };
  paddle::framework::Tensor gpu_tensor;
  paddle::platform::CUDAPlace place;
L
Leo Chen 已提交
288
  phi::GPUContext gpu_ctx(place);
289 290 291 292 293 294 295 296 297 298 299
  gpu_ctx.SetAllocator(paddle::memory::allocation::AllocatorFacade::Instance()
                           .GetAllocator(place, gpu_ctx.stream())
                           .get());
  gpu_ctx.PartialInitWithAllocator();
  paddle::framework::TensorFromVector<bool>(src_vec, gpu_ctx, &gpu_tensor);

  std::vector<bool> dst;
  paddle::framework::TensorToVector<bool>(gpu_tensor, gpu_ctx, &dst);

  for (int i = 0; i < 3 * 3; ++i) {
    EXPECT_EQ(src_vec[i], dst[i]);
300
  }
301
}
302 303
#endif
#ifdef PADDLE_WITH_ASCEND_CL
304 305
{
  std::vector<bool> src_vec = {
306 307 308 309 310 311 312 313 314
      false,
      true,
      false,
      true,
      false,
      true,
      false,
      true,
      false,
315 316 317 318 319 320 321 322 323 324 325
  };
  paddle::framework::Tensor npu_tensor;
  paddle::platform::NPUPlace place(0);
  paddle::platform::NPUDeviceContext npu_ctx(place);
  paddle::framework::TensorFromVector<bool>(src_vec, npu_ctx, &npu_tensor);

  std::vector<bool> dst;
  paddle::framework::TensorToVector<bool>(npu_tensor, npu_ctx, &dst);

  for (int i = 0; i < 3 * 3; ++i) {
    EXPECT_EQ(src_vec[i], dst[i]);
326 327
  }
}
328
#endif
L
Leo Chen 已提交
329
}  // namespace paddle
330

6
633WHU 已提交
331 332 333 334 335
TEST(TensorFromDLPack, Tensor) {
  {
    std::vector<int> src_vec = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    paddle::framework::Tensor cpu_tensor;

336
    cpu_tensor.Resize(phi::make_ddim({3, 3}));
6
633WHU 已提交
337
    paddle::platform::CPUPlace cpu_place;
L
Leo Chen 已提交
338
    phi::CPUContext cpu_ctx(cpu_place);
6
633WHU 已提交
339 340 341 342 343 344 345 346 347 348 349 350 351 352
    paddle::framework::TensorFromVector<int>(src_vec, cpu_ctx, &cpu_tensor);
    paddle::framework::DLPackTensor dlpack_tensor(cpu_tensor, 1);

    paddle::framework::Tensor dst_tensor;
    paddle::framework::TensorFromDLPack(dlpack_tensor, &dst_tensor);

    auto cpu_ptr = cpu_tensor.data<int>();
    auto src_ptr = dst_tensor.data<int>();
    EXPECT_NE(src_ptr, cpu_ptr);
    for (size_t i = 0; i < 9; ++i) {
      EXPECT_EQ(src_ptr[i], cpu_ptr[i]);
    }
  }

353
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
6
633WHU 已提交
354 355 356 357 358 359 360 361
  {
    std::vector<int> src_vec = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    paddle::framework::Tensor cpu_tensor;
    paddle::framework::Tensor gpu_tensor;
    paddle::framework::Tensor dst_tensor;
    paddle::framework::Tensor gpu_tensor_from_dlpack;

    // Copy to CPU Tensor
362
    cpu_tensor.Resize(phi::make_ddim({3, 3}));
6
633WHU 已提交
363
    paddle::platform::CPUPlace cpu_place;
L
Leo Chen 已提交
364
    phi::CPUContext cpu_ctx(cpu_place);
6
633WHU 已提交
365 366 367
    paddle::framework::TensorFromVector<int>(src_vec, cpu_ctx, &cpu_tensor);

    // Copy to GPUTensor
368
    gpu_tensor.Resize(phi::make_ddim({3, 3}));
6
633WHU 已提交
369
    paddle::platform::CUDAPlace gpu_place;
370 371
    auto& gpu_ctx =
        *paddle::platform::DeviceContextPool::Instance().GetByPlace(gpu_place);
6
633WHU 已提交
372
    paddle::framework::TensorFromVector<int>(src_vec, gpu_ctx, &gpu_tensor);
373
    gpu_ctx.Wait();
6
633WHU 已提交
374 375 376

    paddle::framework::DLPackTensor dlpack_tensor(gpu_tensor, 1);
    paddle::framework::TensorFromDLPack(dlpack_tensor, &gpu_tensor_from_dlpack);
377 378
    gpu_ctx.Wait();

6
633WHU 已提交
379
    // Copy from GPU to CPU tensor for comparison
380 381
    paddle::framework::TensorCopy(
        gpu_tensor_from_dlpack, cpu_place, gpu_ctx, &dst_tensor);
6
633WHU 已提交
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
    // Sync before Compare Tensors
    gpu_ctx.Wait();
    const int* src_ptr = src_vec.data();
    const int* cpu_ptr = cpu_tensor.data<int>();
    const int* dst_ptr = dst_tensor.data<int>();
    EXPECT_NE(src_ptr, cpu_ptr);
    EXPECT_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]);
    }
  }
#endif
}

Y
Yi Wang 已提交
397
TEST(TensorContainsNAN, CPU) {
398
  {
399 400
    paddle::framework::Tensor src;
    float* buf = src.mutable_data<float>({3}, paddle::platform::CPUPlace());
401 402 403
    buf[0] = 0.0;
    buf[1] = NAN;
    buf[2] = 0.0;
404
    EXPECT_TRUE(paddle::framework::TensorContainsNAN(src));
405
    buf[1] = 0.0;
406
    EXPECT_FALSE(paddle::framework::TensorContainsNAN(src));
407 408 409
  }

  {
410
    paddle::framework::Tensor src;
A
Abhinav Arora 已提交
411 412 413
    paddle::platform::float16* buf =
        src.mutable_data<paddle::platform::float16>(
            {3}, paddle::platform::CPUPlace());
414 415 416
    buf[0] = 0.0;
    buf[1].x = 0x7fff;
    buf[2] = 0.0;
417
    EXPECT_TRUE(paddle::framework::TensorContainsNAN(src));
418
    buf[1] = 0.0;
419
    EXPECT_FALSE(paddle::framework::TensorContainsNAN(src));
420
  }
Y
Yang Yu 已提交
421 422
}

Y
Yi Wang 已提交
423
TEST(TensorContainsInf, CPU) {
424
  {
425 426
    paddle::framework::Tensor src;
    double* buf = src.mutable_data<double>({3}, paddle::platform::CPUPlace());
427 428 429
    buf[0] = 1.0;
    buf[1] = INFINITY;
    buf[2] = 0.0;
430
    EXPECT_TRUE(paddle::framework::TensorContainsInf(src));
431
    buf[1] = 1.0;
432
    EXPECT_FALSE(paddle::framework::TensorContainsInf(src));
433 434 435
  }

  {
436
    paddle::framework::Tensor src;
A
Abhinav Arora 已提交
437 438 439
    paddle::platform::float16* buf =
        src.mutable_data<paddle::platform::float16>(
            {3}, paddle::platform::CPUPlace());
440 441 442
    buf[0] = 1.0;
    buf[1].x = 0x7c00;
    buf[2] = 0.0;
443
    EXPECT_TRUE(paddle::framework::TensorContainsInf(src));
444
    buf[1] = 1.0;
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
    EXPECT_FALSE(paddle::framework::TensorContainsInf(src));
  }
}

TEST(TensorIsfinite, CPU) {
  {
    paddle::framework::Tensor src, out;
    double* buf = src.mutable_data<double>({3}, paddle::platform::CPUPlace());
    buf[0] = 1.0;
    buf[1] = INFINITY;
    buf[2] = 0.0;
    paddle::framework::TensorIsfinite(src, &out);
    EXPECT_EQ(out.data<bool>()[0], false);
    buf[1] = 1.0;
    paddle::framework::TensorIsfinite(src, &out);
    EXPECT_EQ(out.data<bool>()[0], true);
  }

  {
    paddle::framework::Tensor src, out;
    double* buf = src.mutable_data<double>({3}, paddle::platform::CPUPlace());
    buf[0] = 1.0;
    buf[1] = NAN;
    buf[2] = 0.0;
    paddle::framework::TensorIsfinite(src, &out);
    EXPECT_EQ(out.data<bool>()[0], false);
    buf[1] = 1.0;
    paddle::framework::TensorIsfinite(src, &out);
    EXPECT_EQ(out.data<bool>()[0], true);
  }

  {
    paddle::framework::Tensor src, out;
    paddle::platform::float16* buf =
        src.mutable_data<paddle::platform::float16>(
            {3}, paddle::platform::CPUPlace());
    buf[0] = 1.0;
    buf[1].x = 0x7c00;
    buf[2] = 0.0;
    paddle::framework::TensorIsfinite(src, &out);
    EXPECT_EQ(out.data<bool>()[0], false);
    buf[1] = 1.0;
    paddle::framework::TensorIsfinite(src, &out);
    EXPECT_EQ(out.data<bool>()[0], true);
    buf[1].x = 0x7fff;
    paddle::framework::TensorIsfinite(src, &out);
    EXPECT_EQ(out.data<bool>()[0], false);
492
  }
Y
Yang Yu 已提交
493 494
}

Y
Yi Wang 已提交
495
TEST(Tensor, FromAndToStream) {
496 497 498 499 500 501 502 503 504 505
  framework::Tensor src_tensor;
  int array[6] = {1, 2, 3, 4, 5, 6};
  src_tensor.Resize({2, 3});
  int* src_ptr = src_tensor.mutable_data<int>(platform::CPUPlace());
  for (int i = 0; i < 6; ++i) {
    src_ptr[i] = array[i];
  }
  {
    framework::Tensor dst_tensor;
    auto place = new platform::CPUPlace();
L
Leo Chen 已提交
506
    phi::CPUContext cpu_ctx(*place);
507
    std::ostringstream oss;
Y
Yi Wang 已提交
508
    TensorToStream(oss, src_tensor, cpu_ctx);
509 510

    std::istringstream iss(oss.str());
Y
Yi Wang 已提交
511
    TensorFromStream(iss, &dst_tensor, cpu_ctx);
512 513
    int* dst_ptr = dst_tensor.mutable_data<int>(platform::CPUPlace());
    for (int i = 0; i < 5; ++i) {
514
      EXPECT_EQ(dst_ptr[i], array[i]);
515
    }
516
    EXPECT_EQ(dst_tensor.dims(), src_tensor.dims());
517 518
    delete place;
  }
519
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
520 521 522 523 524 525
  {
    Tensor gpu_tensor;
    gpu_tensor.Resize({2, 3});
    Tensor dst_tensor;

    auto gpu_place = new platform::CUDAPlace();
L
Leo Chen 已提交
526
    phi::GPUContext gpu_ctx(*gpu_place);
W
Wilber 已提交
527 528 529 530
    gpu_ctx.SetAllocator(paddle::memory::allocation::AllocatorFacade::Instance()
                             .GetAllocator(*gpu_place, gpu_ctx.stream())
                             .get());
    gpu_ctx.PartialInitWithAllocator();
531

Y
Yi Wang 已提交
532
    TensorCopy(src_tensor, *gpu_place, gpu_ctx, &gpu_tensor);
533 534

    std::ostringstream oss;
Y
Yi Wang 已提交
535
    TensorToStream(oss, gpu_tensor, gpu_ctx);
536 537

    std::istringstream iss(oss.str());
Y
Yu Yang 已提交
538
    TensorFromStream(
539 540
        iss,
        &dst_tensor,
Y
Yu Yang 已提交
541
        *platform::DeviceContextPool::Instance().Get(platform::CPUPlace()));
542 543 544

    int* dst_ptr = dst_tensor.mutable_data<int>(platform::CPUPlace());
    for (int i = 0; i < 6; ++i) {
545
      EXPECT_EQ(dst_ptr[i], array[i]);
546 547 548 549 550 551
    }
    delete gpu_place;
  }
#endif
}

D
dzhwinter 已提交
552 553
}  // namespace framework
}  // namespace paddle