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

17
#include "paddle/pten/api/lib/utils/tensor_utils.h"
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

namespace paddle {
namespace tests {

using DDim = paddle::framework::DDim;
using DataType = paddle::experimental::DataType;
using DataLayout = paddle::experimental::DataLayout;

using DenseTensor = pten::DenseTensor;
using DenseTensorMeta = pten::DenseTensorMeta;

TEST(tensor_utils, dense_tensor_to_lod_tensor) {
  const DDim dims({2, 1});
  const DataType dtype{DataType::FLOAT32};
  const DataLayout layout{DataLayout::NCHW};
  const std::vector<std::vector<size_t>> lod{{0, 2}};
  DenseTensorMeta meta(dtype, dims, layout, lod);

36 37
  auto alloc =
      std::make_shared<experimental::DefaultAllocator>(platform::CPUPlace());
38 39 40 41 42 43 44

  DenseTensor dense_tensor(alloc, meta);
  float* data = dense_tensor.mutable_data<float>();
  data[0] = 1.0f;
  data[1] = 2.1f;

  framework::LoDTensor lod_tensor;
45
  experimental::MovesStorage(&dense_tensor, &lod_tensor);
46 47 48 49

  CHECK(dense_tensor.lod().size() == lod_tensor.lod().size());
  CHECK(dense_tensor.lod()[0] ==
        static_cast<std::vector<size_t>>((lod_tensor.lod()[0])));
50
  CHECK(dense_tensor.dtype() == pten::TransToPtenDataType(lod_tensor.type()));
51 52 53 54 55 56 57
  CHECK(dense_tensor.layout() ==
        pten::TransToPtenDataLayout(lod_tensor.layout()));
  CHECK(platform::is_cpu_place(lod_tensor.place()));

  CHECK(lod_tensor.data<float>()[0] == 1.0f);
  CHECK(lod_tensor.data<float>()[1] == 2.1f);

58
  auto dense_tensor_1 = experimental::MakePtenDenseTensor(lod_tensor);
59
  CHECK(dense_tensor_1->dims() == dims);
60
  CHECK(dense_tensor_1->dtype() == dtype);
61 62 63 64 65 66 67 68 69 70 71 72 73 74
  CHECK(dense_tensor_1->layout() == layout);
  CHECK(dense_tensor_1->lod().size() == lod.size());
  CHECK(dense_tensor_1->lod()[0] == lod[0]);
  const float* data_1 = dense_tensor_1->data<float>();
  CHECK(data_1[0] == 1.0f);
  CHECK(data_1[1] == 2.1f);
}

TEST(tensor_utils, dense_tensor_to_tensor) {
  const DDim dims({2, 1});
  const DataType dtype{DataType::FLOAT32};
  const DataLayout layout{DataLayout::NCHW};
  DenseTensorMeta meta(dtype, dims, layout);

75 76
  auto alloc =
      std::make_shared<experimental::DefaultAllocator>(platform::CPUPlace());
77 78 79 80 81 82 83

  DenseTensor dense_tensor(alloc, meta);
  float* data = dense_tensor.mutable_data<float>();
  data[0] = 1.0f;
  data[1] = 2.1f;

  framework::Tensor tensor;
84
  experimental::MovesStorage(&dense_tensor, &tensor);
85

86
  CHECK(dense_tensor.dtype() == pten::TransToPtenDataType(tensor.type()));
87 88 89 90 91 92
  CHECK(dense_tensor.layout() == pten::TransToPtenDataLayout(tensor.layout()));
  CHECK(platform::is_cpu_place(tensor.place()));

  CHECK(tensor.data<float>()[0] == 1.0f);
  CHECK(tensor.data<float>()[1] == 2.1f);

93
  auto dense_tensor_1 = experimental::MakePtenDenseTensor(tensor);
94
  CHECK(dense_tensor_1->dims() == dims);
95
  CHECK(dense_tensor_1->dtype() == dtype);
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
  CHECK(dense_tensor_1->layout() == layout);
  const float* data_1 = dense_tensor_1->data<float>();
  CHECK(data_1[0] == 1.0f);
  CHECK(data_1[1] == 2.1f);
}

TEST(PtenUtils, VarToPtTensor) {
  // 1. create Variable
  paddle::framework::Variable v;
  auto selected_rows = v.GetMutable<paddle::framework::SelectedRows>();
  paddle::framework::Tensor* value = selected_rows->mutable_value();
  auto* data = value->mutable_data<int>(paddle::framework::make_ddim({1, 1}),
                                        paddle::platform::CPUPlace());
  data[0] = 123;
  pten::Backend expect_backend = pten::Backend::CPU;

#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
  expect_backend = pten::Backend::CUDA;
#endif
  auto tensor_def = pten::TensorArgDef(
      expect_backend, pten::DataLayout::NCHW, pten::DataType::INT32);
  // 2. test API
118
  auto tensor_x = experimental::MakePtenTensorBaseFromVar(v, tensor_def);
119
  // 3. check result
120
  ASSERT_EQ(tensor_x->dtype(), pten::DataType::INT32);
121 122 123 124
}

}  // namespace tests
}  // namespace paddle