code: def test_mod_export():
      def verify_cpu_export(obj_format):
          mod, params = relay.testing.synthetic.get_workload()
          with relay.build_config(opt_level=3):
              complied_graph_lib = relay.build_module.build(
                  mod, "llvm", params=params
              )
  
          from tvm.contrib import utils
  
          temp = utils.tempdir()
          if obj_format == ".so":
              file_name = "deploy_lib.so"
          else:
              assert obj_format == ".tar"
              file_name = "deploy_lib.tar"
          path_lib = temp.relpath(file_name)
          complied_graph_lib.export_library(path_lib)
  
          # run the setup in a separate function, so the load_lib
          # can get destructed right away
          # test the robustness wrt to parent module destruction
          def setup_gmod():
              loaded_lib = tvm.runtime.load_module(path_lib)
              dev = tvm.cpu(0)
              return loaded_lib["default"](dev)
  
          gmod = setup_gmod()
          # raw api
          set_input = gmod["set_input"]
          run = gmod["run"]
          get_output = gmod["get_output"]
          data = np.random.uniform(-1, 1, size=input_shape(mod)).astype(
              "float32"
          )
          set_input("data", tvm.nd.array(data))
          run()
          out = get_output(0).numpy()
          tvm.testing.assert_allclose(out, verify(data), atol=1e-5)
  
          # graph executor wrapper
          gmod = graph_executor.GraphModule(setup_gmod())
          gmod.set_input("data", data)
          gmod.run()
          out = gmod.get_output(0).numpy()
          tvm.testing.assert_allclose(out, verify(data), atol=1e-5)
  
      def verify_gpu_export(obj_format):
          if not tvm.testing.device_enabled("cuda"):
              print("Skip because cuda is not enabled")
              return
          mod, params = relay.testing.synthetic.get_workload()
          with relay.build_config(opt_level=3):
              complied_graph_lib = relay.build_module.build(
                  mod, "cuda", params=params
              )
  
          from tvm.contrib import utils
  
          temp = utils.tempdir()
          if obj_format == ".so":
              file_name = "deploy_lib.so"
          else:
              assert obj_format == ".tar"
              file_name = "deploy_lib.tar"
          path_lib = temp.relpath(file_name)
          complied_graph_lib.export_library(path_lib)
  
          data = np.random.uniform(-1, 1, size=input_shape(mod)).astype(
              "float32"
          )
  
          # run the setup in a separate function, so the load_lib
          # can get destructed right away
          # test the robustness wrt to parent module destruction
          def setup_gmod():
              loaded_lib = tvm.runtime.load_module(path_lib)
              dev = tvm.cuda()
              return loaded_lib["default"](dev)
  
          gmod = setup_gmod()
          # raw api
          set_input = gmod["set_input"]
          run = gmod["run"]
          get_output = gmod["get_output"]
          set_input("data", tvm.nd.array(data))
          run()
          out = get_output(0).numpy()
          tvm.testing.assert_allclose(out, verify(data), atol=1e-5)
  
          # graph executor wrapper
          gmod = graph_executor.GraphModule(setup_gmod())
          gmod.set_input("data", data)
          gmod.run()
          out = gmod.get_output(0).numpy()
          tvm.testing.assert_allclose(out, verify(data), atol=1e-5)
  
      @tvm.testing.requires_llvm
      def verify_rpc_cpu_export(obj_format):
          mod, params = relay.testing.synthetic.get_workload()
          with relay.build_config(opt_level=3):
              complied_graph_lib = relay.build_module.build(
                  mod, "llvm", params=params
              )
  
          from tvm.contrib import utils
  
          temp = utils.tempdir()
          if obj_format == ".so":
              file_name = "deploy_lib.so"
          else:
              assert obj_format == ".tar"
              file_name = "deploy_lib.tar"
          path_lib = temp.relpath(file_name)
          complied_graph_lib.export_library(path_lib)
  
          from tvm import rpc
  
          remote = rpc.LocalSession()
          remote.upload(path_lib)
          loaded_lib = remote.load_module(path_lib)
          data = np.random.uniform(-1, 1, size=input_shape(mod)).astype(
              "float32"
          )
          dev = remote.cpu()
  
          # raw api
          gmod = loaded_lib["default"](dev)
          set_input = gmod["set_input"]
          run = gmod["run"]
          get_output = gmod["get_output"]
          set_input("data", tvm.nd.array(data, device=dev))
          run()
          out = get_output(0).numpy()
          tvm.testing.assert_allclose(out, verify(data), atol=1e-5)
  
          # graph executor wrapper
          gmod = graph_executor.GraphModule(loaded_lib["default"](dev))
          gmod.set_input("data", data)
          gmod.run()
          out = gmod.get_output(0).numpy()
          tvm.testing.assert_allclose(out, verify(data), atol=1e-5)
  
      def verify_rpc_gpu_export(obj_format):
          if not tvm.testing.device_enabled("cuda"):
              print("Skip because cuda is not enabled")
              return
          mod, params = relay.testing.synthetic.get_workload()
          with relay.build_config(opt_level=3):
              complied_graph_lib = relay.build_module.build(
                  mod, "cuda", params=params
              )
  
          from tvm.contrib import utils
  
          temp = utils.tempdir()
          if obj_format == ".so":
              file_name = "deploy_lib.so"
          else:
              assert obj_format == ".tar"
              file_name = "deploy_lib.tar"
          path_lib = temp.relpath(file_name)
          complied_graph_lib.export_library(path_lib)
  
          from tvm import rpc
  
          def check_remote(server):
              remote = rpc.connect(server.host, server.port)
              remote.upload(path_lib)
              loaded_lib = remote.load_module(path_lib)
              data = np.random.uniform(
                  -1, 1, size=input_shape(mod)
              ).astype("float32")
              dev = remote.cuda()
  
              # raw api
              gmod = loaded_lib["default"](dev)
              set_input = gmod["set_input"]
              run = gmod["run"]
              get_output = gmod["get_output"]
              set_input("data", tvm.nd.array(data, device=dev))
              run()
              out = get_output(0).numpy()
              tvm.testing.assert_allclose(out, verify(data), atol=1e-5)
  
              # graph executor wrapper
              gmod = graph_executor.GraphModule(
                  loaded_lib["default"](dev)
              )
              gmod.set_input("data", data)
              gmod.run()
              out = gmod.get_output(0).numpy()
              tvm.testing.assert_allclose(out, verify(data), atol=1e-5)
  
          check_remote(rpc.Server("127.0.0.1"))
  
      for obj_format in [".so", ".tar"]:
          verify_cpu_export(obj_format)
          verify_gpu_export(obj_format)
          verify_rpc_cpu_export(obj_format)
          verify_rpc_gpu_export(obj_format)
  
imports: import numpy
  import os
  from tvm import relay, runtime
  from tvm.relay import testing
  import tvm
  from tvm.contrib import graph_executor
  from tvm.contrib.debugger import debug_executor
  from tvm.contrib.cuda_graph import cuda_graph_executor
  import tvm.testing
label_desc: Use image node_id 9 for calling a global function with example usage: test_mod_export() and returns: loaded_lib, loaded_lib