Custom backward() breakpoint doesn't get hit

I’m new to Pytorch. I’m running this example:

My breakpoints in the forward() function are hit, but not the ones in the backward function. Why?
I’m using PyCharm CE.

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.

Could you post your code so that me or other people can help you?

the example linked by the original question should do it (edit: meaning not hitting the breakpoint in backward())

Just use the code in the example.

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

import pydevd
pydevd.settrace(suspend=False, trace_only_current_thread=True)


Worked for me. Thank you very much!

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().