Repository navigation
feat(acceleration): remove silent k>kMaxK clamping in CUDA HNSW kernel (v1.8.0, Issue #132) - #4320
Merged
Merged
Conversation
… 1024 with dynamic shared memory Co-authored-by: makr-code <[email protected]>
…(), remove goto, use __builtin_clz Co-authored-by: makr-code <[email protected]>
Copilot
AI
changed the title
[WIP] [CUDA-xxx] Remove silent k > kMaxK clamping in HNSW kernel
feat(acceleration): remove silent k>kMaxK clamping in CUDA HNSW kernel (v1.8.0, Issue #132)
Mar 18, 2026
Owner
|
@copilot Review und Code Audit. Wurden alle Aufgaben des issue erfüllt? |
makr-code
approved these changes
Mar 18, 2026
Copilot stopped work on behalf of
makr-code due to an error
March 18, 2026 06:37
Owner
|
@copilot retry |
makr-code
approved these changes
Mar 18, 2026
Owner
|
@copilot retry |
makr-code
marked this pull request as ready for review
March 18, 2026 10:31
Copilot stopped work on behalf of
makr-code due to an error
March 18, 2026 10:31
4 of 25 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The CUDA HNSW launcher was silently clamping
k > kMaxKtokMaxK=512, returning fewer results than requested with no indication to the caller. Re-ranking pipelines requestingk=512+received silently truncated output.Kernel Changes (
cuda_hnsw_kernels.cu)kMaxK512 → 1024; result buffers (res_dist,res_id) moved from fixed-size local arrays toextern __shared__dynamic shared memory, partitioned per-threadcomputeThreadsPerBlock(k)computes block size via__builtin_clzto keep shared-memory usage ≤ 48 KB at any k:1u << (31 - __builtin_clz(min(128, 49152 / (k*8))))entry_nodeparameter added to support multi-pass launches from non-zero entry pointsbool* h_overflowoutput flag — set and kernel not launched whenk > kMaxK;assert()fires in debug builds instead of silent truncationTraversal Engine (
cuda_hnsw_graph_traversal.cpp)h_overflow⌈k/1024⌉-pass host-merge strategy — each pass uses a spread entry node (pass * num_nodes / num_passes), results deduplicated by node ID then top-k selected viastd::partial_sort; falls back to CPU if any GPU allocation failsBackend Error Propagation (
cuda_backend.cpp)kHnswSinglePassMaxK = 1024uconstant guards bothannBatchSearch()andbatchKnnSearch()setError(InvalidInputShape, ...)→getHealthStatus()automatically returnsBackendHealthStatus::makeDegraded()Tests & CI
tests/test_cuda_hnsw_large_k.cpp: 7 tests covering k=257, k=512, k=1024, k=1025 (multi-pass), health-degraded assertion (release-only), sort order, multiple queries — all run via CPU fallback, no GPU requiredtests/CMakeLists.txt:test_cuda_hnsw_large_k_focusedtarget; excluded from main suite whenTHEMIS_ENABLE_GPU=OFF.github/workflows/02-feature-modules/acceleration/cuda-hnsw-large-k-ci.ymltriggers on changes to kernel/traversal/backend/test filessrc/acceleration/FUTURE_ENHANCEMENTS.md: all roadmap items marked[x]Type of Change
Testing
📚 Research & Knowledge (wenn applicable)
/docs/research/angelegt?/docs/research/implementation_influence/eingetragen?Relevante Quellen:
Checklist
Original prompt
This section details on the original issue you should resolve
<issue_title>CUDA HNSW Kernel: Remove Silent
k > kMaxKClamping</issue_title><issue_description>### Context
This issue implements the roadmap item 'CUDA HNSW Kernel: Remove Silent
k > kMaxKClamping' for the acceleration domain. It is sourced from the consolidated roadmap under 🟡 Medium Priority — Near-term (v1.5.0 – v1.8.0) and targets milestone v1.8.0.Primary detail section: CUDA HNSW Kernel: Remove Silent
k > kMaxKClampingGoal
Deliver the scoped changes for CUDA HNSW Kernel: Remove Silent
k > kMaxKClamping in src/acceleration/ and complete the linked detail section in a release-ready state for v1.8.0.Detailed Scope
CUDA HNSW Kernel: Remove Silent
k > kMaxKClampingPriority: Medium
Target Version: v1.8.0
cuda/cuda_hnsw_kernels.cu:25definesstatic constexpr uint32_t kMaxK = 256u. The launch wrapper at line 325 silently clampsktokMaxKbefore launching:if (k > kMaxK) k = kMaxK;. The caller incuda_backend.cppreceives truncated results with no indication that fewer than the requestedkneighbours were returned. For use cases requiring k > 256 (e.g., re-ranking pipelines requesting k=512 candidates), this produces silently wrong output.Implementation Notes:
[ ]Replace the silent clamp incuda/cuda_hnsw_kernels.cu:325with an explicit error return: setcudaGetLastError()to a sentinel, or add abool* d_overflowoutput flag that the host can check; propagate the overflow condition up throughCUDAVectorBackend::buildHnswAnnIndex()/batchKnnSearchWithGraph()as anAccelerationErrorCode::InvalidInputShapeerror.[ ]IncreasekMaxKto 1024 by moving the per-query result buffers (res_dist[kMaxK],res_id[kMaxK]) from fixed-size shared memory arrays to dynamically allocated shared memory viaextern __shared__; compute required shared memory ask * (sizeof(float) + sizeof(int32_t))per thread and pass it as the third<<<>>>launch argument.[ ]For k > 1024 (extreme re-ranking): fall through to a multi-pass strategy — runkMaxK-at-a-time passes over graph layers and merge on host usingstd::partial_sort.[ ]Add static_assert or CUDA__trap()guard in debug builds whenk > kMaxKis detected; surface asBackendHealthStatus::makeDegraded()in release builds.[ ]Test: addtests/acceleration/test_cuda_hnsw_large_k.cppwith k=257, k=512, k=1024 asserting result count equals requested k.Performance Targets:
Acceptance Criteria
cuda/cuda_hnsw_kernels.cu:325with an explicit error return: setcudaGetLastError()to a sentinel, or add abool* d_overflowoutput flag that the host can check; propagate the overflow condition up throughCUDAVectorBackend::buildHnswAnnIndex()/batchKnnSearchWithGraph()as anAccelerationErrorCode::InvalidInputShapeerror.kMaxKto 1024 by moving the per-query result buffers (res_dist[kMaxK],res_id[kMaxK]) from fixed-size shared memory arrays to dynamically allocated shared memory viaextern __shared__; compute required shared memory ask * (sizeof(float) + sizeof(int32_t))per thread and pass it as the third<<<>>>launch argument.kMaxK-at-a-time passes over graph layers and merge on host usingstd::partial_sort.__trap()guard in debug builds whenk > kMaxKis detected; surface asBackendHealthStatus::makeDegraded()in release builds.tests/acceleration/test_cuda_hnsw_large_k.cppwith k=257, k=512, k=1024 asserting result count equals requested k.Relationships
References
Generated from the consolidated source roadmap. Keep the roadmap and issue in sync when scope changes.
</issue_description>
Comments on the Issue (you are @copilot in this section)
...
k > kMaxKClamping #3963🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.