All nn.Modules have an internal training attribute, which is changed by calling model.train() and model.eval() to switch the behavior of the model, if necessary.
E.g. it is used in dropout layers to disable them during evaluation.
The was_training variable stores the current training state of the model, calls model.eval(), and resets the state at the end using model.train(training=was_training).