Skip to content

Fix compile_fuse broadcast split aliasing bug - #3166

Merged
angeloskath merged 2 commits into
ml-explore:mainfrom
robert-johansson:fix/compile-fuse-broadcast-split
Feb 27, 2026
Merged

angeloskath merged 2 commits into
ml-explore:mainfrom
robert-johansson:fix/compile-fuse-broadcast-split

Conversation

@robert-johansson

Copy link
Copy Markdown
Contributor

Summary

Fixes #3165 — mx.compile crashes with unordered_map::at: key not found on computation graphs with >~100 operations.

  • Root cause: split_one modifies a parent's input vector, silently replacing the const array& a reference in the recurse lambda (which is aliased through that vector). When the split copy fails to fuse, the original broadcast is orphaned — never added to input_set.
  • Fix: Save array orig(a) before split_one. If the copy isn't fused, undo with merge_one and use the original as an input. Preserves the broadcast fusion optimization from Split broadcast so it is always fused in compile #2318.
  • Test: Added test_compile_large_graph_with_broadcasts — builds a ~150-op graph (Gaussian scoring with 15 observations), verifies both cached execution and correctness vs uncompiled.

Test plan

  • New test test_compile_large_graph_with_broadcasts passes
  • Existing test_shared_broadcast still passes (optimization preserved)
  • Verified N=3 through N=100 all pass (previously failed at N=9+)

🤖 Generated with Claude Code

Robert Johansson and others added 2 commits February 24, 2026 23:24
split_one creates a copy of a broadcast array and moves cache parents
to point to the copy by modifying their input vectors. However, the
array reference `a` in the recurse lambda is aliased through a parent's
input vector. When split_one modifies that parent's input, `a` silently
becomes the copy, losing the reference to the original.

When the split copy then fails to be fused (e.g. hitting
max_compile_depth or max_compile_arrays), no code path adds the
original to input_set, so compile_replace crashes with
unordered_map::at on the second (cached) call.

Fix: save the original array before calling split_one. If the split
copy fails to be fused, use merge_one to undo the split and treat the
original as an input. This preserves the broadcast fusion optimization
from commit 2c11d10 while fixing the crash.

Fixes ml-explore#3165

@angeloskath angeloskath left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for finding the bug. I simplified the test and solution and will merge after the tests pass.

@angeloskath
angeloskath merged commit c8536f5 into ml-explore:main Feb 27, 2026
16 checks passed
@robert-johansson
robert-johansson deleted the fix/compile-fuse-broadcast-split branch July 27, 2026 14:11
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.

compile_fuse crash: unordered_map::at on graphs with >~100 ops

2 participants