What forward actually means
The forward pass is the act of taking input data x through every layer in order and producing output y (logits, probabilities, embeddings — whatever the head returns). On the way, PyTorch records every operation in a computation graph so backward can compute gradients later. This is the only thing your forward() method has to do correctly.
forward() is called automatically when you do model(x). Never call model.forward(x) directly — you would skip the hooks PyTorch uses for things like normalization mode, gradient tracking, and DDP synchronization.
forward(), write the shape of every intermediate tensor next to its line. Future-you (and the next person debugging your model) will thank you.Train mode vs eval mode
Some layers behave differently in training and evaluation: dropout (active in train, identity in eval), batch normalization (uses batch statistics in train, running statistics in eval). Switch with model.train() and model.eval(). Forgetting eval() at validation is a famous bug that produces noisy metrics; we will see it again in the training-loop track.
Stop the gradient when you do not need it
For inference and for any "frozen" backbone in transfer learning, wrap forward calls in torch.no_grad() (or torch.inference_mode() for the latest, fastest path). PyTorch will skip building the computation graph and free memory you would otherwise tie up.
.forward(); switch to eval() for inference; and use inference_mode() when you do not need gradients.