Skip to content

[JAX] triangle attention#122

Merged
borisfom merged 8 commits into
mainfrom
triatt_jax
Jul 16, 2025
Merged

[JAX] triangle attention#122
borisfom merged 8 commits into
mainfrom
triatt_jax

Conversation

@mariogeiger

Copy link
Copy Markdown
Collaborator
  • VJP rule
  • Call cuda code only if we are running on a GPU

@mariogeiger mariogeiger changed the title [JAX] Add frontend API for triangle attention [JAX] Add frontend API for triangle operations Jul 3, 2025
@mariogeiger mariogeiger changed the title [JAX] Add frontend API for triangle operations [JAX] Add frontend API for triangle attention Jul 3, 2025
@mariogeiger mariogeiger changed the title [JAX] Add frontend API for triangle attention [JAX] triangle attention Jul 9, 2025
@mariogeiger
mariogeiger requested a review from Copilot July 9, 2025 17:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces a new triangle_attention operator in the JAX backend, complete with custom VJP support and optional CUDA acceleration, and updates documentation and tests accordingly.

  • Added the triangle_attention implementation with a custom forward/backward rule and GPU fallback.
  • Updated the API reference to include a new Triangle section for triangle_attention.
  • Introduced unit tests for gradient correctness and basic functionality on CPU and CUDA.

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
docs/api/cuequivariance_jax.rst Added a Triangle section in the API docs and autogeneration entry.
cuequivariance_jax/tests/triangle_attention_test.py New tests covering gradient checks and output shapes for triangle_attention.
cuequivariance_jax/cuequivariance_jax/triangle/_triangle_attention.py Implemented triangle_attention with custom VJP, JAX reference, and CUDA fallback.
cuequivariance_jax/cuequivariance_jax/triangle/init.py Added license header in new triangle package directory.
cuequivariance_jax/cuequivariance_jax/init.py Exported triangle_attention in the root module’s public API.
Comments suppressed due to low confidence (2)

cuequivariance_jax/cuequivariance_jax/triangle/_triangle_attention.py:42

  • The return type annotation '-> jax.Array' does not reflect that this function returns a tuple of three arrays; consider annotating it as '-> tuple[jax.Array, jax.Array, jax.Array]' for clarity.
) -> jax.Array:  # [B, N, H, S_qo, D]

cuequivariance_jax/cuequivariance_jax/triangle/_triangle_attention.py:69

  • [nitpick] The variable 'a' is ambiguous; consider renaming it to 'attn_logits' or 'attention_scores' to clarify its purpose.
    a = jnp.einsum("...ai,...bi->...ab", q, k, precision=precision)

Comment thread cuequivariance_jax/tests/triangle_attention_test.py
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

@hsadasivan hsadasivan Jul 9, 2025

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.

Empty file. Do we need this?
I see imports are already taken care of in cuequivariance_jax/cuequivariance_jax/init.py

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

file removed

def triangle_attention_fwd(q, k, v, mask, bias, scale, precision=None):
a, lse, amax = fwd_p.bind(q, k, v, mask, bias, scale=scale, precision=precision)
residuals = (a, lse, q, k, v, mask, bias)
return (a, lse, amax), residuals

@hsadasivan hsadasivan Jul 9, 2025

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.

why are we returning residuals? Is it a JAX requirement?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

yes, have a look at this

da, dlse, damax = cotangents

dq, dk, dv, dbias = bwd_p.bind(
da, a, lse, q, k, v, mask, bias, scale=scale, precision=precision

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 consistency with torch, isn't it better to do bias,mask rather than mask,bias ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

good catch, done

def fn(q, k, v, mask, bias):
return cuex.triangle_attention(q, k, v, mask, bias, scale)

output, lse, amax = fn(q, k, v, mask, bias)

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.

why dont we have more elaborate parametrized tests for triattn in jax like in trimul (shapes, precision etc.)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

done

@mariogeiger
mariogeiger requested a review from hsadasivan July 15, 2025 09:05
@borisfom
borisfom merged commit 50d0239 into main Jul 16, 2025
8 checks passed
@mariogeiger
mariogeiger deleted the triatt_jax branch February 2, 2026 08:40
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.

4 participants