Repository navigation
fix(perplexity): cast n_ctx * nv to size_t in KL logits save (Qwen3 16K crash) - #138
Merged
TheTom merged 1 commit intoMay 9, 2026
Conversation
When --kl-divergence-base is set, line 527 sizes the log_probs vector
with int * int. For Qwen3-class models (vocab=151,936) at n_ctx=16384,
the product is 16384 * 151940 = 2,489,610,240 which overflows INT32_MAX
(2,147,483,647). The wrapped negative value sign-extends to a giant
size_t when passed to vector::resize, exceeds vector::max_size, and
throws std::length_error.
Reproduced on M5 Max with Qwen3.6-35B-A3B Q8_0:
llama-perplexity -m <model> -f wiki.test.raw -c 16384 -fa 1 \
--kl-divergence-base /tmp/baseline.dat
perplexity: saving all logits to /tmp/baseline.dat
perplexity: tokenizing the input ..
perplexity: tokenization took 316 ms
perplexity: calculating perplexity over 18 chunks ...
libc++abi: terminating due to uncaught exception of type
std::length_error: vector
Smaller-vocab models (vocab <= ~128K) and shorter context
(n_ctx <= 8192) do not trip the overflow, which is why this only
surfaces on Qwen3 family at the standard 16K PPL bench depth.
Same size_t cast already exists at line 514 for the parallel
logits.reserve allocation; this brings line 527 to the same convention.
After fix the same command runs cleanly to completion. Validated A/B
on Qwen3.6-35B-A3B Q8_0 at 16K context against wikitext-2-raw:
Cache | PPL | KL Div | Top-1 agree
-------+------------------+-----------------+-------------
f16 | 5.3513 +/- 0.032 | (baseline) | -
q8_0 | 5.3508 +/- 0.032 | 0.0014 +/- 0e-5 | 98.708%
turbo3 | 5.3907 +/- 0.032 | 0.0121 +/- 1e-4 | 95.293%
Jashepp
pushed a commit
to Jashepp/llama-cpp-turboquant-tq3-merge
that referenced
this pull request
May 11, 2026
) When --kl-divergence-base is set, line 527 sizes the log_probs vector with int * int. For Qwen3-class models (vocab=151,936) at n_ctx=16384, the product is 16384 * 151940 = 2,489,610,240 which overflows INT32_MAX (2,147,483,647). The wrapped negative value sign-extends to a giant size_t when passed to vector::resize, exceeds vector::max_size, and throws std::length_error. Reproduced on M5 Max with Qwen3.6-35B-A3B Q8_0: llama-perplexity -m <model> -f wiki.test.raw -c 16384 -fa 1 \ --kl-divergence-base /tmp/baseline.dat perplexity: saving all logits to /tmp/baseline.dat perplexity: tokenizing the input .. perplexity: tokenization took 316 ms perplexity: calculating perplexity over 18 chunks ... libc++abi: terminating due to uncaught exception of type std::length_error: vector Smaller-vocab models (vocab <= ~128K) and shorter context (n_ctx <= 8192) do not trip the overflow, which is why this only surfaces on Qwen3 family at the standard 16K PPL bench depth. Same size_t cast already exists at line 514 for the parallel logits.reserve allocation; this brings line 527 to the same convention. After fix the same command runs cleanly to completion. Validated A/B on Qwen3.6-35B-A3B Q8_0 at 16K context against wikitext-2-raw: Cache | PPL | KL Div | Top-1 agree -------+------------------+-----------------+------------- f16 | 5.3513 +/- 0.032 | (baseline) | - q8_0 | 5.3508 +/- 0.032 | 0.0014 +/- 0e-5 | 98.708% turbo3 | 5.3907 +/- 0.032 | 0.0121 +/- 1e-4 | 95.293%
LogicDaemon
pushed a commit
to LogicDaemon/llama-cpp-turboquant
that referenced
this pull request
May 27, 2026
) When --kl-divergence-base is set, line 527 sizes the log_probs vector with int * int. For Qwen3-class models (vocab=151,936) at n_ctx=16384, the product is 16384 * 151940 = 2,489,610,240 which overflows INT32_MAX (2,147,483,647). The wrapped negative value sign-extends to a giant size_t when passed to vector::resize, exceeds vector::max_size, and throws std::length_error. Reproduced on M5 Max with Qwen3.6-35B-A3B Q8_0: llama-perplexity -m <model> -f wiki.test.raw -c 16384 -fa 1 \ --kl-divergence-base /tmp/baseline.dat perplexity: saving all logits to /tmp/baseline.dat perplexity: tokenizing the input .. perplexity: tokenization took 316 ms perplexity: calculating perplexity over 18 chunks ... libc++abi: terminating due to uncaught exception of type std::length_error: vector Smaller-vocab models (vocab <= ~128K) and shorter context (n_ctx <= 8192) do not trip the overflow, which is why this only surfaces on Qwen3 family at the standard 16K PPL bench depth. Same size_t cast already exists at line 514 for the parallel logits.reserve allocation; this brings line 527 to the same convention. After fix the same command runs cleanly to completion. Validated A/B on Qwen3.6-35B-A3B Q8_0 at 16K context against wikitext-2-raw: Cache | PPL | KL Div | Top-1 agree -------+------------------+-----------------+------------- f16 | 5.3513 +/- 0.032 | (baseline) | - q8_0 | 5.3508 +/- 0.032 | 0.0014 +/- 0e-5 | 98.708% turbo3 | 5.3907 +/- 0.032 | 0.0121 +/- 1e-4 | 95.293%
wel97459
pushed a commit
to wel97459/llama-cpp-turboquant
that referenced
this pull request
Jun 4, 2026
) When --kl-divergence-base is set, line 527 sizes the log_probs vector with int * int. For Qwen3-class models (vocab=151,936) at n_ctx=16384, the product is 16384 * 151940 = 2,489,610,240 which overflows INT32_MAX (2,147,483,647). The wrapped negative value sign-extends to a giant size_t when passed to vector::resize, exceeds vector::max_size, and throws std::length_error. Reproduced on M5 Max with Qwen3.6-35B-A3B Q8_0: llama-perplexity -m <model> -f wiki.test.raw -c 16384 -fa 1 \ --kl-divergence-base /tmp/baseline.dat perplexity: saving all logits to /tmp/baseline.dat perplexity: tokenizing the input .. perplexity: tokenization took 316 ms perplexity: calculating perplexity over 18 chunks ... libc++abi: terminating due to uncaught exception of type std::length_error: vector Smaller-vocab models (vocab <= ~128K) and shorter context (n_ctx <= 8192) do not trip the overflow, which is why this only surfaces on Qwen3 family at the standard 16K PPL bench depth. Same size_t cast already exists at line 514 for the parallel logits.reserve allocation; this brings line 527 to the same convention. After fix the same command runs cleanly to completion. Validated A/B on Qwen3.6-35B-A3B Q8_0 at 16K context against wikitext-2-raw: Cache | PPL | KL Div | Top-1 agree -------+------------------+-----------------+------------- f16 | 5.3513 +/- 0.032 | (baseline) | - q8_0 | 5.3508 +/- 0.032 | 0.0014 +/- 0e-5 | 98.708% turbo3 | 5.3907 +/- 0.032 | 0.0121 +/- 1e-4 | 95.293%
KGardevoir
pushed a commit
to KGardevoir/llama-cpp-turboquant
that referenced
this pull request
Jun 16, 2026
) When --kl-divergence-base is set, line 527 sizes the log_probs vector with int * int. For Qwen3-class models (vocab=151,936) at n_ctx=16384, the product is 16384 * 151940 = 2,489,610,240 which overflows INT32_MAX (2,147,483,647). The wrapped negative value sign-extends to a giant size_t when passed to vector::resize, exceeds vector::max_size, and throws std::length_error. Reproduced on M5 Max with Qwen3.6-35B-A3B Q8_0: llama-perplexity -m <model> -f wiki.test.raw -c 16384 -fa 1 \ --kl-divergence-base /tmp/baseline.dat perplexity: saving all logits to /tmp/baseline.dat perplexity: tokenizing the input .. perplexity: tokenization took 316 ms perplexity: calculating perplexity over 18 chunks ... libc++abi: terminating due to uncaught exception of type std::length_error: vector Smaller-vocab models (vocab <= ~128K) and shorter context (n_ctx <= 8192) do not trip the overflow, which is why this only surfaces on Qwen3 family at the standard 16K PPL bench depth. Same size_t cast already exists at line 514 for the parallel logits.reserve allocation; this brings line 527 to the same convention. After fix the same command runs cleanly to completion. Validated A/B on Qwen3.6-35B-A3B Q8_0 at 16K context against wikitext-2-raw: Cache | PPL | KL Div | Top-1 agree -------+------------------+-----------------+------------- f16 | 5.3513 +/- 0.032 | (baseline) | - q8_0 | 5.3508 +/- 0.032 | 0.0014 +/- 0e-5 | 98.708% turbo3 | 5.3907 +/- 0.032 | 0.0121 +/- 1e-4 | 95.293%
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.
What this fixes
When
llama-perplexityis run with--kl-divergence-baseto save logits for KL-divergence comparison, thelog_probsvector attools/perplexity/perplexity.cpp:527is sized with an int×int multiplication that can overflow on Qwen3-class large-vocab models at 16K context.The same
size_tcast already exists 13 lines above (line 514) for the parallellogits.reserve(size_t(n_ctx) * n_vocab)allocation, so this just brings line 527 to the same convention.The math
For Qwen3 family (vocab = 151,936):
The wrapped negative result sign-extends to a giant
size_twhen passed tovector::resize, exceedsvector::max_size, and throws.Smaller-vocab models (≤ ~128K vocab) and shorter context (≤ 8K) don't hit it. Qwen3 family at 16K is the trigger.
Reproducer (before fix)
Hardware: Apple M5 Max, 128 GB unified memory.
Model:
Qwen3.6-35B-A3B-Q8_0.gguf(architectureqwen35moe).Build: pre-fix HEAD of
feature/turboquant-kv-cache(e69af78).Output:
Stack frame 7 in the backtrace points at
perplexity(llama_context*, common_params const&, int) + 9748, with__throw_length_errortwo frames above. That offset corresponds to thelog_probs.resizecall at line 527.Validation (after fix)
Same command runs to completion; KL divergence and top-token agreement collected for f16, q8_0, and turbo3 KV cache types against wikitext-2-raw at 16K context:
Per-test wall: ~190 seconds. Three runs total ~10 minutes.
Notes
llama-perplexityruns are unaffected.ggml-org/llama.cpp:mastersince the perplexity tool was synced from upstream. Happy to also push a mirror PR there if useful.Demo context
Validating TurboQuant turbo3 KV at 256K context for an enterprise pitch next week. Walked into this bug while running the standard llama.cpp suite-B perplexity bench against Qwen3.6 to quantify the turbo3 quality cost. Without the fix, the KL-save path was unusable on Qwen3 family at any meaningful PPL bench depth.