test_creation_dev_api.cc 4.4 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>

W
Wilber 已提交
18
#include "paddle/pten/backends/cpu/cpu_context.h"
19 20
#include "paddle/pten/kernels/empty_kernel.h"
#include "paddle/pten/kernels/full_kernel.h"
21

22
#include "paddle/fluid/memory/allocation/allocator_facade.h"
23 24 25 26 27 28 29 30
#include "paddle/pten/api/lib/utils/allocator.h"
#include "paddle/pten/core/dense_tensor.h"
#include "paddle/pten/core/kernel_registry.h"

namespace pten {
namespace tests {

namespace framework = paddle::framework;
31
using DDim = pten::framework::DDim;
32 33 34

TEST(DEV_API, empty) {
  // 1. create input
W
Wilber 已提交
35
  pten::CPUContext dev_ctx;
36 37 38 39
  dev_ctx.SetDeviceAllocator(
      paddle::memory::allocation::AllocatorFacade::Instance()
          .GetAllocator(paddle::platform::CPUPlace())
          .get());
40 41

  // 2. test API
W
Wilber 已提交
42
  auto out = pten::Empty<float>(dev_ctx, {3, 2}, pten::DataType::INT32);
43 44 45 46 47 48 49 50 51 52 53

  // 3. check result
  ASSERT_EQ(out.dims().size(), 2);
  ASSERT_EQ(out.dims()[0], 3);
  ASSERT_EQ(out.numel(), 6);
  ASSERT_EQ(out.meta().dtype, pten::DataType::INT32);
  ASSERT_EQ(out.meta().layout, pten::DataLayout::NCHW);
}

TEST(DEV_API, empty_like) {
  // 1. create tensor
54
  const auto alloc = std::make_unique<paddle::experimental::DefaultAllocator>(
55
      paddle::platform::CPUPlace());
56 57 58 59 60
  pten::DenseTensor dense_x(
      alloc.get(),
      pten::DenseTensorMeta(pten::DataType::FLOAT32,
                            pten::framework::make_ddim({3, 2}),
                            pten::DataLayout::NCHW));
61 62
  auto* dense_x_data =
      dense_x.mutable_data<float>(paddle::platform::CPUPlace());
63 64 65
  dense_x_data[0] = 0;

  // 2. test API
W
Wilber 已提交
66
  pten::CPUContext dev_ctx;
67 68 69 70
  dev_ctx.SetDeviceAllocator(
      paddle::memory::allocation::AllocatorFacade::Instance()
          .GetAllocator(paddle::platform::CPUPlace())
          .get());
W
Wilber 已提交
71
  auto out = pten::EmptyLike<float>(dev_ctx, dense_x);
72 73 74 75 76 77 78 79 80 81 82 83 84 85

  // 3. check result
  ASSERT_EQ(out.dims().size(), 2);
  ASSERT_EQ(out.dims()[0], 3);
  ASSERT_EQ(out.numel(), 6);
  ASSERT_EQ(out.meta().dtype, pten::DataType::FLOAT32);
  ASSERT_EQ(out.meta().layout, pten::DataLayout::NCHW);
}

TEST(DEV_API, full) {
  // 1. create input
  float val = 1.0;

  // 2. test API
W
Wilber 已提交
86
  pten::CPUContext dev_ctx;
87 88 89 90
  dev_ctx.SetDeviceAllocator(
      paddle::memory::allocation::AllocatorFacade::Instance()
          .GetAllocator(paddle::platform::CPUPlace())
          .get());
W
Wilber 已提交
91
  auto out = pten::Full<float>(dev_ctx, {3, 2}, val, pten::DataType::FLOAT32);
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

  // 3. check result
  ASSERT_EQ(out.dims().size(), 2);
  ASSERT_EQ(out.dims()[0], 3);
  ASSERT_EQ(out.numel(), 6);
  ASSERT_EQ(out.meta().dtype, pten::DataType::FLOAT32);
  ASSERT_EQ(out.meta().layout, pten::DataLayout::NCHW);

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

TEST(DEV_API, full_like) {
  // 1. create tensor
108
  const auto alloc = std::make_unique<paddle::experimental::DefaultAllocator>(
109
      paddle::platform::CPUPlace());
110 111 112 113 114
  pten::DenseTensor dense_x(
      alloc.get(),
      pten::DenseTensorMeta(pten::DataType::FLOAT32,
                            pten::framework::make_ddim({3, 2}),
                            pten::DataLayout::NCHW));
115 116
  auto* dense_x_data =
      dense_x.mutable_data<float>(paddle::platform::CPUPlace());
117 118 119
  dense_x_data[0] = 0;
  float val = 1.0;

W
Wilber 已提交
120
  pten::CPUContext dev_ctx;
121 122 123 124
  dev_ctx.SetDeviceAllocator(
      paddle::memory::allocation::AllocatorFacade::Instance()
          .GetAllocator(paddle::platform::CPUPlace())
          .get());
125 126

  // 2. test API
W
Wilber 已提交
127
  auto out = pten::FullLike<float>(dev_ctx, dense_x, val);
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

  // 3. check result
  ASSERT_EQ(out.dims().size(), 2);
  ASSERT_EQ(out.dims()[0], 3);
  ASSERT_EQ(out.numel(), 6);
  ASSERT_EQ(out.meta().dtype, pten::DataType::FLOAT32);
  ASSERT_EQ(out.meta().layout, pten::DataLayout::NCHW);

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

}  // namespace tests
}  // namespace pten