eager_utils.h 8.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/* 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. */
#pragma once

#include <Python.h>
14 15
#include "paddle/phi/common/backend.h"
#include "paddle/phi/common/data_type.h"
16 17
#include "paddle/phi/common/scalar.h"
#include "paddle/phi/common/scalar_array.h"
18
#include "paddle/phi/core/dense_tensor.h"
19

20 21 22
#include "pybind11/pybind11.h"
#include "pybind11/stl.h"
namespace paddle {
23
class CustomOpKernelContext;
0
0x45f 已提交
24 25 26
namespace framework {
class Scope;
}
27 28
namespace pybind {

29
int TensorDtype2NumpyDtype(phi::DataType dtype);
30

W
wanghuancoder 已提交
31 32
bool IsEagerTensor(PyObject* obj);

33 34 35 36 37 38 39 40
bool PyObject_CheckLongOrConvertToLong(PyObject** obj);
bool PyObject_CheckFloatOrConvertToFloat(PyObject** obj);
bool PyObject_CheckStr(PyObject* obj);
bool CastPyArg2AttrBoolean(PyObject* obj, ssize_t arg_pos);
int CastPyArg2AttrInt(PyObject* obj, ssize_t arg_pos);
int64_t CastPyArg2AttrLong(PyObject* obj, ssize_t arg_pos);
float CastPyArg2AttrFloat(PyObject* obj, ssize_t arg_pos);
std::string CastPyArg2AttrString(PyObject* obj, ssize_t arg_pos);
41 42
paddle::CustomOpKernelContext CastPyArg2CustomOpKernelContext(PyObject* obj,
                                                              ssize_t arg_pos);
43
paddle::experimental::Tensor CastPyArg2Tensor(PyObject* obj, ssize_t arg_pos);
J
Jiabin Yang 已提交
44 45
std::shared_ptr<imperative::VarBase> CastPyArg2VarBase(PyObject* obj,
                                                       ssize_t arg_pos);
46 47
std::vector<paddle::experimental::Tensor> CastPyArg2VectorOfTensor(
    PyObject* obj, ssize_t arg_pos);
48
platform::Place CastPyArg2Place(PyObject* obj, ssize_t arg_pos);
49
framework::Tensor CastPyArg2FrameworkTensor(PyObject* obj, ssize_t arg_pos);
50 51
std::vector<framework::LoDTensor> CastPyArg2VectorOfTensorBase(PyObject* obj,
                                                               ssize_t arg_pos);
52
std::vector<int> CastPyArg2VectorOfInt(PyObject* obj, size_t arg_pos);
J
Jiabin Yang 已提交
53 54
framework::proto::VarType::Type CastPyArg2ProtoType(PyObject* obj,
                                                    ssize_t arg_pos);
55

56
PyObject* ToPyObject(int value);
57
PyObject* ToPyObject(uint32_t value);
58 59 60 61 62 63
PyObject* ToPyObject(bool value);
PyObject* ToPyObject(int64_t value);
PyObject* ToPyObject(float value);
PyObject* ToPyObject(double value);
PyObject* ToPyObject(const char* value);
PyObject* ToPyObject(const std::string& value);
W
wanghuancoder 已提交
64 65
PyObject* ToPyObject(const paddle::experimental::Tensor& value,
                     bool return_py_none_if_not_initialize = false);
66 67
PyObject* ToPyObject(const paddle::experimental::Tensor& value,
                     ssize_t value_idx, PyObject* args, ssize_t arg_idx);
68 69 70 71 72
PyObject* ToPyObject(const std::vector<bool>& value);
PyObject* ToPyObject(const std::vector<int>& value);
PyObject* ToPyObject(const std::vector<int64_t>& value);
PyObject* ToPyObject(const std::vector<float>& value);
PyObject* ToPyObject(const std::vector<double>& value);
73 74
PyObject* ToPyObject(const std::vector<paddle::experimental::Tensor>& value,
                     bool return_py_none_if_not_initialize = false);
75
PyObject* ToPyObject(const platform::Place& value);
76
PyObject* ToPyObject(const framework::LoDTensor* value);
J
Jiabin Yang 已提交
77
PyObject* ToPyObject(const paddle::framework::proto::VarType::Type& dtype);
78
PyObject* ToPyObject(const paddle::framework::proto::VarType& type);
W
wanghuancoder 已提交
79
PyObject* ToPyObject(const void* value);
80 81
PyObject* ToPyObject(
    const std::unordered_map<std::string, std::vector<std::string>>& value);
82

83
template <typename Tuple, size_t N>
84
struct TupleTensorResult {
85
  static void Run(const Tuple& out, PyObject* result) {
86
    TupleTensorResult<Tuple, N - 1>::Run(out, result);
87 88
    PyTuple_SET_ITEM(result, N - 1, ToPyObject(std::get<N - 1>(out)));
  }
89 90 91 92 93 94 95 96 97 98 99

