sparse_api_custom_impl.cc 6.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

15
#include "paddle/phi/api/lib/sparse_api_custom_impl.h"
16 17 18

#include <memory>
#include "glog/logging.h"
19 20
#include "paddle/phi/api/lib/kernel_dispatch.h"
#include "paddle/phi/core/kernel_registry.h"
21 22 23 24 25

namespace paddle {
namespace experimental {
namespace sparse {

26
Tensor to_sparse_coo_impl(const Tensor& x, const int64_t sparse_dim) {
27
  if (x.layout() == phi::DataLayout::SPARSE_COO) {
28 29
    return x;
  }
30

31 32
  // 1. Get kernel signature and kernel
  std::string kernel_name = "dense_to_sparse_coo";
33
  if (x.layout() == phi::DataLayout::SPARSE_CSR) {
34 35 36
    kernel_name = "sparse_csr_to_coo";
  }

37 38 39
  auto kernel_key_set = ParseKernelKeyByInputArgs(x);
  auto kernel_key = kernel_key_set.GetHighestPriorityKernelKey();

40
  auto kernel = phi::KernelFactory::Instance().SelectKernelOrThrowError(
41 42
      kernel_name, kernel_key);

43
  VLOG(6) << "add API kernel key: " << kernel_key;
44 45 46 47
  VLOG(6) << "to API kernel: " << kernel;

  // 2. Get Device Context
  auto* dev_ctx = GetDeviceContextByBackend(kernel_key.backend());
48
  auto kernel_context = phi::KernelContext(dev_ctx);
49 50

  // 3. Auto data transform
51 52
  if (x.layout() == phi::DataLayout::SPARSE_CSR) {
    auto input = std::dynamic_pointer_cast<phi::SparseCsrTensor>(x.impl());
53 54
    kernel_context.EmplaceBackInput(input.get());
  } else {
55
    auto input = std::dynamic_pointer_cast<phi::DenseTensor>(x.impl());
56 57 58 59 60
    kernel_context.EmplaceBackInput(input.get());
    kernel_context.EmplaceBackAttr(sparse_dim);
  }

  // 4. InferMeta
61
  auto indices_meta =
62 63
      phi::DenseTensorMeta(phi::DataType::INT64, {1}, phi::DataLayout::NCHW);
  auto elements_meta = phi::DenseTensorMeta(x.dtype(), {1}, x.layout());
64 65 66

  // 5. Prepare outputs
  // create empty SparseCooTensor
Z
zyfncg 已提交
67 68 69 70
  phi::DenseTensor non_zero_indices(std::make_shared<phi::Allocation>(),
                                    std::move(indices_meta));
  phi::DenseTensor non_zero_elements(std::make_shared<phi::Allocation>(),
                                     std::move(elements_meta));
71
  auto coo = std::make_shared<phi::SparseCooTensor>(
72 73 74 75 76 77 78 79 80 81 82 83
      non_zero_indices, non_zero_elements, x.dims());

  kernel_context.EmplaceBackOutput(coo.get());
  Tensor out;
  out.set_impl(coo);

  // 6. Call kernel
  kernel(&kernel_context);

  return out;
}

84
Tensor to_sparse_csr_impl(const Tensor& x) {
85
  if (x.layout() == phi::DataLayout::SPARSE_CSR) {
86 87 88 89
    return x;
  }
  // 1. Get kernel signature and kernel
  std::string kernel_name = "dense_to_sparse_csr";
90
  if (x.layout() == phi::DataLayout::SPARSE_COO) {
91 92 93
    kernel_name = "sparse_coo_to_csr";
  }

94 95 96
  auto kernel_key_set = ParseKernelKeyByInputArgs(x);
  auto kernel_key = kernel_key_set.GetHighestPriorityKernelKey();

97
  auto kernel = phi::KernelFactory::Instance().SelectKernelOrThrowError(
98 99
      kernel_name, kernel_key);

100
  VLOG(6) << "add API kernel key: " << kernel_key;
101 102 103 104
  VLOG(6) << "to API kernel: " << kernel;

  // 2. Get Device Context
  auto* dev_ctx = GetDeviceContextByBackend(kernel_key.backend());
105
  auto kernel_context = phi::KernelContext(dev_ctx);
106 107

  // 3. Auto data transform
108 109
  if (x.layout() == phi::DataLayout::SPARSE_COO) {
    auto input = std::dynamic_pointer_cast<phi::SparseCooTensor>(x.impl());
110 111
    kernel_context.EmplaceBackInput(input.get());
  } else {
112
    auto input = std::dynamic_pointer_cast<phi::DenseTensor>(x.impl());
113 114 115 116
    kernel_context.EmplaceBackInput(input.get());
  }

  // 4. InferMeta
117
  auto crows_meta =
118
      phi::DenseTensorMeta(phi::DataType::INT64, {1}, phi::DataLayout::NCHW);
119
  auto cols_meta =
120 121
      phi::DenseTensorMeta(phi::DataType::INT64, {1}, phi::DataLayout::NCHW);
  auto elements_meta = phi::DenseTensorMeta(x.dtype(), {1}, x.layout());
122 123 124

  // 5. Prepare outputs
  // create empty SparseCooTensor
Z
zyfncg 已提交
125 126 127 128 129 130
  phi::DenseTensor non_zero_crows(std::make_shared<phi::Allocation>(),
                                  std::move(crows_meta));
  phi::DenseTensor non_zero_cols(std::make_shared<phi::Allocation>(),
                                 std::move(cols_meta));
  phi::DenseTensor non_zero_elements(std::make_shared<phi::Allocation>(),
                                     std::move(elements_meta));
131
  auto csr = std::make_shared<phi::SparseCsrTensor>(
132 133 134 135 136 137 138 139 140 141 142
      non_zero_crows, non_zero_cols, non_zero_elements, x.dims());

  kernel_context.EmplaceBackOutput(csr.get());
  Tensor out;
  out.set_impl(csr);

  // 6. Call kernel
  kernel(&kernel_context);

  return out;
}
Z
zhangkaihuo 已提交
143

144
Tensor to_dense_impl(const Tensor& x) {
145 146
  if (x.layout() != phi::DataLayout::SPARSE_CSR &&
      x.layout() != phi::DataLayout::SPARSE_COO) {
Z
zhangkaihuo 已提交
147 148
    return x;
  }
149

Z
zhangkaihuo 已提交
150 151
  // 1. Get kernel signature and kernel
  std::string kernel_name = "sparse_coo_to_dense";
152
  if (x.layout() == phi::DataLayout::SPARSE_CSR) {
Z
zhangkaihuo 已提交
153 154 155
    kernel_name = "sparse_csr_to_dense";
  }

156 157 158
  auto kernel_key_set = ParseKernelKeyByInputArgs(x);
  auto kernel_key = kernel_key_set.GetHighestPriorityKernelKey();

159
  auto kernel = phi::KernelFactory::Instance().SelectKernelOrThrowError(
Z
zhangkaihuo 已提交
160 161
      kernel_name, kernel_key);

162
  VLOG(6) << "add API kernel key: " << kernel_key;
Z
zhangkaihuo 已提交
163 164 165 166
  VLOG(6) << "to API kernel: " << kernel;

  // 2. Get Device Context
  auto* dev_ctx = GetDeviceContextByBackend(kernel_key.backend());
167
  auto kernel_context = phi::KernelContext(dev_ctx);
Z
zhangkaihuo 已提交
168 169

  // 3. Auto data transform
170 171
  if (x.layout() == phi::DataLayout::SPARSE_COO) {
    auto input = std::dynamic_pointer_cast<phi::SparseCooTensor>(x.impl());
Z
zhangkaihuo 已提交
172 173
    kernel_context.EmplaceBackInput(input.get());
  } else {
174
    auto input = std::dynamic_pointer_cast<phi::SparseCsrTensor>(x.impl());
Z
zhangkaihuo 已提交
175 176 177 178
    kernel_context.EmplaceBackInput(input.get());
  }

  // 4. InferMeta
179
  auto dense_meta = phi::DenseTensorMeta(x.dtype(), x.dims(), x.layout());
Z
zhangkaihuo 已提交
180 181 182

  // 5. Prepare outputs
  // create empty SparseCooTensor
183
  auto dense_out = std::make_shared<phi::DenseTensor>(
Z
zyfncg 已提交
184
      std::make_shared<phi::Allocation>(), std::move(dense_meta));
Z
zhangkaihuo 已提交
185 186 187 188 189 190 191 192 193 194 195

  kernel_context.EmplaceBackOutput(dense_out.get());
  Tensor out;
  out.set_impl(dense_out);

  // 6. Call kernel
  kernel(&kernel_context);

  return out;
}

196 197 198
}  // namespace sparse
}  // namespace experimental
}  // namespace paddle