test_tensor.cpp 19.9 KB
Newer Older
1 2 3 4 5
#include "lite_build_config.h"

#if LITE_BUILD_WITH_MGE
#include "../src/mge/common.h"
#include "../src/mge/network_impl.h"
M
Megvii Engine Team 已提交
6
#include "../src/misc.h"
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
#include "lite/tensor.h"

#include <gtest/gtest.h>

#include <string.h>
#include <memory>

using namespace lite;

TEST(TestTensor, Basic) {
    Layout layout{{1, 3, 224, 224}, 4};
    Tensor tensor1(LiteDeviceType::LITE_CPU);
    Tensor tensor2(LiteDeviceType::LITE_CPU, layout);
    Tensor tensor3(LiteDeviceType::LITE_CPU, layout);
    //! mge tensor has created
    ASSERT_TRUE(TensorHelper::implement(&tensor1));
    ASSERT_TRUE(TensorHelper::implement(&tensor2));
    ASSERT_TRUE(TensorHelper::implement(&tensor3));
    //! check member
    ASSERT_EQ(tensor2.get_device_type(), LiteDeviceType::LITE_CPU);
    ASSERT_EQ(tensor2.get_layout(), layout);
    ASSERT_EQ(tensor3.get_layout(), layout);
    //! check the real tensor
    ASSERT_EQ(tensor2.get_tensor_total_size_in_byte(), 1 * 3 * 224 * 224 * 4);
    ASSERT_EQ(tensor3.get_tensor_total_size_in_byte(), 1 * 3 * 224 * 224 * 4);

    ASSERT_TRUE(TensorHelper::implement(&tensor1)
                        ->cast_final_safe<TensorImplDft>()
                        .host_tensor());

    ASSERT_FALSE(TensorHelper::implement(&tensor1)
                         ->cast_final_safe<TensorImplDft>()
                         .dev_tensor());
    ASSERT_FALSE(TensorHelper::implement(&tensor1)
                         ->cast_final_safe<TensorImplDft>()
                         .dev_tensor());
    ASSERT_TRUE(TensorHelper::implement(&tensor1)
                        ->cast_final_safe<TensorImplDft>()
                        .host_tensor());
}

TEST(TestTensor, SetLayoutReAlloc) {
    Layout layout{{1, 3, 224, 224}, 4};
    Tensor tensor1;
    Tensor tensor2(LiteDeviceType::LITE_CPU, layout);
    Tensor tensor3(LiteDeviceType::LITE_CPU, layout);
    auto old_ptr2 = tensor2.get_memory_ptr();
    auto old_ptr3 = tensor3.get_memory_ptr();

    //! layout set through
    Layout layout1{{1, 3, 100, 100}, 4, LiteDataType::LITE_INT8};
    tensor1.set_layout(layout1);
    tensor2.set_layout(layout1);
    tensor3.set_layout(layout1);
    ASSERT_EQ(tensor2.get_tensor_total_size_in_byte(), 1 * 3 * 100 * 100);
    ASSERT_EQ(tensor3.get_tensor_total_size_in_byte(), 1 * 3 * 100 * 100);
    auto layout2 = TensorHelper::implement(&tensor2)
                           ->cast_final_safe<TensorImplDft>()
                           .host_tensor()
                           ->layout();
    auto layout3 = TensorHelper::implement(&tensor3)
                           ->cast_final_safe<TensorImplDft>()
                           .host_tensor()
                           ->layout();
    ASSERT_EQ(to_lite_layout(layout2), layout1);
    ASSERT_EQ(to_lite_layout(layout3), layout1);

    auto new_ptr2 = tensor2.get_memory_ptr();
    auto new_ptr3 = tensor3.get_memory_ptr();

    ASSERT_EQ(old_ptr2, new_ptr2);
    ASSERT_EQ(old_ptr3, new_ptr3);
}

