Any different between model(input) and model.forward(input)

class MyModel(nn.Module):
    def __init__(self, cuda, word_dim, tag_dim, mem_dim, criterion):
        super(MyModel, self).__init__()
   
    def forward(input):
         .. # do something
         return output

model = MyModel()

Is there any different if I called

model.forward(input)

rather than

model(input)

Because only when I call model.forward(input), IDE (in this case, Pycharm) suggest me argument for forward function.

12 Likes

You should avoid calling Module.forward.
The difference is that all the hooks are dispatched in the __call__ function, so if you call .forward and have hooks in your model, the hooks won’t have any effect

18 Likes

But who/what exactly calls the .forward() function? is it the init()? In my case I am being told in error that the .forward is not getting all the arguments it needs, but I am not able to figure out where in the control flow its getting lost.

3 Likes

As @fmassa said, forward is called in the .__call__ function. Have a look at the line of code.

6 Likes

What so you mean by the “hooks”?

6 Likes

Just avoiding using forward directly doesn’t really solve the issue that it breaks IDE support. One potential solution could be this Solution to different forward for train and inference + IDE support for forward args . Here I just wrapped nn.Module to allow a forward method with both hooks and IDE support is retained.

I haven’t use “hooks” yet, but it is like event trigger.