Skip to content

Mhc optimization: Rewrites einsums for better xla fusion - #4664

Open
h1t35h wants to merge 1 commit into
AI-Hypercomputer:mainfrom
h1t35h:mhc-optimization
Open

Mhc optimization: Rewrites einsums for better xla fusion#4664
h1t35h wants to merge 1 commit into
AI-Hypercomputer:mainfrom
h1t35h:mhc-optimization

Conversation

@h1t35h

@h1t35h h1t35h commented Jul 29, 2026

Copy link
Copy Markdown

Description

XLA optimizations does not seem to gel well with einsum for cases where dimensions are being expanded. Leading to a ~10- 15% performance drop in the current implementation. This change implements expanded einsums with jnp.expand_dims leading to better XLA fusion.

Before (110us):
image
After (97us):
image

Tests

All existing tests passing.

Checklist

Before submitting this PR, please make sure (put X in square brackets):

  • I have performed a self-review of my code. For an optional AI review, add the gemini-review label.
  • I have necessary comments in my code, particularly in hard-to-understand areas.
  • I have run end-to-end tests tests and provided workload links above if applicable.
  • I have made or will make corresponding changes to the doc if needed, including adding new documentation pages to the relevant Table of Contents (toctree directive) as explained in our documentation.

@gemini-code-assist

Copy link
Copy Markdown

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@codecov

codecov Bot commented Jul 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Comment thread src/maxtext/layers/mhc.py
)
layer_input = jnp.einsum("bskd,bsk -> bsd", x, pre_mapping, precision=self.matmul_precision)
# bskd, bsk -> bsd
layer_input = jnp.sum(x * jnp.expand_dims(pre_mapping, axis=3), axis=2)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

for my understanding, do you see perf gain by this change? I thought einsum would be more performant.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yes, even I found it a bit counter-intuitive einsum being more performant is what my understanding was as well. I think while being generally true for some cases like this one it seems better optimization is coming by moving away from it. Specifically, I can see the all-reduce overlap. I tested the changes individually by just changing the einsums and for_iloop but that does not seem to work I'm only seeing the All-Reduce overlap when both the changes are present.

Just for_iloop replaced :
image
Just for_iloop and einsum replaced:
image

I don't have a very solid reasoning behind why of it.

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