Fix triangle_attention custom_vjp fwd/primal pytree mismatch (tuple vs list) - #295
Open
ssiddhantsharma wants to merge 1 commit into
Open
Conversation
…s list) `triangle_attention_custom_vjp` returns `fwd_p.bind(...)`, and for a multiple_results primitive `bind` returns a list. Its custom_vjp fwd rule, however, returned the primal-output element as a tuple `(a, lse, amax)`. JAX versions that enforce exact pytree-structure equality between a custom_vjp fwd rule's primal output and the decorated function's output reject this under reverse-mode AD: TypeError: Custom VJP fwd rule triangle_attention_custom_vjp_fwd ... the fwd rule output's first element had container/pytree structure (tuple ...) while the custom_vjp-decorated function had output container/pytree structure [list ...]. The forward path is unaffected (only bind is called there); only jax.grad / value_and_grad through triangle_attention fails. Return a list so the fwd rule's primal output matches the primal function's output. Signed-off-by: siddhant <siddhaant.sharma@gmail.com>
Supernova-45
approved these changes
Jul 29, 2026
Supernova-45
self-requested a review
July 29, 2026 20:38
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.
Summary
triangle_attentionraises aTypeErrorunder reverse-mode AD when the call sits inside ajax.checkpoint'd body run underlax.scan(the usual AlphaFold3-style Pairformer stack), on JAX versions that enforce exact pytree-structure equality between acustom_vjpfwd rule's primal output and the decorated function's output. Observed onjax==0.10.1:Root cause
triangle_attention_custom_vjpreturnsfwd_p.bind(...), andbindon amultiple_resultsprimitive returns a list. Its fwd rule (triangle_attention_custom_vjp_fwd), however, returns the primal-output element as a tuple(a, lse, amax).custom_vjpcompares the fwd rule's primal-output structure to the decorated function's output;tuple != list, so JAX rejects it under AD.The forward-only path is unaffected (it only calls
bind); onlyjax.grad/value_and_gradfails.Fix
One line in
cuequivariance_jax/triangle/_triangle_attention.py— return a list so the fwd rule matches the primal:Reproduction (jax 0.10.1, CUDA 12)
A bare
jax.gradof a singletriangle_attentioncall does not trip it — the strict fwd/primal structure check fires undercheckpoint/scan, which is exactly how the kernel is used inside a Pairformer trunk.Validation
On
jax==0.10.1+cuequivariance-jax==0.10.0+cuequivariance-ops-jax-cu12==0.10.0(L40S):TypeErrorabove.jax.gradsucceeds, and the fused kernel's gradient matches the module's own reference implementation (triangle_attention_jax_fwd) at cosine = 0.999997 (max|Δ| = 1.0e-1) — the fix changes nothing numerically.Note: newer JAX (0.11.0) appears to have relaxed this fwd/primal structure check, so the crash is not observed there — but the fwd rule should still structurally match the primal, and this restores reverse-mode AD on JAX 0.10.x.