test_fill_api.cc 7.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* 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>
#include <memory>

18
#include "paddle/pten/api/include/api.h"
19

20
#include "paddle/pten/api/lib/utils/allocator.h"
21 22 23
#include "paddle/pten/core/dense_tensor.h"
#include "paddle/pten/core/kernel_registry.h"

24 25 26
namespace paddle {
namespace tests {

27
namespace framework = paddle::framework;
28
using DDim = pten::framework::DDim;
29 30 31 32

// TODO(chenweihang): Remove this test after the API is used in the dygraph
TEST(API, full_like) {
  // 1. create tensor
33
  const auto alloc = std::make_unique<paddle::experimental::DefaultAllocator>(
34 35
      paddle::platform::CPUPlace());
  auto dense_x = std::make_shared<pten::DenseTensor>(
36
      alloc.get(),
37
      pten::DenseTensorMeta(pten::DataType::FLOAT32,
38
                            pten::framework::make_ddim({3, 2}),
39
                            pten::DataLayout::NCHW));
40 41
  auto* dense_x_data =
      dense_x->mutable_data<float>(paddle::platform::CPUPlace());
42 43 44 45 46 47 48 49 50 51
  dense_x_data[0] = 0;

  float val = 1.0;

  paddle::experimental::Tensor x(dense_x);

  // 2. test API
  auto out = paddle::experimental::full_like(x, val, pten::DataType::FLOAT32);

  // 3. check result
52 53
  ASSERT_EQ(out.dims().size(), 2);
  ASSERT_EQ(out.dims()[0], 3);
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
  ASSERT_EQ(out.numel(), 6);
  ASSERT_EQ(out.is_cpu(), true);
  ASSERT_EQ(out.type(), pten::DataType::FLOAT32);
  ASSERT_EQ(out.layout(), pten::DataLayout::NCHW);
  ASSERT_EQ(out.initialized(), true);

  auto dense_out = std::dynamic_pointer_cast<pten::DenseTensor>(out.impl());
  auto* actual_result = dense_out->data<float>();
  for (auto i = 0; i < 6; i++) {
    ASSERT_NEAR(actual_result[i], val, 1e-6f);
  }
}

TEST(API, zeros_like) {
  // 1. create tensor
69
  const auto alloc = std::make_unique<paddle::experimental::DefaultAllocator>(
70 71
      paddle::platform::CPUPlace());
  auto dense_x = std::make_shared<pten::DenseTensor>(
72
      alloc.get(),
73
      pten::DenseTensorMeta(pten::DataType::FLOAT32,
74
                            pten::framework::make_ddim({3, 2}),
75
                            pten::DataLayout::NCHW));
76 77
  auto* dense_x_data =
      dense_x->mutable_data<float>(paddle::platform::CPUPlace());
78 79 80 81 82
  dense_x_data[0] = 1;

  paddle::experimental::Tensor x(dense_x);

  // 2. test API
83
  auto out = paddle::experimental::zeros_like(x, pten::DataType::INT32);
84 85

  // 3. check result
86 87
  ASSERT_EQ(out.dims().size(), 2);
  ASSERT_EQ(out.dims()[0], 3);
88 89
  ASSERT_EQ(out.numel(), 6);
  ASSERT_EQ(out.is_cpu(), true);
90
  ASSERT_EQ(out.type(), pten::DataType::INT32);
91 92 93 94
  ASSERT_EQ(out.layout(), pten::DataLayout::NCHW);
  ASSERT_EQ(out.initialized(), true);

  auto dense_out = std::dynamic_pointer_cast<pten::DenseTensor>(out.impl());
95
  auto* actual_result = dense_out->data<int32_t>();
96
  for (auto i = 0; i < 6; i++) {
97
    ASSERT_EQ(actual_result[i], 0);
98 99 100 101 102
  }
}

TEST(API, ones_like) {
  // 1. create tensor
103
  const auto alloc = std::make_unique<paddle::experimental::DefaultAllocator>(
104 105
      paddle::platform::CPUPlace());
  auto dense_x = std::make_shared<pten::DenseTensor>(
106
      alloc.get(),
107
      pten::DenseTensorMeta(pten::DataType::INT32,
108
                            pten::framework::make_ddim({3, 2}),
109
                            pten::DataLayout::NCHW));
110 111
  auto* dense_x_data =
      dense_x->mutable_data<int32_t>(paddle::platform::CPUPlace());
112 113 114 115 116 117 118 119
  dense_x_data[0] = 0;

  paddle::experimental::Tensor x(dense_x);

  // 2. test API
  auto out = paddle::experimental::ones_like(x, pten::DataType::INT32);

  // 3. check result
120 121
  ASSERT_EQ(out.dims().size(), 2);
  ASSERT_EQ(out.dims()[0], 3);
122 123 124 125 126 127 128 129 130 131 132 133
  ASSERT_EQ(out.numel(), 6);
  ASSERT_EQ(out.is_cpu(), true);
  ASSERT_EQ(out.type(), pten::DataType::INT32);
  ASSERT_EQ(out.layout(), pten::DataLayout::NCHW);
  ASSERT_EQ(out.initialized(), true);

  auto dense_out = std::dynamic_pointer_cast<pten::DenseTensor>(out.impl());
  auto* actual_result = dense_out->data<int32_t>();
  for (auto i = 0; i < 6; i++) {
    ASSERT_EQ(actual_result[i], 1);
  }
}
134

135
TEST(API, full1) {
136
  // 1. create tensor
137
  const auto alloc = std::make_unique<paddle::experimental::DefaultAllocator>(
138 139
      paddle::platform::CPUPlace());

140
  auto dense_shape = std::make_shared<pten::DenseTensor>(
141
      alloc.get(),
142
      pten::DenseTensorMeta(pten::DataType::INT64,
143
                            pten::framework::make_ddim({2}),
144
                            pten::DataLayout::NCHW));
145 146
  auto* shape_data =
      dense_shape->mutable_data<int64_t>(paddle::platform::CPUPlace());
147 148 149 150
  shape_data[0] = 2;
  shape_data[1] = 3;

  auto dense_scalar = std::make_shared<pten::DenseTensor>(
151
      alloc.get(),
152
      pten::DenseTensorMeta(pten::DataType::FLOAT32,
153
                            pten::framework::make_ddim({1}),
154
                            pten::DataLayout::NCHW));
155
  dense_scalar->mutable_data<float>(paddle::platform::CPUPlace())[0] = 1.0;
156 157 158 159 160

  paddle::experimental::Tensor value(dense_scalar);

  paddle::experimental::Tensor tensor_shape(dense_shape);

161 162 163
  float val = 1.0;

  // 2. test API
164 165
  auto out =
      paddle::experimental::full(tensor_shape, value, pten::DataType::FLOAT32);
166 167

  // 3. check result
168
  ASSERT_EQ(out.shape().size(), 2UL);
169
  ASSERT_EQ(out.shape()[0], 2);
170 171 172 173 174 175 176 177 178 179 180 181
  ASSERT_EQ(out.numel(), 6);
  ASSERT_EQ(out.is_cpu(), true);
  ASSERT_EQ(out.type(), pten::DataType::FLOAT32);
  ASSERT_EQ(out.layout(), pten::DataLayout::NCHW);
  ASSERT_EQ(out.initialized(), true);

  auto dense_out = std::dynamic_pointer_cast<pten::DenseTensor>(out.impl());
  auto* actual_result = dense_out->data<float>();
  for (auto i = 0; i < 6; i++) {
    ASSERT_NEAR(actual_result[i], val, 1e-6f);
  }
}
182

183
TEST(API, full2) {
184
  const auto alloc = std::make_unique<paddle::experimental::DefaultAllocator>(
185 186 187
      paddle::platform::CPUPlace());

  auto dense_scalar = std::make_shared<pten::DenseTensor>(
188
      alloc.get(),
189
      pten::DenseTensorMeta(pten::DataType::INT32,
190
                            pten::framework::make_ddim({1}),
191
                            pten::DataLayout::NCHW));
192
  dense_scalar->mutable_data<int>(paddle::platform::CPUPlace())[0] = 2;
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241

  paddle::experimental::Tensor shape_scalar1(dense_scalar);
  paddle::experimental::Tensor shape_scalar2(dense_scalar);
  std::vector<paddle::experimental::Tensor> list_shape{shape_scalar1,
                                                       shape_scalar2};

  float val = 1.0;

  auto out =
      paddle::experimental::full(list_shape, val, pten::DataType::FLOAT32);

  ASSERT_EQ(out.shape().size(), 2UL);
  ASSERT_EQ(out.shape()[0], 2);
  ASSERT_EQ(out.numel(), 4);
  ASSERT_EQ(out.is_cpu(), true);
  ASSERT_EQ(out.type(), pten::DataType::FLOAT32);
  ASSERT_EQ(out.layout(), pten::DataLayout::NCHW);
  ASSERT_EQ(out.initialized(), true);

  auto dense_out = std::dynamic_pointer_cast<pten::DenseTensor>(out.impl());
  auto* actual_result = dense_out->data<float>();
  for (auto i = 0; i < 4; i++) {
    ASSERT_NEAR(actual_result[i], val, 1e-6f);
  }
}

TEST(API, full3) {
  std::vector<int64_t> vector_shape{2, 3};

  float val = 1.0;

  auto out =
      paddle::experimental::full(vector_shape, val, pten::DataType::INT32);

  ASSERT_EQ(out.shape().size(), 2UL);
  ASSERT_EQ(out.shape()[0], 2);
  ASSERT_EQ(out.numel(), 6);
  ASSERT_EQ(out.is_cpu(), true);
  ASSERT_EQ(out.type(), pten::DataType::INT32);
  ASSERT_EQ(out.layout(), pten::DataLayout::NCHW);
  ASSERT_EQ(out.initialized(), true);

  auto dense_out = std::dynamic_pointer_cast<pten::DenseTensor>(out.impl());
  auto* actual_result = dense_out->data<int>();
  for (auto i = 0; i < 6; i++) {
    ASSERT_EQ(actual_result[i], 1);
  }
}

242 243
}  // namespace tests
}  // namespace paddle