sparse_api_custom_impl.cc 6.8 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 21
#include "paddle/phi/api/lib/kernel_dispatch.h"
#include "paddle/phi/api/lib/utils/storage.h"
#include "paddle/phi/core/kernel_registry.h"
22 23 24 25 26

namespace paddle {
namespace experimental {
namespace sparse {

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

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

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

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

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

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

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

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

  // 5. Prepare outputs
  // create empty SparseCooTensor
68 69
  phi::DenseTensor non_zero_indices(
      phi::make_intrusive<paddle::experimental::SharedStorage>(
70
          phi::TransToPhiPlace(kernel_key.backend())),
71
      std::move(indices_meta));
72 73
  phi::DenseTensor non_zero_elements(
      phi::make_intrusive<paddle::experimental::SharedStorage>(
74
          phi::TransToPhiPlace(kernel_key.backend())),
75
      std::move(elements_meta));
76
  auto coo = std::make_shared<phi::SparseCooTensor>(
77 78 79 80 81 82 83 84 85 86 87 88
      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;
}

89
Tensor to_sparse_csr_impl(const Tensor& x) {
90
  if (x.layout() == phi::DataLayout::SPARSE_CSR) {
91 92 93 94
    return x;
  }
  // 1. Get kernel signature and kernel
  std::string kernel_name = "dense_to_sparse_csr";
95
  if (x.layout() == phi::DataLayout::SPARSE_COO) {
96 97 98
    kernel_name = "sparse_coo_to_csr";
  }

99 100 101
  auto kernel_key_set = ParseKernelKeyByInputArgs(x);
  auto kernel_key = kernel_key_set.GetHighestPriorityKernelKey();

102
  auto kernel = phi::KernelFactory::Instance().SelectKernelOrThrowError(
103 104
      kernel_name, kernel_key);

105
  VLOG(6) << "add API kernel key: " << kernel_key;
106 107 108 109
  VLOG(6) << "to API kernel: " << kernel;

  // 2. Get Device Context
  auto* dev_ctx = GetDeviceContextByBackend(kernel_key.backend());
110
  auto kernel_context = phi::KernelContext(dev_ctx);
111 112

  // 3. Auto data transform
113 114
  if (x.layout() == phi::DataLayout::SPARSE_COO) {
    auto input = std::dynamic_pointer_cast<phi::SparseCooTensor>(x.impl());
115 116
    kernel_context.EmplaceBackInput(input.get());
  } else {
117
    auto input = std::dynamic_pointer_cast<phi::DenseTensor>(x.impl());
118 119 120 121
    kernel_context.EmplaceBackInput(input.get());
  }

  // 4. InferMeta
122
  auto crows_meta =
123
      phi::DenseTensorMeta(phi::DataType::INT64, {1}, phi::DataLayout::NCHW);
124
  auto cols_meta =
125 126
      phi::DenseTensorMeta(phi::DataType::INT64, {1}, phi::DataLayout::NCHW);
  auto elements_meta = phi::DenseTensorMeta(x.dtype(), {1}, x.layout());
127 128 129

  // 5. Prepare outputs
  // create empty SparseCooTensor
130 131
  phi::DenseTensor non_zero_crows(
      phi::make_intrusive<paddle::experimental::SharedStorage>(
132
          phi::TransToPhiPlace(kernel_key.backend())),
133
      std::move(crows_meta));
134 135
  phi::DenseTensor non_zero_cols(
      phi::make_intrusive<paddle::experimental::SharedStorage>(
136
          phi::TransToPhiPlace(kernel_key.backend())),
137
      std::move(cols_meta));
138 139
  phi::DenseTensor non_zero_elements(
      phi::make_intrusive<paddle::experimental::SharedStorage>(
140
          phi::TransToPhiPlace(kernel_key.backend())),
141
      std::move(elements_meta));
142
  auto csr = std::make_shared<phi::SparseCsrTensor>(
143 144 145 146 147 148 149 150 151 152 153
      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 已提交
154

155
Tensor to_dense_impl(const Tensor& x) {
156 157
  if (x.layout() != phi::DataLayout::SPARSE_CSR &&
      x.layout() != phi::DataLayout::SPARSE_COO) {
Z
zhangkaihuo 已提交
158 159
    return x;
  }
160

Z
zhangkaihuo 已提交
161 162
  // 1. Get kernel signature and kernel
  std::string kernel_name = "sparse_coo_to_dense";
163
  if (x.layout() == phi::DataLayout::SPARSE_CSR) {
Z
zhangkaihuo 已提交
164 165 166
    kernel_name = "sparse_csr_to_dense";
  }

167 168 169
  auto kernel_key_set = ParseKernelKeyByInputArgs(x);
  auto kernel_key = kernel_key_set.GetHighestPriorityKernelKey();

170
  auto kernel = phi::KernelFactory::Instance().SelectKernelOrThrowError(
Z
zhangkaihuo 已提交
171 172
      kernel_name, kernel_key);

173
  VLOG(6) << "add API kernel key: " << kernel_key;
Z
zhangkaihuo 已提交
174 175 176 177
  VLOG(6) << "to API kernel: " << kernel;

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

  // 3. Auto data transform
181 182
  if (x.layout() == phi::DataLayout::SPARSE_COO) {
    auto input = std::dynamic_pointer_cast<phi::SparseCooTensor>(x.impl());
Z
zhangkaihuo 已提交
183 184
    kernel_context.EmplaceBackInput(input.get());
  } else {
185
    auto input = std::dynamic_pointer_cast<phi::SparseCsrTensor>(x.impl());
Z
zhangkaihuo 已提交
186 187 188 189
    kernel_context.EmplaceBackInput(input.get());
  }

  // 4. InferMeta
190
  auto dense_meta = phi::DenseTensorMeta(x.dtype(), x.dims(), x.layout());
Z
zhangkaihuo 已提交
191 192 193

  // 5. Prepare outputs
  // create empty SparseCooTensor
194 195
  auto dense_out = std::make_shared<phi::DenseTensor>(
      phi::make_intrusive<paddle::experimental::SharedStorage>(
196
          phi::TransToPhiPlace(kernel_key.backend())),
Z
zhangkaihuo 已提交
197 198 199 200 201 202 203 204 205 206 207 208
      std::move(dense_meta));

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

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

  return out;
}

209 210 211
}  // namespace sparse
}  // namespace experimental
}  // namespace paddle