test_creation_dev_api.cc 4.6 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 19 20
#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/kernels/empty_kernel.h"
#include "paddle/phi/kernels/full_kernel.h"
21

22
#include "paddle/fluid/memory/allocation/allocator_facade.h"
23 24 25
#include "paddle/phi/api/lib/utils/allocator.h"
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/kernel_registry.h"
26

27
namespace phi {
28 29 30
namespace tests {

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

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

  // 2. test API
42
  auto out = phi::Empty<int>(dev_ctx, {3, 2}, phi::DataType::INT32);
43 44 45 46 47

  // 3. check result
  ASSERT_EQ(out.dims().size(), 2);
  ASSERT_EQ(out.dims()[0], 3);
  ASSERT_EQ(out.numel(), 6);
48 49
  ASSERT_EQ(out.meta().dtype, phi::DataType::INT32);
  ASSERT_EQ(out.meta().layout, phi::DataLayout::NCHW);
50 51 52 53
}

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
  phi::DenseTensor dense_x(alloc.get(),
                           phi::DenseTensorMeta(phi::DataType::FLOAT32,
                                                phi::make_ddim({3, 2}),
                                                phi::DataLayout::NCHW));
60 61
  auto* dense_x_data =
      dense_x.mutable_data<float>(paddle::platform::CPUPlace());
62 63 64
  dense_x_data[0] = 0;

  // 2. test API
65
  phi::CPUContext dev_ctx;
W
Wilber 已提交
66 67 68 69
  dev_ctx.SetAllocator(paddle::memory::allocation::AllocatorFacade::Instance()
                           .GetAllocator(paddle::platform::CPUPlace())
                           .get());
  dev_ctx.Init();
70
  auto out = phi::EmptyLike<float>(dev_ctx, dense_x);
71 72 73 74 75

  // 3. check result
  ASSERT_EQ(out.dims().size(), 2);
  ASSERT_EQ(out.dims()[0], 3);
  ASSERT_EQ(out.numel(), 6);
76 77
  ASSERT_EQ(out.meta().dtype, phi::DataType::FLOAT32);
  ASSERT_EQ(out.meta().layout, phi::DataLayout::NCHW);
78 79 80 81 82 83 84
}

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

  // 2. test API
85
  phi::CPUContext dev_ctx;
W
Wilber 已提交
86 87 88 89
  dev_ctx.SetAllocator(paddle::memory::allocation::AllocatorFacade::Instance()
                           .GetAllocator(paddle::platform::CPUPlace())
                           .get());
  dev_ctx.Init();
90
  auto out = phi::Full<float>(dev_ctx, {3, 2}, val, phi::DataType::FLOAT32);
91 92 93 94 95

  // 3. check result
  ASSERT_EQ(out.dims().size(), 2);
  ASSERT_EQ(out.dims()[0], 3);
  ASSERT_EQ(out.numel(), 6);
96 97
  ASSERT_EQ(out.meta().dtype, phi::DataType::FLOAT32);
  ASSERT_EQ(out.meta().layout, phi::DataLayout::NCHW);
98 99 100 101 102 103 104 105 106

  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
107
  const auto alloc = std::make_unique<paddle::experimental::DefaultAllocator>(
108
      paddle::platform::CPUPlace());
109 110 111 112
  phi::DenseTensor dense_x(alloc.get(),
                           phi::DenseTensorMeta(phi::DataType::FLOAT32,
                                                phi::make_ddim({3, 2}),
                                                phi::DataLayout::NCHW));
113 114
  auto* dense_x_data =
      dense_x.mutable_data<float>(paddle::platform::CPUPlace());
115 116 117
  dense_x_data[0] = 0;
  float val = 1.0;

118
  phi::CPUContext dev_ctx;
W
Wilber 已提交
119 120 121 122
  dev_ctx.SetAllocator(paddle::memory::allocation::AllocatorFacade::Instance()
                           .GetAllocator(paddle::platform::CPUPlace())
                           .get());
  dev_ctx.Init();
123 124

  // 2. test API
125
  auto out = phi::FullLike<float>(dev_ctx, dense_x, val);
126 127 128 129 130

  // 3. check result
  ASSERT_EQ(out.dims().size(), 2);
  ASSERT_EQ(out.dims()[0], 3);
  ASSERT_EQ(out.numel(), 6);
131 132
  ASSERT_EQ(out.meta().dtype, phi::DataType::FLOAT32);
  ASSERT_EQ(out.meta().layout, phi::DataLayout::NCHW);
133 134 135 136 137 138 139 140

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

}  // namespace tests
141
}  // namespace phi