| 1041 | } |
| 1042 | |
| 1043 | static PyObject *run_python_op(PyObject *self, |
| 1044 | PyObject *args, |
| 1045 | PyObject *kwargs) { |
| 1046 | VLOG(6) << "Call run_python_op"; |
| 1047 | |
| 1048 | if (kwargs == NULL) { |
| 1049 | PyErr_SetString( |
| 1050 | PyExc_TypeError, |
| 1051 | "kwargs cannot be NULL. Please add inputs/outputs/attr/inplace_map!"); |
| 1052 | return NULL; |
| 1053 | } |
| 1054 | |
| 1055 | PyObject *py_op_name = PyDict_GetItemString(kwargs, "name"); |
| 1056 | PyObject *py_input_names = PyDict_GetItemString(kwargs, "input_names"); |
| 1057 | PyObject *py_output_names = PyDict_GetItemString(kwargs, "output_names"); |
| 1058 | PyObject *py_attrs_dict = PyDict_GetItemString(kwargs, "attrs"); |
| 1059 | PyObject *py_inplace_dict = PyDict_GetItemString(kwargs, "inplace_map"); |
| 1060 | |
| 1061 | if (!py_op_name || !py_input_names || !py_output_names || !py_attrs_dict || |
| 1062 | !py_inplace_dict) { |
| 1063 | PyErr_SetString( |
| 1064 | PyExc_KeyError, |
| 1065 | "Required key (inputs/outputs/attr/inplace_map) missing from kwargs."); |
| 1066 | ThrowExceptionToPython(std::current_exception()); |
| 1067 | return nullptr; |
| 1068 | } |
| 1069 | |
| 1070 | std::string op_name = CastPyArg2String(py_op_name, "run_python_op", 0); |
| 1071 | std::vector<std::string> inputs_vec = |
| 1072 | CastPyArg2Strings(py_input_names, "run_python_op", 0); |
| 1073 | std::vector<std::string> outputs_vec = |
| 1074 | CastPyArg2Strings(py_output_names, "run_python_op", 0); |
| 1075 | std::unordered_map<std::string, void *> attrs_map = |
| 1076 | ParsePythonOpAttrs(py_attrs_dict); |
| 1077 | std::unordered_map<std::string, std::string> op_inplace_map = |
| 1078 | ParseStringDict(py_inplace_dict); |
| 1079 | |
| 1080 | VLOG(6) << "Building Python OP [" << op_name << "] with attrs:" << std::endl |
| 1081 | << " op_name: " << op_name << std::endl |
| 1082 | << " inputs: " << paddle::string::join_strings(inputs_vec, ", ") |
| 1083 | << std::endl |
| 1084 | << " outputs: " << paddle::string::join_strings(outputs_vec, ", ") |
| 1085 | << std::endl |
| 1086 | << " attrs[infer_meta_fn_ptr]: " |
| 1087 | << reinterpret_cast<uintptr_t>(attrs_map["infer_meta_fn_ptr"]) |
| 1088 | << std::endl |
| 1089 | << " attrs[fn_ptr]: " |
| 1090 | << reinterpret_cast<uintptr_t>(attrs_map["fn_ptr"]); |
| 1091 | |
| 1092 | const auto &meta_info_map = OpMetaInfoMap::Instance().GetMap(); |
| 1093 | |
| 1094 | auto py_func = CreatePyFuncRunner<Tensor>(attrs_map["fn_ptr"], op_name); |
| 1095 | auto infer_meta_py_func = CreatePyFuncRunner<phi::NativeMetaTensor>( |
| 1096 | attrs_map["infer_meta_fn_ptr"], op_name); |
| 1097 | |
| 1098 | if (meta_info_map.find(op_name) == meta_info_map.end()) { |
| 1099 | VLOG(6) << "Python OP " << op_name << " does not exist, registering..."; |
| 1100 | paddle::framework::RegisterPythonOperator( |
nothing calls this directly
no test coverage detected