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

13 14 15 16 17
#if defined(_MSC_VER)
#include <BaseTsd.h>
typedef SSIZE_T ssize_t;
#endif

18
#include <Python.h>
19

20 21
#include "paddle/phi/common/backend.h"
#include "paddle/phi/common/data_type.h"
22
#include "paddle/phi/common/int_array.h"
23
#include "paddle/phi/common/scalar.h"
24
#include "paddle/phi/core/dense_tensor.h"
25 26 27
#include "pybind11/pybind11.h"
#include "pybind11/stl.h"
namespace paddle {
28
class CustomOpKernelContext;
0
0x45f 已提交
29 30 31
namespace framework {
class Scope;
}
32 33
namespace pybind {

34 35 36 37
#define RETURN_PY_NONE \
  Py_INCREF(Py_None);  \
  return Py_None;

38
int TensorDtype2NumpyDtype(phi::DataType dtype);
39

W
wanghuancoder 已提交
40 41
bool IsEagerTensor(PyObject* obj);

42 43 44 45 46 47
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);
W
wanghuancoder 已提交
48
size_t CastPyArg2AttrSize_t(PyObject* obj, ssize_t arg_pos);
49 50
float CastPyArg2AttrFloat(PyObject* obj, ssize_t arg_pos);
std::string CastPyArg2AttrString(PyObject* obj, ssize_t arg_pos);
51 52
paddle::CustomOpKernelContext CastPyArg2CustomOpKernelContext(PyObject* obj,
                                                              ssize_t arg_pos);
53
paddle::experimental::Tensor CastPyArg2Tensor(PyObject* obj, ssize_t arg_pos);
J
Jiabin Yang 已提交
54 55
std::shared_ptr<imperative::VarBase> CastPyArg2VarBase(PyObject* obj,
                                                       ssize_t arg_pos);
56 57
std::vector<paddle::experimental::Tensor> CastPyArg2VectorOfTensor(
    PyObject* obj, ssize_t arg_pos);
58
platform::Place CastPyArg2Place(PyObject* obj, ssize_t arg_pos);
59
framework::Tensor CastPyArg2FrameworkTensor(PyObject* obj, ssize_t arg_pos);
60 61
std::vector<framework::LoDTensor> CastPyArg2VectorOfTensorBase(PyObject* obj,
                                                               ssize_t arg_pos);
62
std::vector<int> CastPyArg2VectorOfInt(PyObject* obj, size_t arg_pos);
W
wanghuancoder 已提交
63 64 65
std::vector<size_t> CastPyArg2VectorOfSize_t(PyObject* obj, size_t arg_pos);
std::vector<std::vector<size_t>> CastPyArg2VectorOfVectorOfSize_t(
    PyObject* obj, size_t arg_pos);
J
Jiabin Yang 已提交
66 67
framework::proto::VarType::Type CastPyArg2ProtoType(PyObject* obj,
                                                    ssize_t arg_pos);
68 69 70
std::unordered_map<std::wstring, int> CastPyArg2Vocab(PyObject* obj,
                                                      ssize_t arg_pos);
std::vector<std::string> CastPyArg2Strings(PyObject* obj, ssize_t arg_pos);
71

72
PyObject* ToPyObject(int value);
73
PyObject* ToPyObject(uint32_t value);
74 75
PyObject* ToPyObject(bool value);
PyObject* ToPyObject(int64_t value);
W
wanghuancoder 已提交
76
PyObject* ToPyObject(size_t value);
77 78 79 80
PyObject* ToPyObject(float value);
PyObject* ToPyObject(double value);
PyObject* ToPyObject(const char* value);
PyObject* ToPyObject(const std::string& value);
W
wanghuancoder 已提交
81 82
PyObject* ToPyObject(const paddle::experimental::Tensor& value,
                     bool return_py_none_if_not_initialize = false);
83
PyObject* ToPyObject(const paddle::experimental::Tensor& value,
84 85 86
                     ssize_t value_idx,
                     PyObject* args,
                     ssize_t arg_idx);
87 88 89
PyObject* ToPyObject(const std::vector<bool>& value);
PyObject* ToPyObject(const std::vector<int>& value);
PyObject* ToPyObject(const std::vector<int64_t>& value);
W
wanghuancoder 已提交
90
PyObject* ToPyObject(const std::vector<size_t>& value);
91 92
PyObject* ToPyObject(const std::vector<float>& value);
PyObject* ToPyObject(const std::vector<double>& value);
W
wanghuancoder 已提交
93
PyObject* ToPyObject(const std::vector<std::vector<size_t>>& value);
94 95
PyObject* ToPyObject(const std::vector<paddle::experimental::Tensor>& value,
                     bool return_py_none_if_not_initialize = false);
96
PyObject* ToPyObject(const platform::Place& value);
97
PyObject* ToPyObject(const framework::LoDTensor* value);
98
PyObject* ToPyObject(const phi::SelectedRows* value);
J
Jiabin Yang 已提交
99
PyObject* ToPyObject(const paddle::framework::proto::VarType::Type& dtype);
100
PyObject* ToPyObject(const paddle::framework::proto::VarType& type);
W
wanghuancoder 已提交
101
PyObject* ToPyObject(const void* value);
102 103
PyObject* ToPyObject(
    const std::unordered_map<std::string, std::vector<std::string>>& value);
104
PyObject* ToPyObject(const std::unordered_map<std::wstring, int>& value);
105

106
template <typename Tuple, size_t N>
107
struct TupleTensorResult {
108
  static void Run(const Tuple& out, PyObject* result) {
109
    TupleTensorResult<Tuple, N - 1>::Run(out, result);
110 111
    PyTuple_SET_ITEM(result, N - 1, ToPyObject(std::get<N - 1>(out)));
  }
112

113 114 115 116 117
  static void Run(const Tuple& out,
                  PyObject* result,
                  ssize_t value_idx,
                  PyObject* args,
                  ssize_t arg_idx) {
118 119
    TupleTensorResult<Tuple, N - 1>::Run(out, result, value_idx, args, arg_idx);
    if (N - 1 == value_idx) {
120
      PyTuple_SET_ITEM(
121 122
          result,
          N - 1,
123
          ToPyObject(std::get<N - 1>(out), value_idx, args, arg_idx));
124 125 126 127
    } else {
      PyTuple_SET_ITEM(result, N - 1, ToPyObject(std::get<N - 1>(out)));
    }
  }
128 129 130
};

template <typename Tuple>
131
struct TupleTensorResult<Tuple, 1> {
132 133 134
  static void Run(const Tuple& out, PyObject* result) {
    PyTuple_SET_ITEM(result, 0, ToPyObject(std::get<0>(out)));
  }
135

136 137 138 139 140
  static void Run(const Tuple& out,
                  PyObject* result,
                  ssize_t value_idx,
                  PyObject* args,
                  ssize_t arg_idx) {
141
    if (value_idx == 0) {
142 143
      PyTuple_SET_ITEM(
          result, 0, ToPyObject(std::get<0>(out), value_idx, args, arg_idx));
144 145 146 147
    } else {
      PyTuple_SET_ITEM(result, 0, ToPyObject(std::get<0>(out)));
    }
  }
148 149 150 151 152 153 154
};

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

155
  TupleTensorResult<decltype(out), sizeof...(Args)>::Run(out, result);
156 157 158 159