TEST(TestTensor, Reset) {
    Layout layout{{3, 20}, 2, LiteDataType::LITE_FLOAT};
    Tensor tensor1;
    Tensor tensor2(LiteDeviceType::LITE_CPU, layout);
    Tensor tensor3(LiteDeviceType::LITE_CPU, layout);

    auto old_ptr2 = tensor2.get_memory_ptr();
    auto old_ptr3 = tensor3.get_memory_ptr();
    //! make sure memory is allocted
    ASSERT_NO_THROW(memcpy(old_ptr2, old_ptr3, 3 * 20 * 2));

M
Megvii Engine Team 已提交
92 93 94 95
    std::shared_ptr<float> new_ptr2(
            new float[3 * 20], [](float* ptr) { delete[] ptr; });
    std::shared_ptr<float> new_ptr3(
            new float[3 * 20], [](float* ptr) { delete[] ptr; });
96 97 98 99 100 101 102 103 104 105 106 107 108
    tensor1.reset(new_ptr2.get(), layout);
    tensor2.reset(new_ptr2.get(), 3 * 20 * 4);
    tensor3.reset(new_ptr3.get(), 3 * 20 * 4);
    //! After reset the original mem is freed
    /*ASSERT_EXIT((memcpy(old_ptr2, old_ptr3, 3 * 20 * 2), exit(0)),
                ::testing::KilledBySignal(SIGSEGV), ".*");*/

    ASSERT_EQ(tensor2.get_memory_ptr(), new_ptr2.get());
    ASSERT_EQ(tensor3.get_memory_ptr(), new_ptr3.get());

    ASSERT_NO_THROW(memcpy(new_ptr2.get(), new_ptr3.get(), 3 * 20 * 2));

    Layout layout1{{6, 20}, 2, LiteDataType::LITE_FLOAT};
M
Megvii Engine Team 已提交
109 110
    std::shared_ptr<float> ptr2(new float[6 * 20], [](float* ptr) { delete[] ptr; });
    std::shared_ptr<float> ptr3(new float[6 * 20], [](float* ptr) { delete[] ptr; });
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
    tensor2.reset(ptr2.get(), layout1);
    tensor3.reset(ptr3.get(), layout1);

    //! memory is not freed by Tensor reset
    ASSERT_NO_THROW(memcpy(new_ptr2.get(), new_ptr3.get(), 3 * 20 * 2));
    auto host_layout2 = TensorHelper::implement(&tensor2)
                                ->cast_final_safe<TensorImplDft>()
                                .host_tensor()
                                ->layout();
    auto host_layout3 = TensorHelper::implement(&tensor3)
                                ->cast_final_safe<TensorImplDft>()
                                .host_tensor()
                                ->layout();

    ASSERT_EQ(to_lite_layout(host_layout2), layout1);
    ASSERT_EQ(to_lite_layout(host_layout3), layout1);
}

TEST(TestTensor, CrossCNCopy) {
    Layout layout{{1, 3, 224, 224}, 4};
    Tensor tensor1(LiteDeviceType::LITE_CPU);
    Tensor tensor2(LiteDeviceType::LITE_CPU, layout);
    Tensor tensor3(LiteDeviceType::LITE_CPU, layout);
    tensor2.copy_from(tensor3);
    tensor3.copy_from(tensor2);
    auto old_ptr2 = tensor2.get_memory_ptr();
    auto old_ptr3 = tensor3.get_memory_ptr();

    //! test source tenor is empty
    ASSERT_THROW(tensor2.copy_from(tensor1), std::exception);
    tensor1.copy_from(tensor2);
    tensor2.copy_from(tensor3);
    tensor3.copy_from(tensor2);

    ASSERT_EQ(tensor2.get_memory_ptr(), old_ptr2);
    ASSERT_EQ(tensor3.get_memory_ptr(), old_ptr3);
}

