Verify that a branch's tensor is not accessed in another branch fn.
(op_type, branch_graphs)
| 750 | |
| 751 | |
| 752 | def verify_captures(op_type, branch_graphs): |
| 753 | """Verify that a branch's tensor is not accessed in another branch fn.""" |
| 754 | # Note: It is technically not possible for lower-branch_index branches to |
| 755 | # capture tensors from higher-branch_index branches, because of the order of |
| 756 | # branch graph construction, but we check all for completeness and to |
| 757 | # guard against potential future changes. |
| 758 | other_branch_graphs = {g: i for i, g in enumerate(branch_graphs)} |
| 759 | for i, branch_graph in enumerate(branch_graphs): |
| 760 | for t in branch_graph.external_captures: |
| 761 | if not isinstance(t, ops.EagerTensor) and t.graph in other_branch_graphs: |
| 762 | branch_names = ["true_fn", "false_fn"] if op_type == _COND else [ |
| 763 | "branch {}".format(bi) for bi in range(len(branch_graphs))] |
| 764 | raise ValueError( |
| 765 | "Tensor {tname} in {b0name} is accessed from {b1name}.".format( |
| 766 | tname=t.name, |
| 767 | b0name=branch_names[other_branch_graphs[t.graph]], |
| 768 | b1name=branch_names[i])) |
| 769 | |
| 770 | |
| 771 | class _CondGradFuncGraph(util.CondBranchFuncGraph): |
no test coverage detected