manipulation.cc 4.6 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/manipulation.h"
16 17 18 19

#include <memory>

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

27 28 29 30 31 32
PT_DECLARE_MODULE(ManipulationCPU);

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

33 34 35
namespace paddle {
namespace experimental {

36
PD_DLL_DECL Tensor flatten(const Tensor& x, int start_axis, int stop_axis) {
37 38 39 40 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(
      "flatten_contiguous_range", kernel_key);

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

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

53 54
  // 4. InferMeta
  auto out_meta = FlattenInferMeta(dense_x->meta(), start_axis, stop_axis);
55 56 57 58 59 60 61 62 63 64 65 66 67 68

  // 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;
}
69

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
PD_DLL_DECL Tensor cast(const Tensor& x, DataType out_dtype) {
  // 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(
      "cast", 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);
  kernel_context.EmplaceBackAttr(out_dtype);
  kernel_context.EmplaceBackAttr(dense_x->meta().dtype);

87
  // 4. InferMeta
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
  auto out_meta = CastInferMeta(dense_x->meta(), out_dtype);

  // 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;
}

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
PD_DLL_DECL Tensor reshape(const Tensor& x, const std::vector<int64_t>& shape) {
  // 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(
      "reshape2", 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);
  kernel_context.EmplaceBackAttr(shape);

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

  // 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;
}

137 138
}  // namespace experimental
}  // namespace paddle
139 140

PT_REGISTER_API(Manipulation);