Skip to content

[JAX] Return max_logit from TE fused attn - #3112

Open
KshitijLakhani wants to merge 22 commits into
NVIDIA:mainfrom
KshitijLakhani:klakhani/feat/return-softmax-stats
Open

[JAX] Return max_logit from TE fused attn#3112
KshitijLakhani wants to merge 22 commits into
NVIDIA:mainfrom
KshitijLakhani:klakhani/feat/return-softmax-stats

Conversation

@KshitijLakhani

@KshitijLakhani KshitijLakhani commented Jun 10, 2026

Copy link
Copy Markdown
Collaborator

Description

Return the max_logit from TE JAX fused attn to maintain parity with TE PyT fused attn.
Returning the softmax aux stats is a separate request and a bit mroe involved, so it'll be addressed separately.
Exposing the max_logit gives sufficient telemetry insight to users, for starters.

Fixes #2945

Test timing added

  • Non-CP
   Level              Tests added    Approx. time added
  ━━━━━━━  ━━━━━━━━━━━━━━━━━━━━━━━  ━━━━━━━━━━━━━━━━━━━━
   L0       41 (23 pass, 18 skip)                  ~23s
  ───────  ───────────────────────  ────────────────────
   L2       41 (23 pass, 18 skip)                  ~23s
  • CP

   Level                Tests added    Approx. time added
  ━━━━━━━  ━━━━━━━━━━━━━━━━━━━━━━━━━  ━━━━━━━━━━━━━━━━━━━━
   L1       180 (74 pass, 106 skip)                 ~147s
  ───────  ─────────────────────────  ────────────────────
   L2         60 (16 pass, 44 skip)                  ~26s

Type of change

  • Documentation change (change only to the documentation, either a fix or a new content)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Infra/Build change
  • Code refactoring

Changes

Checklist:

  • I have read and followed the contributing guidelines
  • The functionality is complete
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

@KshitijLakhani KshitijLakhani self-assigned this Jun 10, 2026
@KshitijLakhani KshitijLakhani added 2.18 and removed 2.17 labels Jun 25, 2026
@KshitijLakhani
KshitijLakhani force-pushed the klakhani/feat/return-softmax-stats branch 2 times, most recently from 4f3fbb3 to 9a56117 Compare July 8, 2026 23:28
@KshitijLakhani
KshitijLakhani force-pushed the klakhani/feat/return-softmax-stats branch 2 times, most recently from ca8551c to fc151f9 Compare July 27, 2026 00:57
@KshitijLakhani KshitijLakhani added 2.19 and removed 2.18 labels Jul 30, 2026
@KshitijLakhani
KshitijLakhani force-pushed the klakhani/feat/return-softmax-stats branch 2 times, most recently from fd2fff3 to f31d5de Compare August 21, 2026 06:49
@KshitijLakhani
KshitijLakhani marked this pull request as ready for review August 21, 2026 06:49
@greptile-apps

greptile-apps Bot commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds an opt-in per-head max_logit result to JAX fused attention while preserving the existing output-only contract by default.

  • Propagates the option through the public JAX and Flax APIs.
  • Extends the JAX primitive and native FFI result contracts.
  • Reduces local statistics across batches, context-parallel steps, and relevant mesh axes.
  • Adds single-device and distributed coverage across layouts, masks, GQA, and context-parallel strategies.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
transformer_engine/jax/attention.py Adds the public option, tuple-return contract, kernel-availability propagation, and custom-VJP handling for max-logit telemetry.
transformer_engine/jax/cpp_extensions/attention.py Extends fused-attention primitive results and implements per-head reduction, batching, sharding, and context-parallel aggregation.
transformer_engine/jax/csrc/extensions/attention.cpp Extends the native fused-attention forward interface with an optional cuDNN Max output buffer aligned with the Python primitive contract.
transformer_engine/jax/csrc/extensions.h Updates native extension declarations for the optional max-logit result.
transformer_engine/jax/flax/transformer.py Exposes the fused-only telemetry option through Flax while retaining plain-array returns when disabled.
tests/jax/test_fused_attn.py Adds non-context-parallel numerical and backward coverage for the optional auxiliary result.
tests/jax/test_distributed_fused_attn.py Adds distributed max-logit coverage for supported all-gather and ring context-parallel configurations.

Sequence Diagram

sequenceDiagram
    participant App as JAX/Flax caller
    participant API as fused_attn
    participant Primitive as JAX fused-attention primitive
    participant Native as Native TE/cuDNN
    participant Mesh as Mesh reduction
    App->>API: "QKV, return_max_logit=True"
    API->>Primitive: Bind fused forward
    Primitive->>Native: Output and Max buffers
    Native-->>Primitive: Attention output and row-wise Max
    Primitive->>Primitive: Reduce Max to per-head values
    Primitive->>Mesh: pmax across non-head mesh axes
    Mesh-->>API: Global per-head max_logit
    API-->>App: (output, max_logit)
Loading

Reviews (8): Last reviewed commit: "[pre-commit.ci] auto fixes from pre-comm..." | Re-trigger Greptile

