had same question. I think the backward() of a custom Function is called in a separate thread. Try adding “print(current_thread())” (don’t forget the import “from threading import current_thread”) in your backward() and your forward() in your custom module (and maybe some other place in the code). Different things are printed. Still don’t know how to have breakpoints in backward() tho.
I am not able to reproduce it with pdb. Maybe that is different with PyCharm’s breakpoint. The backward() functions are called from C side so that might be breaking these things. You could use pdb instead, which is also more commonly used…
One related bit of information is PyCharm has a breakpoint Suspend Policy, which can be set either to thread or application (can be configured by right clicking on breakpoint red knob), but in this case it still doesn’t fix the issue with callback from C API wrapper.
However when combining that with this the solution from this stackoverflow answer, it seems to now work for me:
So, first thing in your backward function, add the following
I’m using vscode and have the same problem. I put a breakpoint() (Python 3.7+) at the beginning of my backward function and then breakpoints work on debug mode. However when running, it doesn’t stop at breakpoint().
For whoever have the same issue, the solution works on my env: Pycharm 2019.3/Ubuntu 18.04/conda Pytorch 1.3.1/Python 3.6.8 (need to pip install pydevd)
Hey! I’m using vscode on Windows and I can’t get it to stop in the backwards method.
I’ve tried each of:
import pydevd; pydevd.settrace(suspend=True, trace_only_current_thread=True)
import debugpy; debugpy.debug_this_thread()
breakpoint()
import pdb; pdb.set_trace()
with no luck. I’ve tried using pdb, the vscode debugger, PyCharm. None of them stop. Any ideas?
I’m trying to debug into backward hook, breakpoint not working either, vscode on windows, pytorch 1.10. Is backward hook somehow different? Forward hook works fine.
Same issue. A temporary solution is to set ‘justMyCode’ = false in vscode debug config, and explicitly raise an error in the backward function. Then the program will stop at this exception.
inside your backward function instead of calling it right after your import! No need to set suspend=True. (While you probably don’t care about it anymore, I just prove-read this thread to help future readers.)