If you can change the contents of forward method of a layer, you can use self.eval() and with torch.no_grad():
if self.frozen_pretrained_weights:
  self.eval()
    with torch.no_grad():
      output = self.encoders(batch)
  else:
    output = self.encoders(batch)