Skip to content

fix(cuda): guard exhausted candidates in TopK stage 2 - #2104

Merged
jordimas merged 2 commits into
OpenNMT:masterfrom
mzggr0914:fix/cuda-topk-exhausted-candidates
Oct 5, 2026
Merged

jordimas merged 2 commits into
OpenNMT:masterfrom
mzggr0914:fix/cuda-topk-exhausted-candidates

Conversation

@mzggr0914

Copy link
Copy Markdown
Contributor

Skip the invalidation store when a reduction returns NOT_FOUND. Add regression coverage for exhausted rows, masked values, and multiple k values.

Validation: 12 TopK tests passed on Windows CUDA.

AI-assisted analysis, implementation, and test execution.

Skip the invalidation store when a reduction returns NOT_FOUND. Add regression coverage for exhausted rows, masked values, and multiple k values.

Validation: 12 TopK tests passed on Windows CUDA.

AI-assisted analysis, implementation, and test execution.
Copilot AI lite review requested due to automatic review settings September 26, 2026 03:46

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

No unresolved blocking issues were identified.

Review effort: Lite
Findings: None

What changed in this PR

Fixes CUDA TopK stage 2 handling for exhausted candidates and adds regression coverage.

Changes:

  • Skip invalidation stores when reduction returns NOT_FOUND.
  • Add tests for masked values, repeated calls, row positions, data types, and multiple k values.
File Description
tests/​ops_test.cc Adds comprehensive TopK regression coverage.
src/​ops/​topk_gpu.cu Prevents invalid memory writes for exhausted reductions.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@jordimas

Copy link
Copy Markdown
Collaborator

Could you also run the new regression test under CUDA Compute Sanitizer, ideally before and after the fix? The invalid write may not reliably cause an output mismatch, so this would help confirm the regression is caught.

@mzggr0914

Copy link
Copy Markdown
Contributor Author

Could you also run the new regression test under CUDA Compute Sanitizer, ideally before and after the fix? The invalid write may not reliably cause an output mismatch, so this would help confirm the regression is caught.

Thanks, agreed — the output assertions alone may miss the invalid write. I also ran the regression under Compute Sanitizer memcheck in the local validation; the before/after logs are attached.

The before-fix build contains the new regression test on d44d2d0, but not the kernel guard. The fixed kernel and test sources match fbfd438.

Environment: Windows x64, RTX 4070 SUPER, CUDA 12.8.93, Compute Sanitizer 2025.1.0.0; Release build with -lineinfo.

Build Test filter Allocator Result
Before fix CUDA/OpDeviceFPTest.TopKWithExhaustedCandidates/float16 cuda_malloc_async Invalid 2-byte global write in topk_stage_2, topk_gpu.cu:262; exit 99
After fix *TopK* cuda_malloc_async 12 tests passed; 0 memcheck errors; exit 0
After fix *TopK* cub_caching 12 tests passed; 0 memcheck errors; exit 0

All runs used --tool memcheck --error-exitcode 99. The after-fix suite includes the same CUDA FP16 regression. The attachment contains the full logs and reproduction commands.

This confirms that memcheck catches the regression without relying on an output mismatch.

after-memcheck-async.txt
after-memcheck-cub.txt
before-memcheck.txt

@mzggr0914

Copy link
Copy Markdown
Contributor Author

please let me know if anything else is needed from my side.
@jordimas

@jordimas

jordimas commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator

Thanks, great job!

@jordimas
jordimas merged commit 998bb99 into OpenNMT:master Oct 5, 2026
22 checks passed
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.

3 participants