test_pten_tensor.cc 8.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 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 "glog/logging.h"
#include "gtest/gtest.h"
17
#include "paddle/phi/api/include/tensor.h"
18 19 20 21 22 23 24
#include "paddle/phi/core/kernel_registry.h"

PD_DECLARE_KERNEL(copy, CPU, ALL_LAYOUT);

#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
PD_DECLARE_KERNEL(copy, GPU, ALL_LAYOUT);
#endif
25

26
namespace paddle {
27
namespace tests {
28 29

template <typename T>
30
experimental::Tensor InitCPUTensorForTest() {
C
Chen Weihang 已提交
31
  std::vector<int64_t> tensor_shape{5, 5};
32
  auto t1 = experimental::Tensor(paddle::PlaceType::kCPU, tensor_shape);
33 34
  auto* p_data_ptr = t1.mutable_data<T>(paddle::PlaceType::kCPU);
  for (int64_t i = 0; i < t1.size(); i++) {
35
    p_data_ptr[i] = T(5);
36 37 38 39 40 41 42 43 44 45
  }
  return t1;
}

template <typename T>
void TestCopyTensor() {
  auto t1 = InitCPUTensorForTest<T>();
  auto t1_cpu_cp = t1.template copy_to<T>(paddle::PlaceType::kCPU);
  CHECK((paddle::PlaceType::kCPU == t1_cpu_cp.place()));
  for (int64_t i = 0; i < t1.size(); i++) {
C
Chen Weihang 已提交
46
    CHECK_EQ(t1_cpu_cp.template mutable_data<T>()[i], T(5));
47
  }
48
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
49 50 51 52 53 54
  VLOG(2) << "Do GPU copy test";
  auto t1_gpu_cp = t1_cpu_cp.template copy_to<T>(paddle::PlaceType::kGPU);
  CHECK((paddle::PlaceType::kGPU == t1_gpu_cp.place()));
  auto t1_gpu_cp_cp = t1_gpu_cp.template copy_to<T>(paddle::PlaceType::kGPU);
  CHECK((paddle::PlaceType::kGPU == t1_gpu_cp_cp.place()));
  auto t1_gpu_cp_cp_cpu =
55 56 57
      t1_gpu_cp_cp.template copy_to<T>(paddle::PlaceType::kCPU);
  CHECK((paddle::PlaceType::kCPU == t1_gpu_cp_cp_cpu.place()));
  for (int64_t i = 0; i < t1.size(); i++) {
C
Chen Weihang 已提交
58
    CHECK_EQ(t1_gpu_cp_cp_cpu.template mutable_data<T>()[i], T(5));
59
  }
60 61 62 63
#endif
}

void TestAPIPlace() {
C
Chen Weihang 已提交
64
  std::vector<int64_t> tensor_shape = {5, 5};
65
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
66
  auto t1 = experimental::Tensor(paddle::PlaceType::kGPU, tensor_shape);
67
  t1.mutable_data<float>(paddle::PlaceType::kGPU);
68 69
  CHECK((paddle::PlaceType::kGPU == t1.place()));
#endif
70
  auto t2 = experimental::Tensor(paddle::PlaceType::kCPU, tensor_shape);
71
  t2.mutable_data<float>(paddle::PlaceType::kCPU);
72 73 74 75
  CHECK((paddle::PlaceType::kCPU == t2.place()));
}

void TestAPISizeAndShape() {
C
Chen Weihang 已提交
76
  std::vector<int64_t> tensor_shape = {5, 5};
77
  auto t1 = experimental::Tensor(paddle::PlaceType::kCPU, tensor_shape);
78 79 80 81
  CHECK_EQ(t1.size(), 25);
  CHECK(t1.shape() == tensor_shape);
}

H
Hao Lin 已提交
82 83 84 85 86
void TestAPISlice() {
  std::vector<int64_t> tensor_shape_origin1 = {5, 5};
  std::vector<int64_t> tensor_shape_sub1 = {3, 5};
  std::vector<int64_t> tensor_shape_origin2 = {5, 5, 5};
  std::vector<int64_t> tensor_shape_sub2 = {1, 5, 5};
87
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
88
  auto t1 = experimental::Tensor(paddle::PlaceType::kGPU, tensor_shape_origin1);
89
  t1.mutable_data<float>(paddle::PlaceType::kGPU);
H
Hao Lin 已提交
90 91
  CHECK(t1.slice(0, 5).shape() == tensor_shape_origin1);
  CHECK(t1.slice(0, 3).shape() == tensor_shape_sub1);
92
  auto t2 = experimental::Tensor(paddle::PlaceType::kGPU, tensor_shape_origin2);
93
  t2.mutable_data<float>(paddle::PlaceType::kGPU);
H
Hao Lin 已提交
94 95
  CHECK(t2.slice(4, 5).shape() == tensor_shape_sub2);
#endif
96
  auto t3 = experimental::Tensor(paddle::PlaceType::kCPU, tensor_shape_origin1);
97
  t3.mutable_data<float>(paddle::PlaceType::kCPU);
H
Hao Lin 已提交
98 99
  CHECK(t3.slice(0, 5).shape() == tensor_shape_origin1);
  CHECK(t3.slice(0, 3).shape() == tensor_shape_sub1);
100
  auto t4 = experimental::Tensor(paddle::PlaceType::kCPU, tensor_shape_origin2);
101
  t4.mutable_data<float>(paddle::PlaceType::kCPU);
H
Hao Lin 已提交
102 103 104 105 106
  CHECK(t4.slice(4, 5).shape() == tensor_shape_sub2);

  // Test writing function for sliced tensor
  auto t = InitCPUTensorForTest<float>();
  auto t_sliced = t.slice(0, 1);
107 108
  auto* t_sliced_data_ptr =
      t_sliced.mutable_data<float>(paddle::PlaceType::kCPU);
H
Hao Lin 已提交
109 110 111
  for (int64_t i = 0; i < t_sliced.size(); i++) {
    t_sliced_data_ptr[i] += static_cast<float>(5);
  }
112
  auto* t_data_ptr = t.mutable_data<float>(paddle::PlaceType::kCPU);
H
Hao Lin 已提交
113 114 115 116 117
  for (int64_t i = 0; i < t_sliced.size(); i++) {
    CHECK_EQ(t_data_ptr[i], static_cast<float>(10));
  }
}

118 119
template <typename T>
paddle::DataType TestDtype() {
C
Chen Weihang 已提交
120
  std::vector<int64_t> tensor_shape = {5, 5};
121
  auto t1 = experimental::Tensor(paddle::PlaceType::kCPU, tensor_shape);
122
  t1.template mutable_data<T>(paddle::PlaceType::kCPU);
123 124 125 126 127
  return t1.type();
}

template <typename T>
void TestCast(paddle::DataType data_type) {
C
Chen Weihang 已提交
128
  std::vector<int64_t> tensor_shape = {5, 5};
129
  auto t1 = experimental::Tensor(paddle::PlaceType::kCPU, tensor_shape);
130
  t1.template mutable_data<T>(paddle::PlaceType::kCPU);
131
  auto t2 = t1.cast(data_type);
132
  CHECK(t2.type() == data_type);
133
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
134
  auto tg1 = experimental::Tensor(paddle::PlaceType::kGPU);
135
  tg1.reshape(tensor_shape);
136
  tg1.template mutable_data<T>(paddle::PlaceType::kGPU);
137 138 139
  auto tg2 = tg1.cast(data_type);
  CHECK(tg2.type() == data_type);
#endif
140 141 142 143 144 145 146
}

void GroupTestCopy() {
  VLOG(2) << "Float cpu-cpu-gpu-gpu-cpu";
  TestCopyTensor<float>();
  VLOG(2) << "Double cpu-cpu-gpu-gpu-cpu";
  TestCopyTensor<double>();
147
  VLOG(2) << "int cpu-cpu-gpu-gpu-cpu";
148 149 150 151 152 153 154 155 156
  TestCopyTensor<int>();
  VLOG(2) << "int64 cpu-cpu-gpu-gpu-cpu";
  TestCopyTensor<int64_t>();
  VLOG(2) << "int16 cpu-cpu-gpu-gpu-cpu";
  TestCopyTensor<int16_t>();
  VLOG(2) << "int8 cpu-cpu-gpu-gpu-cpu";
  TestCopyTensor<int8_t>();
  VLOG(2) << "uint8 cpu-cpu-gpu-gpu-cpu";
  TestCopyTensor<uint8_t>();
157
  VLOG(2) << "complex<float> cpu-cpu-gpu-gpu-cpu";
158
  TestCopyTensor<paddle::complex64>();
159
  VLOG(2) << "complex<double> cpu-cpu-gpu-gpu-cpu";
160
  TestCopyTensor<paddle::complex128>();
161 162
  VLOG(2) << "Fp16 cpu-cpu-gpu-gpu-cpu";
  TestCopyTensor<paddle::float16>();
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
}

void GroupTestCast() {
  VLOG(2) << "int cast";
  TestCast<int>(paddle::DataType::FLOAT32);
  VLOG(2) << "int32 cast";
  TestCast<int32_t>(paddle::DataType::FLOAT32);
  VLOG(2) << "int64 cast";
  TestCast<int64_t>(paddle::DataType::FLOAT32);
  VLOG(2) << "double cast";
  TestCast<double>(paddle::DataType::FLOAT32);
  VLOG(2) << "bool cast";
  TestCast<bool>(paddle::DataType::FLOAT32);
  VLOG(2) << "uint8 cast";
  TestCast<uint8_t>(paddle::DataType::FLOAT32);
  VLOG(2) << "float cast";
  TestCast<float>(paddle::DataType::FLOAT32);
180
  VLOG(2) << "complex<float> cast";
181
  TestCast<paddle::complex64>(paddle::DataType::FLOAT32);
182
  VLOG(2) << "complex<double> cast";
183
  TestCast<paddle::complex128>(paddle::DataType::FLOAT32);
184 185
  VLOG(2) << "float16 cast";
  TestCast<paddle::float16>(paddle::DataType::FLOAT16);
186 187 188 189 190 191 192 193 194 195
}

void GroupTestDtype() {
  CHECK(TestDtype<float>() == paddle::DataType::FLOAT32);
  CHECK(TestDtype<double>() == paddle::DataType::FLOAT64);
  CHECK(TestDtype<int>() == paddle::DataType::INT32);
  CHECK(TestDtype<int64_t>() == paddle::DataType::INT64);
  CHECK(TestDtype<int16_t>() == paddle::DataType::INT16);
  CHECK(TestDtype<int8_t>() == paddle::DataType::INT8);
  CHECK(TestDtype<uint8_t>() == paddle::DataType::UINT8);
196 197
  CHECK(TestDtype<paddle::complex64>() == paddle::DataType::COMPLEX64);
  CHECK(TestDtype<paddle::complex128>() == paddle::DataType::COMPLEX128);
198
  CHECK(TestDtype<paddle::float16>() == paddle::DataType::FLOAT16);
199 200
}

201
void TestInitilized() {
202
  experimental::Tensor test_tensor(paddle::PlaceType::kCPU, {1, 1});
203
  CHECK(test_tensor.is_initialized() == true);
204
  test_tensor.mutable_data<float>(paddle::PlaceType::kCPU);
205
  CHECK(test_tensor.is_initialized() == true);
206
  float* tensor_data = test_tensor.mutable_data<float>();
207 208 209 210 211 212 213 214
  for (int i = 0; i < test_tensor.size(); i++) {
    tensor_data[i] = 0.5;
  }
  for (int i = 0; i < test_tensor.size(); i++) {
    CHECK(tensor_data[i] == 0.5);
  }
}

C
Chen Weihang 已提交
215 216 217 218 219
void TestJudgeTensorType() {
  experimental::Tensor test_tensor(paddle::PlaceType::kCPU, {1, 1});
  CHECK(test_tensor.is_dense_tensor() == true);
}

220
TEST(PhiTensor, All) {
C
Chen Weihang 已提交
221 222
  VLOG(2) << "TestCopy";
  GroupTestCopy();
223 224 225 226 227 228
  VLOG(2) << "TestDtype";
  GroupTestDtype();
  VLOG(2) << "TestShape";
  TestAPISizeAndShape();
  VLOG(2) << "TestPlace";
  TestAPIPlace();
C
Chen Weihang 已提交
229 230 231 232
  VLOG(2) << "TestSlice";
  TestAPISlice();
  VLOG(2) << "TestCast";
  GroupTestCast();
233 234
  VLOG(2) << "TestInitilized";
  TestInitilized();
C
Chen Weihang 已提交
235 236
  VLOG(2) << "TestJudgeTensorType";
  TestJudgeTensorType();
237
}
238 239

}  // namespace tests
240
}  // namespace paddle