code: def _expand_and_optimize_ir(torchscript):
      """
      Given a torch.jit.ScriptModule, convert it to a optimized
      torch._C.Graph and dict of model parameter's names to tensors.
      """
      graph = torchscript.forward.graph
  
      # From PyTorch code: Inline function and method calls.
      torch._C._jit_pass_inline(graph)
      # From PyTorch code: This inlines the forked section in the fork()
      # callsite and replaces uses of the result of wait() calls with the
      # values produced from the (now-inlined) forked section.
      torch._C._jit_pass_inline_fork_wait(graph)
      # Starting from the return node, marks all nodes that feed into the
      # output, as well as nodes with side effects. Any nodes not marked are
      # eliminated.
      torch._C._jit_pass_dce(graph)
      # From PyTorch code: checks well-formedness and invariants of graph.
      torch._C._jit_pass_lint(graph)
      # Replaces a couple specific ops patterns (add, sub, mul, div, chunk).
      if version_lt(torch, "1.6.0"):
          torch._C._jit_pass_canonicalize_ops(graph)
          torch._C._jit_pass_lint(graph)
  
          # From PyTorch code: This pass catches all of the small, easy to catch
          # peephole optimizations you might be interested in doing.
          #     Eliminate no-op 'expand' nodes
          #     Simplify x.t().t() to x
          # pass disabled for v1.6.0 and onwards, wrongly captures the shape of dummy inputs during tracing.
          torch._C._jit_pass_peephole(graph, addmm_fusion_enabled=False)
      else:
          # v1.6.0 pass renamed
          torch._C._jit_pass_canonicalize_graph_fuser_ops(graph)
      torch._C._jit_pass_lint(graph)
  
      # From PyTorch docs: Renumber the graph so that all structurally
      # equivalent graphs have same numbers.
      graph = torch._C._jit_pass_canonicalize(graph)
      torch._C._jit_pass_lint(graph)
      if version_lt(torch, "1.6.0"):
          # v1.6.0 JIT changes disallows pulling list values out of
          # prim::Constant. We can only pull scalar values. constant
          # propagation removes `listConstruct` and results in list values.
          # We disallow constant prop pass to keep them as scalars, and rely
          # on our own constant prop to interpret `listConstruct`.
          torch._C._jit_pass_constant_propagation(graph)
      # NOTE: Don't need another DCE, it's included in constant propagation.
      torch._C._jit_pass_lint(graph)
  
      # Get the params_dict and rename the getattr nodes in the graph
      graph, params_dict, buffer_dict = _jit_pass_lower_graph(
          graph, torchscript
      )
  
      return graph, params_dict, buffer_dict
  
imports: import torch
  from coremltools._deps import version_lt
  from coremltools.converters.mil.mil import types
label_desc: Use image node_id 2 for calling a global function with example usage: _expand_and_optimize_ir(torchscript) and returns: graph,  params_dict,  buffer_dict