Skip to content

Commit 37e0f6d

Browse files
authored
Merge pull request #21 from JiabinYang/refine_python_c_api
refine python c api
2 parents aeec32f + 9b476a0 commit 37e0f6d

File tree

4 files changed

+51
-55
lines changed

4 files changed

+51
-55
lines changed

paddle/fluid/pybind/eager.cc

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ namespace pybind {
3232

3333
namespace py = ::pybind11;
3434

35-
PyTypeObject* pEagerTensorType;
35+
PyTypeObject* p_eager_tensor_type;
3636

3737
PyObject* eagertensor_new(PyTypeObject* type, PyObject* args,
3838
PyObject* kwargs) {
3939
PyObject* obj = type->tp_alloc(type, 0);
4040
if (obj) {
41-
auto v = (EagerTensorObject*)obj; // NOLINT
41+
auto v = reinterpret_cast<EagerTensorObject*>(obj);
4242
new (&(v->eagertensor)) egr::EagerTensor();
4343
}
4444
return obj;
@@ -49,16 +49,11 @@ static void eagertensor_dealloc(EagerTensorObject* self) {
4949
Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self));
5050
}
5151

52-
static int eagertensor_init(EagerTensorObject* self, PyObject* args,
53-
PyObject* kwargs) {
54-
return 0;
55-
}
56-
5752
extern struct PyGetSetDef variable_properties[];
5853

5954
extern PyMethodDef variable_methods[];
6055