TEST(TestTensor, SharedTensorMemory) {
    Layout layout{{1, 3, 224, 224}, 4};
    Tensor tensor1(LiteDeviceType::LITE_CPU);
    {
        Tensor tensor2(LiteDeviceType::LITE_CPU, layout);
        tensor1.share_memory_with(tensor2);
        auto ptr1 = tensor1.get_memory_ptr();
        auto ptr2 = tensor2.get_memory_ptr();
        ASSERT_EQ(ptr1, ptr2);
    }
    // check after tensor2 destroy, tensor1 can also visit
    auto ptr1 = static_cast<float*>(tensor1.get_memory_ptr());
    size_t length = tensor1.get_tensor_total_size_in_byte() /
                    tensor1.get_layout().get_elem_size();
    for (size_t i = 0; i < length; i++) {
        ptr1[i] = i;
    }
}

TEST(TestTensor, Reshape) {
    Layout layout{{1, 3, 224, 224}, 4};
    Tensor tensor2(LiteDeviceType::LITE_CPU, layout);
    auto ptr = tensor2.get_memory_ptr();

    //! test wrong case
    ASSERT_THROW(tensor2.reshape({-1, -1, 3 * 224 * 224}), std::exception);
    ASSERT_THROW(tensor2.reshape({-1, 3, 3 * 224 * 224}), std::exception);
    ASSERT_THROW(tensor2.reshape({1, 3, 3 * 224 * 224}), std::exception);
    ASSERT_THROW(tensor2.reshape({3, 3, 3 * 224 * 224}), std::exception);

    tensor2.reshape({3 * 224 * 224});
    ASSERT_EQ(tensor2.get_layout().ndim, 1);
    ASSERT_EQ(tensor2.get_layout().data_type, LiteDataType::LITE_FLOAT);
    ASSERT_EQ(tensor2.get_layout().shapes[0], 3 * 224 * 224);
    tensor2.reshape({-1, 224, 224});
    ASSERT_EQ(tensor2.get_layout().ndim, 3);
    ASSERT_EQ(tensor2.get_layout().shapes[0], 3);
    ASSERT_EQ(tensor2.get_layout().shapes[1], 224);

    ASSERT_EQ(tensor2.get_memory_ptr(), ptr);
}

TEST(TestTensor, Slice) {
    Layout layout{{20, 20}, 2};
    Tensor tensor2(LiteDeviceType::LITE_CPU, layout);
    auto ptr = tensor2.get_memory_ptr();

    //! test source tenor is empty
    ASSERT_THROW(tensor2.slice({5, 10, 10}, {10, 15}), std::exception);
    ASSERT_THROW(tensor2.slice({5, 10}, {10, 15}, {5}), std::exception);
    ASSERT_THROW(tensor2.slice({5, 10}, {10, 15, 10}), std::exception);
    for (int i = 0; i < 20 * 20; i++) {
        *(static_cast<float*>(ptr) + i) = i;
    }
    auto check = [&](size_t start, size_t end, size_t step) {
        Tensor tensor3;
M
Megvii Engine Team 已提交
205
        tensor3.copy_from(*tensor2.slice({start, start}, {end, end}, {step, step}));
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 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 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
        float* new_ptr = static_cast<float*>(tensor3.get_memory_ptr());
        for (size_t i = start; i < end; i += step) {
            for (size_t j = start; j < end; j += step) {
                ASSERT_EQ(float(i * 20 + j), *new_ptr);
                ++new_ptr;
            }
        }
    };
    check(5, 10, 1);
    check(5, 11, 2);
    check(2, 18, 4);

    Tensor tensor3;
    tensor3.copy_from(*tensor2.slice({3}, {9}, {2}));
    float* new_ptr = static_cast<float*>(tensor3.get_memory_ptr());
    for (size_t i = 3; i < 9; i += 2) {
        for (size_t j = 0; j < 20; j++) {
            ASSERT_EQ(float(i * 20 + j), *new_ptr);
            ++new_ptr;
        }
    }
}

