code: def _get_intermediates(func_graph):
      """Returns all tensors in `func_graph` that should be accumulated."""
      # We currently accumulate output tensors of most ops in the function and rely
      # on the pruning pass to get rid of the unused accumulators at runtime.
      # However, this can bloat the GraphDef and make debugging harder so we perform
      # some optimizations.
      #
      # Optimization we currently perform:
      # 1. We do not accumulate tensors which already have an accumulator
      #    in the loop body.
      # 2. We do not accumulate outputs of Identity nodes. When building the
      #    FuncGraph, we add an Identity node for each output (see
      #    `AutomaticControlDependencies.mark_as_return`). Accumulating outputs
      #    of all these nodes bloats the GraphDef quite a bit so we remove those.
      #    Since the gradient of an Identity node does not rely on its forward op's
      #    input this is safe to do.
      #
      # Other possible optimizations:
      # 1. Only accumulate tensors that will be required by the backward pass.
      #    This will require running the gradient pass and hence would increase the
      #    graph building time for the forward pass.
      # 2. Do not accumulate Const nodes created inside the loop body.
      # 3. Do not accumulate loop vars that are returned as-is just like captured
      #    tensors.
      intermediates = []
      reverse_captures = dict(
          (v.ref(), k) for k, v in func_graph.captures
      )
  
      for op in func_graph.get_operations():
          if op.type == "Identity":
              continue
          # Accumulating mutexes can cause deadlock.
          if op.type == "MutexLock":
              continue
          for o in op.outputs:
              if (
                  o is not func_graph.inputs[0]
                  and o.dtype != dtypes.resource  # Loop counter.
                  and _get_accumulator(  # Do not accumulate resource tensors.
                      o
                  )
                  is None
                  and o.ref()  # Has existing accumulator.
                  not in reverse_captures
              ):  # Captured value, hence loop invariant.
                  intermediates.append(o)
      return intermediates
  
imports: import collections
  from tensorflow.core.framework import attr_value_pb2
  from tensorflow.python.client import pywrap_tf_session
  from tensorflow.python.eager import backprop_util
  from tensorflow.python.framework import auto_control_deps_utils
  from tensorflow.python.framework import constant_op
  from tensorflow.python.framework import dtypes
  from tensorflow.python.framework import func_graph
  from tensorflow.python.framework import indexed_slices
  from tensorflow.python.framework import ops
  from tensorflow.python.framework import tensor
  from tensorflow.python.framework import tensor_shape
  from tensorflow.python.framework import tensor_spec
  from tensorflow.python.framework import tensor_util
  from tensorflow.python.ops import array_ops
  from tensorflow.python.ops import control_flow_ops
  from tensorflow.python.ops import control_flow_util
  from tensorflow.python.ops import control_flow_util_v2
  from tensorflow.python.ops import default_gradient
  from tensorflow.python.ops import gen_functional_ops
  from tensorflow.python.ops import gen_resource_variable_ops
  from tensorflow.python.ops import gradients_util
  from tensorflow.python.ops import handle_data_util
  from tensorflow.python.ops import list_ops
  from tensorflow.python.ops import math_ops
  from tensorflow.python.ops import tensor_array_ops
  from tensorflow.python.ops import while_v2_indexed_slices_rewriter
  from tensorflow.python.util import compat
  from tensorflow.python.util import nest
  from tensorflow.python.util import object_identity
  from tensorflow.python.util import variable_utils
label_desc: Use image node_id 4 for calling a global function with example usage: _get_intermediates(func_graph) and returns: intermediates