tqchen commented on a change in pull request #7919:
URL: https://github.com/apache/tvm/pull/7919#discussion_r624266948
##########
File path: src/runtime/registry.cc
##########
@@ -102,6 +103,89 @@ std::vector<std::string> Registry::ListNames() {
return keys;
}
+/*!
+ * \brief Execution environment specific API registry.
+ *
+ * This registry stores C API function pointers about
+ * execution environment(e.g. python) specific API function that
+ * we need for specific low-level handling(e.g. signal checking).
+ *
+ * We only stores the C API function when absolutely necessary (e.g. when
signal handler
+ * cannot trap back into python). Always consider use the PackedFunc FFI when
possible
+ * in other cases.
+ */
+class EnvCAPIRegistry {
+ public:
+ /*!
+ * \brief Callback to check if signals have been sent to the process and
+ * if so invoke the registered signal handler in the frontend
environment.
+ *
+ * When runnning TVM in another langugage(python), the signal handler
+ * may not be immediately executed, but instead the signal is marked
+ * in the interpreter state(to ensure non-blocking of the signal handler).
+ *
+ * \return 0 if no error happens, -1 if error happens.
+ */
+ typedef int (*F_PyErr_CheckSignals)();
+
+ /*!
+ * \brief Clear the error indicator.
+ */
+ typedef void (*F_PyErr_Clear)();
+
+ // NOTE: the following function are only registered
+ // in a python environment.
+ /*!
+ * \brief PyErr_CheckSignal function
+ */
+ F_PyErr_CheckSignals pyerr_check_signals = nullptr;
+ /*!
+ * \brief PyErrClear function
+ */
+ F_PyErr_Clear pyerr_clear = nullptr;
+
+ static EnvCAPIRegistry* Global() {
+ static EnvCAPIRegistry* inst = new EnvCAPIRegistry();
+ return inst;
+ }
+
+ // register environment(e.g. python) specific api functions
+ void Register(const std::string& symbol_name, void* fptr) {
+ if (symbol_name == "PyErr_CheckSignals") {
+ Update(symbol_name, &pyerr_check_signals, fptr);
+ } else if (symbol_name == "PyErr_Clear") {
+ Update(symbol_name, &pyerr_clear, fptr);
+ } else {
+ LOG(FATAL) << "Unknown env API " << symbol_name;
+ }
+ }
+
+ // implementation of tvm::runtime::EnvCheckSignals
+ void CheckSignals() {
+ // check python signal to see if there are exception raised
+ if (pyerr_check_signals != nullptr && (*pyerr_check_signals)() != 0) {
+ ICHECK_NOTNULL(pyerr_clear);
Review comment:
I have thought about these. Placing in FFI layer also have overheads(of
introducing such function to the API). Given the two functions are clearly
defined, I eventually decided to place them here as in their original form(to
remove one level of indirection)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]