test_sparse_utils_api.cc 9.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/* 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,
11 12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See
the License for the specific language governing permissions and
13 14 15 16 17
limitations under the License. */

#include <gtest/gtest.h>
#include <memory>

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

20
#include "paddle/phi/api/include/sparse_api.h"
21

22 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"
#include "paddle/phi/core/sparse_coo_tensor.h"
26

27 28
PD_DECLARE_KERNEL(dense_to_sparse_coo, CPU, ALL_LAYOUT);

29 30 31 32
TEST(API, to_sparse_coo) {
  const auto alloc = std::make_shared<paddle::experimental::DefaultAllocator>(
      paddle::platform::CPUPlace());

33
  auto dense_x = std::make_shared<phi::DenseTensor>(
34
      alloc.get(),
35 36 37
      phi::DenseTensorMeta(phi::DataType::FLOAT32,
                           phi::make_ddim({3, 3}),
                           phi::DataLayout::NCHW));
38

39
  phi::CPUPlace cpu;
40 41 42 43 44 45 46 47 48 49 50
  const int64_t sparse_dim = 2;
  auto* dense_x_data = dense_x->mutable_data<float>(cpu);
  float dense_data[3][3] = {{0.0, 1.0, 0.0}, {2.0, 0.0, 3.0}, {3.2, 0.0, 0.0}};
  std::vector<float> non_zero_data = {1.0, 2.0, 3.0, 3.2};
  std::vector<int64_t> indices_data = {0, 1, 1, 2, 1, 0, 2, 0};
  std::vector<int64_t> cols_data = {1, 0, 2, 0};
  std::vector<int64_t> crows_data = {0, 1, 3, 4};
  const int64_t non_zero_num = 4;

  std::copy(&dense_data[0][0], &dense_data[0][0] + 9, dense_x_data);

51
  phi::CPUContext dev_ctx_cpu;
W
Wilber 已提交
52
  dev_ctx_cpu.Init();
53 54 55

  // 1. test dense_to_sparse_coo
  paddle::experimental::Tensor x(dense_x);
56
  auto out = paddle::experimental::sparse::to_sparse_coo(x, sparse_dim);
57
  auto coo = std::dynamic_pointer_cast<phi::SparseCooTensor>(out.impl());
58 59 60 61 62 63 64 65 66
  ASSERT_EQ(coo->nnz(), non_zero_num);
  int cmp_indices = memcmp(coo->non_zero_indices().data<int64_t>(),
                           indices_data.data(),
                           indices_data.size() * sizeof(int64_t));
  ASSERT_EQ(cmp_indices, 0);
  int cmp_elements = memcmp(coo->non_zero_elements().data<float>(),
                            non_zero_data.data(),
                            non_zero_data.size() * sizeof(float));
  ASSERT_EQ(cmp_elements, 0);
67 68

  // 1. test sparse_csr_to_coo
69 70 71 72 73 74 75 76 77 78 79 80
  auto dense_dims = phi::make_ddim({3, 3});
  phi::DenseTensorMeta crows_meta(
      phi::DataType::INT64, {dense_dims[0] + 1}, phi::DataLayout::NCHW);
  phi::DenseTensorMeta cols_meta(
      phi::DataType::INT64, {non_zero_num}, phi::DataLayout::NCHW);
  phi::DenseTensorMeta values_meta(
      phi::DataType::FLOAT32, {non_zero_num}, phi::DataLayout::NCHW);

  phi::CPUPlace place;
  phi::DenseTensor crows(alloc.get(), crows_meta);
  phi::DenseTensor cols(alloc.get(), cols_meta);
  phi::DenseTensor values(alloc.get(), values_meta);
81 82 83 84 85 86 87 88 89 90
  memcpy(crows.mutable_data<int64_t>(place),
         crows_data.data(),
         crows_data.size() * sizeof(int64_t));
  memcpy(cols.mutable_data<int64_t>(place),
         cols_data.data(),
         cols_data.size() * sizeof(int64_t));
  memcpy(values.mutable_data<float>(place),
         non_zero_data.data(),
         non_zero_data.size() * sizeof(float));
  auto csr =
91
      std::make_shared<phi::SparseCsrTensor>(crows, cols, values, dense_dims);
92
  paddle::experimental::Tensor csr_x(csr);
93
  auto out2 = paddle::experimental::sparse::to_sparse_coo(csr_x, sparse_dim);
94

95
  auto coo2 = std::dynamic_pointer_cast<phi::SparseCooTensor>(out.impl());
96 97 98 99 100 101 102 103 104
  ASSERT_EQ(coo2->nnz(), non_zero_num);
  int cmp_indices2 = memcmp(coo2->non_zero_indices().data<int64_t>(),
                            indices_data.data(),
                            indices_data.size() * sizeof(int64_t));
  ASSERT_EQ(cmp_indices2, 0);
  int cmp_elements2 = memcmp(coo2->non_zero_elements().data<float>(),
                             non_zero_data.data(),
                             non_zero_data.size() * sizeof(float));
  ASSERT_EQ(cmp_elements2, 0);
105
}
106 107 108 109 110