  static void Run(const Tuple& out, PyObject* result, ssize_t value_idx,
                  PyObject* args, ssize_t arg_idx) {
    TupleTensorResult<Tuple, N - 1>::Run(out, result, value_idx, args, arg_idx);
    if (N - 1 == value_idx) {
      PyTuple_SET_ITEM(result, N - 1, ToPyObject(std::get<N - 1>(out),
                                                 value_idx, args, arg_idx));
    } else {
      PyTuple_SET_ITEM(result, N - 1, ToPyObject(std::get<N - 1>(out)));
    }
  }
100 101 102
};

template <typename Tuple>
103
struct TupleTensorResult<Tuple, 1> {
104 105 106
  static void Run(const Tuple& out, PyObject* result) {
    PyTuple_SET_ITEM(result, 0, ToPyObject(std::get<0>(out)));
  }
107 108 109 110 111 112 113 114 115 116

  static void Run(const Tuple& out, PyObject* result, ssize_t value_idx,
                  PyObject* args, ssize_t arg_idx) {
    if (value_idx == 0) {
      PyTuple_SET_ITEM(result, 0,
                       ToPyObject(std::get<0>(out), value_idx, args, arg_idx));
    } else {
      PyTuple_SET_ITEM(result, 0, ToPyObject(std::get<0>(out)));
    }
  }
117 118 119 120 121 122 123
};

template <typename... Args>
PyObject* ToPyObject(const std::tuple<Args...>& out) {
  auto len = sizeof...(Args);
  PyObject* result = PyTuple_New(len);

124
  TupleTensorResult<decltype(out), sizeof...(Args)>::Run(out, result);
125 126 127 128

  return result;
}

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
template <typename... Args>
PyObject* ToPyObject(const std::tuple<Args...>& out, ssize_t value_idx,
                     PyObject* args, ssize_t arg_idx) {
  // For inplace op, directly return the input PyObject of the inplace tensor.
  // [Parameter]
  // out: Outputs tuple after executing op.
  // value_idx: Index of inplace tensor in outputs tuple. Used to find the
  // output inplace tensor.
  // args: Input PyObject.
  // arg_idx: Index of inplace PyObject in input args. Used to find the input
  // inplace PyObject.
  auto len = sizeof...(Args);
  PyObject* result = PyTuple_New(len);

  TupleTensorResult<decltype(out), sizeof...(Args)>::Run(out, result, value_idx,
                                                         args, arg_idx);

  return result;
}

149 150 151 152 153 154 155
paddle::experimental::Scalar CastPyArg2Scalar(PyObject* obj,
                                              const std::string& op_type,
                                              ssize_t arg_pos);

paddle::experimental::ScalarArray CastPyArg2ScalarArray(
    PyObject* obj, const std::string& op_type, ssize_t arg_pos);

156 157 158
paddle::experimental::Place CastPyArg2Place(PyObject* obj,
                                            const std::string& op_type,
                                            ssize_t arg_pos);
159 160 161 162 163

paddle::experimental::DataType CastPyArg2DataType(PyObject* obj,
                                                  const std::string& op_type,
                                                  ssize_t arg_pos);

H
hong 已提交
164
paddle::optional<const paddle::experimental::Tensor&> GetOptionalTensorFromArgs(
165 166 167
    const std::string& op_type, const std::string& arg_name, PyObject* args,
    ssize_t arg_idx, bool dispensable = false);

168 169 170 171
paddle::experimental::Tensor& GetTensorFromArgs(const std::string& op_type,
                                                const std::string& arg_name,
                                                PyObject* args, ssize_t arg_idx,
                                                bool dispensable = false);
172

173
std::vector<paddle::experimental::Tensor> GetTensorListFromArgs(
174 175 176
    const std::string& op_type, const std::string& arg_name, PyObject* args,
    ssize_t arg_idx, bool dispensable = false);

177 178 179 180 181
paddle::experimental::Tensor* GetTensorPtrFromArgs(const std::string& op_type,
                                                   const std::string& arg_name,
                                                   PyObject* args,
                                                   ssize_t arg_idx,
                                                   bool dispensable = false);
182

183
std::vector<paddle::experimental::Tensor*> GetTensorPtrListFromArgs(
184 185 186
    const std::string& op_type, const std::string& arg_name, PyObject* args,
    ssize_t arg_idx, bool dispensable = false);

W
wanghuancoder 已提交
187 188 189 190 191 192 193 194
std::vector<paddle::experimental::Tensor*> GetTensorPtrListFromPyObject(
    PyObject* obj);

std::vector<paddle::experimental::Tensor> GetTensorListFromPyObject(
    PyObject* obj);

paddle::experimental::Tensor& GetTensorFromPyObject(PyObject* obj);

J
Jiabin Yang 已提交
195
// end of Slice related methods
196

0
0x45f 已提交
197 198 199
std::vector<paddle::framework::Scope*> GetScopePtrListFromArgs(
    const std::string& op_type, const std::string& arg_name, PyObject* args,
    ssize_t arg_idx, bool dispensable);
J
Jiabin Yang 已提交
200

201 202
}  // namespace pybind
}  // namespace paddle