Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
151 commits
Select commit Hold shift + click to select a range
2b9fbc5
refactor nvte_get_fused_attn_backend with FE calls
cyanguwa May 6, 2026
16b837c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 6, 2026
5a482f9
Merge branch 'main' into fe_check_support
cyanguwa May 6, 2026
42bcd89
replace code+string with string only
cyanguwa May 7, 2026
de8e814
clean up logic/comments/structure
cyanguwa May 8, 2026
4b8c7ed
Merge branch 'main' into fe_check_support
cyanguwa May 8, 2026
81e59a9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 8, 2026
5640c68
Merge branch 'main' into fe_check_support
cyanguwa May 8, 2026
6c5126d
fix compilation errors
cyanguwa May 8, 2026
d35bff7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 8, 2026
f6fc585
remove handle from API; add bottom_right_diagonal
cyanguwa May 8, 2026
3e666b0
add batch_size to API
cyanguwa May 8, 2026
056aba6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 8, 2026
e054863
fix jax binding
cyanguwa May 8, 2026
a7fe928
specify o_dtype for FP8s
cyanguwa May 8, 2026
c9b22b5
fix BRCM and custom_fp8 tests
cyanguwa May 8, 2026
ac44e66
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 8, 2026
9131b2d
add o_format/etc to API and other tweaks
cyanguwa May 8, 2026
956f159
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 8, 2026
b21f606
minor tweaks for docstring
cyanguwa May 8, 2026
3421920
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 8, 2026
7956b43
replace with nvte_get_fused_attn_backend_v2 and add NVTEFusedAttnConfig
cyanguwa May 8, 2026
e86fc67
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 8, 2026
e2561d0
fix FP8 tests
cyanguwa May 12, 2026
724a12f
add do_dtype and dqkv_dtype to API
cyanguwa May 12, 2026
3ae36df
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 12, 2026
3532e98
Merge branch 'main' into fe_check_support
cyanguwa May 12, 2026
3bed68d
Merge branch 'main' into fe_check_support
cyanguwa Jul 7, 2026
d634f3b
Merge branch 'main' into fe_check_support
cyanguwa Jul 8, 2026
1c09072
replace with opaque handle
cyanguwa Jul 11, 2026
ac19f9d
repeat with fwd/bwd params
cyanguwa Jul 14, 2026
88a327c
remove bucketed b/t_q/t_kv
cyanguwa Jul 14, 2026
391fe2e
thread _v2 through, fix default scaling mode, make config specific to…
cyanguwa Jul 14, 2026
877bf99
Merge branch 'main' into fe_check_support
cyanguwa Jul 14, 2026
261bb9a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 14, 2026
c03d852
reorder struct fields, consolidate APIs to derive, make_config, make_…
cyanguwa Jul 15, 2026
eadd005
simplify fused attn config/params wrappers via set_attr helper, add g…
cyanguwa Jul 16, 2026
d052fcd
Merge branch 'main' into fe_check_support
cyanguwa Jul 17, 2026
28f5a8c
clean up derived fields, debug probe/exec graph mismatches
cyanguwa Jul 17, 2026
ade19fe
match fused attn availability probe to runtime for FP8 specs and per-…
cyanguwa Jul 18, 2026
42e9747
fix bias for jax
cyanguwa Jul 18, 2026
c66028a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 18, 2026
cf3265e
fused-attn: share graph cache across threads via mutex, require cuDNN…
cyanguwa Jul 20, 2026
cda01d7
Cache attention-backend selection keyed on (NVTE_* env, attention_par…
cyanguwa Jul 21, 2026
aa34ccb
use macros for attr_sizes[], cache_key_tuple(), and fprintf in cache …
cyanguwa Jul 21, 2026
642b58a
Revert "use macros for attr_sizes[], cache_key_tuple(), and fprintf i…
cyanguwa Jul 21, 2026
97cb2b7
remove graph cache debug code
cyanguwa Jul 21, 2026
17e5fe0
review and clean up
cyanguwa Jul 21, 2026
f802afc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 21, 2026
d65c617
guard against pre-scale bias
cyanguwa Jul 21, 2026
47421b9
fix score mod Jax tests
cyanguwa Jul 21, 2026
b6d04eb
Mirror PyTorch NVTE_DEBUG logging in JAX fused-attn backend selection…
cyanguwa Jul 21, 2026
bcbc084
tidy up on jax side
cyanguwa Jul 21, 2026
1a8c087
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 21, 2026
1632f7b
fix lint
cyanguwa Jul 22, 2026
c0233a1
fix nvte_get_fused_attn_backend shim, docstring, bias/softmax pointers
cyanguwa Jul 22, 2026
928dd33
add device_id as a key
cyanguwa Jul 22, 2026
7ff8058
add docstring for FP8 recipes
cyanguwa Jul 22, 2026
1db50d9
fix doc/ipynb
cyanguwa Jul 22, 2026
dca9585
avoid duplicate checks for fused backend and force to 0 for bias shap…
cyanguwa Jul 22, 2026
7585a1b
reduce ipynb diffs
cyanguwa Jul 22, 2026
bd557ca
Merge branch 'main' into fe_check_support
cyanguwa Jul 22, 2026
a32b671
Merge branch 'main' into fe_check_support
cyanguwa Jul 23, 2026
cbf6550
add fused attn graph cache debug code
cyanguwa Jul 24, 2026
bf4bfc0
fix kv cache probes
cyanguwa Jul 24, 2026
c7ea7be
Merge branch 'main' into fe_check_support
cyanguwa Jul 26, 2026
c056cbe
deduplicate L0 pytest tests
cyanguwa Jul 26, 2026
e890b2a
Merge remote-tracking branch 'origin/fe_check_support' into fe_check_…
cyanguwa Jul 26, 2026
16df390
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 26, 2026
a6da26e
fix merge with torch.compile PRs
cyanguwa Jul 26, 2026
3f06482
Merge branch 'main' into fe_check_support
cyanguwa Jul 27, 2026
e2d1fc9
remove redundant change
cyanguwa Jul 27, 2026
fa6e636
group newly enabled SWA tests to tiers L0/L1 in Jax
cyanguwa Jul 27, 2026
5b33337
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 27, 2026
6461b66
fix lint
cyanguwa Jul 27, 2026
00429e7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 27, 2026
988c9ba
fix torch.compile for get_backend
cyanguwa Jul 27, 2026
96ee857
Merge branch 'main' into fe_check_support
cyanguwa Jul 27, 2026
d374a3d
a cleaner way to make torch.compile work
cyanguwa Jul 28, 2026
288046c
skip fused attn checks for flash tests
cyanguwa Jul 28, 2026
1d10f80
add tq/tkv to per-step cp configs
cyanguwa Jul 28, 2026
e3300ea
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 28, 2026
7b604a2
Merge branch 'main' into fe_check_support
cyanguwa Jul 28, 2026
fbf1988
Merge branch 'main' into fe_check_support
cyanguwa Aug 4, 2026
dadb07f
Merge branch 'main' into fe_check_support
cyanguwa Aug 13, 2026
71e8982
fix jax CI
cyanguwa Aug 13, 2026
8fdd81d
temporary changes: cache debug, timers, single flight, is_probe, dry-…
cyanguwa Aug 14, 2026
e027294
Revert "temporary changes: cache debug, timers, single flight, is_pro…
cyanguwa Aug 14, 2026
734aca7
fix probe/exec bias drift in keys
cyanguwa Aug 14, 2026
d2f7774
fix probe/exec cp drift in keys
cyanguwa Aug 14, 2026
6201d04
fix MHA init to avoid probe/exec drift
cyanguwa Aug 14, 2026
157a975
WIP: graph cache and restructuring of impl
cyanguwa Aug 14, 2026
6d2b577
revert consolidating of derive() and make nvte_get_backend_v2 only re…
cyanguwa Aug 14, 2026
ce58a99
incorporate PR5
cyanguwa Aug 14, 2026
35d6da1
address review comments
cyanguwa Aug 14, 2026
24f53ba
fix CI failures
cyanguwa Aug 15, 2026
2a7eed9
more fixes: cache only genuine cuDNN refusals, bound the graph cache …
cyanguwa Aug 17, 2026
76ae514
WIP: restructuring and polishing
cyanguwa Aug 19, 2026
63b959a
WIP: clean up graph cache/debug
cyanguwa Aug 19, 2026
a82b6bd
WIP: tidy up structure
cyanguwa Aug 20, 2026
244fc73
Merge branch 'main' into fe_check_support
cyanguwa Aug 20, 2026
fbc09d4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 20, 2026
596c227
fix compile warnings and misc changes
cyanguwa Aug 20, 2026
4a20bba
Merge branch 'main' into fe_check_support
cyanguwa Aug 20, 2026
9609324
WIP: trim code/comments
cyanguwa Aug 21, 2026
76092d1
WIP: trim code/comments v2
cyanguwa Aug 21, 2026
b1417ca
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 21, 2026
da7aa18
minor fixes
cyanguwa Aug 22, 2026
c5b31e7
fix notebook
cyanguwa Aug 24, 2026
2a86e66
fix notebook
cyanguwa Aug 24, 2026
108c87f
more fixes
cyanguwa Aug 24, 2026
d56bf1d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 24, 2026
f0bb146
Merge branch 'main' into fe_check_support
cyanguwa Aug 26, 2026
cb0f2f3
Merge remote-tracking branch 'upstream/main' into fe_check_support
cyanguwa Aug 27, 2026
63f5ed8
minor tweaks of notebook
cyanguwa Aug 27, 2026
61fee23
reorder FusedAttnFwd/BwdParams scalars to match FusedAttnConfig
cyanguwa Aug 27, 2026
15a50d0
improvements/fixes
cyanguwa Aug 27, 2026
4f8e7d7
fix jax merge
cyanguwa Aug 27, 2026
4fd2657
Merge branch 'main' into fe_check_support
cyanguwa Aug 27, 2026
9cf5959
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 27, 2026
cf5deb9
remove redundant comments
cyanguwa Aug 27, 2026
2cc172e
fix linter
cyanguwa Aug 27, 2026
7a6c28a
revert to some main condition
cyanguwa Aug 27, 2026
a70360a
fix thd in jax
cyanguwa Aug 27, 2026
ff284f4
fix CP AG gradient count
cyanguwa Aug 28, 2026
87ae24f
add test for fused error message; thread t_q/kv to probe in tests
cyanguwa Aug 28, 2026
1712ef5
add jax error message test
cyanguwa Aug 28, 2026
2326342
swap input_batch for num_segments in jax get_backend
cyanguwa Aug 28, 2026
ec8b1ec
minor tweaks based on AI review
cyanguwa Aug 28, 2026
527e1cd
swap input_batch for num_segments in jax get_backend; more files
cyanguwa Aug 28, 2026
018e929
Merge branch 'main' into fe_check_support
cyanguwa Aug 28, 2026
1828910
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 28, 2026
7e56d0a
restrict MLA to sm100+
cyanguwa Aug 28, 2026
7fb4db1
drop token counts for bshd/sbhd as well
cyanguwa Aug 28, 2026
90847a0
minor tweak to last commit regarding dropping token counts
cyanguwa Aug 28, 2026
29fb985
UnsupportedByCudnn struct; distinguish multi-dev on single thread; re…
cyanguwa Aug 28, 2026
54d9348
allow mixed THD+2BSHD on sm80 for kv cache
cyanguwa Aug 28, 2026
6f8ae9d
Merge branch 'main' into fe_check_support
cyanguwa Sep 1, 2026
4968849
fix merge conflicts
cyanguwa Sep 2, 2026
7102107
add back fp8 sink bwd condition
cyanguwa Sep 2, 2026
8b9ddd7
recover 3 CP tests by removing brcm + thd constriction for p2p
cyanguwa Sep 2, 2026
5356a82
Merge branch 'main' into fe_check_support
cyanguwa Sep 2, 2026
81cd2c0
merge main
cyanguwa Sep 3, 2026
83cd9fa
improve jupiter notebook
cyanguwa Sep 3, 2026
0f60e59
improve jupiter notebook
cyanguwa Sep 3, 2026
ff2bf57
improve jupiter notebook
cyanguwa Sep 3, 2026
61ef743
improve jupiter notebook
cyanguwa Sep 3, 2026
991a4aa
improve jupiter notebook
cyanguwa Sep 3, 2026
1b5b7a5
tweak test files
cyanguwa Sep 3, 2026
dafb68e
minor tweaks to common
cyanguwa Sep 3, 2026
ccf5aab
tweak docstrings
cyanguwa Sep 3, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions docs/envvars.rst
Original file line number Diff line number Diff line change
Expand Up @@ -190,18 +190,18 @@ backend-selection overview.
:Default: ``1``
:Description: Enable or disable UnfusedDotProductAttention backend (native PyTorch). When set to ``0``, UnfusedDotProductAttention will not be used.

.. envvar:: NVTE_FUSED_ATTN_BACKEND

:Type: ``int`` (1 or 2)
:Default: Auto-selected
:Description: Request a cuDNN FusedAttention backend when that request is supported by the active fused-attention path. ``1`` = F16_arbitrary_seqlen (cuDNN, any seq len), ``2`` = FP8 backend. If not set, the backend is automatically selected based on the input configuration. BF16/FP16 attention uses sub-backend ``1`` when eligible. FP8 attention uses sub-backend ``2`` when FP8 DPA is enabled and supported by the architecture, cuDNN version, and input configuration.

.. envvar:: NVTE_FUSED_ATTN_USE_FAv2_BWD

:Type: ``int`` (0 or 1)
:Default: ``0``
:Description: When using FusedAttention, use FlashAttention-2 implementation for the backward pass instead of the cuDNN implementation. This can be useful due to performance differences between various versions of flash-attn and FusedAttention.

.. envvar:: NVTE_FUSED_ATTN_CACHE_DEBUG

:Type: ``int`` (0, 1 or 2), optionally followed by ``:<ranks>``
:Default: ``0``
:Description: Log FusedAttention graph cache activity to stderr, prefixed with ``[FUSED-ATTN-CACHE]``. ``1`` prints an end-of-run summary of the cache counters and the mean time of each cuDNN build stage. ``2`` additionally traces every event as it happens: each graph built, each graph cuDNN accepts and the cache keeps, each lookup and whether it hit or missed, each first execution that compiles kernels, and each execution. When the launcher exports a rank, only rank 0 logs; append ``:<ranks>`` to override, as in ``1:all`` for level 1 on every rank or ``2:0,3`` for level 2 on ranks 0 and 3.

.. envvar:: NVTE_ALLOW_NONDETERMINISTIC_ALGO

:Type: ``int`` (0 or 1)
Expand Down
64 changes: 47 additions & 17 deletions docs/examples/attention/attention.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,7 @@
"NVTE_DEBUG = 0/1 # disables/enables debugging\n",
"NVTE_DEBUG_LEVEL = 0/1/2 # enables logging.WARNING/INFO/DEBUG-level messages\n",
"```\n",
"<div class=\"alert alert-info\">\n",
"<b>Note:</b>\n",
" \n",
"These flags are supported in PyTorch only as of Transformer Engine 2.0. JAX support is expected to be added in the future.\n",
"</div>"
"These flags are available in both PyTorch and Jax in TE v2.20 onwards."
]
},
{
Expand Down Expand Up @@ -330,6 +326,51 @@
"!NVTE_DEBUG=1 NVTE_DEBUG_LEVEL=2 python example_attention.py"
]
},
{
"cell_type": "markdown",
"id": "fc0c9e3d",
"metadata": {},
"source": [
"For the FusedAttention backend, cuDNN graphs are cached and reused, and the following flag helps understand the activities taking place in the cache. `NVTE_FUSED_ATTN_CACHE_DEBUG` is supported in both PyTorch and JAX.\n",
"```\n",
"NVTE_FUSED_ATTN_CACHE_DEBUG = 0 # disables cache diagnostics\n",
"NVTE_FUSED_ATTN_CACHE_DEBUG = 1/2:ranks # enables cache diagnostics (events)/(tracing):select ranks for information collection\n",
"```\n",
"At level 1, these cache events are collected and reported in the end-of-run summary: `create_graph` (number of graphs created), `cache_graph` (number of graphs cached), `hit` (number of cache hits), `miss` (number of cache misses), `build_plans` (number of graphs whose plans are built), and `execute` (number of plan executions). These event counters are tallied up per backend (f16 and fp8), per pass (forward and backward), and per thread. They help paint a picture of the graph reuse rate and caching effectiveness. Another part of the end-of-run summary are the build times of various stages of the cuDNN graph. Out of the 5 cuDNN calls, `validate`, `build_operation_graph`, `create_execution_plans`, `check_support`, and `build_plans`, `build_plans` is the most expensive one, because it is where the kernels get compiled. It happens once per eligible graph and is expected to execute many times in a real-life run.\n",
"\n",
"For more verbose diagnostics, level 2 enables tracing for all the events taking place in the cache. For example, for every `miss` event, a \"MISS\" line is printed in the logs, as well as the cache key that triggered it. This helps identify if the cache is processing the right config as we intended.\n",
"\n",
"By default, only rank 0 is enabled for both level 1 and level 2. Users can use the \"level:ranks\" format to specify a select set of ranks to collect diagnostics on. For example, \"2:0,3\" means to enable level 2 diagnostics on ranks 0 and 3. An example of the level 2 diagnostics, which also includes the level 1 summary, is as follows, for a `tests/pytorch/attention/test_attention.py::test_dot_product_attention` test.\n",
"```\n",
"[FUSED-ATTN-CACHE] rank=0 | tid=0 dev=0 | f16 fwd MISS | train=1 det=0 cg=0 maxlogit=0 mask=0 bias=0 wl=-1 wr=-1 brd=0 softmax=0 scale_mode=0 dropout=0 attn_scale=1 qkv_dt=5 o_dt=5 do_dt=6 dqkv_dt=6 qkv_lay=0 o_fmt=0 do_fmt=8 dqkv_lay=26 qkv_sif=8 do_sif=8 b=8 h=16 hg=16 dqk=64 dv=64 sq=128 skv=128 tq=0 tkv=0 bb=0 btq=0 btkv=0 npk=0 npv=0 psk=0 psv=0 mppk=0 mppv=0 bias_b=0 bias_h=0 bias_sq=0 bias_skv=0\n",
"[FUSED-ATTN-CACHE] rank=0 | tid=0 dev=0 | f16 fwd CREATE_GRAPH | hit= 0, miss= 1, create_graph= 1, cache_graph= 0, build_plans= 0, execute= 0\n",
"[FUSED-ATTN-CACHE] rank=0 | tid=0 dev=0 | f16 fwd CACHE_GRAPH | hit= 0, miss= 1, create_graph= 1, cache_graph= 1, build_plans= 0, execute= 0\n",
"...\n",
"[FUSED-ATTN-CACHE] rank=0 | tid=0 dev=0 | f16 fwd HIT | train=1 det=0 cg=0 maxlogit=0 mask=0 bias=0 wl=-1 wr=-1 brd=0 softmax=0 scale_mode=0 dropout=0 attn_scale=1 qkv_dt=5 o_dt=5 do_dt=6 dqkv_dt=6 qkv_lay=0 o_fmt=0 do_fmt=8 dqkv_lay=26 qkv_sif=8 do_sif=8 b=8 h=16 hg=16 dqk=64 dv=64 sq=128 skv=128 tq=0 tkv=0 bb=0 btq=0 btkv=0 npk=0 npv=0 psk=0 psv=0 mppk=0 mppv=0 bias_b=0 bias_h=0 bias_sq=0 bias_skv=0\n",
"[FUSED-ATTN-CACHE] rank=0 | tid=0 dev=0 | f16 fwd BUILD_PLANS | hit= 3, miss= 1, create_graph= 1, cache_graph= 1, build_plans= 1, execute= 0\n",
"...\n",
"[FUSED-ATTN-CACHE] rank=0 | tid=0 dev=0 | f16 fwd EXECUTE | hit= 5, miss= 1, create_graph= 1, cache_graph= 1, build_plans= 1, execute= 1\n",
"...\n",
"[FUSED-ATTN-CACHE] rank=0 | ===== summary begin =====\n",
"[FUSED-ATTN-CACHE] rank=0 | tid=0 dev=0 | f16 fwd | hit= 5, miss= 1, create_graph= 1, cache_graph= 1, build_plans= 1, execute= 1\n",
"[FUSED-ATTN-CACHE] rank=0 | tid=0 dev=0 | f16 bwd | hit= 1, miss= 1, create_graph= 1, cache_graph= 1, build_plans= 0, execute= 0\n",
"[FUSED-ATTN-CACHE] rank=0 | tid=1 dev=0 | f16 bwd | hit= 4, miss= 0, create_graph= 0, cache_graph= 0, build_plans= 1, execute= 1\n",
"[FUSED-ATTN-CACHE] rank=0 | tid=all dev=all | f16 fwd | hit= 5, miss= 1, create_graph= 1, cache_graph= 1, build_plans= 1, execute= 1\n",
"[FUSED-ATTN-CACHE] rank=0 | tid=all dev=all | f16 bwd | hit= 5, miss= 1, create_graph= 1, cache_graph= 1, build_plans= 1, execute= 1\n",
"[FUSED-ATTN-CACHE] rank=0 | f16 fwd validate | calls=1 | time= 0.168 ms/call\n",
"[FUSED-ATTN-CACHE] rank=0 | f16 fwd build_operation_graph | calls=1 | time= 11.405 ms/call\n",
"[FUSED-ATTN-CACHE] rank=0 | f16 fwd create_execution_plans | calls=1 | time= 2.054 ms/call\n",
"[FUSED-ATTN-CACHE] rank=0 | f16 fwd check_support | calls=1 | time= 0.044 ms/call\n",
"[FUSED-ATTN-CACHE] rank=0 | f16 fwd build_plans | calls=1 | time= 327.083 ms/call\n",
"[FUSED-ATTN-CACHE] rank=0 | f16 bwd validate | calls=1 | time= 0.009 ms/call\n",
"[FUSED-ATTN-CACHE] rank=0 | f16 bwd build_operation_graph | calls=1 | time= 1.026 ms/call\n",
"[FUSED-ATTN-CACHE] rank=0 | f16 bwd create_execution_plans | calls=1 | time= 2.874 ms/call\n",
"[FUSED-ATTN-CACHE] rank=0 | f16 bwd check_support | calls=1 | time= 0.016 ms/call\n",
"[FUSED-ATTN-CACHE] rank=0 | f16 bwd build_plans | calls=1 | time= 514.599 ms/call\n",
"[FUSED-ATTN-CACHE] rank=0 | ===== summary end =====\n",
"```"
]
},
{
"cell_type": "markdown",
"id": "611d8fdb",
Expand All @@ -346,18 +387,7 @@
"NVTE_FUSED_ATTN = 0 # disables cuDNN attention; default = 1\n",
"```\n",
"\n",
"**cuDNN attention sub-backends:**\n",
"This environment variable allows users to express their preference of cuDNN attention sub-backends. However, the elected sub-backend will only be used *if* it is eligible, i.e. if it has support for the provided inputs and runtime environment.\n",
"```\n",
"NVTE_FUSED_ATTN_BACKEND = 1/2 # user preference of cuDNN sub-backend\n",
"```\n",
"\n",
"```\n",
"<div class=\"alert alert-info\">\n",
"<b>Note</b>\n",
" \n",
"Environment variables <code>NVTE_FLASH_ATTN</code>, <code>NVTE_UNFUSED_ATTN</code>, <code>NVTE_FUSED_ATTN_BACKEND</code>, and <code>NVTE_FUSED_ATTN_USE_FAv2_BWD</code> are supported in PyTorch. <code>NVTE_FUSED_ATTN</code> and <code>NVTE_ALLOW_NONDETERMINISTIC_ALGO</code> are supported in both PyTorch and JAX.\n",
"</div>\n",
"Environment variables <code>NVTE_FLASH_ATTN</code>, <code>NVTE_UNFUSED_ATTN</code>, and <code>NVTE_FUSED_ATTN_USE_FAv2_BWD</code> are supported in PyTorch. <code>NVTE_FUSED_ATTN</code> and <code>NVTE_ALLOW_NONDETERMINISTIC_ALGO</code> are supported in both PyTorch and JAX.\n",
"\n",
"### 2.3 Example Tests\n",
"\n",
Expand Down
31 changes: 16 additions & 15 deletions docs/examples/jax/attention_context_parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,21 +245,22 @@ def context_parallel_supported() -> Tuple[bool, str]:
return False, f"needs {cp_size} GPUs"

has_kernel = is_fused_attn_kernel_available(
True,
dtype,
dtype,
QKVLayout.THD_THD_THD,
AttnBiasType.NO_BIAS,
AttnMaskType.PADDING_CAUSAL_MASK,
AttnSoftmaxType.VANILLA_SOFTMAX,
0.0,
num_query_heads,
num_kv_heads,
seq,
seq,
head_dim,
head_dim,
window_size,
is_training=True,
batch_size=batch,
q_dtype=dtype,
kv_dtype=dtype,
qkv_layout=QKVLayout.THD_THD_THD,
attn_bias_type=AttnBiasType.NO_BIAS,
attn_mask_type=AttnMaskType.PADDING_CAUSAL_MASK,
softmax_type=AttnSoftmaxType.VANILLA_SOFTMAX,
dropout_probability=0.0,
q_num_heads=num_query_heads,
kv_num_heads=num_kv_heads,
q_max_seqlen=seq,
kv_max_seqlen=seq,
head_dim_qk=head_dim,
head_dim_v=head_dim,
window_size=window_size,
)
if not has_kernel:
return False, "no fused attention kernel for the THD SWA shape"
Expand Down
31 changes: 16 additions & 15 deletions docs/examples/jax/test_attention.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,22 @@ def _context_parallel_supported():
return False, f"needs {cp_size} GPUs"

has_kernel = is_fused_attn_kernel_available(
True,
jnp.bfloat16,
jnp.bfloat16,
QKVLayout.THD_THD_THD,
AttnBiasType.NO_BIAS,
AttnMaskType.PADDING_CAUSAL_MASK,
AttnSoftmaxType.VANILLA_SOFTMAX,
0.0,
128,
8,
65536,
65536,
128,
128,
(8192, 0),
is_training=True,
batch_size=2,
q_dtype=jnp.bfloat16,
kv_dtype=jnp.bfloat16,
qkv_layout=QKVLayout.THD_THD_THD,
attn_bias_type=AttnBiasType.NO_BIAS,
attn_mask_type=AttnMaskType.PADDING_CAUSAL_MASK,
softmax_type=AttnSoftmaxType.VANILLA_SOFTMAX,
dropout_probability=0.0,
q_num_heads=128,
kv_num_heads=8,
q_max_seqlen=65536,
kv_max_seqlen=65536,
head_dim_qk=128,
head_dim_v=128,
window_size=(8192, 0),
)
if not has_kernel:
return False, "no fused attention kernel for the THD SWA shape"
Expand Down
39 changes: 1 addition & 38 deletions tests/jax/test_distributed_fused_attn.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,6 @@ def impl_test_self_attn(
is_training = True
batch, seqlen, num_head, hidden = data_shape

if not is_fused_attn_kernel_available(
is_training,
dtype,
dtype,
QKVLayout.BS3HD,
attn_bias_type,
attn_mask_type,
softmax_type,
dropout_prob,
num_head,
num_head,
seqlen,
seqlen,
hidden,
hidden,
None, # no window
):
pytest.skip("No FusedAttn backend found")

Comment thread
cyanguwa marked this conversation as resolved.
col_ref = self.generate_collectives_count_ref(
mesh_shape,
mesh_axes,
Expand Down Expand Up @@ -234,25 +215,6 @@ def test_cross_attn(

batch, seqlen, num_head, hidden = data_shape

if not is_fused_attn_kernel_available(
is_training,
dtype,
dtype,
QKVLayout.BSHD_BS2HD,
attn_bias_type,
attn_mask_type,
softmax_type,
dropout_prob,
num_head,
num_head,
seqlen,
seqlen,
hidden,
hidden,
None, # no window
):
pytest.skip("No FusedAttn backend found")

col_ref = self.generate_collectives_count_ref()
runner = FusedAttnRunner(
batch,
Expand Down Expand Up @@ -491,6 +453,7 @@ def impl_test_context_parallel_attn(
def check_has_backend_for_mask(mask_type):
return is_fused_attn_kernel_available(
is_training,
batch,
dtype,
dtype,
qkv_layout,
Expand Down
92 changes: 88 additions & 4 deletions tests/jax/test_fused_attn.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""Tests for fused attention"""
import os
from enum import Enum, auto
from dataclasses import dataclass, field
from dataclasses import dataclass, field, replace
from functools import partial
from math import sqrt
from typing import Any, Callable, Mapping, Tuple, Optional, Dict
Expand Down Expand Up @@ -48,11 +48,14 @@
)

from distributed_test_base import assert_equal_collectives
from utils import assert_allclose, print_debug_tensor_stats
from utils import assert_allclose, get_test_level, print_debug_tensor_stats

# Get determinism
_deterministic = not bool(int(os.getenv("NVTE_ALLOW_NONDETERMINISTIC_ALGO", "1")))

# CI test level
_TEST_LEVEL = get_test_level()


@pytest.fixture(autouse=True, scope="module")
def init():
Expand Down Expand Up @@ -383,6 +386,48 @@ def score_mod(_graph, score, _tensors):
)


def test_fused_attn_backend_message():
"""Test the error messaging of the fused attention backend query."""
baseline = FusedAttnHelper(
is_training=True,
batch_size=2,
q_dtype=jnp.bfloat16,
kv_dtype=jnp.bfloat16,
qkv_layout=QKVLayout.BSHD_BSHD_BSHD,
attn_bias_type=AttnBiasType.NO_BIAS,
attn_mask_type=AttnMaskType.NO_MASK,
softmax_type=AttnSoftmaxType.VANILLA_SOFTMAX,
dropout_probability=0.0,
q_num_heads=8,
kv_num_heads=8,
q_max_seqlen=128,
kv_max_seqlen=128,
head_dim_qk=64,
head_dim_v=64,
window_size=(-1, -1),
attn_scale=0.125,
)

# One of TE's rules is violated and the error message is surfaced
backend, message = replace(
baseline, attn_bias_type=AttnBiasType.PRE_SCALE_BIAS
).get_fused_attn_backend()
assert backend == NVTE_Fused_Attn_Backend.NVTE_No_Backend
assert message == "Fused attention does not support pre-scale bias."

# No error message if supported; otherwise skip the test
backend, message = baseline.get_fused_attn_backend()
if backend == NVTE_Fused_Attn_Backend.NVTE_No_Backend:
pytest.skip(f"FusedAttention does not support the baseline config: {message}")
assert message == ""

# All TE rules have cleared; now gets rejected by cuDNN's support check
# cuDNN's error message might change across cuDNN versions, so only verify the presence of the string
backend, message = replace(baseline, head_dim_qk=1024, head_dim_v=1024).get_fused_attn_backend()
assert backend == NVTE_Fused_Attn_Backend.NVTE_No_Backend
assert message != ""


class BiasShape(Enum):
"""
Enum class to represent the different bias shapes used in the fused attention.
Expand Down Expand Up @@ -482,6 +527,27 @@ def _get_max_segments_per_sequence(self):
return 1

def _check_configs(self):
# Trim SWA configs for L0 and L1 to reduce test time; need to trim more in future test refactoring.
if self.window_size is not None and (
self.dropout_prob != 0.0 or self.attn_bias_type is not AttnBiasType.NO_BIAS
):
if _TEST_LEVEL == "L0" and (
self.softmax_type != AttnSoftmaxType.VANILLA_SOFTMAX
or self.dtype != jnp.bfloat16
or self.attn_bias_type is not AttnBiasType.POST_SCALE_BIAS
or self.attn_mask_type is not AttnMaskType.NO_MASK
):
pytest.skip(
"Trimmed SWA+bias/dropout config: only vanilla-softmax + bf16 + post_scale_bias"
" + no-mask runs at L0"
)
if _TEST_LEVEL == "L1" and (
self.dtype != jnp.float16 or self.softmax_type != AttnSoftmaxType.LEARNABLE_SOFTMAX
):
pytest.skip(
"Trimmed SWA+bias/dropout config: only float16 + learnable-softmax runs at L1"
)

# TODO(KshitijLakhani): probably add/move this to is_fused_attn_available
if self.qkv_layout.is_thd() and not self.attn_mask_type.is_padding():
pytest.skip("THD format requires padding masks.")
Expand Down Expand Up @@ -584,8 +650,21 @@ def _check_configs(self):
"is either BSHD_BSHD_BSHD or THD_THD_THD"
)

self.backend = FusedAttnHelper(
bias_batch = bias_heads = bias_seqlen_q = bias_seqlen_kv = None
if self.attn_bias_type == AttnBiasType.POST_SCALE_BIAS:
if self.bias_shape == BiasShape._1HSS:
bias_batch, bias_heads = 1, self.num_heads_q
elif self.bias_shape == BiasShape._B1SS:
bias_batch, bias_heads = self.batch_size, 1
elif self.bias_shape == BiasShape._BHSS:
bias_batch, bias_heads = self.batch_size, self.num_heads_q
elif self.bias_shape == BiasShape._11SS:
bias_batch, bias_heads = 1, 1
bias_seqlen_q, bias_seqlen_kv = self.max_seqlen_q, self.max_seqlen_kv

self.backend, message = FusedAttnHelper(
self.is_training,
self.batch_size,
self.dtype,
self.dtype,
self.qkv_layout,
Expand All @@ -600,9 +679,14 @@ def _check_configs(self):
self.head_dim_qk,
self.head_dim_v,
(-1, -1) if self.window_size is None else self.window_size,
bottom_right_diagonal=self.attn_mask_type.is_bottom_right(),
bias_batch=bias_batch,
bias_heads=bias_heads,
bias_seqlen_q=bias_seqlen_q,
bias_seqlen_kv=bias_seqlen_kv,
).get_fused_attn_backend()
if self.backend != NVTE_Fused_Attn_Backend.NVTE_F16_arbitrary_seqlen:
pytest.skip("Unsupported inputs combination or device compute capability.")
pytest.skip(message)

if (
self.attn_bias_type == AttnBiasType.POST_SCALE_BIAS
Expand Down
Loading
Loading