TEST(TestTensor, SliceCopy) {
    Layout layout{{20, 20}, 2};
    Tensor tensor(LiteDeviceType::LITE_CPU, layout);
    //! alloc memory
    auto ptr = static_cast<float*>(tensor.get_memory_ptr());

    Layout layout_slice{{20, 10}, 2};
    Tensor tensor0(LiteDeviceType::LITE_CPU, layout_slice);
    auto ptr0 = tensor0.get_memory_ptr();
    for (int i = 0; i < 10 * 20; i++) {
        *(static_cast<float*>(ptr0) + i) = i;
    }
    Tensor tensor1(LiteDeviceType::LITE_CPU, layout_slice);
    auto ptr1 = tensor1.get_memory_ptr();
    for (int i = 0; i < 10 * 20; i++) {
        *(static_cast<float*>(ptr1) + i) = i + 200;
    }

    auto slice0 = tensor.slice({0, 0}, {20, 10});
    auto slice1 = tensor.slice({0, 10}, {20, 20});

    slice0->copy_from(tensor0);
    slice1->copy_from(tensor1);

    ASSERT_FALSE(slice0->is_continue_memory());
    ASSERT_FALSE(slice1->is_continue_memory());

    for (size_t i = 0; i < 20; i++) {
        for (size_t j = 0; j < 10; j++) {
            ASSERT_EQ(float(i * 10 + j), *ptr);
            ++ptr;
        }
        for (size_t j = 0; j < 10; j++) {
            ASSERT_EQ(float(i * 10 + j + 200), *ptr);
            ++ptr;
        }
    }
    slice0->fill_zero();
    Tensor tmp;
    tmp.copy_from(*slice0);
    float* tmp_ptr = static_cast<float*>(tmp.get_memory_ptr());
    for (size_t i = 0; i < 20; i++) {
        for (size_t j = 0; j < 10; j++) {
            ASSERT_EQ(float(0), *tmp_ptr);
            ++tmp_ptr;
        }
    }
}

TEST(TestTensor, GetPtrOffset) {
    Layout layout{{20, 20}, 2};
    Tensor tensor(LiteDeviceType::LITE_CPU, layout);
    //! alloc memory
    auto ptr = static_cast<float*>(tensor.get_memory_ptr());

    auto ptr_offset = tensor.get_memory_ptr({10, 10});
    ASSERT_EQ(ptr_offset, ptr + 10 * 20 + 10);

    auto slice0 = tensor.slice({0, 0}, {20, 10});
    auto slice1 = tensor.slice({0, 10}, {20, 20});

    ASSERT_FALSE(slice0->is_continue_memory());
    ASSERT_FALSE(slice1->is_continue_memory());

    auto ptr_offset_slice0 = slice0->get_memory_ptr({6, 5});
    auto ptr_offset_slice1 = slice1->get_memory_ptr({2, 5});

    ASSERT_EQ(ptr_offset_slice0, ptr + 6 * 20 + 5);
    ASSERT_EQ(ptr_offset_slice1, ptr + 2 * 20 + 10 + 5);
}

TEST(TestTensor, Concat) {
    Layout layout{{5, 5, 5}, 3};
    std::vector<Tensor> tensors;
    for (int i = 0; i < 4; i++) {
        Tensor tensor(LiteDeviceType::LITE_CPU, layout);
        auto ptr = static_cast<float*>(tensor.get_memory_ptr());
        for (int n = 0; n < 5 * 5 * 5; n++) {
            ptr[n] = i;
        }
        tensors.push_back(tensor);
    }
    auto check = [&](int dim) {
        auto new_tensor = TensorUtils::concat(tensors, dim);
        auto ptr = static_cast<float*>(new_tensor->get_memory_ptr());
        size_t stride = std::pow(5, (3 - dim));
        for (int i = 0; i < 4; i++) {
            for (size_t j = 0; j < stride; j++) {
                ASSERT_EQ(ptr[i * stride + j], i);
            }
        }
    };
    check(0);
    check(1);
    check(2);
}

