eager_utils.h 5.6 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/scalar.h"
#include "paddle/phi/common/scalar_array.h"
16
#include "paddle/phi/core/dense_tensor.h"
17

18 19 20 21 22 23
#include "pybind11/pybind11.h"
#include "pybind11/stl.h"
namespace paddle {
namespace pybind {

typedef struct {
24 25
  PyObject_HEAD paddle::experimental::Tensor tensor;
} TensorObject;
26

27
int TensorDtype2NumpyDtype(phi::DataType dtype);
28

29 30 31 32 33 34 35 36
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);
37
paddle::experimental::Tensor CastPyArg2Tensor(PyObject* obj, ssize_t arg_pos);
J
Jiabin Yang 已提交
38 39
std::shared_ptr<imperative::VarBase> CastPyArg2VarBase(PyObject* obj,
                                                       ssize_t arg_pos);
40 41
std::vector<paddle::experimental::Tensor> CastPyArg2VectorOfTensor(
    PyObject* obj, ssize_t arg_pos);
42
platform::Place CastPyArg2Place(PyObject* obj, ssize_t arg_pos);
43
framework::Tensor CastPyArg2FrameworkTensor(PyObject* obj, ssize_t arg_pos);
44 45
std::vector<framework::LoDTensor> CastPyArg2VectorOfTensorBase(PyObject* obj,
                                                               ssize_t arg_pos);
46
std::vector<int> CastPyArg2VectorOfInt(PyObject* obj, size_t arg_pos);
J
Jiabin Yang 已提交
47 48
framework::proto::VarType::Type CastPyArg2ProtoType(PyObject* obj,
                                                    ssize_t arg_pos);
49 50 51 52 53 54 55
PyObject* ToPyObject(int value);
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);
56
PyObject* ToPyObject(const paddle::experimental::Tensor& value);
57 58 59 60 61
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);
62
PyObject* ToPyObject(const std::vector<paddle::experimental::Tensor>& value);
63
PyObject* ToPyObject(const platform::Place& value);
64
PyObject* ToPyObject(const framework::LoDTensor* value);
J
Jiabin Yang 已提交
65
PyObject* ToPyObject(const paddle::framework::proto::VarType::Type& dtype);
66
PyObject* ToPyObject(const paddle::framework::proto::VarType& type);
W
wanghuancoder 已提交
67
PyObject* ToPyObject(const void* value);
68 69
PyObject* ToPyObject(
    const std::unordered_map<std::string, std::vector<std::string>>& value);
70

71
template <typename Tuple, size_t N>
72
struct TupleTensorResult {
73
  static void Run(const Tuple& out, PyObject* result) {
74
    TupleTensorResult<Tuple, N - 1>::Run(out, result);
75 76 77 78 79
    PyTuple_SET_ITEM(result, N - 1, ToPyObject(std::get<N - 1>(out)));
  }
};

template <typename Tuple>
80
struct TupleTensorResult<Tuple, 1> {
81 82 83 84 85 86 87 88 89 90
  static void Run(const Tuple& out, PyObject* result) {
    PyTuple_SET_ITEM(result, 0, ToPyObject(std::get<0>(out)));
  }
};

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

91
  TupleTensorResult<decltype(out), sizeof...(Args)>::Run(out, result);
92 93 94 95

  return result;
}

96 97 98 99 100 101 102
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);

103 104 105 106
paddle::optional<paddle::experimental::Tensor> GetOptionalTensorFromArgs(
    const std::string& op_type, const std::string& arg_name, PyObject* args,
    ssize_t arg_idx, bool dispensable = false);

107 108 109 110
paddle::experimental::Tensor& GetTensorFromArgs(const std::string& op_type,
                                                const std::string& arg_name,
                                                PyObject* args, ssize_t arg_idx,
                                                bool dispensable = false);
111

112
std::vector<paddle::experimental::Tensor> GetTensorListFromArgs(
113 114 115
    const std::string& op_type, const std::string& arg_name, PyObject* args,
    ssize_t arg_idx, bool dispensable = false);

116 117 118 119 120
paddle::experimental::Tensor* GetTensorPtrFromArgs(const std::string& op_type,
                                                   const std::string& arg_name,
                                                   PyObject* args,
                                                   ssize_t arg_idx,
                                                   bool dispensable = false);
121

122
std::vector<paddle::experimental::Tensor*> GetTensorPtrListFromArgs(
123 124 125
    const std::string& op_type, const std::string& arg_name, PyObject* args,
    ssize_t arg_idx, bool dispensable = false);

J
Jiabin Yang 已提交
126 127
// end of Slice related methods

128 129
}  // namespace pybind
}  // namespace paddle