unary_kernel.h 7.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 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.

#pragma once

17 18
#include "paddle/phi/common/int_array.h"
#include "paddle/phi/core/ddim.h"
19 20 21
#include "paddle/phi/core/sparse_coo_tensor.h"
#include "paddle/phi/core/sparse_csr_tensor.h"

22 23 24 25
namespace phi {
namespace sparse {

#define DECLARE_SPARSE_UNARY_KERNEL(prefix)                                    \
26
  template <typename T, typename Context>                                      \
27
  void prefix##CooKernel(                                                      \
28 29 30
      const Context& dev_ctx, const SparseCooTensor& x, SparseCooTensor* out); \
                                                                               \
  template <typename T, typename Context>                                      \
31
  void prefix##CsrKernel(                                                      \
32 33
      const Context& dev_ctx, const SparseCsrTensor& x, SparseCsrTensor* out);

34 35 36 37 38 39 40 41 42 43 44 45
#define DECLARE_SPARSE_UNARY_KERNEL_WITH_ONE_ATTR(prefix, attr) \
  template <typename T, typename Context>                       \
  void prefix##CooKernel(const Context& dev_ctx,                \
                         const SparseCooTensor& x,              \
                         float attr,                            \
                         SparseCooTensor* out);                 \
                                                                \
  template <typename T, typename Context>                       \
  void prefix##CsrKernel(const Context& dev_ctx,                \
                         const SparseCsrTensor& x,              \
                         float attr,                            \
                         SparseCsrTensor* out);
46

47 48 49 50 51 52 53
DECLARE_SPARSE_UNARY_KERNEL(Sin)
DECLARE_SPARSE_UNARY_KERNEL(Tan)
DECLARE_SPARSE_UNARY_KERNEL(Asin)
DECLARE_SPARSE_UNARY_KERNEL(Atan)
DECLARE_SPARSE_UNARY_KERNEL(Sinh)
DECLARE_SPARSE_UNARY_KERNEL(Asinh)
DECLARE_SPARSE_UNARY_KERNEL(Atanh)
54
DECLARE_SPARSE_UNARY_KERNEL(Relu)
55 56
DECLARE_SPARSE_UNARY_KERNEL(Tanh)
DECLARE_SPARSE_UNARY_KERNEL(Square)
57
DECLARE_SPARSE_UNARY_KERNEL(Sqrt)
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
DECLARE_SPARSE_UNARY_KERNEL(Log1p)
DECLARE_SPARSE_UNARY_KERNEL(Abs)
DECLARE_SPARSE_UNARY_KERNEL_WITH_ONE_ATTR(Pow, factor)

template <typename T, typename Context>
void ScaleCooKernel(const Context& dev_ctx,
                    const SparseCooTensor& x,
                    float scale,
                    float bias,
                    bool bias_after_scale,
                    SparseCooTensor* out);

template <typename T, typename Context>
void ScaleCsrKernel(const Context& dev_ctx,
                    const SparseCsrTensor& x,
                    float scale,
                    float bias,
                    bool bias_after_scale,
                    SparseCsrTensor* out);
77 78

template <typename T, typename Context>
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
void DivCooScalarKernel(const Context& dev_ctx,
                        const SparseCooTensor& x,
                        float scalar,
                        SparseCooTensor* out);

template <typename T, typename Context>
void DivCsrScalarKernel(const Context& dev_ctx,
                        const SparseCsrTensor& x,
                        float scalar,
                        SparseCsrTensor* out);

template <typename T, typename Context>
void CastCooKernel(const Context& dev_ctx,
                   const SparseCooTensor& x,
                   DataType index_dtype,
                   DataType value_dtype,
                   SparseCooTensor* out);

template <typename T, typename Context>
void CastCsrKernel(const Context& dev_ctx,
                   const SparseCsrTensor& x,
                   DataType index_dtype,
                   DataType value_dtype,
                   SparseCsrTensor* out);

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
template <typename T, typename Context>
void TransposeCooKernel(const Context& dev_ctx,
                        const SparseCooTensor& x,
                        const std::vector<int>& perm,
                        SparseCooTensor* out);

template <typename T, typename Context>
void TransposeCsrKernel(const Context& dev_ctx,
                        const SparseCsrTensor& x,
                        const std::vector<int>& perm,
                        SparseCsrTensor* out);

template <typename T, typename Context>
SparseCooTensor TransposeCoo(const Context& dev_ctx,
                             const SparseCooTensor& x,
                             const std::vector<int>& perm) {
  PADDLE_ENFORCE_EQ(x.sparse_dim(),
                    perm.size(),
                    phi::errors::InvalidArgument(
                        "size of perm must be equal than the x.sparse_dim()"));
  SparseCooTensor coo;
  TransposeCooKernel<T, Context>(dev_ctx, x, perm, &coo);
  return coo;
}

template <typename T, typename Context>
SparseCsrTensor TransposeCsr(const Context& dev_ctx,
                             const SparseCsrTensor& x,
                             const std::vector<int>& perm) {
  PADDLE_ENFORCE_LE(
      2,
      perm.size(),
      phi::errors::InvalidArgument("size of perm must be equal to 2 or 3"));
  PADDLE_ENFORCE_GE(
      3,
      perm.size(),
      phi::errors::InvalidArgument("size of perm must be equal to 2 or 3"));
  SparseCsrTensor csr;
  TransposeCsrKernel<T, Context>(dev_ctx, x, perm, &csr);
  return csr;
}

146 147 148 149
template <typename T, typename Context>
SparseCooTensor ReluCoo(const Context& dev_ctx, const SparseCooTensor& x) {
  SparseCooTensor coo;
  ReluCooKernel<T, Context>(dev_ctx, x, &coo);
150 151 152
  return coo;
}

153 154 155 156 157 158 159
template <typename T, typename Context>
SparseCooTensor ReluCsr(const Context& dev_ctx, const SparseCooTensor& x) {
  SparseCooTensor csr;
  ReluCsrKernel<T, Context>(dev_ctx, x, &csr);
  return csr;
}

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
template <typename T, typename Context>
void ReshapeCooKernel(const Context& dev_ctx,
                      const SparseCooTensor& x,
                      const phi::IntArray& shape,
                      SparseCooTensor* out);

template <typename T, typename Context>
void ReshapeCsrKernel(const Context& dev_ctx,
                      const SparseCsrTensor& x,
                      const phi::IntArray& shape,
                      SparseCsrTensor* out);

template <typename T, typename Context>
SparseCooTensor ReshapeCoo(const Context& dev_ctx,
                           const SparseCooTensor& x,
                           const phi::IntArray& shape) {
  SparseCooTensor coo;
  ReshapeCooKernel<T, Context>(dev_ctx, x, shape, &coo);
  return coo;
}

template <typename T, typename Context>
SparseCsrTensor ReshapeCsr(const Context& dev_ctx,
                           const SparseCsrTensor& x,
                           const phi::IntArray& shape) {
  PADDLE_ENFORCE_LE(
      2,
      shape.size(),
      phi::errors::InvalidArgument("size of shape must be equal to 2 or 3"));
  PADDLE_ENFORCE_GE(
      3,
      shape.size(),
      phi::errors::InvalidArgument("size of shape must be equal to 2 or 3"));
  SparseCsrTensor csr;
  ReshapeCsrKernel<T, Context>(dev_ctx, x, shape, &csr);
  return csr;
}

198 199
}  // namespace sparse
}  // namespace phi