Skip to content

MHC Projection Fusion - #4638

Open
muskansh-google wants to merge 4 commits into
AI-Hypercomputer:mainfrom
muskansh-google:mhc-projection-fusion
Open

MHC Projection Fusion#4638
muskansh-google wants to merge 4 commits into
AI-Hypercomputer:mainfrom
muskansh-google:mhc-projection-fusion

Conversation

@muskansh-google

@muskansh-google muskansh-google commented Jul 28, 2026

Copy link
Copy Markdown

Description

This PR optimizes the Manifold-Constrained Hyper Connections (mHC) layer by fusing three independent projection matmuls (pre, post, and residual) into a single unified projection to reduce TPU kernel launch overhead.

Tests

Please describe how you tested this change, and include any instructions and/or
commands to reproduce.

  • Manually tested for a custom model on v6e-256 chips and got a 0.3% imrpovement in TFLOPs
  • Updated test_mhc_lite_doubly_stochastic to work with the updated post-matmul API.
  • Added test_weight_concatenation_equivalence to verify mathematical equivalence between the fused projection and the sequential baseline.

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.

@dandragona dandragona left a comment

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.

LGTM. Thank you!

@muskansh-google
muskansh-google marked this pull request as ready for review July 28, 2026 16:25
@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.

@RissyRan RissyRan left a comment

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.

LGTM, just a minor comment.

Comment thread src/maxtext/layers/mhc.py

def mapping(self, x: Array, alpha_scale: Array, alpha: Array, beta: Array, scale: float, eps: float = 0.0):
"""Helper function for both pre and post mappings."""
# In MaxText, we match weight precision to activations before Matmul

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.

Shall we keep this comment? It should also apply to beta and alpha_scale.

Comment thread src/maxtext/layers/mhc.py

def res_mapping(self, x: Array):
"""Helper function for residual mapping."""
# In MaxText, we match weight precision to activations before Matmul

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.

How we keep this comment?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants