fix(cuda): KVarN decode combine kernel crashes at ≥768K context (missing shared-mem opt-in) - #116
Open
chimpera wants to merge 1 commit into
Open
Conversation
… mem The KVarN decode combine reduction kernel (ggml_cuda_fattn_kvarn_decode_combine_kernel) holds n_splits partials in dynamic shared memory, where n_splits = ceil(n_kv / SPLIT_TOKENS) (SPLIT_TOKENS == 64) grows linearly with context. Without opting the kernel into the larger dynamic-shared-memory limit, the launch hits CUDA's 48KB default per-block ceiling once n_splits * sizeof(float) >= 49152 B, i.e. at n_kv >= ~786432 tokens, and fails with cudaErrorInvalidConfiguration (surfaced as "CUDA error: invalid argument"), aborting the server. Any kvarn-KV preset (--cache-type-k/-v kvarn* + --flash-attn on) therefore hard-crashes at the prefill->decode boundary for contexts above ~768K, even though --ctx-size allows 1M. The sibling MMA kernels in fattn-mma-kvarn-case.cuh already call cudaFuncSetAttribute(cudaFuncAttributeMaxDynamicSharedMemorySize, ...); the combine kernel in fattn-mma-kvarn-decode.cuh was missed. Add the same opt-in there, clamped to the device's cudaDevAttrMaxSharedMemoryPerBlockOptin and guarded by GGML_USE_MUSA, matching the existing pattern. Verified on RTX 5090 (sm_120): a 900K-context needle retrieval that hard-crashed pre-fix now completes (~449 tok/s prefill, correct retrieval, server survives past the prefill->decode boundary). No regressions at small context. Co-Authored-By: Claude <noreply@anthropic.com>
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.
Problem
Any KVarN-KV preset (
--cache-type-k/-v kvarn*+--flash-attn on) hard-crashes the server at contexts above ~768K (e.g. a 1M--ctx-sizebuild dies at the prefill→decode boundary):Prefill completes, then the server aborts on the first decode step. Not OOM and not a quality issue — the answer never gets to return.
Root cause
The KVarN decode combine reduction kernel holds
n_splitspartials in dynamic shared memory, wheren_splits = ceil(n_kv / SPLIT_TOKENS)andSPLIT_TOKENS == 64. That shared-mem request grows linearly with context:CUDA's default per-block dynamic-shared-memory limit is 48 KB (49152 B); requesting more requires opting the kernel in via
cudaFuncAttributeMaxDynamicSharedMemorySize. The sibling MMA kernels infattn-mma-kvarn-case.cuhalready do this (lines 424/538/715/812/817), but the combine kernel infattn-mma-kvarn-decode.cuhwas missed — so at n_kv ≥ 786432 the launch is rejected withcudaErrorInvalidConfiguration("invalid argument"). Threshold matches exactly: 786432 = 12288 × 64 = 49152 / 4.Fix
Add the same
cudaFuncSetAttributeopt-in for the combine kernel before its launch, clamped to the device'scudaDevAttrMaxSharedMemoryPerBlockOptinand guarded byGGML_USE_MUSA, mirroring the existing pattern. One localized change infattn-mma-kvarn-decode.cuh.Verification
On an RTX 5090 (sm_120), a 900K-context needle-retrieval request that hard-crashed before the patch now completes: prefill ~449 tok/s, correct retrieval,
finish=stop, server survives past the prefill→decode boundary. No regression at small context (32K–640K unchanged).The patched file is identical between
mainandv0.4.3, so this applies cleanly to either; targeted atv0.4.3since that's where it was developed/tested — feel free to retarget tomain.🤖 Generated with Claude Code