Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Eager, Performance optimization] math op sink to Cpp level ( + and - operator as an example ) #45811

Merged
merged 25 commits into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions paddle/fluid/pybind/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,7 @@ if(WITH_PYTHON)
set(PYBIND_SRCS eager_py_layer.cc ${PYBIND_SRCS})
set(PYBIND_SRCS eager_legacy_op_function.cc ${PYBIND_SRCS})
set(PYBIND_SRCS eager_op_function.cc ${PYBIND_SRCS})
set(PYBIND_SRCS eager_math_op_patch.cc ${PYBIND_SRCS})
list(APPEND PYBIND_DEPS eager_api)
list(APPEND PYBIND_DEPS autograd_meta)
list(APPEND PYBIND_DEPS backward)
Expand Down
21 changes: 20 additions & 1 deletion paddle/fluid/pybind/eager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,20 @@ int StringTensorInit(PyObject* self, PyObject* args, PyObject* kwargs) {
return 1;
}

void AddPyMethodDefs(std::vector<PyMethodDef>* vector, PyMethodDef* methods) {
if (!vector->empty()) {
// remove nullptr terminator
vector->pop_back();
}
while (true) {
vector->push_back(*methods);
if (!methods->ml_name) {
break;
}
methods++;
}
}

static void TensorDealloc(TensorObject* self) {
if (self->weakrefs != NULL)
PyObject_ClearWeakRefs(reinterpret_cast<PyObject*>(self));
Expand All @@ -1124,6 +1138,7 @@ extern struct PyGetSetDef variable_properties[];
extern struct PyGetSetDef string_tensor_variable_properties[];

extern PyMethodDef variable_methods[];
extern PyMethodDef math_op_patch_methods[];
extern PyMethodDef string_tensor_variable_methods[];

PyNumberMethods number_methods;
Expand All @@ -1133,6 +1148,10 @@ PyMappingMethods mapping_methods;
void BindEager(pybind11::module* module) {
auto m = module->def_submodule("eager");

static std::vector<PyMethodDef> methods;
AddPyMethodDefs(&methods, variable_methods);
AddPyMethodDefs(&methods, math_op_patch_methods);

auto heap_type = reinterpret_cast<PyHeapTypeObject*>(
PyType_Type.tp_alloc(&PyType_Type, 0));
heap_type->ht_name = ToPyObject("Tensor");
Expand All @@ -1144,7 +1163,7 @@ void BindEager(pybind11::module* module) {
type->tp_as_number = &number_methods;
type->tp_as_sequence = &sequence_methods;
type->tp_as_mapping = &mapping_methods;
type->tp_methods = variable_methods;
type->tp_methods = methods.data();
type->tp_getset = variable_properties;
type->tp_init = TensorInit;
type->tp_new = TensorNew;
Expand Down
Loading