@KshitijLakhani
KshitijLakhani force-pushed the klakhani/feat/return-softmax-stats branch from cc33228 to 8dabb52 Compare August 21, 2026 07:45
@KshitijLakhani KshitijLakhani changed the title [JAX] Return max_logit and softmax aux stats from TE JAX fused attn [JAX] Return max_logit from TE JAX fused attn Aug 21, 2026
@KshitijLakhani KshitijLakhani changed the title [JAX] Return max_logit from TE JAX fused attn [JAX] Return max_logit from TE fused attn Aug 21, 2026
@KshitijLakhani
KshitijLakhani force-pushed the klakhani/feat/return-softmax-stats branch 2 times, most recently from 2cb2b8d to 032eeea Compare August 24, 2026 16:21
NVTE_Bias_Type bias_type, NVTE_Fused_Attn_Backend backend,
void *softmax_buf, void *rng_state_buf = nullptr,
void *bias_buf = nullptr,
void *softmax_buf, void *max_buf = nullptr,

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.

Can we rename max_buf to max_logits_buf

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.

Addressed in SHA 704957a

nvte_set_tensor_param(&max_aux, kNVTERowwiseData, &max_aux_data);
}

NVTETensor &rng_state_aux = tensor_pack->tensors[size++];

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.

Do we need to check the capacity of tensor_pack->tensors here?

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.

I was not too concerned about it here, because I think the max size is 10 and with max_logit we will have 5 tensors only: 1 softmax + 1 max + 1 RNG + 1 bias + 1 offset = 5 maximum so I wasn't concerned about exceeding.

However, that's a good point for the future when someone is packing in more tensors in here. Let me push a change not only for max_logit but also for the other 4 tensors packed in sothat it serves as a template for any tensors being packed in the future

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.

Addressed in SHA 2e71bc8

(batch, head, q.shape[1], 1), -jnp.inf, dtype=jnp.float32
)
return output_per_step, softmax_aux_per_step
max_logit_per_step = FusedAttnFwdPrimitive._empty_or_neg_inf_max_logit(

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 does this instance use empty max logits?

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.

That's a good question @jberchtold-nvidia !
TL;DR: So, this is the no-compute branch for a fully causal masked Ring step and hence attention, softmax, isn't calculated in this branch and we need a neutral placeholder (-inf/0) because veery lax.cond branch must return the same shapes .

Let me try expanding on that:
With causal Ring attention, every rank cycles through KV blocks from all CP ranks. Some received KV blocks are entirely in the future relative to that rank’s query block and because causal attention must not attend to any token in such a block, TE skips the cuDNN attention call completely. So skip_compute does not run attention, softmax, or max-logit computation. It returns identity values so the JAX control flow and subsequent accumulation remain valid.

When max logits are requested, the helper returns [H] filled with -inf, so the skipped step cannot affect the running maximum. FWIW, it returns (0,) only when max-logit return is disabled.

@@ -399,6 +399,12 @@ def test_softcap_score_mod_with_aux_params_backward(
),
]

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.

Can you run L1 CI on this PR? I don't see a CI run

@KshitijLakhani KshitijLakhani Aug 24, 2026

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.

I had run the test suite locally only. Launched CI right now. Thanks for the reminder !

…x aux to the user from TE JAX fused attn output

Signed-off-by: Kshitij Lakhani <klakhani@nvidia.com>
… and softmax through the JAX fused attn primitives

Signed-off-by: Kshitij Lakhani <klakhani@nvidia.com>
Signed-off-by: Kshitij Lakhani <klakhani@nvidia.com>
…JAX fused attn tests

Signed-off-by: Kshitij Lakhani <klakhani@nvidia.com>
Signed-off-by: Kshitij Lakhani <klakhani@nvidia.com>
Signed-off-by: Kshitij Lakhani <klakhani@nvidia.com>
Signed-off-by: Kshitij Lakhani <klakhani@nvidia.com>
Signed-off-by: Kshitij Lakhani <klakhani@nvidia.com>
pre-commit-ci Bot and others added 11 commits August 24, 2026 14:15
… function for it

Signed-off-by: Kshitij Lakhani <klakhani@nvidia.com>
Signed-off-by: Kshitij Lakhani <klakhani@nvidia.com>
Signed-off-by: Kshitij Lakhani <klakhani@nvidia.com>
Signed-off-by: Kshitij Lakhani <klakhani@nvidia.com>
Signed-off-by: Kshitij Lakhani <klakhani@nvidia.com>
Signed-off-by: Kshitij Lakhani <klakhani@nvidia.com>
Signed-off-by: Kshitij Lakhani <klakhani@nvidia.com>
Signed-off-by: Kshitij Lakhani <klakhani@nvidia.com>
Signed-off-by: Kshitij Lakhani <klakhani@nvidia.com>
Signed-off-by: Kshitij Lakhani <klakhani@nvidia.com>
@KshitijLakhani
KshitijLakhani force-pushed the klakhani/feat/return-softmax-stats branch from b305c49 to 90a3eaf Compare August 24, 2026 21:16
@KshitijLakhani

Copy link
Copy Markdown
Collaborator Author

/te-ci jax L0 L1 L2

Signed-off-by: Kshitij Lakhani <klakhani@nvidia.com>
Signed-off-by: Kshitij Lakhani <klakhani@nvidia.com>
@KshitijLakhani
KshitijLakhani force-pushed the klakhani/feat/return-softmax-stats branch from 3e247dd to 2e71bc8 Compare August 24, 2026 21:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Expose max_logit (and softmax aux statistics) from JAX fused attention higher-level APIs

2 participants