61-
PyTypeObject EagerTensorType = {
56+
PyTypeObject eager_tensor_type = {
6257
PyVarObject_HEAD_INIT(NULL, 0) "core_avx.eager.EagerTensor", /* tp_name */
6358
sizeof(EagerTensorObject), /* tp_basicsize */
6459
0, /* tp_itemsize */
@@ -78,50 +73,50 @@ PyTypeObject EagerTensorType = {
7873
0, /* tp_setattro */
7974
0, /* tp_as_buffer */
8075
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
81-
Py_TPFLAGS_HEAPTYPE, /* tp_flags */
82-
0, /* tp_doc */
83-
0, /* tp_traverse */
84-
0, /* tp_clear */
85-
0, /* tp_richcompare */
86-
0, /* tp_weaklistoffset */
87-
0, /* tp_iter */
88-
0, /* tp_iternext */
89-
variable_methods, /* tp_methods */
90-
0, /* tp_members */
91-
variable_properties, /* tp_getset */
92-
0, /* tp_base */
93-
0, /* tp_dict */
94-
0, /* tp_descr_get */
95-
0, /* tp_descr_set */
96-
0, /* tp_dictoffset */
97-
(initproc)eagertensor_init, /* tp_init */
98-
0, /* tp_alloc */
99-
eagertensor_new, /* tp_new */
100-
0, /* tp_free */
101-
0, /* tp_is_gc */
102-
0, /* tp_bases */
103-
0, /* tp_mro */
104-
0, /* tp_cache */
105-
0, /* tp_subclasses */
106-
0, /* tp_weaklist */
107-
0, /* tp_del */
108-
0 /* tp_version_tag */
76+
Py_TPFLAGS_HEAPTYPE, /* tp_flags */
77+
0, /* tp_doc */
78+
0, /* tp_traverse */
79+
0, /* tp_clear */
80+
0, /* tp_richcompare */
81+
0, /* tp_weaklistoffset */
82+
0, /* tp_iter */
83+
0, /* tp_iternext */
84+
variable_methods, /* tp_methods */
85+
0, /* tp_members */
86+
variable_properties, /* tp_getset */
87+
0, /* tp_base */
88+
0, /* tp_dict */
89+
0, /* tp_descr_get */
90+
0, /* tp_descr_set */
91+
0, /* tp_dictoffset */
92+
0, /* tp_init */
93+
0, /* tp_alloc */
94+
eagertensor_new, /* tp_new */
95+
0, /* tp_free */
96+
0, /* tp_is_gc */
97+
0, /* tp_bases */
98+
0, /* tp_mro */
99+
0, /* tp_cache */
100+
0, /* tp_subclasses */
101+
0, /* tp_weaklist */
102+
0, /* tp_del */
103+
0 /* tp_version_tag */
109104
};
110105

111106
void BindEager(pybind11::module* module) {
112107
auto m = module->def_submodule("eager");
113108

114-
pEagerTensorType = &EagerTensorType;
115-
if (PyType_Ready(&EagerTensorType) < 0) {
109+
p_eager_tensor_type = &eager_tensor_type;
110+
if (PyType_Ready(&eager_tensor_type) < 0) {
116111
PADDLE_THROW(platform::errors::Fatal(
117112
"Init Paddle erroe in BindEager(PyType_Ready)."));
118113
return;
119114
}
120115

121-
Py_INCREF(&EagerTensorType);
116+
Py_INCREF(&eager_tensor_type);
122117
if (PyModule_AddObject(m.ptr(), "EagerTensor",
123-
reinterpret_cast<PyObject*>(&EagerTensorType)) < 0) {
124-
Py_DECREF(&EagerTensorType);
118+
reinterpret_cast<PyObject*>(&eager_tensor_type)) < 0) {
119+
Py_DECREF(&eager_tensor_type);
125120
Py_DECREF(m.ptr());
126121
PADDLE_THROW(platform::errors::Fatal(
127122
"Init Paddle erroe in BindEager(PyModule_AddObject)."));

paddle/fluid/pybind/eager_functions.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ bool check_numpy_available() {
7777
return ret;
7878
}
7979

80-
extern PyTypeObject* pEagerTensorType;
80+
extern PyTypeObject* p_eager_tensor_type;
8181

8282
static PyObject* eager_api_set_expected_place(PyObject* self, PyObject* args,
8383
PyObject* kwargs) {
@@ -190,9 +190,9 @@ static inline PyObject* eager_api_numpy_to_tensor(PyObject* numpy_data,
190190
std::shared_ptr<pten::DenseTensor> densetensor(
191191
new pten::DenseTensor(std::move(shared_storage), std::move(meta)));
192192

193-
PyObject* obj = pEagerTensorType->tp_alloc(pEagerTensorType, 0);
193+
PyObject* obj = p_eager_tensor_type->tp_alloc(p_eager_tensor_type, 0);
194194
if (obj) {
195-
auto v = (EagerTensorObject*)obj; // NOLINT
195+
auto v = reinterpret_cast<EagerTensorObject*>(obj);
196196
new (&(v->eagertensor)) egr::EagerTensor();
197197
v->eagertensor.set_impl(densetensor);
198198
v->eagertensor.set_name(egr::Controller::Instance().GenerateUniqueName());

paddle/fluid/pybind/eager_properties.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ int init_numpy_p() {
4646
}
4747
static const int numpy_initialized_m = init_numpy_p();
4848

49-
extern PyTypeObject* pEagerTensorType;
49+
extern PyTypeObject* p_eager_tensor_type;
5050

5151
PyObject* eager_tensor_properties_get_name(EagerTensorObject* self,
5252
void* closure) {

paddle/fluid/pybind/eager_utils.cc

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ limitations under the License. */
2828
namespace paddle {
2929
namespace pybind {
3030

31-
extern PyTypeObject* pEagerTensorType;
31+
extern PyTypeObject* p_eager_tensor_type;
3232

3333
bool PyObject_CheckLongOrConvertToLong(PyObject** obj) {
3434
if ((PyLong_Check(*obj) && !PyBool_Check(*obj))) {
@@ -49,7 +49,7 @@ bool PyObject_CheckLongOrConvertToLong(PyObject** obj) {
4949

5050
bool PyObject_CheckFloatOrConvertToFloat(PyObject** obj) {
5151
// sometimes users provide PyLong or numpy.int64 but attr is float
52-
if (PyFloat_Check(*obj) || PyLong_Check(*obj)) { // NOLINT
52+
if (PyFloat_Check(*obj) || PyLong_Check(*obj)) {
5353
return true;
5454
}
5555
if (std::string((reinterpret_cast<PyTypeObject*>((*obj)->ob_type))->tp_name)
@@ -94,7 +94,7 @@ int CastPyArg2AttrInt(PyObject* obj, ssize_t arg_pos) {
9494

9595
int64_t CastPyArg2AttrLong(PyObject* obj, ssize_t arg_pos) {
9696
if (PyObject_CheckLongOrConvertToLong(&obj)) {
97-
return (int64_t)PyLong_AsLong(obj); // NOLINT
97+
return reinterpret_cast<int64_t>(PyLong_AsLong(obj));
9898
} else {
9999
PADDLE_THROW(platform::errors::InvalidArgument(
100100
"argument (position %d) must be "
@@ -130,7 +130,8 @@ std::string CastPyArg2AttrString(PyObject* obj, ssize_t arg_pos) {
130130
}
131131

132132
egr::EagerTensor CastPyArg2EagerTensor(PyObject* obj, ssize_t arg_pos) {
133-
if (PyObject_IsInstance(obj, reinterpret_cast<PyObject*>(pEagerTensorType))) {
133+
if (PyObject_IsInstance(obj,
134+
reinterpret_cast<PyObject*>(p_eager_tensor_type))) {
134135
return reinterpret_cast<EagerTensorObject*>(obj)->eagertensor;
135136
} else {
136137
PADDLE_THROW(platform::errors::InvalidArgument(
@@ -148,8 +149,8 @@ std::vector<egr::EagerTensor> CastPyArg2VectorOfEagerTensor(PyObject* obj,
148149
PyObject* item = nullptr;
149150
for (Py_ssize_t i = 0; i < len; i++) {
150151
item = PyList_GetItem(obj, i);
151-
if (PyObject_IsInstance(item,
152-
reinterpret_cast<PyObject*>(pEagerTensorType))) {
152+
if (PyObject_IsInstance(
153+
item, reinterpret_cast<PyObject*>(p_eager_tensor_type))) {
153154
result.emplace_back(
154155
reinterpret_cast<EagerTensorObject*>(item)->eagertensor);
155156
} else {
@@ -165,8 +166,8 @@ std::vector<egr::EagerTensor> CastPyArg2VectorOfEagerTensor(PyObject* obj,
165166
PyObject* item = nullptr;
166167
for (Py_ssize_t i = 0; i < len; i++) {
167168
item = PyTuple_GetItem(obj, i);
168-
if (PyObject_IsInstance(item,
169-
reinterpret_cast<PyObject*>(pEagerTensorType))) {
169+
if (PyObject_IsInstance(
170+
item, reinterpret_cast<PyObject*>(p_eager_tensor_type))) {
170171
result.emplace_back(
171172
reinterpret_cast<EagerTensorObject*>(item)->eagertensor);
172173
} else {
@@ -211,7 +212,7 @@ PyObject* ToPyObject(const std::string& value) {
211212
}
212213

213214
PyObject* ToPyObject(const egr::EagerTensor& value) {
214-
PyObject* obj = pEagerTensorType->tp_alloc(pEagerTensorType, 0);
215+
PyObject* obj = p_eager_tensor_type->tp_alloc(p_eager_tensor_type, 0);
215216
if (obj) {
216217
auto v = reinterpret_cast<EagerTensorObject*>(obj);
217218
new (&(v->eagertensor)) egr::EagerTensor();
@@ -277,7 +278,7 @@ PyObject* ToPyObject(const std::vector<egr::EagerTensor>& value) {
277278
PyObject* result = PyList_New((Py_ssize_t)value.size());
278279

279280
for (size_t i = 0; i < value.size(); i++) {
280-
PyObject* obj = pEagerTensorType->tp_alloc(pEagerTensorType, 0);
281+
PyObject* obj = p_eager_tensor_type->tp_alloc(p_eager_tensor_type, 0);
281282
if (obj) {
282283
auto v = reinterpret_cast<EagerTensorObject*>(obj);
283284
new (&(v->eagertensor)) egr::EagerTensor();

0 commit comments

Comments
 (0)