math.cc 9.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2021 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/pten/api/include/math.h"
16 17 18 19 20

#include <memory>

#include "glog/logging.h"

21
#include "paddle/pten/api/lib/api_registry.h"
22 23
#include "paddle/pten/api/lib/kernel_dispatch.h"
#include "paddle/pten/api/lib/utils/allocator.h"
24
#include "paddle/pten/core/kernel_registry.h"
25
#include "paddle/pten/include/core.h"
26
#include "paddle/pten/include/infermeta.h"
C
Chen Weihang 已提交
27
#include "paddle/pten/infermeta/unary.h"
28

29 30 31 32 33 34
PT_DECLARE_MODULE(MathCPU);

#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
PT_DECLARE_MODULE(MathCUDA);
#endif

35 36 37
namespace paddle {
namespace experimental {

38 39 40
PD_DLL_DECL Tensor mean(const Tensor& x,
                        const std::vector<int64_t>& axis,
                        bool keep_dim) {
41 42 43 44
  // 1. Get kernel signature and kernel
  auto kernel_key_set = ParseKernelKeyByInputArgs(x);
  auto kernel_key = kernel_key_set.GetHigestPriorityKernelKey();
  auto kernel = pten::KernelFactory::Instance().SelectKernelOrThrowError(
45
      "reduce_mean", kernel_key);
46 47 48

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

  // 3. Auto data transform
  auto dense_x = std::dynamic_pointer_cast<pten::DenseTensor>(x.impl());
  kernel_context.EmplaceBackInput(dense_x);

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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
  // The real value of reduce_all will be get in kernel
  // so use default value(false) is OK.
  bool reduce_all = false;

  DataType out_dtype = DataType::UNDEFINED;

  kernel_context.EmplaceBackAttr(axis);
  kernel_context.EmplaceBackAttr(keep_dim);
  kernel_context.EmplaceBackAttr(reduce_all);
  kernel_context.EmplaceBackAttr(dense_x->dtype());
  kernel_context.EmplaceBackAttr(out_dtype);

  // 4. InferShape
  auto out_meta = ReduceInferMeta(dense_x->meta(), axis, keep_dim);

  // 5. Prepare outputs
  Tensor out;
  const auto allocator =
      std::make_shared<paddle::experimental::DefaultAllocator>(
          pten::TransToFluidPlace(kernel_key.backend()));
  auto dense_out = std::make_shared<pten::DenseTensor>(allocator, out_meta);
  kernel_context.EmplaceBackOutput(dense_out);
  out.set_impl(dense_out);

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

  return out;
}

PD_DLL_DECL Tensor sum(const Tensor& x,
                       const std::vector<int64_t>& axis,
                       DataType dtype,
                       bool keep_dim) {
  // 1. Get kernel signature and kernel
  auto kernel_key_set = ParseKernelKeyByInputArgs(x);
  auto kernel_key = kernel_key_set.GetHigestPriorityKernelKey();
  auto kernel = pten::KernelFactory::Instance().SelectKernelOrThrowError(
      "reduce_sum", kernel_key);

  // 2. Get Device Context
  auto* dev_ctx = GetDeviceContextByBackend(kernel_key.backend());
  auto kernel_context = pten::KernelContext(dev_ctx);

  // 3. Auto data transform
  auto dense_x = std::dynamic_pointer_cast<pten::DenseTensor>(x.impl());
  kernel_context.EmplaceBackInput(dense_x);

  // The real value of reduce_all will be get in kernel
  // so use default value(false) is OK.
  bool reduce_all = false;

  DataType out_dtype = DataType::UNDEFINED;
  if (dense_x->dtype() == DataType::BOOL ||
      dense_x->dtype() == DataType::INT32 ||
      dense_x->dtype() == DataType::INT64) {
    out_dtype = DataType::INT64;
  }

  kernel_context.EmplaceBackAttr(axis);
  kernel_context.EmplaceBackAttr(keep_dim);
  kernel_context.EmplaceBackAttr(reduce_all);
  kernel_context.EmplaceBackAttr(dense_x->dtype());
  kernel_context.EmplaceBackAttr(out_dtype);

120
  // 4. InferMeta
121
  auto out_meta = ReduceInferMeta(dense_x->meta(), axis, keep_dim);
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

  // 5. Prepare outputs
  Tensor out;
  const auto allocator =
      std::make_shared<paddle::experimental::DefaultAllocator>(
          pten::TransToFluidPlace(kernel_key.backend()));
  auto dense_out = std::make_shared<pten::DenseTensor>(allocator, out_meta);
  kernel_context.EmplaceBackOutput(dense_out);
  out.set_impl(dense_out);

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

  return out;
}

138
PD_DLL_DECL Tensor add(const Tensor& x, const Tensor& y) {
139
  // 1. Get kernel signature and kernel
140
  auto kernel_key_set = ParseKernelKeyByInputArgs(x, y);
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
  auto kernel_key = kernel_key_set.GetHigestPriorityKernelKey();
  auto kernel = pten::KernelFactory::Instance().SelectKernelOrThrowError(
      "elementwise_add", kernel_key);

  // 2. Get Device Context
  auto* dev_ctx = GetDeviceContextByBackend(kernel_key.backend());
  auto kernel_context = pten::KernelContext(dev_ctx);

  // 3. Auto data transform
  auto dense_x = std::dynamic_pointer_cast<pten::DenseTensor>(x.impl());
  kernel_context.EmplaceBackInput(dense_x);
  auto dense_y = std::dynamic_pointer_cast<pten::DenseTensor>(y.impl());
  kernel_context.EmplaceBackInput(dense_y);
  kernel_context.EmplaceBackAttr(-1);

156 157
  // 4. InferMeta
  auto out_meta = ElementwiseInferMeta(dense_x->meta(), dense_y->meta(), -1);
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

  // 5. Prepare outputs
  Tensor out;
  const auto allocator = std::make_shared<DefaultAllocator>(
      pten::TransToFluidPlace(kernel_key.backend()));
  auto dense_out = std::make_shared<pten::DenseTensor>(allocator, out_meta);
  kernel_context.EmplaceBackOutput(dense_out);
  out.set_impl(dense_out);

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

  return out;
}

173 174
PD_DLL_DECL Tensor subtract(const Tensor& x, const Tensor& y) {
  // 1. Get kernel signature and kernel
175
  auto kernel_key_set = ParseKernelKeyByInputArgs(x, y);
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
  auto kernel_key = kernel_key_set.GetHigestPriorityKernelKey();
  auto kernel = pten::KernelFactory::Instance().SelectKernelOrThrowError(
      "elementwise_sub", kernel_key);

  // 2. Get Device Context
  auto* dev_ctx = GetDeviceContextByBackend(kernel_key.backend());
  auto kernel_context = pten::KernelContext(dev_ctx);

  // 3. Auto data transform
  auto dense_x = std::dynamic_pointer_cast<pten::DenseTensor>(x.impl());
  kernel_context.EmplaceBackInput(dense_x);
  auto dense_y = std::dynamic_pointer_cast<pten::DenseTensor>(y.impl());
  kernel_context.EmplaceBackInput(dense_y);
  kernel_context.EmplaceBackAttr(-1);

191 192
  // 4. InferMeta
  auto out_meta = ElementwiseInferMeta(dense_x->meta(), dense_y->meta(), -1);
193 194 195 196 197 198 199 200 201 202 203 204 205 206

  // 5. Prepare outputs
  Tensor out;
  const auto allocator = std::make_shared<DefaultAllocator>(
      pten::TransToFluidPlace(kernel_key.backend()));
  auto dense_out = std::make_shared<pten::DenseTensor>(allocator, out_meta);
  kernel_context.EmplaceBackOutput(dense_out);
  out.set_impl(dense_out);

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

  return out;
}
207 208 209

PD_DLL_DECL Tensor divide(const Tensor& x, const Tensor& y) {
  // 1. Get kernel signature and kernel
210
  auto kernel_key_set = ParseKernelKeyByInputArgs(x, y);
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
  auto kernel_key = kernel_key_set.GetHigestPriorityKernelKey();
  auto kernel = pten::KernelFactory::Instance().SelectKernelOrThrowError(
      "elementwise_div", kernel_key);

  // 2. Get Device Context
  auto* dev_ctx = GetDeviceContextByBackend(kernel_key.backend());
  auto kernel_context = pten::KernelContext(dev_ctx);

  // 3. Auto data transform
  auto dense_x = std::dynamic_pointer_cast<pten::DenseTensor>(x.impl());
  kernel_context.EmplaceBackInput(dense_x);
  auto dense_y = std::dynamic_pointer_cast<pten::DenseTensor>(y.impl());
  kernel_context.EmplaceBackInput(dense_y);
  kernel_context.EmplaceBackAttr(-1);

226 227
  // 4. InferMeta
  auto out_meta = ElementwiseInferMeta(dense_x->meta(), dense_y->meta(), -1);
228 229 230 231 232 233 234 235 236 237 238 239 240 241

  // 5. Prepare outputs
  Tensor out;
  const auto allocator = std::make_shared<DefaultAllocator>(
      pten::TransToFluidPlace(kernel_key.backend()));
  auto dense_out = std::make_shared<pten::DenseTensor>(allocator, out_meta);
  kernel_context.EmplaceBackOutput(dense_out);
  out.set_impl(dense_out);

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

  return out;
}
Y
YuanRisheng 已提交
242 243 244

PD_DLL_DECL Tensor multiply(const Tensor& x, const Tensor& y) {
  // 1. Get kernel signature and kernel
245
  auto kernel_key_set = ParseKernelKeyByInputArgs(x, y);
Y
YuanRisheng 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
  auto kernel_key = kernel_key_set.GetHigestPriorityKernelKey();
  auto kernel = pten::KernelFactory::Instance().SelectKernelOrThrowError(
      "elementwise_mul", kernel_key);

  // 2. Get Device Context
  auto* dev_ctx = GetDeviceContextByBackend(kernel_key.backend());
  auto kernel_context = pten::KernelContext(dev_ctx);

  // 3. Auto data transform
  auto dense_x = std::dynamic_pointer_cast<pten::DenseTensor>(x.impl());
  kernel_context.EmplaceBackInput(dense_x);
  auto dense_y = std::dynamic_pointer_cast<pten::DenseTensor>(y.impl());
  kernel_context.EmplaceBackInput(dense_y);
  kernel_context.EmplaceBackAttr(-1);

261 262
  // 4. InferMeta
  auto out_meta = ElementwiseInferMeta(dense_x->meta(), dense_y->meta(), -1);
Y
YuanRisheng 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276

  // 5. Prepare outputs
  Tensor out;
  const auto allocator = std::make_shared<DefaultAllocator>(
      pten::TransToFluidPlace(kernel_key.backend()));
  auto dense_out = std::make_shared<pten::DenseTensor>(allocator, out_meta);
  kernel_context.EmplaceBackOutput(dense_out);
  out.set_impl(dense_out);

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

  return out;
}
277 278
}  // namespace experimental
}  // namespace paddle
279 280

PT_REGISTER_API(Math);