In most PyTorch examples, I see out = model(input) instead of out = model.forward(input). I understand that the latter doesn't handle any hooks, and the first option is generally preferred.
However, in the Annotated Transformer implementation of the Attention Is All You Need Transformer, they use model.forward() when getting the output from the entire model, but all layers within the model are called using layer(x) instead of layer.forward(x). I assume this explicit use of .forward() is intentional, but can't figure out why.
Edit: Very sorry for not being clear on what I meant by layer. The code I am referring to has structure like:
class model(nn.Module):
def __init__(self, layer1, layer2, layer3):
super(model, self).__init__()
self.layer1 = layer1
self.layer2 = layer2
self.layer3 = layer3
def forward(self, x):
return self.layer2(self.layer1(x))
class layer1(nn.Module):
def __init__(self, inputs):
super(layer3, self).__init__()
## set stuff here
def forward(self, x):
## do stuff to x
return x
class layer2(nn.Module):
def __init__(self, layer, N):
super(layer2, self).__init__()
## set stuff here
def forward(self, x):
## do more stuff to x
return x
As you can see, within model, x is propagated through all the layers by just layer(x), but in this particular example, the outputs are calculated by out = model.forward(input).