#if LITE_WITH_CUDA
TEST(TestTensor, BasicDevice) {
    Layout layout{{1, 3, 224, 224}, 4};
    Tensor tensor1(LiteDeviceType::LITE_CUDA, layout);
    Tensor tensor2(LiteDeviceType::LITE_CPU, layout);
    //! mge tensor has created
    ASSERT_TRUE(TensorHelper::implement(&tensor1));
    ASSERT_TRUE(TensorHelper::implement(&tensor2));

    //! check member
    ASSERT_EQ(tensor1.get_device_type(), LiteDeviceType::LITE_CUDA);
    ASSERT_EQ(tensor2.get_device_type(), LiteDeviceType::LITE_CPU);
    ASSERT_EQ(tensor2.get_layout(), layout);
    //! check the real tensor
    ASSERT_EQ(tensor1.get_tensor_total_size_in_byte(), 1 * 3 * 224 * 224 * 4);
    ASSERT_EQ(tensor2.get_tensor_total_size_in_byte(), 1 * 3 * 224 * 224 * 4);

    ASSERT_TRUE(TensorHelper::implement(&tensor2)
                        ->cast_final_safe<TensorImplDft>()
                        .host_tensor());

    ASSERT_FALSE(TensorHelper::implement(&tensor2)
                         ->cast_final_safe<TensorImplDft>()
                         .dev_tensor());
    ASSERT_TRUE(TensorHelper::implement(&tensor1)
                        ->cast_final_safe<TensorImplDft>()
                        .dev_tensor());
    ASSERT_FALSE(TensorHelper::implement(&tensor1)
                         ->cast_final_safe<TensorImplDft>()
                         .host_tensor());
}

TEST(TestTensor, SetLayoutReAllocDevice) {
    Layout layout{{1, 3, 224, 224}, 4};
    Tensor tensor2(LiteDeviceType::LITE_CUDA, layout);
    auto old_ptr2 = tensor2.get_memory_ptr();

    //! layout set through
    Layout layout1{{1, 3, 100, 100}, 4, LiteDataType::LITE_INT8};
    tensor2.set_layout(layout1);
    ASSERT_EQ(tensor2.get_tensor_total_size_in_byte(), 1 * 3 * 100 * 100);
    auto layout2 = TensorHelper::implement(&tensor2)
                           ->cast_final_safe<TensorImplDft>()
                           .dev_tensor()
                           ->layout();
    ASSERT_EQ(to_lite_layout(layout2), layout1);

    auto new_ptr2 = tensor2.get_memory_ptr();

    ASSERT_EQ(old_ptr2, new_ptr2);
}

TEST(TestTensor, CrossCNCopyDevice) {
    Layout layout{{1, 3, 224, 224}, 4};
    Tensor tensor0;
    Tensor tensor1(LiteDeviceType::LITE_CPU);
    Tensor tensor2(LiteDeviceType::LITE_CPU, layout);
    Tensor tensor3(LiteDeviceType::LITE_CUDA, layout);

    tensor2.copy_from(tensor3);
    tensor3.copy_from(tensor2);

    auto old_ptr2 = tensor2.get_memory_ptr();
    auto old_ptr3 = tensor3.get_memory_ptr();
    ASSERT_THROW(tensor3.copy_from(tensor1), std::exception);

    tensor1.copy_from(tensor3);
    tensor0.copy_from(tensor3);

    tensor2.copy_from(tensor3);
    tensor3.copy_from(tensor2);

    ASSERT_EQ(tensor2.get_memory_ptr(), old_ptr2);
    ASSERT_EQ(tensor3.get_memory_ptr(), old_ptr3);
}

