Example #1
0
 def __del__(self):
     if func_graph_module is None or memory is None:
         return
     try:
         while self._cache:
             self._cache.popitem()
         memory.dismantle_ordered_dict(self._cache)
     except:  # pylint: disable=bare-except
         pass
Example #2
0
 def clear_captures(self):
   # TODO(b/115366440): Delete this method when a custom OrderedDict is added.
   # Clearing captures using clear() leaves some cycles around.
   while self._captures:
     self._captures.popitem()
   memory.dismantle_ordered_dict(self._captures)
   while self._deferred_captures:
     self._deferred_captures.popitem()
   memory.dismantle_ordered_dict(self._deferred_captures)
Example #3
0
def dismantle_func_graph(func_graph):
    """Removes reference cycles in `func_graph` FuncGraph.

  Helpful for making sure the garbage collector doesn't need to run when
  the FuncGraph goes out of scope, e.g. in tests using defun with
  @test_util.run_in_graph_and_eager_modes(assert_no_eager_garbage=True).

  Args:
    func_graph: A `FuncGraph` object to destroy. `func_graph` is unusable
      after this function.
  """
    # TODO(b/115366440): Delete this method when a custom OrderedDict is added.
    # Clearing captures using clear() leaves some cycles around.
    while func_graph.captures:
        func_graph.captures.popitem()
    memory.dismantle_ordered_dict(func_graph.captures)
    ops.dismantle_graph(func_graph)
Example #4
0
def dismantle_func_graph(func_graph):
  """Removes reference cycles in `func_graph` FuncGraph.

  Helpful for making sure the garbage collector doesn't need to run when
  the FuncGraph goes out of scope, e.g. in tests using defun with
  @test_util.run_in_graph_and_eager_modes(assert_no_eager_garbage=True).

  Args:
    func_graph: A `FuncGraph` object to destroy. `func_graph` is unusable
      after this function.
  """
  # TODO(b/115366440): Delete this method when a custom OrderedDict is added.
  # Clearing captures using clear() leaves some cycles around.
  while func_graph.captures:
    func_graph.captures.popitem()
  memory.dismantle_ordered_dict(func_graph.captures)
  ops.dismantle_graph(func_graph)