class_docstr: The state used for constructing the gradient graph for a while loop.
  
  We create a _GradLoopState for each while loop in forward and its
  corresponding while loop in backprop. This gives us access to both
  the forward and the backprop WhileContexts.
  
  During the construction of gradient graph, any time when we detect
  a forward value that is needed for backprop, we create a history
  accumulator and add it to `history_map`. Any time when we backprop
  a loop switch op (in _SwitchGrad), we add the grad merge op in
  `switch_map`.
class_docstr_tok: ["The","state","used","for","constructing","the","gradient","graph","for","a","while","loop",".","We","create","a","_GradLoopState","for","each","while","loop","in","forward","and","its","corresponding","while","loop","in","backprop",".","This","gives","us","access","to","both","the","forward","and","the","backprop","WhileContexts",".","During","the","construction","of","gradient","graph",",","any","time","when","we","detect","a","forward","value","that","is","needed","for","backprop",",","we","create","a","history","accumulator","and","add","it","to","`","history_map","`",".","Any","time","when","we","backprop","a","loop","switch","op","(","in","_SwitchGrad",")",",","we","add","the","grad","merge","op","in","`","switch_map","`","."]
code: def __init__(self, forward_ctxt, outer_grad_state):
      # The grad loop state for the outer while loop.
      self._outer_grad_state = None
  
      # The while loop context for forward.
      self._forward_context = None
  
      # The loop counter added by AddForwardLoopCounter. It is the value
      # of the loop counter for the next iteration.
      self._forward_index = None
  
      # A sync op for forward.
      self._forward_sync = None
  
      # The while loop context for backprop.
      self._grad_context = None
  
      # The loop counter added by AddBackpropLoopCounter. It is the value
      # of the loop counter for the current iteration.
      self._grad_index = None
  
      # A sync op for backprop.
      self._grad_sync = None
  
      # Information needed by backprop.
      self._history_map = {}
      self._switch_map = {}
      self._unused_exits = []
      self._deferred_exits = []
      self._forward_loop_exits = list(forward_ctxt.loop_exits)
      self._pending_exits_count = len(forward_ctxt.loop_exits)
  
      self._outer_grad_state = outer_grad_state
      if outer_grad_state:
          outer_forward_ctxt = outer_grad_state.forward_context
      else:
          if not hasattr(forward_ctxt, "outer_context"):
              raise ValueError(
                  "Failed to call gradients on a while loop without"
                  "properly serializing graph via MetaGraphDef"
              )
          outer_forward_ctxt = forward_ctxt.outer_context
  
      # Add the forward loop counter.
      with forward_ctxt._graph.as_default():  # pylint: disable=protected-access
          if outer_forward_ctxt:
              outer_forward_ctxt.Enter()
          cnt, forward_index = forward_ctxt.AddForwardLoopCounter(
              outer_grad_state
          )
          if outer_forward_ctxt:
              outer_forward_ctxt.Exit()
      self._forward_context = forward_ctxt
      self._forward_index = forward_index
  
      # Add the backprop WhileContext, and the backprop loop counter.
      if outer_grad_state:
          # This is a nested loop. Remember the iteration counts for each
          # execution of this inner loop.
          outer_forward_ctxt.AddName(cnt.name)
          history_cnt = outer_grad_state.AddForwardAccumulator(cnt)
  
          outer_grad_ctxt = outer_grad_state.grad_context
          outer_grad_ctxt.Enter()
          self._grad_context = control_flow_ops.WhileContext(
              maximum_iterations=forward_ctxt.maximum_iterations,
              parallel_iterations=forward_ctxt.parallel_iterations,
              back_prop=forward_ctxt.back_prop,
              swap_memory=forward_ctxt.swap_memory,
              name=forward_ctxt.name,
              grad_state=self,
          )
          real_cnt = outer_grad_state.AddBackpropAccumulatedValue(
              history_cnt, cnt
          )
          self._grad_index = self._grad_context.AddBackpropLoopCounter(
              real_cnt, outer_grad_state
          )
          outer_grad_ctxt.Exit()
      else:
          if outer_forward_ctxt:
              outer_forward_ctxt.Enter()
          self._grad_context = control_flow_ops.WhileContext(
              maximum_iterations=forward_ctxt.maximum_iterations,
              parallel_iterations=forward_ctxt.parallel_iterations,
              back_prop=forward_ctxt.back_prop,
              swap_memory=forward_ctxt.swap_memory,
              name=forward_ctxt.name,
              grad_state=self,
          )
          self._grad_index = self._grad_context.AddBackpropLoopCounter(
              cnt, outer_grad_state
          )
          if outer_forward_ctxt:
              outer_forward_ctxt.Exit()
  
imports: from tensorflow.python.framework import constant_op
  from tensorflow.python.framework import dtypes
  from tensorflow.python.framework import ops
  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_v2_func_graphs
  from tensorflow.python.ops import default_gradient
  from tensorflow.python.ops import gen_data_flow_ops
  from tensorflow.python.ops import gen_resource_variable_ops
  from tensorflow.python.ops import resource_variable_ops
label_desc: Use image node_id 1 to create a new _GradLoopState object  with example: obj = _GradLoopState(forward_ctxt, outer_grad_state)