Skip to content

Commit

Permalink
Fixed a bug where the layout graph was not correctly constructed when…
Browse files Browse the repository at this point in the history
… there is a loop in the graph.

PiperOrigin-RevId: 721509089
  • Loading branch information
Google AI Edge authored and copybara-github committed Jan 30, 2025
1 parent 25988cc commit fc05a1b
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/ui/src/components/visualizer/worker/graph_processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,9 @@ export class GraphProcessor {
generateLayoutGraphConnections(modelGraph: ModelGraph) {
modelGraph.layoutGraphEdges = {};

// Find all op nodes that don't have incoming edges.
const opNodesWithoutIncomingEdges: OpNode[] = [];
// Find all op nodes that don't have incoming edges, or have both incoming
// edges and outgoing edges.
const seedOpNodes: OpNode[] = [];
for (const node of modelGraph.nodes) {
if (!isOpNode(node) || node.hideInLayout) {
continue;
Expand All @@ -376,13 +377,20 @@ export class GraphProcessor {
(edge) =>
!(modelGraph.nodesById[edge.sourceNodeId] as OpNode).hideInLayout,
);
if (filteredIncomingEdges.length === 0) {
opNodesWithoutIncomingEdges.push(node);
const filteredOutgoingEdges = (node.outgoingEdges || []).filter(
(edge) =>
!(modelGraph.nodesById[edge.targetNodeId] as OpNode).hideInLayout,
);
if (
filteredIncomingEdges.length === 0 ||
(filteredIncomingEdges.length > 0 && filteredOutgoingEdges.length > 0)
) {
seedOpNodes.push(node);
}
}

// Do a BFS from opNodesWithoutIncomingEdges.
const queue: OpNode[] = [...opNodesWithoutIncomingEdges];
const queue: OpNode[] = [...seedOpNodes];
const seenNodeIds = new Set<string>();
while (queue.length > 0) {
const curNode = queue.shift();
Expand Down

0 comments on commit fc05a1b

Please sign in to comment.