test_dense_tensor.cc 3.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.

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 "gtest/gtest.h"

#include "paddle/pten/core/dense_tensor.h"
#include "paddle/pten/tests/core/allocator.h"

namespace pten {
namespace tests {

TEST(dense_tensor, meta) {
  const DDim dims({1, 2});
  const DataType dtype{DataType::INT8};
  const DataLayout layout{DataLayout::NHWC};
  // TODO(Shixiaowei02): need to check the lod is valid.
28
  const LoD lod{};
29 30 31 32 33

  DenseTensorMeta meta_0;
  CHECK(!meta_0.valid());

  DenseTensorMeta meta_1(dtype, dims);
34
  CHECK(meta_1.dtype == dtype);
35 36 37 38
  CHECK(meta_1.dims == dims);
  CHECK(meta_1.valid());

  DenseTensorMeta meta_2(dtype, dims, layout);
39
  CHECK(meta_2.dtype == dtype);
40 41 42 43 44
  CHECK(meta_2.dims == dims);
  CHECK(meta_2.layout == layout);
  CHECK(meta_2.valid());

  DenseTensorMeta meta_3(dtype, dims, layout, lod);
45
  CHECK(meta_3.dtype == dtype);
46 47 48 49 50 51
  CHECK(meta_3.dims == dims);
  CHECK(meta_3.layout == layout);
  CHECK(meta_3.lod == lod);
  CHECK(meta_3.valid());

  DenseTensorMeta meta_4(meta_3);
52
  CHECK(meta_4.dtype == dtype);
53 54 55 56 57 58
  CHECK(meta_4.dims == dims);
  CHECK(meta_4.layout == layout);
  CHECK(meta_4.lod == lod);
  CHECK(meta_4.valid());

  DenseTensorMeta meta_5(std::move(meta_4));
59
  CHECK(meta_5.dtype == dtype);
60 61 62 63 64 65 66 67
  CHECK(meta_5.dims == dims);
  CHECK(meta_5.layout == layout);
  CHECK(meta_5.lod == lod);
  CHECK(meta_5.valid());
}

TEST(dense_tensor, def_ctor) {
  DenseTensor tensor_0;
68
  CHECK(tensor_0.valid());
69 70 71 72 73 74
}

TEST(dense_tensor, ctor) {
  const DDim dims({1, 2});
  const DataType dtype{DataType::INT8};
  const DataLayout layout{DataLayout::NHWC};
75
  const LoD lod{};
76 77 78 79 80 81 82 83 84
  DenseTensorMeta meta(dtype, dims, layout, lod);

  auto alloc = std::make_shared<FancyAllocator>();

  auto check_dense_tensor = [](const DenseTensor& t,
                               const DenseTensorMeta& m) -> bool {
    bool r{true};
    r = r && (t.numel() == product(m.dims));
    r = r && (t.dims() == m.dims);
85
    r = r && (t.dtype() == m.dtype);
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    r = r && (t.layout() == m.layout);
    r = r && (t.place() == paddle::platform::CPUPlace());
    r = r && t.initialized();
    r = r && t.IsSharedWith(t);
    return r;
  };

  DenseTensor tensor_0(alloc, meta);
  check_dense_tensor(tensor_0, meta);

  DenseTensor tensor_1(alloc, DenseTensorMeta(meta));
  check_dense_tensor(tensor_0, meta);

  DenseTensor tensor_2(make_intrusive<TensorStorage>(alloc), meta);
  CHECK_NOTNULL(tensor_2.mutable_data<int8_t>());
  check_dense_tensor(tensor_2, meta);
}

TEST(dense_tensor, resize) {
  const DDim dims({1, 2});
  const DataType dtype{DataType::INT8};
  const DataLayout layout{DataLayout::NHWC};
108
  const LoD lod{};
109 110 111 112 113
  DenseTensorMeta meta(dtype, dims, layout, lod);

  auto alloc = std::make_shared<FancyAllocator>();
  DenseTensor tensor_0(alloc, meta);

114
  CHECK_EQ(tensor_0.capacity(), 2u);
115
  tensor_0.Resize({1, 2, 3});
116
  CHECK_EQ(tensor_0.capacity(), 6u);
117
  tensor_0.mutable_data<int8_t>();
118
  CHECK_EQ(tensor_0.capacity(), 6u);
119 120 121 122 123

  auto storage = tensor_0.release();
  CHECK_EQ(storage->size(), 6u);
}

124 125 126 127
TEST(dense_tensor, shallow_copy) {
  const DDim dims({1, 2});
  const DataType dtype{DataType::INT8};
  const DataLayout layout{DataLayout::NHWC};
128
  const LoD lod{};
129 130 131 132 133 134 135 136 137 138
  DenseTensorMeta meta(dtype, dims, layout, lod);

  auto alloc = std::make_shared<FancyAllocator>();
  DenseTensor tensor_0(alloc, meta);

  DenseTensor tensor_1(tensor_0);
  CHECK(tensor_0.meta() == tensor_1.meta());
  CHECK(tensor_0.release() == tensor_1.release());
}

139 140
}  // namespace tests
}  // namespace pten