TEST(API, to_sparse_csr) {
  const auto alloc = std::make_shared<paddle::experimental::DefaultAllocator>(
      paddle::platform::CPUPlace());

111
  auto dense_x = std::make_shared<phi::DenseTensor>(
112
      alloc.get(),
113 114 115
      phi::DenseTensorMeta(phi::DataType::FLOAT32,
                           phi::make_ddim({3, 3}),
                           phi::DataLayout::NCHW));
116

117
  phi::CPUPlace cpu;
118 119 120 121 122 123 124 125 126 127 128
  const int64_t sparse_dim = 2;
  auto* dense_x_data = dense_x->mutable_data<float>(cpu);
  float dense_data[3][3] = {{0.0, 1.0, 0.0}, {2.0, 0.0, 3.0}, {3.2, 0.0, 0.0}};
  std::vector<float> non_zero_data = {1.0, 2.0, 3.0, 3.2};
  std::vector<int64_t> indices_data = {0, 1, 1, 2, 1, 0, 2, 0};
  std::vector<int64_t> cols_data = {1, 0, 2, 0};
  std::vector<int64_t> crows_data = {0, 1, 3, 4};
  const int64_t non_zero_num = 4;

  std::copy(&dense_data[0][0], &dense_data[0][0] + 9, dense_x_data);

129
  phi::CPUContext dev_ctx_cpu;
130 131 132

  // 1. test dense_to_sparse_csr
  paddle::experimental::Tensor x(dense_x);
133
  auto out = paddle::experimental::sparse::to_sparse_csr(x);
134 135
  auto csr = std::dynamic_pointer_cast<phi::SparseCsrTensor>(out.impl());
  auto check = [&](const phi::SparseCsrTensor& csr) {
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    ASSERT_EQ(csr.non_zero_cols().numel(), non_zero_num);
    int cmp_crows = memcmp(csr.non_zero_crows().data<int64_t>(),
                           crows_data.data(),
                           crows_data.size() * sizeof(int64_t));
    ASSERT_EQ(cmp_crows, 0);
    int cmp_cols = memcmp(csr.non_zero_cols().data<int64_t>(),
                          cols_data.data(),
                          cols_data.size() * sizeof(int64_t));
    ASSERT_EQ(cmp_cols, 0);
    int cmp_elements = memcmp(csr.non_zero_elements().data<float>(),
                              non_zero_data.data(),
                              non_zero_data.size() * sizeof(float));
    ASSERT_EQ(cmp_elements, 0);
  };
  check(*csr);

  // 1. test sparse_coo_to_csr
153 154 155 156 157 158 159 160 161
  auto dense_dims = phi::make_ddim({3, 3});
  phi::DenseTensorMeta indices_meta(
      phi::DataType::INT64, {sparse_dim, non_zero_num}, phi::DataLayout::NCHW);
  phi::DenseTensorMeta values_meta(
      phi::DataType::FLOAT32, {non_zero_num}, phi::DataLayout::NCHW);

  phi::CPUPlace place;
  phi::DenseTensor indices(alloc.get(), indices_meta);
  phi::DenseTensor values(alloc.get(), values_meta);
162 163 164 165 166 167 168
  memcpy(indices.mutable_data<int64_t>(place),
         indices_data.data(),
         indices_data.size() * sizeof(int64_t));
  memcpy(values.mutable_data<float>(place),
         non_zero_data.data(),
         non_zero_data.size() * sizeof(float));
  auto coo =
169
      std::make_shared<phi::SparseCooTensor>(indices, values, dense_dims);
170
  paddle::experimental::Tensor coo_x(coo);
171
  auto out2 = paddle::experimental::sparse::to_sparse_csr(coo_x);
172

173
  auto csr2 = std::dynamic_pointer_cast<phi::SparseCsrTensor>(out.impl());
174 175
  check(*csr2);
}
Z
zhangkaihuo 已提交
176 177 178 179 180