TEST(TestTensor, PinnedHostMem) {
    Layout layout{{1, 3, 224, 224}, 4};
    Tensor tensor1(LiteDeviceType::LITE_CPU);
    bool is_pinned_host = true;
    Tensor tensor2(LiteDeviceType::LITE_CUDA, layout, is_pinned_host);
    Tensor tensor3(LiteDeviceType::LITE_CUDA, layout);
    tensor2.copy_from(tensor3);
    tensor3.copy_from(tensor2);

    ASSERT_EQ(tensor2.is_pinned_host(), true);
    ASSERT_EQ(tensor3.is_pinned_host(), false);

    auto old_ptr2 = tensor2.get_memory_ptr();
    auto old_ptr3 = tensor3.get_memory_ptr();

    //! test source tenor is empty
    ASSERT_THROW(tensor2.copy_from(tensor1), std::exception);
    tensor1.copy_from(tensor2);
    tensor2.copy_from(tensor3);
    tensor3.copy_from(tensor2);

    ASSERT_EQ(tensor2.get_memory_ptr(), old_ptr2);
    ASSERT_EQ(tensor3.get_memory_ptr(), old_ptr3);
}

TEST(TestTensor, DeviceId) {
M
Megvii Engine Team 已提交
428
    if (get_device_count(LITE_CUDA) <= 1)
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 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 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
        return;
    Layout layout{{1, 3, 224, 224}, 4};
    Tensor tensor2(0, LiteDeviceType::LITE_CUDA, layout);
    Tensor tensor3(1, LiteDeviceType::LITE_CUDA, layout);

    tensor2.copy_from(tensor3);
    tensor3.copy_from(tensor2);

    Tensor tensor1;
    tensor1.copy_from(tensor2);
    tensor1.copy_from(tensor3);
}

TEST(TestTensor, SliceDevice) {
    Layout layout{{20, 20}, 2};
    Tensor host_tensor0;
    Tensor dev_tensor0(LiteDeviceType::LITE_CUDA, layout);
    host_tensor0.copy_from(dev_tensor0);
    auto ptr = host_tensor0.get_memory_ptr();

    for (int i = 0; i < 20 * 20; i++) {
        *(static_cast<float*>(ptr) + i) = i;
    }
    dev_tensor0.copy_from(host_tensor0);

    auto check = [&](size_t start, size_t end, size_t step) {
        Tensor host_tensor;
        host_tensor.copy_from(
                *dev_tensor0.slice({start, start}, {end, end}, {step, step}));
        float* new_ptr = static_cast<float*>(host_tensor.get_memory_ptr());
        for (size_t i = start; i < end; i += step) {
            for (size_t j = start; j < end; j += step) {
                ASSERT_EQ(float(i * 20 + j), *new_ptr);
                ++new_ptr;
            }
        }
    };
    check(5, 10, 1);
    check(5, 11, 2);
    check(2, 18, 4);
}

TEST(TestTensor, MemSetDevice) {
    Layout layout{{20, 20}, 2, LiteDataType::LITE_INT8};
    Tensor host_tensor0(LiteDeviceType::LITE_CPU, layout);
    Tensor dev_tensor0(LiteDeviceType::LITE_CUDA, layout);
    auto check = [&](uint8_t val, const Tensor& tensor) {
        auto ptr = static_cast<uint8_t*>(tensor.get_memory_ptr());
        for (int i = 0; i < 20 * 20; i++) {
            ASSERT_EQ(val, *(ptr + i));
        }
    };
    host_tensor0.fill_zero();
    check(0, host_tensor0);

    Tensor host_tensor1;
    dev_tensor0.fill_zero();
    host_tensor1.copy_from(dev_tensor0);
    check(0, host_tensor1);
}

