test_data_transform.cc 3.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* Copyright (c) 2022 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
#include "paddle/phi/api/include/api.h"
#include "paddle/phi/common/complex.h"
20
#include "paddle/phi/common/place.h"
21 22
#include "paddle/phi/core/compat/convert_utils.h"
#include "paddle/phi/core/dense_tensor.h"
23 24 25 26 27 28 29 30 31 32
#include "paddle/phi/core/kernel_registry.h"

PD_DECLARE_KERNEL(full, CPU, ALL_LAYOUT);
PD_DECLARE_KERNEL(matmul, CPU, ALL_LAYOUT);

#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
PD_DECLARE_KERNEL(full, GPU, ALL_LAYOUT);
PD_DECLARE_KERNEL(matmul, GPU, ALL_LAYOUT);
PD_DECLARE_KERNEL(copy, GPU, ALL_LAYOUT);
#endif
33 34 35 36 37 38 39 40 41 42

namespace paddle {
namespace tests {

// TODO(chenweihang): Remove this test after the API is used in the dygraph
TEST(API, data_transform_same_place) {
  // 1. create tensor
  auto x = paddle::experimental::full({3, 3},
                                      1.0,
                                      experimental::DataType::COMPLEX128,
43
                                      experimental::CPUPlace());
44 45

  auto y = paddle::experimental::full(
46
      {3, 3}, 2.0, experimental::DataType::FLOAT32, experimental::CPUPlace());
47

48
  std::vector<phi::dtype::complex<double>> sum(9, 6.0);
49 50 51 52 53 54 55 56 57

  // 2. test API
  auto out = paddle::experimental::matmul(x, y, false, false);

  // 3. check result
  ASSERT_EQ(out.dims().size(), 2);
  ASSERT_EQ(out.dims()[0], 3);
  ASSERT_EQ(out.dims()[1], 3);
  ASSERT_EQ(out.numel(), 9);
58 59
  ASSERT_EQ(out.type(), phi::DataType::COMPLEX128);
  ASSERT_EQ(out.layout(), phi::DataLayout::NCHW);
60 61
  ASSERT_EQ(out.initialized(), true);

62
  auto dense_out = std::dynamic_pointer_cast<phi::DenseTensor>(out.impl());
63 64 65

  for (size_t i = 0; i < 9; i++) {
    ASSERT_NEAR(sum[i].real,
66
                dense_out->data<phi::dtype::complex<double>>()[i].real,
67 68
                1e-6f);
    ASSERT_NEAR(sum[i].imag,
69
                dense_out->data<phi::dtype::complex<double>>()[i].imag,
70 71 72 73 74 75 76 77
                1e-6f);
  }
}

#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
TEST(Tensor, data_transform_diff_place) {
  // 1. create tensor
  auto x = paddle::experimental::full(
78
      {3, 3}, 1.0, experimental::DataType::FLOAT64, experimental::CPUPlace());
79 80

  auto y = paddle::experimental::full(
81
      {3, 3}, 2.0, experimental::DataType::FLOAT64, experimental::GPUPlace());
82 83 84 85 86 87 88 89 90 91 92

  std::vector<float> sum(9, 6.0);

  // 2. test API
  auto out = paddle::experimental::matmul(x, y, false, false);

  // 3. check result
  ASSERT_EQ(out.dims().size(), 2);
  ASSERT_EQ(out.dims()[0], 3);
  ASSERT_EQ(out.dims()[1], 3);
  ASSERT_EQ(out.numel(), 9);
93 94
  ASSERT_EQ(out.dtype(), phi::DataType::FLOAT64);
  ASSERT_EQ(out.layout(), phi::DataLayout::NCHW);
95 96
  ASSERT_EQ(out.initialized(), true);
  ASSERT_EQ(out.impl()->place(),
97
            phi::TransToPhiPlace(experimental::Backend::GPU));
98

99
  auto ref_out = experimental::copy_to(out, experimental::CPUPlace(), true);
100

101
  auto dense_out = std::dynamic_pointer_cast<phi::DenseTensor>(ref_out.impl());
102 103 104 105 106 107 108 109 110
  for (size_t i = 0; i < 9; i++) {
    ASSERT_NEAR(sum[i], dense_out->data<double>()[i], 1e-6f);
  }
}

#endif

}  // namespace tests
}  // namespace paddle