[JAX] Add attention tutorials - #3162
Conversation
Signed-off-by: Kshitij Lakhani <klakhani@nvidia.com>
Greptile SummaryThis PR adds two JAX attention tutorials to the TransformerEngine documentation: a single-GPU BSHD tutorial covering GQA+SWA and DeepSeek-style MLA head dimensions, and a multi-GPU context-parallel tutorial covering packed THD GQA+SWA with Ring and AllGather strategies.
Confidence Score: 5/5Safe to merge; the change is purely additive tutorial documentation with no modifications to the core TE library. All changes are new documentation files and a test helper. The tutorial Python code is self-contained and only exercised when the appropriate hardware is present. The test file correctly defers module imports into test bodies so skip guards fire before any large tensor allocations happen for the CP case. The single finding is a wasteful tensor allocation in an assertion helper that runs only on 4-GPU Hopper hardware. Files Needing Attention: docs/examples/jax/test_attention.py line 131 — unnecessary large-tensor allocation inside _assert_cp_result. Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[te_jax_integration.rst] --> B[attention.rst\nindex page]
B --> C[attention_single_gpu.rst]
B --> D[attention_context_parallel.rst]
C -->|literalinclude| E[attention.py\nBSHD tutorial code]
C -->|literalinclude| F[attention.out\ncaptured output]
D -->|literalinclude| G[attention_context_parallel.py\nTHD CP tutorial code]
D -->|literalinclude| H[attention_context_parallel.out\ncaptured output]
E --> E1[FlaxNativeGQAAttention\nnative JAX baseline]
E --> E2[TEDotProductAttention\nTE wrapper]
E --> E3[MLA variant\nasymmetric Q/K vs V dim]
G --> G1[create_packed_segment_ids_and_pos\npacked THD inputs]
G --> G2[build_cp_mesh\nJAX Mesh + MeshResource]
G --> G3[shard_for_context_parallel\nstripe reorder + device_put]
G --> G4[fused_thd_attention\nRing / AllGather CP]
I[test_attention.py] -->|deferred import| E
I -->|deferred import under @requires_cp| G
Reviews (7): Last reviewed commit: "[pre-commit.ci] auto fixes from pre-comm..." | Re-trigger Greptile |
|
|
||
| **TODO — Coming soon.** | ||
| This document walks through replacing a plain JAX implementation of BSHD | ||
| attention with TransformerEngine's fused ``DotProductAttention``. The example |
There was a problem hiding this comment.
Thoughts on another sentence in here to mention TE/JAX attn has more features than shown in this tutorial?
Something like
TransformerEngine's DotProductAttention supports a variety of features. The full documentation can be found here "link". The tutorial example uses
grouped-query attention (GQA) <https://arxiv.org/abs/2305.13245>_ and
sliding-window attention (SWA).
There was a problem hiding this comment.
We can use the table we had in recent presentations (or just turn that into words to use here).
There was a problem hiding this comment.
I am thinking of adding a sentence and API reference as @jberchtold-nvidia suggested
The table might change the scope of this documentation from samples to support matrix i feel. So that could be a separate effort that we can then link here in the samples once it is ready. Is that okay @cyanguwa ?
| On a single GB200, this run is roughly **52x faster** for the fwd+bwd of this | ||
| BSHD GQA + SWA example. This compares TE ``DotProductAttention`` against the | ||
| native JAX baseline above, which materializes attention scores with XLA ops; it | ||
| is not a comparison against ``jax.nn.dot_product_attention(..., |
There was a problem hiding this comment.
it is not a comparison against jax.nn.dot_product_attention(..., implementation="cudnn")
Does native JAX jax.nn.dot_product_attention with backend cudnn support SWA? My understanding is native JAX did not support SWA via cuDNN. If so, can you mention that as the reasoning for why we aren't comparing against jax.nn.dot_production_attention with cuDNN backend
There was a problem hiding this comment.
The API does support SWA: https://docs.jax.dev/en/latest/_autosummary/jax.nn.dot_product_attention.html
I tried examples with the cuDNN backend separately to confirm that SWA is indeed supported
The motivation I had in mind for this sample/tutorial was to compare against native XLA backend and not the cuDNN backend (the comment here just helps clarify the same)
| timing loop follows the same forward+backward pattern as ``speedometer`` while | ||
| keeping those sharding controls visible. | ||
|
|
||
| .. literalinclude:: attention_context_parallel.py |
There was a problem hiding this comment.
Should we add in the following entries to show where CP excels?
- JAX attention jax.nn.dpa: OOM
- TE DPA without CP: OOM
If you picked small/medium shapes for the example and they don't actually OOM without CP in these isolated examples, I think that's fine to keep as is without this suggestion
There was a problem hiding this comment.
Can we add some speedup numbers as well, CP vs non-CP?
There was a problem hiding this comment.
Okay let me add speed up numbers and tweak the config numbers to pick an appropriate config for the same
Signed-off-by: Kshitij Lakhani <klakhani@nvidia.com>
Signed-off-by: Kshitij Lakhani <klakhani@nvidia.com>
Signed-off-by: Kshitij Lakhani <klakhani@nvidia.com>
| JAX: Attention with TransformerEngine | ||
| ===================================== | ||
| JAX: BSHD Attention with TransformerEngine | ||
| ========================================== |
There was a problem hiding this comment.
How about keeping "JAX: Attention with TransformerEngine" and then use "JAX: Context-Parallel Attention with TransformerEngine" for the CP file? We can walk through some of the features non-CP/CP supports before getting into specific examples. I think jberchtold suggested this, but also, the last section of this file seems to cover "other features" in attention as well.
There was a problem hiding this comment.
How about keeping "JAX: Attention with TransformerEngine" and then use "JAX: Context-Parallel Attention with TransformerEngine" for the CP file?
Sounds good
|
|
||
| **TODO — Coming soon.** | ||
| This document walks through replacing a plain JAX implementation of BSHD | ||
| attention with TransformerEngine's fused ``DotProductAttention``. The example |
There was a problem hiding this comment.
We can use the table we had in recent presentations (or just turn that into words to use here).
| This document walks through replacing a plain JAX implementation of BSHD | ||
| attention with TransformerEngine's fused ``DotProductAttention``. The example | ||
| uses `grouped-query attention (GQA) <https://arxiv.org/abs/2305.13245>`_ and | ||
| sliding-window attention (SWA). |
There was a problem hiding this comment.
Maybe add a link to SWA as well, or remove the one for GQA for consistency?
|
|
||
| `← Back to the JAX integration overview <../te_jax_integration.html>`_ | ||
|
|
||
| 1. Baseline: native BSHD GQA + SWA |
There was a problem hiding this comment.
How about "1. Baseline: native-Jax BSHD GQA + SWA"? Also, how is the "unfused attn" path compare to this baseline? Should we use that path instead of reimplementing FlaxNativeGQAAttention here?
There was a problem hiding this comment.
Sure I can do Baseline: native-xla BSHD GQA + SWA
I did think about this but the general flow of other sections seems to be: start with a native jax (non TE API) implementation and then show the user how to "port" it to TE so keeping that spirit in mind, I stayed away from using the TE unfused API
| :language: python | ||
| :start-after: # ATTENTION_IMPORTS_START | ||
| :end-before: # ATTENTION_IMPORTS_END | ||
|
|
There was a problem hiding this comment.
Maybe add a single line before the next code block starts? Same for the next block.
|
|
||
| See LICENSE for license information. | ||
|
|
||
| JAX: Context-Parallel THD Attention with Transformer Engine |
There was a problem hiding this comment.
How about "JAX: Context-Parallel Attention with Transformer Engine" and
"CP supports BSHD, THD with blah (link to our docs), and we are going to demonstrate a few popular user configs..."?
| ``[batch, seq, heads, dim]``, and the sequence dimension can pack several | ||
| shorter segments. The ``SequenceDescriptor`` tells TE which tokens belong to | ||
| which packed segment and which token slots are padding. This tutorial uses | ||
| sixteen padded segments per sequence and a 64k sequence length. |
There was a problem hiding this comment.
"sixteen padded segments per sequence, a 64k sequence length, and 2 sequences per batch."?
| :language: python | ||
| :start-after: # ATTENTION_CP_IMPORTS_START | ||
| :end-before: # ATTENTION_CP_IMPORTS_END | ||
|
|
There was a problem hiding this comment.
"The tensor inputs are created as follows."?
| timing loop follows the same forward+backward pattern as ``speedometer`` while | ||
| keeping those sharding controls visible. | ||
|
|
||
| .. literalinclude:: attention_context_parallel.py |
There was a problem hiding this comment.
Can we add some speedup numbers as well, CP vs non-CP?
| :end-before: # ATTENTION_CP_MESH_END | ||
|
|
||
|
|
||
| 3. Fused THD attention call |
Signed-off-by: Kshitij Lakhani <klakhani@nvidia.com>
Signed-off-by: Kshitij Lakhani <klakhani@nvidia.com>
eeaab44 to
82872d6
Compare
Signed-off-by: Kshitij Lakhani <klakhani@nvidia.com>
9908e58 to
c8b9cc4
Compare
Signed-off-by: Kshitij Lakhani <klakhani@nvidia.com>
3d1fdbd to
a137c0e
Compare
for more information, see https://pre-commit.ci
Description
Add attention tutorial for integrating TE into an existing framework
Type of change
Changes
Add a page for non-CP attn tutorial
Add a page for CP attn tutorial
Checklist: