Verify that the ControlFlowInfo of the graph has valid loop structure.
| 39 | |
| 40 | // Verify that the ControlFlowInfo of the graph has valid loop structure. |
| 41 | Status ValidateControlFlowInfo(const Graph* graph, |
| 42 | const std::vector<ControlFlowInfo>& cf_info) { |
| 43 | std::unordered_map<string, Frame> frames; |
| 44 | for (const Node* node : graph->op_nodes()) { |
| 45 | const ControlFlowInfo& cf = cf_info[node->id()]; |
| 46 | if (!cf.frame || !cf.parent_frame) { |
| 47 | // Skip nodes unreachable from the source node. They might be pruned |
| 48 | // later. |
| 49 | continue; |
| 50 | } |
| 51 | |
| 52 | Frame& frame = frames[cf.frame_name]; |
| 53 | Frame* parent = &frames[cf_info[cf.parent_frame->id()].frame_name]; |
| 54 | if (frame.parent == nullptr) { |
| 55 | frame.parent = parent; |
| 56 | frame.name = cf.frame_name; |
| 57 | } else if (frame.parent != parent) { |
| 58 | return errors::Internal( |
| 59 | "Invalid loop structure: Mismatched parent frames for \"", |
| 60 | cf.frame_name, "\": \"", parent->name, "\" vs \"", frame.parent->name, |
| 61 | "\". The node giving this error: ", FormatNodeForError(*node), |
| 62 | ". This is an internal bug, please file a bug report with " |
| 63 | "instructions on how to reproduce the error."); |
| 64 | } |
| 65 | if (IsLoopCond(node)) { |
| 66 | // ForwardLoopCounter runs in the same frame as the forward loop and |
| 67 | // BackPropLoopCounter runs in the same frame as the backprop loop. They |
| 68 | // are the only cases that multiple loops share the same frame. |
| 69 | if (frame.loop_cond && |
| 70 | !absl::StrContains(frame.loop_cond->name(), "LoopCounter") && |
| 71 | !absl::StrContains(node->name(), "LoopCounter")) { |
| 72 | return errors::InvalidArgument( |
| 73 | "Invalid loop structure: Loop \"", cf.frame_name, |
| 74 | "\" has more than one LoopCond node: ", FormatNodeForError(*node), |
| 75 | " and ", FormatNodeForError(*frame.loop_cond), |
| 76 | ". This is an internal bug, please file a bug report with " |
| 77 | "instructions on how to reproduce the error."); |
| 78 | } |
| 79 | frame.loop_cond = node; |
| 80 | } |
| 81 | } |
| 82 | return Status::OK(); |
| 83 | } |
| 84 | } // namespace |
| 85 | |
| 86 | Status BuildControlFlowInfo(const Graph* g, std::vector<ControlFlowInfo>* info, |
no test coverage detected