Skip to content

[PrismNet][ROCm] Return the empty result from CK flash attention on an empty batch (#195812) - #195812

Closed
rmiao wants to merge 1 commit into
pytorch:mainfrom
rmiao:export-D118577322
Closed

rmiao wants to merge 1 commit into
pytorch:mainfrom
rmiao:export-D118577322

Conversation

@rmiao

@rmiao rmiao commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

Attention over zero rows is an empty result, not an error, and CUDA returns it. The ROCm
CK entries instead abort with "batch size must be positive" (mha_fwd_ck.hip:280,
mha_varlen_fwd_ck.hip:246), so the same graph serves fine on CUDA and dies on AMD. Eager
code rarely notices, because the composite scaled_dot_product_attention guards
numel == 0 and returns zeros; a compiled graph does not get that guard, since the sizes
are symbolic at trace time and it is traced away, leaving AOTI to call the backend op
directly. PrismNet hits this whenever no request routes to a segment and its attention
tower is handed zero rows -- normal at serving, and every other op in the graph absorbs it.

Both files already have a "nothing to compute" branch for seqlen_k == 0, so rather than
add a hand-written empty tuple this drops the check and extends that branch to an empty
batch. At zero rows the allocations above it are already the correct empty result, which
means only the kernel launch is skipped and the empty and non-empty paths cannot drift
apart in shape or dtype. While in mha_fwd_ck, the two drop_seed_offset assignments move
above the branch as well: they were set only on the launching path, but the
p_dropout > 0 epilogue below dereferences them unconditionally.

Measured on a local MI300X, zero-row _scaled_dot_product_flash_attention,
_scaled_dot_product_efficient_attention and _flash_attention_forward now all return the
empty result instead of aborting. Probing the rest of the DHEN op mix at zero rows found
nothing else to fix -- linear, addmm, matmul, layer_norm, softmax, bmm, baddbmm, cat and sum
all already absorb an empty batch -- so attention was the only gap.

Test Plan:
buck2 test fbcode//mode/opt-amd-gpu -c fbcode.rocm_arch=mi300 fbcode//caffe2/test:test_transformers_cuda -- --regex 'test_fused_attention_empty_batch'

https://www.internalfb.com/intern/testinfra/testrun/22236523204207138

Reviewed By: kqfu

Differential Revision: D118577322

cc @jeffdaily @sunway513 @jithunnair-amd @pruthvistony @ROCmSupport @jataylo @hongxiayang @naromero77amd @pragupta @jerrymannil @xinyazhang

@pytorch-bot

pytorch-bot Bot commented Sep 3, 2026

Copy link
Copy Markdown

This appears to be a diff that was exported from phabricator, but the PR author does not have sufficient permissions to run CI. @rmiao, please do step 2 of internal wiki to get write access so you do not need to get CI approvals in the future. If you think this is a mistake, please contact the Pytorch Dev Infra team.

@pytorch-bot

pytorch-bot Bot commented Sep 3, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/195812

Note: Links to docs will display an error until the docs builds have been completed.

✅ You can merge normally! (2 Unrelated Failures)

As of commit bc696ab with merge base 81deebd (image):

CRCR (non-blocking) - The following CRCR downstream CI jobs failed but are non-blocking:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorch-bot pytorch-bot Bot added the module: rocm AMD GPU support for Pytorch label Sep 3, 2026
@meta-codesync

meta-codesync Bot commented Sep 3, 2026

Copy link
Copy Markdown

@rmiao has exported this pull request. If you are a Meta employee, you can view the originating Diff in D118577322.

@meta-codesync meta-codesync Bot changed the title [PrismNet][ROCm] Return the empty result from CK flash attention on an empty batch [PrismNet][ROCm] Return the empty result from CK flash attention on an empty batch (#195812) Sep 3, 2026
@rmiao
rmiao force-pushed the export-D118577322 branch from 56f4332 to 0a49c49 Compare September 3, 2026 21:02
rmiao added a commit to rmiao/pytorch that referenced this pull request Sep 3, 2026
…n empty batch (pytorch#195812)

Summary:

Attention over zero rows is an empty result, not an error, and CUDA returns it. The ROCm
CK entries instead abort with "batch size must be positive" (`mha_fwd_ck.hip:280`,
`mha_varlen_fwd_ck.hip:246`), so the same graph serves fine on CUDA and dies on AMD. Eager
code rarely notices, because the composite `scaled_dot_product_attention` guards
`numel == 0` and returns zeros; a compiled graph does not get that guard, since the sizes
are symbolic at trace time and it is traced away, leaving AOTI to call the backend op
directly. PrismNet hits this whenever no request routes to a segment and its attention
tower is handed zero rows -- normal at serving, and every other op in the graph absorbs it.

Both files already have a "nothing to compute" branch for `seqlen_k == 0`, so rather than
add a hand-written empty tuple this drops the check and extends that branch to an empty
batch. At zero rows the allocations above it are already the correct empty result, which
means only the kernel launch is skipped and the empty and non-empty paths cannot drift
apart in shape or dtype. While in `mha_fwd_ck`, the two `drop_seed_offset` assignments move
above the branch as well: they were set only on the launching path, but the
`p_dropout > 0` epilogue below dereferences them unconditionally.

Measured on a local MI300X, zero-row `_scaled_dot_product_flash_attention`,
`_scaled_dot_product_efficient_attention` and `_flash_attention_forward` now all return the
empty result instead of aborting. Probing the rest of the DHEN op mix at zero rows found
nothing else to fix -- linear, addmm, matmul, layer_norm, softmax, bmm, baddbmm, cat and sum
all already absorb an empty batch -- so attention was the only gap.

Test Plan:
buck2 test fbcode//mode/opt-amd-gpu -c fbcode.rocm_arch=mi300 fbcode//caffe2/test:test_transformers_cuda -- --regex 'test_fused_attention_empty_batch'

https://www.internalfb.com/intern/testinfra/testrun/22236523204207138

Reviewed By: kqfu

Differential Revision: D118577322
rmiao added a commit to rmiao/pytorch that referenced this pull request Sep 3, 2026
…n empty batch (pytorch#195812)

Summary:

Attention over zero rows is an empty result, not an error, and CUDA returns it. The ROCm
CK entries instead abort with "batch size must be positive" (`mha_fwd_ck.hip:280`,
`mha_varlen_fwd_ck.hip:246`), so the same graph serves fine on CUDA and dies on AMD. Eager
code rarely notices, because the composite `scaled_dot_product_attention` guards
`numel == 0` and returns zeros; a compiled graph does not get that guard, since the sizes
are symbolic at trace time and it is traced away, leaving AOTI to call the backend op
directly. PrismNet hits this whenever no request routes to a segment and its attention
tower is handed zero rows -- normal at serving, and every other op in the graph absorbs it.

Both files already have a "nothing to compute" branch for `seqlen_k == 0`, so rather than
add a hand-written empty tuple this drops the check and extends that branch to an empty
batch. At zero rows the allocations above it are already the correct empty result, which
means only the kernel launch is skipped and the empty and non-empty paths cannot drift
apart in shape or dtype. While in `mha_fwd_ck`, the two `drop_seed_offset` assignments move
above the branch as well: they were set only on the launching path, but the
`p_dropout > 0` epilogue below dereferences them unconditionally.

Measured on a local MI300X, zero-row `_scaled_dot_product_flash_attention`,
`_scaled_dot_product_efficient_attention` and `_flash_attention_forward` now all return the
empty result instead of aborting. Probing the rest of the DHEN op mix at zero rows found
nothing else to fix -- linear, addmm, matmul, layer_norm, softmax, bmm, baddbmm, cat and sum
all already absorb an empty batch -- so attention was the only gap.

Test Plan:
buck2 test fbcode//mode/opt-amd-gpu -c fbcode.rocm_arch=mi300 fbcode//caffe2/test:test_transformers_cuda -- --regex 'test_fused_attention_empty_batch'

https://www.internalfb.com/intern/testinfra/testrun/22236523204207138

Reviewed By: kqfu

Differential Revision: D118577322
rmiao added a commit to rmiao/pytorch that referenced this pull request Sep 3, 2026
…n empty batch (pytorch#195812)

Summary:

Attention over zero rows is an empty result, not an error, and CUDA returns it. The ROCm
CK entries instead abort with "batch size must be positive" (`mha_fwd_ck.hip:280`,
`mha_varlen_fwd_ck.hip:246`), so the same graph serves fine on CUDA and dies on AMD. Eager
code rarely notices, because the composite `scaled_dot_product_attention` guards
`numel == 0` and returns zeros; a compiled graph does not get that guard, since the sizes
are symbolic at trace time and it is traced away, leaving AOTI to call the backend op
directly. PrismNet hits this whenever no request routes to a segment and its attention
tower is handed zero rows -- normal at serving, and every other op in the graph absorbs it.

Both files already have a "nothing to compute" branch for `seqlen_k == 0`, so rather than
add a hand-written empty tuple this drops the check and extends that branch to an empty
batch. At zero rows the allocations above it are already the correct empty result, which
means only the kernel launch is skipped and the empty and non-empty paths cannot drift
apart in shape or dtype. While in `mha_fwd_ck`, the two `drop_seed_offset` assignments move
above the branch as well: they were set only on the launching path, but the
`p_dropout > 0` epilogue below dereferences them unconditionally.

Measured on a local MI300X, zero-row `_scaled_dot_product_flash_attention`,
`_scaled_dot_product_efficient_attention` and `_flash_attention_forward` now all return the
empty result instead of aborting. Probing the rest of the DHEN op mix at zero rows found
nothing else to fix -- linear, addmm, matmul, layer_norm, softmax, bmm, baddbmm, cat and sum
all already absorb an empty batch -- so attention was the only gap.

Test Plan:
buck2 test fbcode//mode/opt-amd-gpu -c fbcode.rocm_arch=mi300 fbcode//caffe2/test:test_transformers_cuda -- --regex 'test_fused_attention_empty_batch'

https://www.internalfb.com/intern/testinfra/testrun/22236523204207138

Reviewed By: kqfu

Differential Revision: D118577322
rmiao added a commit to rmiao/pytorch that referenced this pull request Sep 3, 2026
…n empty batch (pytorch#195812)

Summary:

Attention over zero rows is an empty result, not an error, and CUDA returns it. The ROCm
CK entries instead abort with "batch size must be positive" (`mha_fwd_ck.hip:280`,
`mha_varlen_fwd_ck.hip:246`), so the same graph serves fine on CUDA and dies on AMD. Eager
code rarely notices, because the composite `scaled_dot_product_attention` guards
`numel == 0` and returns zeros; a compiled graph does not get that guard, since the sizes
are symbolic at trace time and it is traced away, leaving AOTI to call the backend op
directly. PrismNet hits this whenever no request routes to a segment and its attention
tower is handed zero rows -- normal at serving, and every other op in the graph absorbs it.

Both files already have a "nothing to compute" branch for `seqlen_k == 0`, so rather than
add a hand-written empty tuple this drops the check and extends that branch to an empty
batch. At zero rows the allocations above it are already the correct empty result, which
means only the kernel launch is skipped and the empty and non-empty paths cannot drift
apart in shape or dtype. While in `mha_fwd_ck`, the two `drop_seed_offset` assignments move
above the branch as well: they were set only on the launching path, but the
`p_dropout > 0` epilogue below dereferences them unconditionally.

Measured on a local MI300X, zero-row `_scaled_dot_product_flash_attention`,
`_scaled_dot_product_efficient_attention` and `_flash_attention_forward` now all return the
empty result instead of aborting. Probing the rest of the DHEN op mix at zero rows found
nothing else to fix -- linear, addmm, matmul, layer_norm, softmax, bmm, baddbmm, cat and sum
all already absorb an empty batch -- so attention was the only gap.

Test Plan:
buck2 test fbcode//mode/opt-amd-gpu -c fbcode.rocm_arch=mi300 fbcode//caffe2/test:test_transformers_cuda -- --regex 'test_fused_attention_empty_batch'

https://www.internalfb.com/intern/testinfra/testrun/22236523204207138

Reviewed By: kqfu

Differential Revision: D118577322
@rmiao
rmiao force-pushed the export-D118577322 branch from 0a49c49 to 497459d Compare September 9, 2026 03:39
rmiao added a commit to rmiao/pytorch that referenced this pull request Sep 9, 2026
…n empty batch (pytorch#195812)

Summary:

Attention over zero rows is an empty result, not an error, and CUDA returns it. The ROCm
CK entries instead abort with "batch size must be positive" (`mha_fwd_ck.hip:280`,
`mha_varlen_fwd_ck.hip:246`), so the same graph serves fine on CUDA and dies on AMD. Eager
code rarely notices, because the composite `scaled_dot_product_attention` guards
`numel == 0` and returns zeros; a compiled graph does not get that guard, since the sizes
are symbolic at trace time and it is traced away, leaving AOTI to call the backend op
directly. PrismNet hits this whenever no request routes to a segment and its attention
tower is handed zero rows -- normal at serving, and every other op in the graph absorbs it.

Both files already have a "nothing to compute" branch for `seqlen_k == 0`, so rather than
add a hand-written empty tuple this drops the check and extends that branch to an empty
batch. At zero rows the allocations above it are already the correct empty result, which
means only the kernel launch is skipped and the empty and non-empty paths cannot drift
apart in shape or dtype. While in `mha_fwd_ck`, the two `drop_seed_offset` assignments move
above the branch as well: they were set only on the launching path, but the
`p_dropout > 0` epilogue below dereferences them unconditionally.

Measured on a local MI300X, zero-row `_scaled_dot_product_flash_attention`,
`_scaled_dot_product_efficient_attention` and `_flash_attention_forward` now all return the
empty result instead of aborting. Probing the rest of the DHEN op mix at zero rows found
nothing else to fix -- linear, addmm, matmul, layer_norm, softmax, bmm, baddbmm, cat and sum
all already absorb an empty batch -- so attention was the only gap.

Test Plan:
buck2 test fbcode//mode/opt-amd-gpu -c fbcode.rocm_arch=mi300 fbcode//caffe2/test:test_transformers_cuda -- --regex 'test_fused_attention_empty_batch'

https://www.internalfb.com/intern/testinfra/testrun/22236523204207138

Reviewed By: kqfu

Differential Revision: D118577322
rmiao added a commit to rmiao/pytorch that referenced this pull request Sep 9, 2026
…n empty batch (pytorch#195812)

Summary:

Attention over zero rows is an empty result, not an error, and CUDA returns it. The ROCm
CK entries instead abort with "batch size must be positive" (`mha_fwd_ck.hip:280`,
`mha_varlen_fwd_ck.hip:246`), so the same graph serves fine on CUDA and dies on AMD. Eager
code rarely notices, because the composite `scaled_dot_product_attention` guards
`numel == 0` and returns zeros; a compiled graph does not get that guard, since the sizes
are symbolic at trace time and it is traced away, leaving AOTI to call the backend op
directly. PrismNet hits this whenever no request routes to a segment and its attention
tower is handed zero rows -- normal at serving, and every other op in the graph absorbs it.

Both files already have a "nothing to compute" branch for `seqlen_k == 0`, so rather than
add a hand-written empty tuple this drops the check and extends that branch to an empty
batch. At zero rows the allocations above it are already the correct empty result, which
means only the kernel launch is skipped and the empty and non-empty paths cannot drift
apart in shape or dtype. While in `mha_fwd_ck`, the two `drop_seed_offset` assignments move
above the branch as well: they were set only on the launching path, but the
`p_dropout > 0` epilogue below dereferences them unconditionally.

Measured on a local MI300X, zero-row `_scaled_dot_product_flash_attention`,
`_scaled_dot_product_efficient_attention` and `_flash_attention_forward` now all return the
empty result instead of aborting. Probing the rest of the DHEN op mix at zero rows found
nothing else to fix -- linear, addmm, matmul, layer_norm, softmax, bmm, baddbmm, cat and sum
all already absorb an empty batch -- so attention was the only gap.

Test Plan:
buck2 test fbcode//mode/opt-amd-gpu -c fbcode.rocm_arch=mi300 fbcode//caffe2/test:test_transformers_cuda -- --regex 'test_fused_attention_empty_batch'

https://www.internalfb.com/intern/testinfra/testrun/22236523204207138

Reviewed By: kqfu

Differential Revision: D118577322
…n empty batch (pytorch#195812)

Summary:

Attention over zero rows is an empty result, not an error, and CUDA returns it. The ROCm
CK entries instead abort with "batch size must be positive" (`mha_fwd_ck.hip:280`,
`mha_varlen_fwd_ck.hip:246`), so the same graph serves fine on CUDA and dies on AMD. Eager
code rarely notices, because the composite `scaled_dot_product_attention` guards
`numel == 0` and returns zeros; a compiled graph does not get that guard, since the sizes
are symbolic at trace time and it is traced away, leaving AOTI to call the backend op
directly. PrismNet hits this whenever no request routes to a segment and its attention
tower is handed zero rows -- normal at serving, and every other op in the graph absorbs it.

Both files already have a "nothing to compute" branch for `seqlen_k == 0`, so rather than
add a hand-written empty tuple this drops the check and extends that branch to an empty
batch. At zero rows the allocations above it are already the correct empty result, which
means only the kernel launch is skipped and the empty and non-empty paths cannot drift
apart in shape or dtype. While in `mha_fwd_ck`, the two `drop_seed_offset` assignments move
above the branch as well: they were set only on the launching path, but the
`p_dropout > 0` epilogue below dereferences them unconditionally.

Measured on a local MI300X, zero-row `_scaled_dot_product_flash_attention`,
`_scaled_dot_product_efficient_attention` and `_flash_attention_forward` now all return the
empty result instead of aborting. Probing the rest of the DHEN op mix at zero rows found
nothing else to fix -- linear, addmm, matmul, layer_norm, softmax, bmm, baddbmm, cat and sum
all already absorb an empty batch -- so attention was the only gap.

Test Plan:
buck2 test fbcode//mode/opt-amd-gpu -c fbcode.rocm_arch=mi300 fbcode//caffe2/test:test_transformers_cuda -- --regex 'test_fused_attention_empty_batch'

https://www.internalfb.com/intern/testinfra/testrun/22236523204207138

Reviewed By: kqfu

Differential Revision: D118577322
@rmiao
rmiao force-pushed the export-D118577322 branch from 497459d to bc696ab Compare September 9, 2026 14:40
rmiao added a commit to rmiao/pytorch that referenced this pull request Sep 9, 2026
…n empty batch (pytorch#195812)

Summary:

Attention over zero rows is an empty result, not an error, and CUDA returns it. The ROCm
CK entries instead abort with "batch size must be positive" (`mha_fwd_ck.hip:280`,
`mha_varlen_fwd_ck.hip:246`), so the same graph serves fine on CUDA and dies on AMD. Eager
code rarely notices, because the composite `scaled_dot_product_attention` guards
`numel == 0` and returns zeros; a compiled graph does not get that guard, since the sizes
are symbolic at trace time and it is traced away, leaving AOTI to call the backend op
directly. PrismNet hits this whenever no request routes to a segment and its attention
tower is handed zero rows -- normal at serving, and every other op in the graph absorbs it.

Both files already have a "nothing to compute" branch for `seqlen_k == 0`, so rather than
add a hand-written empty tuple this drops the check and extends that branch to an empty
batch. At zero rows the allocations above it are already the correct empty result, which
means only the kernel launch is skipped and the empty and non-empty paths cannot drift
apart in shape or dtype. While in `mha_fwd_ck`, the two `drop_seed_offset` assignments move
above the branch as well: they were set only on the launching path, but the
`p_dropout > 0` epilogue below dereferences them unconditionally.

Measured on a local MI300X, zero-row `_scaled_dot_product_flash_attention`,
`_scaled_dot_product_efficient_attention` and `_flash_attention_forward` now all return the
empty result instead of aborting. Probing the rest of the DHEN op mix at zero rows found
nothing else to fix -- linear, addmm, matmul, layer_norm, softmax, bmm, baddbmm, cat and sum
all already absorb an empty batch -- so attention was the only gap.

Test Plan:
buck2 test fbcode//mode/opt-amd-gpu -c fbcode.rocm_arch=mi300 fbcode//caffe2/test:test_transformers_cuda -- --regex 'test_fused_attention_empty_batch'

https://www.internalfb.com/intern/testinfra/testrun/22236523204207138

Reviewed By: kqfu

Differential Revision: D118577322
@meta-codesync

meta-codesync Bot commented Sep 9, 2026

Copy link
Copy Markdown

@pytorchbot merge

(Initiating merge automatically since Phabricator Diff has merged)

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Sep 9, 2026
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Adding the topic: not user facing label automatically: this co-dev PR is being merged after its internal diff landed, without a release-notes label. If this change is user facing, please replace the label with the appropriate release notes: ... one.

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

pytorchmergebot pushed a commit that referenced this pull request Sep 11, 2026
…lable (#196643)

test_fused_attention_empty_batch (#195812) forces preferred_rocm_fa_library("ck") on ROCm, and setting that preference raises on architectures without CK SDPA support, so the test has failed on every mi200 (gfx90a) run of rocm-mi200.yml since it landed (34 jobs between 2026-09-04 and 2026-09-10). Falling back to the default library is not an option: with AOTriton the same test aborts in backward with "batch size must be positive" (verified on gfx950), so the empty-batch fix covers the CK path only. This gates the test on PLATFORM_SUPPORTS_CK_SDPA, the predicate the other CK-only tests in this file already use, so gfx90a skips instead of erroring.

Test plan is in the commit message; gfx90a validation via ciflow/rocm-mi200 (expect the test to report skipped in the shard that runs test_transformers.py), gfx942/gfx950 keep running it.

This PR was authored with the assistance of an AI agent (Claude). The analysis and test results were verified by hand.

Pull Request resolved: #196643
Approved by: https://github.com/jeffdaily

Co-authored-by: Jeff Daily <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request Merged meta-exported module: rocm AMD GPU support for Pytorch topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants