Add `val` to the current context and its outer context recursively.
(self, val)
| 1635 | return self._pivot_for_pred |
| 1636 | |
| 1637 | def AddValue(self, val): |
| 1638 | """Add `val` to the current context and its outer context recursively.""" |
| 1639 | result = val |
| 1640 | new_value = val.name not in self._values |
| 1641 | # Don't treat ops in this context as new values. Usually all known values |
| 1642 | # are in self._values, except when we're importing a while loop inside this |
| 1643 | # WhileContext. Since there's a cycle in this case, `val` may be part of the |
| 1644 | # imported while loop but not yet processed by this context and added to |
| 1645 | # self._values in _AddOpInternal. We only want to process external input |
| 1646 | # tensors to the while loop here. |
| 1647 | new_value &= val.op._control_flow_context is not self # pylint: disable=protected-access |
| 1648 | if new_value: |
| 1649 | self._values.add(val.name) |
| 1650 | |
| 1651 | # If we are in a grad context and val is from its forward context, |
| 1652 | # use GetRealValue(), which adds the logic to save the history of |
| 1653 | # val in forward. |
| 1654 | grad_ctxt = ops.get_default_graph()._get_control_flow_context() |
| 1655 | if grad_ctxt: |
| 1656 | grad_ctxt = grad_ctxt.GetWhileContext() |
| 1657 | if grad_ctxt.grad_state: |
| 1658 | forward_ctxt = util.GetWhileContext(val.op) |
| 1659 | if util.IsLoopExit(val.op): |
| 1660 | forward_ctxt = forward_ctxt.outer_context |
| 1661 | if forward_ctxt: |
| 1662 | forward_ctxt = forward_ctxt.GetWhileContext() |
| 1663 | if forward_ctxt == grad_ctxt.grad_state.forward_context: |
| 1664 | real_val = grad_ctxt.grad_state.GetRealValue(val) |
| 1665 | self._external_values[val.name] = real_val |
| 1666 | return real_val |
| 1667 | |
| 1668 | if self._outer_context is not None: |
| 1669 | result = self._outer_context.AddValue(val) |
| 1670 | # Create an Enter to make `result` known to this loop context. |
| 1671 | with ops.control_dependencies(None): |
| 1672 | enter = _Enter( |
| 1673 | result, |
| 1674 | self._name, |
| 1675 | is_constant=True, |
| 1676 | parallel_iterations=self._parallel_iterations) |
| 1677 | enter.graph.prevent_feeding(enter) |
| 1678 | if self._outer_context: |
| 1679 | self._outer_context.AddInnerOp(enter.op) |
| 1680 | # Fix the control inputs and control flow context of these enter ops. |
| 1681 | self._FixControlInputsAndContext([enter]) |
| 1682 | |
| 1683 | # Add `enter` in this context. |
| 1684 | self._values.add(enter.name) |
| 1685 | self._external_values[val.name] = enter |
| 1686 | result = enter |
| 1687 | else: |
| 1688 | actual_val = self._external_values.get(val.name) |
| 1689 | if actual_val is not None: |
| 1690 | result = actual_val |
| 1691 | return result |
| 1692 | |
| 1693 | def AddOp(self, op): |
| 1694 | """Add `op` to the current context.""" |
no test coverage detected