Skip to content

Studio: walk each GGUF tokenizer array once per process and seek past it on later reads - #15687

Merged
danielhanchen merged 9 commits into
mainfrom
perf/gguf-walk-once
Oct 28, 2026
Merged

danielhanchen merged 9 commits into
mainfrom
perf/gguf-walk-once

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

Follow-up to #15593 (buffered GGUF string walks, merged).

A cold local GGUF load reads the header many times before llama-server starts: _read_gguf_metadata runs for the route preflight, the diffusion check, the non-chat refusal check and the real read, and _gguf_scan_tensor_bytes plus each gguf_metadata reader (general, chat template, nextn, embedding length, staged dims, MTP standalone, mrope, TTS) walks the same tokenizer arrays again. With #15593 each walk is buffered, but there are still 23 to 36 walks of 150k to 262k entries per file. This PR walks each array once per process and replays later walks of the same unchanged file as one seek.

What changes

  • utils/models/gguf_metadata.py: skip_gguf_strings and take_gguf_strings_by_first_byte get a small memo in front of the unchanged walks. A hit seeks to the remembered end and returns the remembered count (and, for take, a new list of the remembered entries).
  • Key: (st_dev, st_ino, st_size, st_mtime_ns, st_ctime_ns) from os.fstat on the open file, plus the start offset, the entry count and the kind of walk. A finished take also answers a skip of the same array, since every entry was whole and at most 1 MiB.
  • Only plain files opened with open(path, "rb") take part. BytesIO, wrappers, pipes and streams that cannot seek back never do.
  • A walk is not remembered when the file reports inode 0, when its mtime or ctime is less than 3 s before the walk began (covers FAT's 2 s and HFS+ 1 s timestamps, so a later same-size rewrite always moves a timestamp past the stored one), when fstat after the walk differs from before it (written during the walk), or when the kept entries of a take exceed 1 MiB. Growing files (downloads) change size and are recent.
  • LRU of 256 entries under one lock. In practice only the load path calls take, and its kept entries are tens of KB.
  • Nothing changes in what any reader returns, raises, or where it leaves the file position.

Results

Fresh process per sample, running the readers of a cold local load in order (4x _read_gguf_metadata, _gguf_scan_tensor_bytes, 8 gguf_metadata readers), base = #15593 head, interleaved, n=8 per arm. Walk counts and bytes read are deterministic.

file (vocab) base CPU ms median [range] this PR walks bytes read
Qwen3-0.6B Q8_0 (152k) 761 [528-800] 161 [120-186] 23 -> 2 66.9 MB -> 9.0 MB
Qwen3.5-9B Q8_0 (248k) 1237 [850-1305] 262 [174-292] 36 -> 3 127.3 MB -> 16.6 MB
gemma-4-E2B Q4_K_M (262k) 1667 [1347-1960] 320 [256-390] 36 -> 3 171.6 MB -> 20.0 MB
gpt-oss-20b Q2_K (201k) 1385 [1092-1631] 254 [214-290] 36 -> 3 152.4 MB -> 17.4 MB
Mistral-Small-3.2-24B Q4_K_M (131k) 834 [667-1003] 173 [130-190] 23 -> 2 92.0 MB -> 10.7 MB

Base and head ranges do not overlap for any file. A separate rerun of the Qwen3-0.6B row (3 rounds per arm) gave 543 to 575 ms vs 93 to 140 ms, with the same 23 -> 2 walks and 66.9 MB -> 9.0 MB read.

Equivalence: 60 real GGUF headers plus 300 fuzzed ones (chunk straddles, entries over 1 MiB, corrupt lengths, truncation, invalid UTF-8) through 18 reader entry points, comparing return values, exception type and message and the final tell() on files and BytesIO: 17,736 checks, 0 differences, with the memo warm across readers. Calling every reader three times on a warm memo also gives 0 differences. Python 3.9, 3.10, 3.11, 3.12 and 3.14: 0 mismatches.

Tests

  • New tests/test_gguf_walk_memo.py (20): a hit reads no entries and equals a fresh walk; a finished take answers the skip; hits return a new list; offset and count are part of the key; a same-size rewrite with the old mtime restored, a rename over the file with the old mtime, an in-place rewrite where ctime is the creation time (as on Windows), a recently changed file, a growing file, inode 0, a change during the walk and BytesIO are all walked again; the LRU bound and the 1 MiB kept cap hold; 8 threads agree; the llama reader and the metadata readers share one walk. Base (Studio: walk GGUF tokenizer string arrays through one buffer #15593 head, test copied in): 2 failed, 12 errors. This PR: 20 passed.
  • Existing test_gguf_string_walk.py, test_gguf_metadata.py, test_gguf_header_delta.py, test_gguf_admission_token_count.py, test_gguf_image_capability.py, test_gguf_metadata_log_volume.py: all pass on both base and this PR.

Not covered

  • Run on Linux x64 only. No Windows, macOS or ARM64 run, and no FAT, SMB or OneDrive volume. The code is standard library only (os.fstat, stat.S_ISREG, time.time_ns) with no OS branches.
  • On Windows st_ctime is the creation time, so a rewrite in place that keeps the same size and also restores the exact old mtime (to 100 ns) would not be seen until the process restarts. A rename or copy over the file changes the file index, and any normal write moves mtime. The existing metadata caches in this module trust the same fields.
  • Network shares whose server clock is more than 3 s behind the client, combined with a same-size rewrite inside one timestamp tick, are not covered by the settle window.
  • No full Studio load with llama-server; the read sequence comes from the call sites.

Base automatically changed from studio-gguf-buffered-walk to main October 28, 2026 08:15
@danielhanchen danielhanchen self-assigned this Oct 28, 2026
@danielhanchen
danielhanchen marked this pull request as ready for review October 28, 2026 09:23
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-28T09:48:00.397531Z ec744aa Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: 033b88ba0a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

shimmyshimmer pushed a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Oct 28, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: ec744aadc6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit d2d6f76 into main Oct 28, 2026
2 of 41 checks passed
@danielhanchen
danielhanchen deleted the perf/gguf-walk-once branch October 28, 2026 10:55
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.

1 participant