Skip to content

fix: size Python buffers to the actual context - #2436

Merged
abetlen merged 4 commits into
abetlen:mainfrom
remote-controlled-man:codex/prflow-afa54df0df08
Nov 22, 2026
Merged

abetlen merged 4 commits into
abetlen:mainfrom
remote-controlled-man:codex/prflow-afa54df0df08

Conversation

@remote-controlled-man

Copy link
Copy Markdown
Contributor

Fixes #1438.

llama.cpp rounds context capacities up, but Python allocated token history and full logits using the requested n_ctx. With n_ctx=200, the native capacity is 256: full-logits generation fails after position 200, and ordinary generation silently loses the remaining evaluated token history.

Allocate both buffers using the native capacity already used by completion limits. The existing small-model fixture covers n_ctx=0, 200 and 256 with both logits modes and explicit full-logits prompt lookup, forcing generation to the context boundary and checking usage and exact token history.

Reproducer on main (CPU):

from contextlib import closing
from huggingface_hub import hf_hub_download
from llama_cpp import Llama

path = hf_hub_download("ggml-org/models", "tinyllamas/stories15M-q4_0.gguf")
with closing(Llama(model_path=path, n_ctx=200, logits_all=True,
           n_gpu_layers=0, verbose=False)) as model:
    token = model.tokenize(b"the", add_bos=False)[-1]
    model("Hello " * 32, max_tokens=1000, temperature=0,
          echo=True, logit_bias={str(token): 1000})

Main raises ValueError: could not broadcast input array from shape (32000,) into shape (0,); the fix completes normally.

The separate implicit logits-mode selection issue is tracked in #2384.

Validation: final regressions give 6 failures and 3 passing controls on original main, then 9 passes with the fix. Full make test: 35 passed. make lint: passed (Ruff 0.17.6).
Environment: macOS arm64 (Apple M5 Pro), Python 3.12.14, native llama.cpp afe77e18f, built with the installed macOS 26.5 SDK; regression runs use CPU layers.

AI assistance: Codex helped diagnose, implement, test and independently review this change.

Copilot AI balanced review requested due to automatic review settings November 22, 2026 02:55

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 was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@abetlen
abetlen merged commit c0d5530 into abetlen:main Nov 22, 2026
15 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.

Crashing with "IndexError: index 200 is out of bounds for axis 0 with size 200"

3 participants