class_docstr: Tests for compatibility with FX.
class_docstr_tok: ["Tests","for","compatibility","with","FX","."]
code: def test_package_fx_with_imports(self):
      import package_a.subpackage
  
      # Manually construct a graph that invokes a leaf function
      graph = Graph()
      a = graph.placeholder("x")
      b = graph.placeholder("y")
      c = graph.call_function(
          package_a.subpackage.leaf_function, (a, b)
      )
      d = graph.call_function(torch.sin, (c,))
      graph.output(d)
      gm = GraphModule(torch.nn.Module(), graph)
  
      f = BytesIO()
      with PackageExporter(f) as pe:
          pe.intern("**")
          pe.save_pickle("model", "model.pkl", gm)
      f.seek(0)
  
      pi = PackageImporter(f)
      loaded_gm = pi.load_pickle("model", "model.pkl")
      input_x = torch.rand(2, 3)
      input_y = torch.rand(2, 3)
  
      self.assertTrue(
          torch.allclose(
              loaded_gm(input_x, input_y), gm(input_x, input_y)
          )
      )
  
      # Check that the packaged version of the leaf_function dependency is
      # not the same as in the outer env.
      packaged_dependency = pi.import_module("package_a.subpackage")
      self.assertTrue(packaged_dependency is not package_a.subpackage)
  
file_path: pytorch/test/package/test_package_fx.py
imports: from io import BytesIO
  import torch
  from torch.fx import Graph, GraphModule, symbolic_trace
  from torch.package import ObjMismatchError, PackageExporter, PackageImporter, sys_importer
  from torch.testing._internal.common_utils import run_tests
label_desc: Use image node_id 4 for calling the TestPackageFX obj's underlying member method code with example usage: obj.test_package_fx_with_imports() without return types