code: def graph_to_pytorch_model(
      graph_name: str, graph: Graph, placement=None
  ) -> Tuple[set, str]:
      nodes = graph.topo_sort()
  
      # handle module node and function node differently
      # only need to generate code for module here
      import_pkgs = set()
      node_codes = []
      node_python_mappings = {}
      cuda_remapped_id = None
      if placement:
          cuda_remapped_id = generate_cuda_mapping(placement)
      for node in nodes:
          if node.operation:
              if placement and isinstance(node.operation, ToDevice):
                  cuda_remapped_id = cast(dict, cuda_remapped_id)
                  node.operation.override_device_repr(
                      "cuda:%d"
                      % cuda_remapped_id[node.operation.device]
                  )
  
              if node.operation.type == "shared":
                  continue
              pkg_name = cast(
                  PyTorchOperation, node.operation
              ).get_import_pkg()
              if pkg_name is not None:
                  import_pkgs.add(pkg_name)
  
              py_variable_name = _format_variable_name(
                  node.name, graph_name
              )
              node_code = node.operation.to_init_code(py_variable_name)
              if node_code is not None:
                  if (
                      placement
                      and node in placement
                      and len(node_code) > 0
                  ):
                      if isinstance(placement[node], GPUDevice):
                          assert cuda_remapped_id is not None
                          device_repr = (
                              "cuda:%d"
                              % cuda_remapped_id[placement[node]]
                          )
                      else:
                          device_repr = placement[node].device_repr()
                      node_codes.append(
                          f"{node_code}.to('{device_repr}')"
                      )
                  else:
                      node_codes.append(node_code)
  
                  # Map to module hierarchies in original search space python code
                  node_python_mappings[
                      py_variable_name
                  ] = node.python_name
  
      node_codes.append(
          f"self.{STATE_DICT_PY_MAPPING} = {node_python_mappings}"
      )
  
      if graph.input_node.operation.io_names is None:
          input_code = "*_inputs"
      else:
          for name in graph.input_node.operation.io_names:
              assert not name.startswith(graph_name)
          input_code = ", ".join(graph.input_node.operation.io_names)
  
      edge_codes = []
      sorted_nodes = graph.topo_sort()
      for node in sorted_nodes:
          if node.operation:
              inputs, inputs_value = _format_inputs(node, graph_name)
              node_name = _format_variable_name(node.name, graph_name)
              submodule_name = node_name
              if node.operation.type == "shared":
                  submodule_name = _format_variable_name(
                      node.operation.parameters["reference"], graph_name
                  )
              edge_codes.append(
                  node.operation.to_forward_code(
                      submodule_name, node_name, inputs, inputs_value
                  )
              )
  
      output_names, _ = _format_inputs(graph.output_node, graph_name)
      if not output_names:
          raise RuntimeError(
              '"forward" function should have return value(s): {}, {}, {}'.format(
                  output_names, graph_name, graph.output_node
              )
          )
      output_code = ", ".join(output_names)
  
      linebreak = "\n        "
      return import_pkgs, _PyTorchModelTemplate.format(
          graph_name=(
              "Graph" if graph_name == "_graph" else graph_name
          ),
          inputs=input_code,
          outputs=output_code,
          nodes=linebreak.join(node_codes),
          edges=linebreak.join(edge_codes),
      )
  
file_path: nni/nni/nas/space/pytorch/codegen.py
imports: import logging
  import re
  from typing import Dict, List, Tuple, Any, cast
  from nni.common.device import Device, GPUDevice
  from nni.nas.space.graph import IllegalGraphError, Edge, Graph, Node, GraphModelSpace
  from nni.nas.space.graph_op import PyTorchOperation
  from nni.nas.utils import STATE_DICT_PY_MAPPING
  from .op_def import ToDevice
label_desc: Use image node_id 6 for calling a global function with example usage: graph_to_pytorch_model(graph_name, graph, placement) and returns: import_pkgs,  _PyTorchModelTemplate