Complete `ops` so that the transformed graph is valid. Partially copying a graph can lead to a malformed graph. For instance, copying half of a while construct is likely to result in an invalid graph. This function attempts to add missing ops so that the transformation result in a valid gra
(ops, control_ios)
| 673 | |
| 674 | |
| 675 | def _add_control_flow_ops(ops, control_ios): |
| 676 | """Complete `ops` so that the transformed graph is valid. |
| 677 | |
| 678 | Partially copying a graph can lead to a malformed graph. For instance, |
| 679 | copying half of a while construct is likely to result in an invalid graph. |
| 680 | This function attempts to add missing ops so that the transformation result |
| 681 | in a valid graph. |
| 682 | |
| 683 | Args: |
| 684 | ops: list of ops (modifed in-place). |
| 685 | control_ios: object created by a call to `util.ControlOutputs`. |
| 686 | """ |
| 687 | # Find while contexts. |
| 688 | control_flow_contexts = set() |
| 689 | for op in ops: |
| 690 | cfc = op._control_flow_context # pylint: disable=protected-access |
| 691 | if cfc: |
| 692 | control_flow_contexts.add(cfc) |
| 693 | # Find new ops. |
| 694 | new_ops = [] |
| 695 | for cfc in control_flow_contexts: |
| 696 | if cfc.IsWhileContext(): |
| 697 | new_ops += select.get_walks_intersection_ops( |
| 698 | [enter_t.op for enter_t in cfc.loop_enters], |
| 699 | [exit_t.op for exit_t in cfc.loop_exits], |
| 700 | control_ios=control_ios) |
| 701 | # Add new ops. |
| 702 | new_ops_set = set(new_ops) |
| 703 | ops_set = frozenset(ops) |
| 704 | for op in new_ops_set: |
| 705 | if op not in ops_set: |
| 706 | ops.append(op) |
| 707 | |
| 708 | |
| 709 | def graph_replace(target_ts, replacement_ts, dst_scope="", |
no test coverage detected