Add optimized Pallas kernel for mHC - #4649
Draft
dandragona wants to merge 3 commits into
Draft
Conversation
dandragona
requested review from
NuojCheng,
RissyRan,
bvandermoon,
gagika,
gobbleturk,
jiangjy1982,
parambole,
richjames0,
shralex,
shuningjin and
suexu1025
as code owners
July 28, 2026 21:34
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
dandragona
requested review from
A9isha,
SurbhiJainUSC,
abhinavclemson,
aireenmei,
darisoy,
dipannita08,
hengtaoguo,
huytransformer,
igorts-git,
khatwanimohit,
vipannalla and
xibinliu
as code owners
July 28, 2026 21:34
dandragona
marked this pull request as draft
July 28, 2026 21:34
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds mHC Pallas Kernel to MaxText: https://xprof.corp.google.com/trace_viewer/dandragona-2944179191919190860?view_start=59318.405&view_end=59445.703
Key changes:
src/maxtext/kernels/residual/mhc_common.py(shared operations)src/maxtext/kernels/residual/mhc_fwd_kernels.py(forward kernels & wrapper)src/maxtext/kernels/residual/mhc_bwd_kernels.py(backward kernels & wrapper)src/maxtext/layers/mhc.py) controlled by the newenable_mhc_pallas_kernelconfig flag.REQUIRES: TP = 1
mHC Pallas Kernels Architecture & Flow
The Manifold-Constrained Hyper Connections (mHC) algorithm introduces a structured residual pathway that projects activations before and after applying a layer (like Attention or MLP) to stabilize training in deep networks.
Because mHC computes dynamic projection coefficients on the fly based on the activations, a native JAX implementation would require materializing large intermediate tensors (such as sequence-length dimension matrices), causing high memory usage (OOM) and latency.
The custom TPU Pallas kernels optimize this by fusing operations along the sequence dimension and computing gradients manually via custom VJP.
1. Forward Pass Flow
The forward pass is split into three main steps:
_coeff_fwd):X(shape[T, K, D]) whereTis batch * seq,Kis the number of heads/branches, andDis the feature dimension.Xto compute pre-coefficientsH_pre, post-coefficientsH_post, and the residual projection matrixres_M.mhc_coeffs._pre_apply_fwd):H_preto the activationsXto get the input to the layer:layer_in = sum(H_pre * X)._post_apply_fwd):X, the layer outputlayer_out,H_post, and the residual matrixres_M.Y = res_M * X + H_post * layer_out.2. Backward Pass Flow (VJP)
The backward pass is implemented via custom VJP (
_mhc_pallas_vjp_bwd_impl) to avoid as autograd did not work natively:_post_apply_bwdcomputes the gradients for activations:dx(usingres_Mandd_out) anddlo(usingH_postandd_out)._pre_apply_bwd):XandH_prefrom the layer input gradientd_layer_in._coeff_bwd):dH_pre,dH_post, anddres_Mback to the inputXand all mHC projection parameters (phi) usingjax.vjponmhc_coeffs.Tests
tests/unit/mhc_kernel_test.py.pre-commit) pass locally.Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.