It depends of course on your implementation, but you could use the linked training argument as:
model = models.resnet18()
model.fc.register_forward_hook(lambda m, inp, out: F.dropout(out, p=0.5, training=m.training))