cpp_wrapper: rethrow Python exceptions, when present (#141369)

When running fallback operations in `cpp_wrapper` mode, Python errors thrown in the fallback should be propagated up the stack. This PR fixes the current situation, which discards all Python errors thrown in the fallback op in favor of an uninformative `RuntimeError`.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/141369
Approved by: https://github.com/desertfire
ghstack dependencies: #141368, #141580
This commit is contained in:
Benjamin Glass
2024-12-04 21:08:45 +00:00
committed by PyTorch MergeBot
parent 4613bd393d
commit dd7debdbe8
2 changed files with 8 additions and 2 deletions

View File

@ -2294,13 +2294,16 @@ class CppWrapperCodeCache(CppPythonBindingsCodeCache):
std::vector<AtenTensorHandle> output_handles(%s);
try {
inductor_entry_impl(input_handles.data(), output_handles.data());
if (PyErr_Occurred()) {
return nullptr;
}
return pack_tensor_handle_list(output_handles);
} catch(std::exception const& e) {
PyErr_SetString(PyExc_RuntimeError, e.what());
return {};
return nullptr;
} catch(...) {
PyErr_SetString(PyExc_RuntimeError, "unhandled error");
return {};
return nullptr;
}
}
"""