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
Failed to trace C extension function in numpy #154
Comments
I believe this is a I'm not a numpy expert, I may spend some time in this in the future, but I'm on vacation now and have limited time working on this. However, anyone is welcome to figure out why |
Hi, I investigated this issue by debugging the source code of CPython with GDB. static PyObject *
trace_call_function(PyThreadState *tstate,
PyObject *func,
PyObject **args, Py_ssize_t nargs,
PyObject *kwnames)
{
PyObject *x;
if (PyCFunction_Check(func)) {
C_TRACE(x, _PyObject_Vectorcall(func, args, nargs, kwnames));
return x;
}
else if (Py_TYPE(func) == &PyMethodDescr_Type && nargs > 0) {
/* We need to create a temporary bound method as argument
for profiling.
If nargs == 0, then this cannot work because we have no
"self". In any case, the call itself would raise
TypeError (foo needs an argument), so we just skip
profiling. */
PyObject *self = args[0];
func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
if (func == NULL) {
return NULL;
}
C_TRACE(x, _PyObject_Vectorcall(func,
args+1, nargs-1,
kwnames));
Py_DECREF(func);
return x;
}
return _PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
} The direct cause is that Here is the stack trace from GDB:
What I haven't figured out yet is that:
Also, I tried to trace I think this is important since THE most important usage of python nowadays, IMHO, is deep learning. A tool that can trace the implementations of those complex deep learning frameworks is utterly desired. Hope we can find a solution for this. Thanks! |
I agree that this is a very important usage. I'm not an expert on CPython implementation, I can understand your investigation but I do not know the root cause of it either. However, I do know pretty well about viztracer's mechanism. VizTracer relies on the CPython hook function, to be more specific, From another point of view, a ML/DL engineer, even with the fact that they are working heavily with the ML framework, may not need to trace every single fundamental call, especially for profiling, maybe for debugging too. They might get enough information from the traceable call stack. For example, I know sklearn is using viztracer for profiling and they get something pretty useful from it. So for this issue, I have two proposals:
|
It seems C extensions won't be supported in VizTracer and it's probably out of scope due to the way it works. Are there alternatives? |
Well, C extensions are supported - C functions are not. Basically if you call into a C extension from Python code, it will be recorded. VizTracer is not able to log all the C calls. In short, anything logged by py-spy can log C-level functions, yes. perf is designed for C-level functions so it will work too (the Python-level support is not as good). However, both py-spy and perf are sampling profilers, not tracers. So if you need profiling, they are great tools, but they can do what VizTracer does. |
I appreciate your explanation and your support for this project. Off topic: |
Hi, first thank you for making this awesome project !
I am trying to trace the execution of numpy with the following code:
The output result.json is
The expected
np.add
event is missing.How to make such C extension functions traceable?
The text was updated successfully, but these errors were encountered: