elementwise_kernel.cu 3.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
/* 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 <thrust/equal.h>
#include <thrust/execution_policy.h>

#include "paddle/phi/kernels/elementwise_add_kernel.h"
#include "paddle/phi/kernels/sparse/elementwise_kernel.h"
#include "paddle/phi/kernels/sparse/empty_kernel.h"

#include "paddle/phi/core/enforce.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/core/visit_type.h"

namespace phi {
namespace sparse {

template <typename T, typename IntT>
void ElementWiseAddCooGPUKernel(const GPUContext& dev_ctx,
                                const SparseCooTensor& x,
                                const SparseCooTensor& y,
                                SparseCooTensor* out) {
  const auto& x_indices = x.indices();
  const auto& y_indices = y.indices();
  PADDLE_ENFORCE_EQ(
      x_indices.numel(),
      y_indices.numel(),
      phi::errors::PreconditionNotMet(
          "The numel of x.indices() and y.indices() should be equal"));
  const IntT* x_indices_ptr = x_indices.data<IntT>();
  const IntT* y_indices_ptr = y_indices.data<IntT>();
#ifdef PADDLE_WITH_HIP
  bool is_same = thrust::equal(thrust::hip::par.on(dev_ctx.stream()),
#else
  bool is_same = thrust::equal(thrust::cuda::par.on(dev_ctx.stream()),
#endif
                               x_indices_ptr,
                               x_indices_ptr + x_indices.numel(),
                               y_indices_ptr);
  PADDLE_ENFORCE_EQ(
      is_same,
      true,
      phi::errors::PreconditionNotMet(
          "Currently, ElementWiseAddCooKernel only supports the case "
          "where x and y have the same indices"));
  EmptyLikeCooKernel<T, GPUContext>(dev_ctx, x, out);
  phi::AddKernel<T, GPUContext>(
      dev_ctx, x.values(), y.values(), out->mutable_values());
}

template <typename T, typename Context>
void ElementWiseAddCooKernel(const Context& dev_ctx,
                             const SparseCooTensor& x,
                             const SparseCooTensor& y,
                             SparseCooTensor* out) {
  PD_VISIT_BASE_INTEGRAL_TYPES(x.indices().dtype(), "VerifyIndices", ([&] {
                                 ElementWiseAddCooGPUKernel<T, data_t>(
                                     dev_ctx, x, y, out);
                               }));
}

}  // namespace sparse
}  // namespace phi

PD_REGISTER_KERNEL(add_coo_coo,
                   GPU,
                   ALL_LAYOUT,
                   phi::sparse::ElementWiseAddCooKernel,
                   float,
                   double,
                   int16_t,
                   int,
                   int64_t,
                   phi::dtype::float16) {
  kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO);
  kernel->InputAt(1).SetDataLayout(phi::DataLayout::SPARSE_COO);
}