Skip to content

Split broadcast so it is always fused in compile - #2318

Merged
angeloskath merged 2 commits into
mainfrom
compile-broadcast
Jun 27, 2025
Merged

angeloskath merged 2 commits into
mainfrom
compile-broadcast

Conversation

@angeloskath

Copy link
Copy Markdown
Member

What this does becomes clear with an example. For the following function

def f(a, b, c):
    bb = mx.broadcast_to(b, c.shape)
    return (a + bb * c), bb.sum()

the compiled graph changes as follows

broadcast_small

Benchmarking on transformer training the speedup is consistent but minimal (~3%-4%) across M2, M2 Ultra and A100.

Splitting broadcast is safe because it is in principle a noop, but we could consider splitting some copies (astype) and redoing them if it will allow us to fuse more things.

@awni I tried to think if there is a possibility of messing up the graph but I think it is ok since to split a broadcast means we certainly are going to produce a fused primitive.

@angeloskath
angeloskath requested a review from awni June 27, 2025 00:44
@awni

awni commented Jun 27, 2025

Copy link
Copy Markdown
Member

That's a very elegant solution.. I didn't think of it.

@awni awni 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.

🚀

@angeloskath
angeloskath merged commit 2c11d10 into main Jun 27, 2025
@angeloskath
angeloskath deleted the compile-broadcast branch June 27, 2025 05:08
faisalmemon pushed a commit to faisalmemon/mlx that referenced this pull request Oct 30, 2025
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.

2 participants