Skip to content

fix(perplexity): cast n_ctx * nv to size_t in KL logits save (Qwen3 16K crash) - #138

Merged
TheTom merged 1 commit into
TheTom:feature/turboquant-kv-cachefrom
Defilan:fix/perplexity-kl-save-int-overflow
May 9, 2026
Merged

TheTom merged 1 commit into
TheTom:feature/turboquant-kv-cachefrom
Defilan:fix/perplexity-kl-save-int-overflow

Conversation

@Defilan

@Defilan Defilan commented May 9, 2026

Copy link
Copy Markdown

What this fixes

When llama-perplexity is run with --kl-divergence-base to save logits for KL-divergence comparison, the log_probs vector at tools/perplexity/perplexity.cpp:527 is sized with an int×int multiplication that can overflow on Qwen3-class large-vocab models at 16K context.

// before
log_probs.resize(n_ctx * nv);

// after
log_probs.resize(size_t(n_ctx) * nv);

The same size_t cast already exists 13 lines above (line 514) for the parallel logits.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):

nv          = 2 * ((n_vocab + 1) / 2) + 4 = 151,940
n_ctx * nv  = 16384 * 151,940              = 2,489,610,240
INT32_MAX                                  = 2,147,483,647
                                              ↑ overflow

The wrapped negative result sign-extends to a giant size_t when passed to vector::resize, exceeds vector::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 (architecture qwen35moe).
Build: pre-fix HEAD of feature/turboquant-kv-cache (e69af78).

llama-perplexity \
  -m Qwen3.6-35B-A3B-Q8_0.gguf \
  -f wiki.test.raw \
  -ctk f16 -ctv f16 \
  -ngl 99 -fa 1 \
  --threads 6 \
  -c 16384 \
  --kl-divergence-base /tmp/baseline.dat

Output:

perplexity: saving all logits to /tmp/baseline.dat
perplexity: tokenizing the input ..
perplexity: tokenization took 316.362 ms
perplexity: calculating perplexity over 18 chunks, n_ctx=16384, batch_size=2048, n_seq=1
WARNING: Using native backtrace. Set GGML_BACKTRACE_LLDB for more info.
...
libc++abi: terminating due to uncaught exception of type
  std::length_error: vector

Stack frame 7 in the backtrace points at perplexity(llama_context*, common_params const&, int) + 9748, with __throw_length_error two frames above. That offset corresponds to the log_probs.resize call 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:

Cache PPL KL divergence vs f16 Top-1 agreement
f16 5.3513 ± 0.032 (baseline) —
q8_0 5.3508 ± 0.032 0.001362 ± 2.3e-5 98.708%
turbo3 5.3907 ± 0.032 0.012050 ± 1.4e-4 95.293%

Per-test wall: ~190 seconds. Three runs total ~10 minutes.

Notes

  • Mac/Metal build verified end-to-end (compile + run).
  • Behavior change is limited to the KL-save code path; non-KL llama-perplexity runs are unaffected.
  • This is upstream-relevant: the same overflow exists in ggml-org/llama.cpp:master since 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.

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%
@TheTom
TheTom merged commit 7e34166 into TheTom:feature/turboquant-kv-cache May 9, 2026
1 check passed
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%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants