tensor_test.cc 7.0 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 22
  Tensor tt;
  tt.set_dims(make_ddim({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>();
36 37
  } catch (paddle::framework::EnforceNotMet err) {
    caught = true;
F
fengjiayi 已提交
38
    std::string msg =
F
fengjiayi 已提交
39
        "Tenosr holds no memory. Call Tensor::mutable_data first.";
40 41 42 43 44 45
    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 已提交
46 47
}

F
fengjiayi 已提交
48
/* following tests are not available at present
F
fengjiayi 已提交
49 50 51 52 53
   because Memory::Alloc() and Memory::Free() have not been ready.

TEST(Tensor, MutableData) {
  using namespace paddle::framework;
  using namespace paddle::platform;
54
  {
F
fengjiayi 已提交
55
    Tensor src_tensor;
56 57 58
    float* p1 = nullptr;
    float* p2 = nullptr;
    // initialization
F
fengjiayi 已提交
59
    p1 = src_tensor.mutable_data<float>(make_ddim({1, 2, 3}), CPUPlace());
60
    EXPECT_NE(p1, nullptr);
F
fengjiayi 已提交
61
    // set src_tensor a new dim with large size
62
    // momery is supposed to be re-allocated
F
fengjiayi 已提交
63
    p2 = src_tensor.mutable_data<float>(make_ddim({3, 4}), CPUPlace());
64 65
    EXPECT_NE(p2, nullptr);
    EXPECT_NE(p1, p2);
F
fengjiayi 已提交
66
    // set src_tensor a new dim with same size
67
    // momery block is supposed to be unchanged
F
fengjiayi 已提交
68
    p1 = src_tensor.mutable_data<float>(make_ddim({2, 2, 3}), CPUPlace());
69
    EXPECT_EQ(p1, p2);
F
fengjiayi 已提交
70
    // set src_tensor a new dim with smaller size
71
    // momery block is supposed to be unchanged
F
fengjiayi 已提交
72
    p2 = src_tensor.mutable_data<float>(make_ddim({2, 2}), CPUPlace());
73 74
    EXPECT_EQ(p1, p2);
  }
F
fengjiayi 已提交
75

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

TEST(Tensor, ShareDataFrom) {
  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 已提交
108
      dst_tensor.ShareDataFrom<float>(src_tensor);
F
fengjiayi 已提交
109 110
    } catch (EnforceNotMet err) {
      caught = true;
F
fengjiayi 已提交
111 112 113
      std::string msg = "Tenosr holds no memory. Call Tensor::mutable_data
first."; const char* what = err.what(); for (size_t i = 0; i < msg.length();
++i) { ASSERT_EQ(what[i], msg[i]);
F
fengjiayi 已提交
114 115 116 117 118
      }
    }
    ASSERT_TRUE(caught);

    src_tensor.mutable_data<int>(make_ddim({2, 3, 4}), CPUPlace());
F
fengjiayi 已提交
119
    dst_tensor.ShareDataFrom<int>(src_tensor);
F
fengjiayi 已提交
120 121 122 123 124 125 126
    ASSERT_EQ(src_tensor.data<int>(), dst_tensor.data<int>());
  }

  {
    Tensor src_tensor;
    Tensor dst_tensor;
    src_tensor.mutable_data<int>(make_ddim({2, 3, 4}), GPUPlace());
F
fengjiayi 已提交
127
    dst_tensor.ShareDataFrom<int>(src_tensor);
F
fengjiayi 已提交
128 129 130 131 132 133 134 135 136 137
    ASSERT_EQ(src_tensor.data<int>(), dst_tensor.data<int>());
  }
}

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 已提交
138
    Tensor slice_tensor = src_tensor.Slice<int>(1, 3);
F
fengjiayi 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    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);
  }

  {
    Tensor src_tensor;
    src_tensor.mutable_data<double>(make_ddim({6, 9}), GPUPlace());
F
fengjiayi 已提交
161
    Tensor slice_tensor = src_tensor.Slice<double>(2, 6);
F
fengjiayi 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    DDim slice_dims = slice_tensor.dims();
    ASSERT_EQ(arity(slice_dims), 2);
    EXPECT_EQ(slice_dims[0], 4);
    EXPECT_EQ(slice_dims[1], 9);

    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);
  }
}

181 182 183 184 185 186 187 188 189
TEST(Tensor, CopyFrom) {
  using namespace paddle::framework;
  using namespace paddle::platform;

  Tensor src_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));
  Tensor dst_tensor;
F
fengjiayi 已提交
190
  dst_tensor.CopyFrom<int>(src_tensor, CPUPlace());
191 192 193 194 195 196
  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]);
  }

F
fengjiayi 已提交
197 198
  Tensor slice_tensor = src_tensor.Slice<int>(1, 2);
  dst_tensor.CopyFrom<int>(slice_tensor, CPUPlace());
199 200 201 202 203 204 205
  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]);
  }
}
F
fengjiayi 已提交
206
*/