Skip to content

Fix cycle error in FusionOptimizer#2171

Closed
jaanerik wants to merge 1 commit into
pymc-devs:mainfrom
jaanerik:fix-fusion-optimizer-cycle
Closed

Fix cycle error in FusionOptimizer#2171
jaanerik wants to merge 1 commit into
pymc-devs:mainfrom
jaanerik:fix-fusion-optimizer-cycle

Conversation

@jaanerik
Copy link
Copy Markdown
Contributor

Fix cycle error in FusionOptimizer by preventing overlapping subgraphs and bitset corruption

Description

This PR addresses a structural regression in FusionOptimizer (introduced in recent ordering changes) where certain complex topologies or multi-output subgraphs could trigger a ValueError: graph contains cycles during the final topological sort of discovered subgraphs.

The Problem:

  1. Overlapping Subgraphs: The discovery loop did not check if a node was already "claimed" by a previously discovered subgraph. This allowed nodes to be shared between subgraphs, creating circular dependencies in the meta-graph of subgraphs.
  2. Bitset Corruption: When a multi-output subgraph was discovered, the logic for updating successor dependencies incorrectly updated nodes within that same subgraph. This caused ancestors to appear as if they depended on their own descendants, corrupting the priority keys used for expansion and leading to non-convex subgraphs.

Related Issue

Checklist

Type of change

  • Bug fix

@ricardoV94
Copy link
Copy Markdown
Member

I can't reproduce the issue and the tests were passing fine on main. Closing this, feel free to comment on the original issue if you have a working failing example

@ricardoV94 ricardoV94 closed this May 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: walk_toposort spuriously raises 'graph contains cycles' on acyclic int-keyed graphs > 256 nodes

2 participants