Skip to content

fix: preserve GGUF metadata at buffer boundaries - #2458

Open
remote-controlled-man wants to merge 1 commit into
abetlen:mainfrom
remote-controlled-man:codex/prflow-1f27289742a1
Open

remote-controlled-man wants to merge 1 commit into
abetlen:mainfrom
remote-controlled-man:codex/prflow-1f27289742a1

Conversation

@remote-controlled-man

Copy link
Copy Markdown
Contributor

Fix metadata reads that exactly fill the current native buffer.

The llama.cpp metadata APIs return the string length without the terminating NUL byte. The Python wrapper only resized for lengths greater than the buffer capacity, so a key or value of exactly that capacity lost its final byte. A truncated UTF-8 character caused Llama to discard all metadata and fall back to the wrong chat format; a truncated Jinja comment could make an otherwise valid model fail to load.

Resize for lengths greater than or equal to the capacity in both key and value reads. Add 17 regressions using actual GGUF files and the native loader: ASCII/UTF-8 keys and values at 1023/1024/1025 bytes, chat-template selection, and a value exactly filling a buffer enlarged by a longer key. No binding or vendor changes.

Validation:

  • Before the fix, the regression matrix has 7 failures and 10 passing controls; after the fix all 17 pass.
  • A real TinyLlama stories15M-q4_0.gguf with inserted metadata reproduces the load failure at a 1024-byte chat template and the UTF-8 metadata fallback. After the fix, all four end-to-end cases load, retain their metadata and use chat_template.default; the same chat request has 4 prompt tokens and identical greedy output. The UTF-8 fallback previously used llama-2, 15 prompt tokens and different output.
  • make test: 43 passed, no skips, on the final commit.
  • make lint: Ruff checks passed; all 30 Python files pass the formatting check.
  • git diff --check: passed.

Environment: macOS arm64, Python 3.12.14, bundled llama.cpp f5e6915c2, existing native build; end-to-end model runs use n_gpu_layers=0 and fixed inputs.

Copilot AI balanced review requested due to automatic review settings November 27, 2026 13:36

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.

This branch has not been deployed

No deployments
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.

2 participants