TEST(TestTensor, DeviceSliceCopy) {
    Layout layout{{20, 20}, 2};
    Tensor tensor(LiteDeviceType::LITE_CUDA, layout);
    //! alloc memory
    tensor.get_memory_ptr();

    Layout layout_slice{{20, 10}, 2};
    Tensor tensor0(LiteDeviceType::LITE_CPU, layout_slice);
    auto ptr0 = tensor0.get_memory_ptr();
    for (int i = 0; i < 10 * 20; i++) {
        *(static_cast<float*>(ptr0) + i) = i;
    }
    Tensor tensor1(LiteDeviceType::LITE_CPU, layout_slice);
    auto ptr1 = tensor1.get_memory_ptr();
    for (int i = 0; i < 10 * 20; i++) {
        *(static_cast<float*>(ptr1) + i) = i + 200;
    }

    auto slice0 = tensor.slice({0, 0}, {20, 10});
    auto slice1 = tensor.slice({0, 10}, {20, 20});

    slice0->copy_from(tensor0);
    slice1->copy_from(tensor1);

    ASSERT_FALSE(slice0->is_continue_memory());
    ASSERT_FALSE(slice1->is_continue_memory());

    Tensor host_tensor;
    host_tensor.copy_from(tensor);
    auto ptr = static_cast<float*>(host_tensor.get_memory_ptr());

    for (size_t i = 0; i < 20; i++) {
        for (size_t j = 0; j < 10; j++) {
            ASSERT_EQ(float(i * 10 + j), *ptr);
            ++ptr;
        }
        for (size_t j = 0; j < 10; j++) {
            ASSERT_EQ(float(i * 10 + j + 200), *ptr);
            ++ptr;
        }
    }
    slice0->fill_zero();
    Tensor tmp;
    tmp.copy_from(*slice0);
    float* tmp_ptr = static_cast<float*>(tmp.get_memory_ptr());
    for (size_t i = 0; i < 20; i++) {
        for (size_t j = 0; j < 10; j++) {
            ASSERT_EQ(float(0), *tmp_ptr);
            ++tmp_ptr;
        }
    }
}

TEST(TestTensor, ConcatDevice) {
    Layout layout{{5, 5, 5}, 3};
    std::vector<Tensor> tensors;
    for (int i = 0; i < 4; i++) {
        Tensor tensor(LiteDeviceType::LITE_CPU, layout);
        auto ptr = static_cast<float*>(tensor.get_memory_ptr());
        for (int n = 0; n < 5 * 5 * 5; n++) {
            ptr[n] = i;
        }
        tensors.push_back(tensor);
    }
    auto check = [&](int dim) {
        auto new_tensor =
                TensorUtils::concat(tensors, dim, LiteDeviceType::LITE_CUDA, 0);

        Tensor tensor(LiteDeviceType::LITE_CPU);
        tensor.copy_from(*new_tensor);
        auto ptr = static_cast<float*>(tensor.get_memory_ptr());
        size_t stride = std::pow(5, (3 - dim));
        for (int i = 0; i < 4; i++) {
            for (size_t j = 0; j < stride; j++) {
                ASSERT_EQ(ptr[i * stride + j], i);
            }
        }
        ASSERT_EQ(new_tensor->get_device_type(), LiteDeviceType::LITE_CUDA);
        ASSERT_EQ(new_tensor->get_device_id(), 0);
    };
    check(0);
    check(1);
    check(2);
}
574 575 576 577 578 579 580 581 582 583 584

TEST(TestTensor, CudaOutputDevice) {
    Layout layout{{1, 4}, 2};
    bool is_pinned_host = true;
    Tensor tensor(LiteDeviceType::LITE_CUDA, layout, is_pinned_host);
    // If is_pinned_host is true, when calling update_from_implement(), the device type
    // should always be updated with
    // get_device_from_locator(m_host_tensor->comp_node().locator()).
    tensor.update_from_implement();
    ASSERT_EQ(tensor.get_device_type(), LiteDeviceType::LITE_CUDA);
}
585 586 587 588
#endif
#endif

// vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}