test_elementwise_dev_api.cc 8.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>

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

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

26 27 28
namespace pten {
namespace tests {

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

32
TEST(DEV_API, add) {
33
  // 1. create tensor
34
  const auto alloc = std::make_unique<paddle::experimental::DefaultAllocator>(
35
      paddle::platform::CPUPlace());
36 37 38 39 40
  pten::DenseTensor dense_x(
      alloc.get(),
      pten::DenseTensorMeta(pten::DataType::FLOAT32,
                            pten::framework::make_ddim({3, 10}),
                            pten::DataLayout::NCHW));
41 42
  auto* dense_x_data =
      dense_x.mutable_data<float>(paddle::platform::CPUPlace());
43

44 45 46 47 48
  pten::DenseTensor dense_y(
      alloc.get(),
      pten::DenseTensorMeta(pten::DataType::FLOAT32,
                            pten::framework::make_ddim({10}),
                            pten::DataLayout::NCHW));
49 50
  auto* dense_y_data =
      dense_y.mutable_data<float>(paddle::platform::CPUPlace());
51 52 53 54 55 56 57 58 59 60 61 62 63

  float sum[3][10] = {0.0};
  for (size_t i = 0; i < 3; ++i) {
    for (size_t j = 0; j < 10; ++j) {
      dense_x_data[i * 10 + j] = (i * 10 + j) * 1.0;
      sum[i][j] = (i * 10 + j) * 1.0 + j * 2.0;
    }
  }
  for (size_t i = 0; i < 10; ++i) {
    dense_y_data[i] = i * 2.0;
  }

  // 2. test API
W
Wilber 已提交
64
  pten::CPUContext dev_ctx;
65 66 67 68
  dev_ctx.SetDeviceAllocator(
      paddle::memory::allocation::AllocatorFacade::Instance()
          .GetAllocator(paddle::platform::CPUPlace())
          .get());
69
  auto dense_out = pten::Add<float>(dev_ctx, dense_x, dense_y);
70 71 72 73

  // 3. check result
  ASSERT_EQ(dense_out.dims().size(), 2);
  ASSERT_EQ(dense_out.dims()[0], 3);
Y
YuanRisheng 已提交
74 75
  ASSERT_EQ(dense_out.dtype(), pten::DataType::FLOAT32);
  ASSERT_EQ(dense_out.layout(), pten::DataLayout::NCHW);
76 77 78 79 80 81 82 83 84

  auto expect_result = sum;
  auto actual_result0 = dense_out.data<float>()[0];
  auto actual_result1 = dense_out.data<float>()[1];
  auto actual_result2 = dense_out.data<float>()[10];
  ASSERT_NEAR(expect_result[0][0], actual_result0, 1e-6f);
  ASSERT_NEAR(expect_result[0][1], actual_result1, 1e-6f);
  ASSERT_NEAR(expect_result[1][0], actual_result2, 1e-6f);
}
85 86 87

TEST(DEV_API, subtract) {
  // 1. create tensor
88
  const auto alloc = std::make_unique<paddle::experimental::DefaultAllocator>(
89
      paddle::platform::CPUPlace());
90 91 92 93 94
  pten::DenseTensor dense_x(
      alloc.get(),
      pten::DenseTensorMeta(pten::DataType::FLOAT32,
                            pten::framework::make_ddim({3, 10}),
                            pten::DataLayout::NCHW));
95 96
  auto* dense_x_data =
      dense_x.mutable_data<float>(paddle::platform::CPUPlace());
97

98 99 100 101 102
  pten::DenseTensor dense_y(
      alloc.get(),
      pten::DenseTensorMeta(pten::DataType::FLOAT32,
                            pten::framework::make_ddim({10}),
                            pten::DataLayout::NCHW));
103 104
  auto* dense_y_data =
      dense_y.mutable_data<float>(paddle::platform::CPUPlace());
105 106 107 108 109 110 111 112 113 114 115 116 117

  float sub[3][10] = {0.0};
  for (size_t i = 0; i < 3; ++i) {
    for (size_t j = 0; j < 10; ++j) {
      dense_x_data[i * 10 + j] = (i * 10 + j) * 1.0;
      sub[i][j] = (i * 10 + j) * 1.0 - j * 2.0;
    }
  }
  for (size_t i = 0; i < 10; ++i) {
    dense_y_data[i] = i * 2.0;
  }

  // 2. test API
W
Wilber 已提交
118
  pten::CPUContext dev_ctx;
119 120 121 122
  dev_ctx.SetDeviceAllocator(
      paddle::memory::allocation::AllocatorFacade::Instance()
          .GetAllocator(paddle::platform::CPUPlace())
          .get());
123
  auto dense_out = pten::Subtract<float>(dev_ctx, dense_x, dense_y);
124 125 126 127

  // 3. check result
  ASSERT_EQ(dense_out.dims().size(), 2);
  ASSERT_EQ(dense_out.dims()[0], 3);
128
  ASSERT_EQ(dense_out.dtype(), pten::DataType::FLOAT32);
129 130 131 132 133 134 135 136 137 138
  ASSERT_EQ(dense_out.meta().layout, pten::DataLayout::NCHW);

  auto expect_result = sub;
  auto actual_result0 = dense_out.data<float>()[0];
  auto actual_result1 = dense_out.data<float>()[1];
  auto actual_result2 = dense_out.data<float>()[10];
  ASSERT_NEAR(expect_result[0][0], actual_result0, 1e-6f);
  ASSERT_NEAR(expect_result[0][1], actual_result1, 1e-6f);
  ASSERT_NEAR(expect_result[1][0], actual_result2, 1e-6f);
}
139 140 141

TEST(DEV_API, divide) {
  // 1. create tensor
142
  const auto alloc = std::make_unique<paddle::experimental::DefaultAllocator>(
143
      paddle::platform::CPUPlace());
144 145 146 147 148
  pten::DenseTensor dense_x(
      alloc.get(),
      pten::DenseTensorMeta(pten::DataType::FLOAT32,
                            pten::framework::make_ddim({3, 10}),
                            pten::DataLayout::NCHW));
149 150
  auto* dense_x_data =
      dense_x.mutable_data<float>(paddle::platform::CPUPlace());
151

152 153 154 155 156
  pten::DenseTensor dense_y(
      alloc.get(),
      pten::DenseTensorMeta(pten::DataType::FLOAT32,
                            pten::framework::make_ddim({10}),
                            pten::DataLayout::NCHW));
157 158
  auto* dense_y_data =
      dense_y.mutable_data<float>(paddle::platform::CPUPlace());
159 160 161 162 163 164 165 166 167 168 169 170 171

  float div[3][10] = {0.0};
  for (size_t i = 0; i < 3; ++i) {
    for (size_t j = 0; j < 10; ++j) {
      dense_x_data[i * 10 + j] = (i * 10 + j) * 1.0;
      div[i][j] = (i * 10 + j) * 1.0 / (j * 2.0 + 1);
    }
  }
  for (size_t i = 0; i < 10; ++i) {
    dense_y_data[i] = i * 2.0 + 1;
  }

  // 2. test API
W
Wilber 已提交
172
  pten::CPUContext dev_ctx;
173 174 175 176
  dev_ctx.SetDeviceAllocator(
      paddle::memory::allocation::AllocatorFacade::Instance()
          .GetAllocator(paddle::platform::CPUPlace())
          .get());
177
  auto dense_out = pten::Divide<float>(dev_ctx, dense_x, dense_y);
178 179 180 181

  // 3. check result
  ASSERT_EQ(dense_out.dims().size(), 2);
  ASSERT_EQ(dense_out.dims()[0], 3);
Y
YuanRisheng 已提交
182 183
  ASSERT_EQ(dense_out.dtype(), pten::DataType::FLOAT32);
  ASSERT_EQ(dense_out.layout(), pten::DataLayout::NCHW);
184 185 186 187 188 189 190 191 192

  auto expect_result = div;
  auto actual_result0 = dense_out.data<float>()[0];
  auto actual_result1 = dense_out.data<float>()[1];
  auto actual_result2 = dense_out.data<float>()[10];
  ASSERT_NEAR(expect_result[0][0], actual_result0, 1e-6f);
  ASSERT_NEAR(expect_result[0][1], actual_result1, 1e-6f);
  ASSERT_NEAR(expect_result[1][0], actual_result2, 1e-6f);
}
193

Y
YuanRisheng 已提交
194 195
TEST(DEV_API, multiply) {
  // 1. create tensor
196
  const auto alloc = std::make_unique<paddle::experimental::DefaultAllocator>(
Y
YuanRisheng 已提交
197
      paddle::platform::CPUPlace());
198 199 200 201 202
  pten::DenseTensor dense_x(
      alloc.get(),
      pten::DenseTensorMeta(pten::DataType::FLOAT32,
                            pten::framework::make_ddim({3, 10}),
                            pten::DataLayout::NCHW));
203 204
  auto* dense_x_data =
      dense_x.mutable_data<float>(paddle::platform::CPUPlace());
Y
YuanRisheng 已提交
205

206 207 208 209 210
  pten::DenseTensor dense_y(
      alloc.get(),
      pten::DenseTensorMeta(pten::DataType::FLOAT32,
                            pten::framework::make_ddim({10}),
                            pten::DataLayout::NCHW));
211 212
  auto* dense_y_data =
      dense_y.mutable_data<float>(paddle::platform::CPUPlace());
Y
YuanRisheng 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225

  float mul[3][10] = {0.0};
  for (size_t i = 0; i < 3; ++i) {
    for (size_t j = 0; j < 10; ++j) {
      dense_x_data[i * 10 + j] = (i * 10 + j) * 1.0;
      mul[i][j] = (i * 10 + j) * 1.0 * j * 2.0;
    }
  }
  for (size_t i = 0; i < 10; ++i) {
    dense_y_data[i] = i * 2.0;
  }

  // 2. test API
W
Wilber 已提交
226
  pten::CPUContext dev_ctx;
227 228 229 230
  dev_ctx.SetDeviceAllocator(
      paddle::memory::allocation::AllocatorFacade::Instance()
          .GetAllocator(paddle::platform::CPUPlace())
          .get());
231
  auto dense_out = pten::Multiply<float>(dev_ctx, dense_x, dense_y);
Y
YuanRisheng 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

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

  auto expect_result = mul;
  auto actual_result0 = dense_out.data<float>()[0];
  auto actual_result1 = dense_out.data<float>()[1];
  auto actual_result2 = dense_out.data<float>()[10];
  ASSERT_NEAR(expect_result[0][0], actual_result0, 1e-6f);
  ASSERT_NEAR(expect_result[0][1], actual_result1, 1e-6f);
  ASSERT_NEAR(expect_result[1][0], actual_result2, 1e-6f);
}
247 248
}  // namespace tests
}  // namespace pten