mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #1891] [CLOSED] logit_bias: normalize a long list once per request (65,929 entries: +175 ms -> +24 ms) #3740
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#3740
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Niko1221/Strata/pull/1891
Author: @qni-live
Created: 10/10/2026
Status: ❌ Closed
Base:
main← Head:logit-bias-normalize-once📝 Commits (1)
f29a369logit_bias: normalize a long list once per request instead of five times📊 Changes
2 files changed (+146 additions, -3 deletions)
View changed files
📝
serve/logit_bias.py(+64 -3)📝
serve/test_logit_bias.py(+82 -0)📄 Description
Follow-up to #1646 (and #1627). One file changed (
serve/logit_bias.py) plus tests. No engine change, no new option, the default path returns and raises exactly what it did.Result: with a 65,929-entry list a request now costs +24 ms instead of +175 ms on a live 0.1.42 engine; the normalization alone drops from 204 ms to 8 ms (five calls on a freshly parsed list). Same results, same errors, 12 tests (5 existing, 7 new).
Why
A standing ban of a whole script is a long list: all Han, Kana and Hangul tokens of the vocabulary are 65,929 IDs (1.05 MB of JSON), sent with every request. On v0.1.42 it is normalized five times per request:
bias_api.normalizein_openai(line 5217),validate_requestat 5228 and again ingenerate(1569), andengine_keytwice (sampling_keysruns for the cache key at 1253 and for the GEN line at 1577; eachengine_keynormalizes, sorts and joins a ~790 KB string). In isolation that is about 200 ms of Python per request, whatever the answer length.Measured on a live 0.1.42 engine (2x RTX PRO 4500 layer split, Linux, 7,091-token prompt with prefix-cache hit, temperature 0, 9 runs per cell, medians): the list cost +175 ms at
max_tokens1, +173 ms at 50 and +180 ms at 400 (+10 to +18 % on a one-second agent turn). The engine's own decode rate is not affected (-1 %).What changed
normalize()andengine_key()keep their signatures and results. The module remembers the last normalized lists:_last;vocab_sizeis part of the key, so validation against the vocabulary is unchanged);The key is the JSON text, not the list: Python compares
[[7, False]]and[[7, 0]]as equal, and a==key would turn a ban into "no bias".normalize()hands out a copy, so a caller cannot change a cached entry.engine_key()builds its string once per cached list.How it was checked
python -m unittest serve.test_logit_bias: the 5 existing tests and 7 new ones pass (same answers as the uncached function for dict and pair-list forms and both vocabulary sizes;Falseis not0in either order; bad lists are never stored, includingTrue == 1, a duplicate ID and NaN; vocabulary size is part of the key; the result is a copy; an equal list in a later request is a hit; the cache is bounded).engine_keystring (786,687 characters). A cold first request is 99 ms.What it leaves alone
serve/does.Not tested: HIP and SYCL engines (Python only, no backend file touched),
--batch(rejected before this code, unchanged).Written with AI assistance (Claude); numbers reviewed by me.
🤖 Generated with Claude Code
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.