Skip to content

Fix shared-memory value buffer offset in CUDA CTC top-k kernel - #4213

Open
saisharan0103 wants to merge 1 commit into
pytorch:mainfrom
saisharan0103:mergepath/issue-4181-pointer-arithmetic-bug-in-first-matrix
Open

Fix shared-memory value buffer offset in CUDA CTC top-k kernel#4213
saisharan0103 wants to merge 1 commit into
pytorch:mainfrom
saisharan0103:mergepath/issue-4181-pointer-arithmetic-bug-in-first-matrix

Conversation

@saisharan0103

Copy link
Copy Markdown

Fixes #4181.

first_matrix__bitonic_topk_kernel computed block_topk_value from block_topk_key with block_topk_key + sizeof(float) * beam. Since block_topk_key is a float*, that pointer addition was scaled again by sizeof(float), placing the int value buffer 16 * beam bytes after the key buffer instead of the intended 4 * beam bytes.

The shared-memory allocation only reserves space for beam float keys followed by beam int values, so queue.store could write top-k values beyond the allocated result region and corrupt output or fail on architectures with stricter shared-memory bounds checking.

This changes the value buffer pointer to start immediately after the beam float keys while keeping the fix local to first_matrix__bitonic_topk_kernel, so the existing smem_size calculation in CTC_prob_first_step_V2 remains consistent.

Coverage was added to the CUDA CTC decoder test with a deterministic decode path that exercises the first-step top-k kernel and checks stable CUDA decoder output shape/content.

ruff check src/libtorchaudio/cuctc/src/ctc_prefix_decoder_kernel_v2.cu test/torchaudio_unittest/models/decoder/cuda_ctc_decoder_test.py reports no new findings on the changed files.
Ran pytest -x locally with no new failures.

…bitonic_topk_kernel` causes shared memory out-of-bounds write

Closes pytorch#4181
@saisharan0103
saisharan0103 requested a review from a team as a code owner August 2, 2026 17:15
@pytorch-bot

pytorch-bot Bot commented Aug 2, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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

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

@meta-cla

meta-cla Bot commented Aug 2, 2026

Copy link
Copy Markdown

Hi @saisharan0103!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

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.

Pointer arithmetic bug in first_matrix__bitonic_topk_kernel causes shared memory out-of-bounds write

1 participant