code: def export_model(opt: Opt):
      """
      Export a model to TorchScript so that inference can be run outside of ParlAI.
      """
  
      if version.parse(torch.__version__) < version.parse("1.7.0"):
          raise NotImplementedError(
              "TorchScript export is only supported for Torch 1.7 and higher!"
          )
      else:
          # Only load TorchScriptGreedySearch now, because this will trigger scripting of
          # associated modules
          from parlai.torchscript.modules import TorchScriptGreedySearch
  
      overrides = {
          "model_parallel": False  # model_parallel is not currently supported when TorchScripting,
      }
      if opt.get("script_module"):
          script_module_name, script_class_name = opt[
              "script_module"
          ].split(":", 1)
          script_module = importlib.import_module(script_module_name)
          script_class = getattr(script_module, script_class_name)
      else:
          script_class = TorchScriptGreedySearch
      if "override" not in opt:
          opt["override"] = {}
      for k, v in overrides.items():
          opt[k] = v
          opt["override"][k] = v
  
      # Create the unscripted greedy-search module
      agent = create_agent(opt, requireModelExists=True)
      original_module = script_class(agent)
  
      # Script the module and save
      instantiated = script_class(agent)
      if not opt["no_cuda"]:
          instantiated = instantiated.cuda()
      if opt.get("enable_inference_optimizations"):
          scripted_model = torch.jit.optimize_for_inference(
              torch.jit.script(instantiated.eval())
          )
      else:
          scripted_model = torch.jit.script(instantiated)
  
      with PathManager.open(opt["scripted_model_file"], "wb") as f:
          torch.jit.save(scripted_model, f)
  
      # Compare the original module to the scripted module against the test inputs
      if len(opt["input"]) > 0:
          inputs = opt["input"].split("|")
          print("\nGenerating given the original unscripted module:")
          _run_conversation(module=original_module, inputs=inputs)
          print("\nGenerating given the scripted module:")
          _run_conversation(module=scripted_model, inputs=inputs)
  
file_path: ParlAI/parlai/scripts/torchscript.py
imports: import importlib
  from typing import List
  import torch.jit
  import torch.nn
  from packaging import version
  from parlai.core.agents import create_agent
  from parlai.core.opt import Opt
  from parlai.core.params import ParlaiParser
  from parlai.core.script import ParlaiScript, register_script
  from parlai.utils.io import PathManager
label_desc: Use image node_id 1 for calling a global function with example usage: export_model(opt) without return types