  return result;
}

160
template <typename... Args>
161 162 163 164
PyObject* ToPyObject(const std::tuple<Args...>& out,
                     ssize_t value_idx,
                     PyObject* args,
                     ssize_t arg_idx) {
165 166 167 168 169 170 171 172 173 174 175
  // 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);

176 177
  TupleTensorResult<decltype(out), sizeof...(Args)>::Run(
      out, result, value_idx, args, arg_idx);
178 179 180 181

  return result;
}

182 183 184 185
paddle::experimental::Scalar CastPyArg2Scalar(PyObject* obj,
                                              const std::string& op_type,
                                              ssize_t arg_pos);

186 187 188 189
paddle::experimental::Scalar CastNumpy2Scalar(PyObject* obj,
                                              const std::string& op_type,
                                              ssize_t arg_pos);

190 191 192
paddle::experimental::IntArray CastPyArg2IntArray(PyObject* obj,
                                                  const std::string& op_type,
                                                  ssize_t arg_pos);
193

194 195
paddle::Place CastPyArg2Place(PyObject* obj,
                              const std::string& op_type,
196
                              ssize_t arg_pos);
197

198 199
paddle::DataType CastPyArg2DataType(PyObject* obj,
                                    const std::string& op_type,
200
                                    ssize_t arg_pos);
201

202
paddle::optional<paddle::experimental::Tensor> GetOptionalTensorFromArgs(
203 204 205 206 207
    const std::string& op_type,
    const std::string& arg_name,
    PyObject* args,
    ssize_t arg_idx,
    bool dispensable = false);
208

209 210
paddle::experimental::Tensor& GetTensorFromArgs(const std::string& op_type,
                                                const std::string& arg_name,
211 212
                                                PyObject* args,
                                                ssize_t arg_idx,
213
                                                bool dispensable = false);
214

215
std::vector<paddle::experimental::Tensor> GetTensorListFromArgs(
216 217 218 219 220
    const std::string& op_type,
    const std::string& arg_name,
    PyObject* args,
    ssize_t arg_idx,
    bool dispensable = false);
221

222 223 224 225 226
paddle::experimental::Tensor* GetTensorPtrFromArgs(const std::string& op_type,
                                                   const std::string& arg_name,
                                                   PyObject* args,
                                                   ssize_t arg_idx,
                                                   bool dispensable = false);
227

228
std::vector<paddle::experimental::Tensor*> GetTensorPtrListFromArgs(
229 230 231 232 233
    const std::string& op_type,
    const std::string& arg_name,
    PyObject* args,
    ssize_t arg_idx,
    bool dispensable = false);
234

W
wanghuancoder 已提交
235 236 237 238 239 240 241 242
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 已提交
243
// end of Slice related methods
244

0
0x45f 已提交
245
std::vector<paddle::framework::Scope*> GetScopePtrListFromArgs(
246 247 248 249 250
    const std::string& op_type,
    const std::string& arg_name,
    PyObject* args,
    ssize_t arg_idx,
    bool dispensable);
J
Jiabin Yang 已提交
251

252 253
}  // namespace pybind
}  // namespace paddle