code: def _constant_propagation(
      fn, new_graph, constant_nodes, constant_node_num_outputs
  ):
      try:
          if len(constant_nodes) > 0:
              with tf.Graph().as_default() as graph:
                  tf.import_graph_def(new_graph, name="")
  
                  # We're only making one call to `sess.run()` in order to compute constant values.
                  # In this context, the default optimization settings make everything dramatically
                  # slower and more memory-intensive.
                  if _get_version(tf.__version__) < _StrictVersion(
                      "1.13.1"
                  ):
                      session_config = tf.ConfigProto()
                      session_config.graph_options.optimizer_options.opt_level = (
                          tf.OptimizerOptions.L0
                      )
                      sess = tf.Session(
                          graph=graph, config=session_config
                      )
                  else:
                      session_config = tf.compat.v1.ConfigProto()
                      session_config.graph_options.optimizer_options.opt_level = (
                          tf.compat.v1.OptimizerOptions.L0
                      )
                      session_config.graph_options.rewrite_options.disable_meta_optimizer = (
                          True
                      )
                      sess = tf.compat.v1.Session(
                          graph=graph, config=session_config
                      )
  
                  query_list = list()
                  control_flow_ops = list()
                  for c in constant_nodes:
                      for j in range(constant_node_num_outputs[c]):
                          query = c + ":" + str(j)
                          lower_query = query.lower()
                          if (
                              "switch" in lower_query
                              or "cond" in lower_query
                          ):
                              control_flow_ops.append(query)
                          else:
                              query_list.append(query)
                  result_list = sess.run(query_list)
                  result = {
                      query_list[i]: result_list[i]
                      for i in range(len(query_list))
                  }
                  # propagate switch one by one
                  for op in control_flow_ops:
                      try:
                          res = sess.run([op])
                          result.update({op: res[0]})
                      except:
                          logger.warning(
                              '[Constant Propagation] Skip "dead" tensor: {}'.format(
                                  op
                              )
                          )
                          result.update({op: None})
  
                  sess.close()
  
              for k, v in fn.graph.items():
                  if k in constant_node_num_outputs:
                      if constant_node_num_outputs[k] == 1:
                          result_entry = k + ":0"
                          try:
                              (
                                  v.value,
                                  v.datatype,
                              ) = numpy_val_to_builtin_val(
                                  result[result_entry]
                              )
                          except:
                              logger.error(result_entry)
                              logger.error(result[result_entry])
                      else:
                          values = [
                              result[k + ":" + str(i)]
                              for i in range(
                                  constant_node_num_outputs[k]
                              )
                          ]
                          try:
                              npval = [
                                  numpy_val_to_builtin_val(i)
                                  for i in values
                              ]
                              v.datatype = types.tuple(
                                  tuple([val[1] for val in npval])
                              )
                              v.value = v.datatype()
                              for idx, val in enumerate(npval):
                                  v.value.val[idx] = val[0]
                          except:
                              logger.error(values)
              for k, v in fn.graph.items():
                  if v.op == "get_tuple":
                      inp = fn.graph[v.inputs[0]]
                      idx = v.attr["index"]
                      if inp.value is not None:
                          v.value = inp.value.val[idx]
                          v.datatype = inp.datatype.T[idx]
  
      except Exception as e:
          logger.exception(
              "Constant Propagation pass failed: {}".format(e)
          )
  
file_path: coremltools/coremltools/converters/mil/frontend/tensorflow/tf_graph_pass/constant_propagation.py
imports: import gc
  from distutils.version import StrictVersion
  import tensorflow
  from coremltools import _logger
  from coremltools._deps import _get_version
  from coremltools.converters.mil.mil import types
  from coremltools.converters.mil.mil.types.type_mapping import numpy_val_to_builtin_val
  from ..basic_graph_ops import const_determined_nodes
label_desc: Use image node_id 2 for calling a global function with example usage: _constant_propagation(fn, new_graph, constant_nodes, constant_node_num_outputs) without return types