Is there a way to debug the backward method of Function class?

Hello guys,
I wrote my cost function with Function class. Actually I have implemented the forward and backward of my own cost function. I want to know is it possible to debug backward function like forward (There are some logical problems in backward function; I would like to find them). The pdb has allowed me to debug my forward step by step. But I could not debug backward function like forward. Could you please tell me is it possible?
Thakns

Same way you debug forwards:

import pdb
pdb.set_trace()

Here’s a more complete snippet:

import torch

class MyLoss(torch.autograd.Function):
  def forward(self, x):
    return x.view(-1).sum(0)
  def backward(self, x):
    import pdb
    pdb.set_trace()
    return x

v = torch.autograd.Variable(torch.randn(5, 5), requires_grad=True)
loss = MyLoss()(v)
loss.backward()
4 Likes

Thank you! I did debugging using python -m pdb sourceFile.py and then used break point setting approaches. i did not know about this approach. :slight_smile: