class_docstr: Tests for compatibility with TorchScript.
class_docstr_tok: ["Tests","for","compatibility","with","TorchScript","."]
docstr: Test tensors shared across eager and ScriptModules are serialized once.
docstr_tok: ["Test","tensors","shared","across","eager","and","ScriptModules","are","serialized","once","."]
code: def test_save_shared_tensors(self):
      """
      Test tensors shared across eager and ScriptModules are serialized once.
      """
      from package_a.test_module import (
          ModWithSubmodAndTensor,
          ModWithTensor,
      )
  
      shared_tensor = torch.rand(2, 3, 4)
      scripted_mod = torch.jit.script(ModWithTensor(shared_tensor))
  
      mod1 = ModWithSubmodAndTensor(shared_tensor, scripted_mod)
      mod2 = ModWithSubmodAndTensor(shared_tensor, scripted_mod)
  
      buffer = BytesIO()
      with PackageExporter(buffer) as e:
          e.intern("**")
          e.save_pickle("res", "tensor", shared_tensor)
          e.save_pickle("res", "mod1.pkl", mod1)
          e.save_pickle("res", "mod2.pkl", mod2)
  
      buffer.seek(0)
      importer = PackageImporter(buffer)
      loaded_mod_1 = importer.load_pickle("res", "mod1.pkl")
  
      # assert that there is only one storage stored in package
      file_structure = importer.file_structure(
          include=".data/*.storage"
      )
      self.assertTrue(
          len(file_structure.children[".data"].children) == 1
      )
  
      input = torch.rand(2, 3, 4)
      self.assertEqual(loaded_mod_1(input), mod1(input))
  
imports: from io import BytesIO
  from textwrap import dedent
  from unittest import skipIf
  import torch
  from torch.package import PackageExporter, PackageImporter
  from torch.testing._internal.common_utils import IS_FBCODE, IS_SANDCASTLE, run_tests
label_desc: Use image node_id 17 for calling the TestPackageScript obj's underlying member method code with example usage: obj.test_save_shared_tensors() without return types