tensor_test.cc 7.6 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

18 19 20
namespace framework = paddle::framework;
namespace platform = paddle::platform;

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

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

34 35
  bool caught = false;
  try {
F
fengjiayi 已提交
36
    src_tensor.data<double>();
37
  } catch (platform::EnforceNotMet err) {
38
    caught = true;
F
fengjiayi 已提交
39
    std::string msg =
Z
zchen0211 已提交
40
        "holder_ should not be null\nTensor holds no memory. Call "
Y
Yan Chunwei 已提交
41
        "Tensor::mutable_data first.";
42 43 44 45 46 47
    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 已提交
48 49
}

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

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

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

129 130
    src_tensor.mutable_data<int>(framework::make_ddim({2, 3, 4}),
                                 platform::CPUPlace());
131
    dst_tensor.ShareDataWith(src_tensor);
F
fengjiayi 已提交
132 133 134
    ASSERT_EQ(src_tensor.data<int>(), dst_tensor.data<int>());
  }

135
#ifdef PADDLE_WITH_CUDA
136
  {
137 138 139 140
    framework::Tensor src_tensor;
    framework::Tensor dst_tensor;
    src_tensor.mutable_data<int>(framework::make_ddim({2, 3, 4}),
                                 platform::CUDAPlace());
141
    dst_tensor.ShareDataWith(src_tensor);
142 143 144
    ASSERT_EQ(src_tensor.data<int>(), dst_tensor.data<int>());
  }
#endif
F
fengjiayi 已提交
145 146 147 148
}

TEST(Tensor, Slice) {
  {
149 150 151 152 153
    framework::Tensor src_tensor;
    src_tensor.mutable_data<int>(framework::make_ddim({5, 3, 4}),
                                 platform::CPUPlace());
    framework::Tensor slice_tensor = src_tensor.Slice(1, 3);
    framework::DDim slice_dims = slice_tensor.dims();
F
fengjiayi 已提交
154 155 156 157 158 159 160 161
    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>(
162
        src_tensor.mutable_data<int>(src_tensor.dims(), platform::CPUPlace()));
F
fengjiayi 已提交
163 164
    uintptr_t slice_data_address =
        reinterpret_cast<uintptr_t>(slice_tensor.data<int>());
165 166 167
    uintptr_t slice_mutable_data_address =
        reinterpret_cast<uintptr_t>(slice_tensor.mutable_data<int>(
            slice_tensor.dims(), platform::CPUPlace()));
F
fengjiayi 已提交
168 169 170 171 172
    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);
  }

173
#ifdef PADDLE_WITH_CUDA
174
  {
175 176 177 178 179
    framework::Tensor src_tensor;
    src_tensor.mutable_data<double>(framework::make_ddim({6, 9}),
                                    platform::CUDAPlace());
    framework::Tensor slice_tensor = src_tensor.Slice(2, 6);
    framework::DDim slice_dims = slice_tensor.dims();
180 181 182
    ASSERT_EQ(arity(slice_dims), 2);
    EXPECT_EQ(slice_dims[0], 4);
    EXPECT_EQ(slice_dims[1], 9);
F
fengjiayi 已提交
183

184 185
    uintptr_t src_data_address =
        reinterpret_cast<uintptr_t>(src_tensor.data<double>());
186 187 188
    uintptr_t src_mutable_data_address =
        reinterpret_cast<uintptr_t>(src_tensor.mutable_data<double>(
            src_tensor.dims(), platform::CUDAPlace()));
189 190
    uintptr_t slice_data_address =
        reinterpret_cast<uintptr_t>(slice_tensor.data<double>());
191 192 193
    uintptr_t slice_mutable_data_address =
        reinterpret_cast<uintptr_t>(slice_tensor.mutable_data<double>(
            slice_tensor.dims(), platform::CUDAPlace()));
194 195 196 197 198
    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 已提交
199 200
}

F
fengjiayi 已提交
201
TEST(Tensor, ReshapeToMatrix) {
202 203
  framework::Tensor src;
  int* src_ptr = src.mutable_data<int>({2, 3, 4, 9}, platform::CPUPlace());
F
WIP  
fengjiayi 已提交
204 205 206
  for (int i = 0; i < 2 * 3 * 4 * 9; ++i) {
    src_ptr[i] = i;
  }
207
  framework::Tensor res = framework::ReshapeToMatrix(src, 2);
F
WIP  
fengjiayi 已提交
208 209
  ASSERT_EQ(res.dims()[0], 2 * 3);
  ASSERT_EQ(res.dims()[1], 4 * 9);
Z
zchen0211 已提交
210
}
D
dzhwinter 已提交
211 212

TEST(Tensor, Layout) {
213 214 215 216
  framework::Tensor src;
  ASSERT_EQ(src.layout(), framework::DataLayout::kNHWC);
  src.set_layout(framework::DataLayout::kAnyLayout);
  ASSERT_EQ(src.layout(), framework::DataLayout::kAnyLayout);
D
dzhwinter 已提交
217
}