def forward(self, input):
              self.modified_tensor += 1
              return input + self.modified_tensor
  scripted_module = torch.jit.script(MyModule2().eval())
  frozen_module = torch.jit.freeze(scripted_module, preserved_attrs=["version"])
  # we've manually preserved `version`, so it still exists on the frozen module and can be modified
  assert frozen_module.version == 1
  frozen_module.version = 2
  # `modified_tensor` is detected as being mutated in the forward, so freezing preserves
  # it to retain model semantics
  assert frozen_module(torch.tensor(1)) == torch.tensor(12)
  # now that we've run it once, the next result will be incremented by one
  assert frozen_module(torch.tensor(1)) == torch.tensor(13)

Note:
 Freezing submodule attributes is also supported: frozen_module =
 torch.jit.freeze(scripted_module,
 preserved_attrs=["submodule.version"])

Note: