api_custom_impl.cc 4.5 KB
Newer Older
1
/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14

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/api_custom_impl.h"
16

17 18 19 20 21 22 23
#include "paddle/phi/api/lib/api_registry.h"
#include "paddle/phi/api/lib/api_utils.h"
#include "paddle/phi/api/lib/data_transform.h"
#include "paddle/phi/api/lib/kernel_dispatch.h"
#include "paddle/phi/api/lib/utils/storage.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/core/meta_tensor.h"
24 25 26
#include "paddle/phi/infermeta/binary.h"
#include "paddle/phi/infermeta/multiary.h"
#include "paddle/phi/infermeta/nullary.h"
27
#include "paddle/phi/infermeta/unary.h"
28

29
#include "glog/logging.h"
30

31 32 33
namespace paddle {
namespace experimental {

34
Tensor copy_to_impl(const Tensor& x, Backend backend, bool blocking) {
35 36 37 38
  // 1. Get kernel signature and kernel
  auto kernel_key_set = ParseKernelKeyByInputArgs(x);
  kernel_key_set.backend_set = kernel_key_set.backend_set | BackendSet(backend);
  auto kernel_key = kernel_key_set.GetHigestPriorityKernelKey();
39
  auto kernel = phi::KernelFactory::Instance().SelectKernelOrThrowError(
40 41 42 43 44 45 46
      "copy", kernel_key);

  VLOG(0) << "to API kernel key: " << kernel_key;
  VLOG(0) << "to API kernel: " << kernel;

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

  // 3. Auto data transform
50
  auto dense_x = std::dynamic_pointer_cast<phi::DenseTensor>(x.impl());
51
  kernel_context.EmplaceBackInput(dense_x.get());
52 53
  kernel_context.EmplaceBackAttr(blocking);

54
  // 4. Prepare outputs & InferMeta
55 56 57 58 59 60 61
  auto dense_out = std::make_shared<phi::DenseTensor>(
      phi::make_intrusive<paddle::experimental::SharedStorage>(
          phi::TransToPtenPlace(backend)),
      phi::DenseTensorMeta());
  phi::MetaTensor meta_out(dense_out.get());
  phi::UnchangedInferMeta(*dense_x, &meta_out);
  dense_out->mutable_data(phi::TransToPtenPlace(backend));
62
  kernel_context.EmplaceBackOutput(dense_out.get());
63 64 65
  Tensor out;
  out.set_impl(dense_out);

66
  // 5. Call kernel
67 68 69 70 71
  kernel(&kernel_context);

  return out;
}

72 73 74 75 76 77 78 79 80
std::vector<Tensor> split_impl(const Tensor& x,
                               const ScalarArray& num_or_sections,
                               const Scalar& axis) {
  auto kernel_key_set = ParseKernelKeyByInputArgs(x);
  auto kernel_key = kernel_key_set.GetHigestPriorityKernelKey();

  Backend kernel_backend = kernel_key.backend();
  DataLayout kernel_layout = kernel_key.layout();
  DataType kernel_data_type = kernel_key.dtype();
C
chentianyu03 已提交
81

82
  auto kernel = phi::KernelFactory::Instance().SelectKernelOrThrowError(
C
chentianyu03 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
      "split", {kernel_backend, kernel_layout, kernel_data_type});
  VLOG(6) << "split API kernel key: [" << kernel_backend << ", "
          << kernel_layout << ", " << kernel_data_type << "]";
  VLOG(6) << "split API kernel: " << kernel;

  auto* dev_ctx = GetDeviceContextByBackend(kernel_backend);

  auto dense_x = PrepareData(x, kernel.InputAt(0), {});

  // Calculate the number of out tensors
  size_t out_number;
  if (num_or_sections.GetData().size() == 1) {
    out_number = num_or_sections.GetData()[0];
  } else {
    out_number = num_or_sections.GetData().size();
  }

  std::vector<Tensor> out;
  auto dense_outs = SetKernelOutput(out_number, kernel_backend, &out);
102
  std::vector<phi::MetaTensor> meta_outs;
C
chentianyu03 已提交
103 104 105 106
  for (size_t i = 0; i < out_number; ++i) {
    meta_outs.push_back(dense_outs[i]);
  }

107
  phi::SplitInferMeta(
C
chentianyu03 已提交
108 109 110
      MakeMetaTensor(*dense_x), num_or_sections, axis, &meta_outs);

  using kernel_signature = void (*)(const platform::DeviceContext&,
111 112 113 114
                                    const phi::DenseTensor&,
                                    const phi::ScalarArray&,
                                    const phi::Scalar&,
                                    std::vector<phi::DenseTensor*>&);
C
chentianyu03 已提交
115 116 117
  auto* kernel_fn = kernel.GetVariadicKernelFn<kernel_signature>();
  (*kernel_fn)(*dev_ctx,
               *dense_x,
118 119
               phi::ScalarArray(num_or_sections),
               phi::Scalar(axis),
C
chentianyu03 已提交
120 121 122 123
               dense_outs);

  return out;
}
124

125 126
}  // namespace experimental
}  // namespace paddle