tensor_test.cc 8.0 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.
F
fengjiayi 已提交
14

Y
Yi Wang 已提交
15
#include "paddle/fluid/framework/tensor.h"
F
fengjiayi 已提交
16
#include <gtest/gtest.h>
17
#include <string>
D
dzhwinter 已提交
18
#include "paddle/fluid/platform/float16.h"
F
fengjiayi 已提交
19

20 21 22
namespace framework = paddle::framework;
namespace platform = paddle::platform;

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

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

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

TEST(Tensor, MutableData) {
53
  {
54
    framework::Tensor src_tensor;
55 56 57
    float* p1 = nullptr;
    float* p2 = nullptr;
    // initialization
58 59
    p1 = src_tensor.mutable_data<float>(framework::make_ddim({1, 2, 3}),
                                        platform::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
63 64
    p2 = src_tensor.mutable_data<float>(framework::make_ddim({3, 4}),
                                        platform::CPUPlace());
65 66
    EXPECT_NE(p2, nullptr);
    EXPECT_NE(p1, p2);
F
fengjiayi 已提交
67
    // set src_tensor a new dim with same size
68
    // momery block is supposed to be unchanged
69 70
    p1 = src_tensor.mutable_data<float>(framework::make_ddim({2, 2, 3}),
                                        platform::CPUPlace());
71
    EXPECT_EQ(p1, p2);
F
fengjiayi 已提交
72
    // set src_tensor a new dim with smaller size
73
    // momery block is supposed to be unchanged
74 75
    p2 = src_tensor.mutable_data<float>(framework::make_ddim({2, 2}),
                                        platform::CPUPlace());
76 77
    EXPECT_EQ(p1, p2);
  }
L
liaogang 已提交
78

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

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

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

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

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

172
#ifdef PADDLE_WITH_CUDA
173
  {
174 175 176 177 178
    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();
179 180 181
    ASSERT_EQ(arity(slice_dims), 2);
    EXPECT_EQ(slice_dims[0], 4);
    EXPECT_EQ(slice_dims[1], 9);
F
fengjiayi 已提交
182

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

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

TEST(Tensor, Layout) {
212
  framework::Tensor src;
M
mozga-intel 已提交
213
  ASSERT_EQ(src.layout(), framework::DataLayout::kNCHW);
214 215
  src.set_layout(framework::DataLayout::kAnyLayout);
  ASSERT_EQ(src.layout(), framework::DataLayout::kAnyLayout);
D
dzhwinter 已提交
216
}
D
dzhwinter 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230

TEST(Tensor, FP16) {
  using platform::float16;
  framework::Tensor src;
  float16* src_ptr = src.mutable_data<float16>({2, 3}, platform::CPUPlace());
  for (int i = 0; i < 2 * 3; ++i) {
    src_ptr[i] = static_cast<float16>(i);
  }
  EXPECT_EQ(src.memory_size(), 2 * 3 * sizeof(float16));
  // EXPECT a human readable error message
  // src.data<uint8_t>();
  // Tensor holds the wrong type, it holds N6paddle8platform7float16E at
  // [/paddle/Paddle/paddle/fluid/framework/tensor_impl.h:43]
}