TEST(API, to_dense) {
  const auto alloc = std::make_shared<paddle::experimental::DefaultAllocator>(
      paddle::platform::CPUPlace());

181
  phi::CPUPlace cpu;
Z
zhangkaihuo 已提交
182 183 184 185 186 187 188
  const int64_t sparse_dim = 2;
  float dense_data[3][3] = {{0.0, 1.0, 0.0}, {2.0, 0.0, 3.0}, {3.2, 0.0, 0.0}};
  std::vector<float> non_zero_data = {1.0, 2.0, 3.0, 3.2};
  std::vector<int64_t> indices_data = {0, 1, 1, 2, 1, 0, 2, 0};
  std::vector<int64_t> cols_data = {1, 0, 2, 0};
  std::vector<int64_t> crows_data = {0, 1, 3, 4};
  const int64_t non_zero_num = 4;
189
  auto dense_dims = phi::make_ddim({3, 3});
Z
zhangkaihuo 已提交
190

191
  phi::CPUContext dev_ctx_cpu;
Z
zhangkaihuo 已提交
192 193

  // 1. test sparse_coo_to_dense
194 195 196 197 198 199 200 201
  phi::DenseTensorMeta indices_meta(
      phi::DataType::INT64, {sparse_dim, non_zero_num}, phi::DataLayout::NCHW);
  phi::DenseTensorMeta values_meta(
      phi::DataType::FLOAT32, {non_zero_num}, phi::DataLayout::NCHW);

  phi::CPUPlace place;
  phi::DenseTensor indices(alloc.get(), indices_meta);
  phi::DenseTensor values(alloc.get(), values_meta);
Z
zhangkaihuo 已提交
202 203 204 205 206 207 208
  memcpy(indices.mutable_data<int64_t>(place),
         indices_data.data(),
         indices_data.size() * sizeof(int64_t));
  memcpy(values.mutable_data<float>(place),
         non_zero_data.data(),
         non_zero_data.size() * sizeof(float));
  auto coo =
209
      std::make_shared<phi::SparseCooTensor>(indices, values, dense_dims);
Z
zhangkaihuo 已提交
210 211

  paddle::experimental::Tensor coo_x(coo);
212
  auto out = paddle::experimental::sparse::to_dense(coo_x);
213
  auto dense_out = std::dynamic_pointer_cast<phi::DenseTensor>(out.impl());
Z
zhangkaihuo 已提交
214 215 216 217 218
  int cmp1 =
      memcmp(dense_out->data<float>(), &dense_data[0][0], 9 * sizeof(float));
  ASSERT_EQ(cmp1, 0);

  // 1. test sparse_csr_to_dense
219 220 221 222 223 224
  phi::DenseTensorMeta crows_meta(
      phi::DataType::INT64, {dense_dims[0] + 1}, phi::DataLayout::NCHW);
  phi::DenseTensorMeta cols_meta(
      phi::DataType::INT64, {non_zero_num}, phi::DataLayout::NCHW);
  phi::DenseTensor crows(alloc.get(), crows_meta);
  phi::DenseTensor cols(alloc.get(), cols_meta);
Z
zhangkaihuo 已提交
225 226 227 228 229 230 231 232 233 234
  memcpy(crows.mutable_data<int64_t>(place),
         crows_data.data(),
         crows_data.size() * sizeof(int64_t));
  memcpy(cols.mutable_data<int64_t>(place),
         cols_data.data(),
         cols_data.size() * sizeof(int64_t));
  memcpy(values.mutable_data<float>(place),
         non_zero_data.data(),
         non_zero_data.size() * sizeof(float));
  auto csr =
235
      std::make_shared<phi::SparseCsrTensor>(crows, cols, values, dense_dims);
Z
zhangkaihuo 已提交
236
  paddle::experimental::Tensor csr_x(csr);
237
  auto out2 = paddle::experimental::sparse::to_dense(csr_x);
Z
zhangkaihuo 已提交
238

239
  auto dense_out2 = std::dynamic_pointer_cast<phi::DenseTensor>(out.impl());
Z
zhangkaihuo 已提交
240 241 242 243
  int cmp2 =
      memcmp(dense_out2->data<float>(), &dense_data[0][0], 9 * sizeof(float));
  ASSERT_EQ(cmp2, 0);
}