Skip to content

laya scorer: load the tokenizer once, not on every score() call - #217

Merged
bong-water-water-bong merged 1 commit into
mainfrom
laya-scorer-tokenizer-once
Sep 29, 2026
Merged

bong-water-water-bong merged 1 commit into
mainfrom
laya-scorer-tokenizer-once

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Scorer::score() read and parsed tokenizer.json again on every call. On strixhalo that costs about 48 ms of each routing decision; the timings come from instrumenting score() while running the Laya encoder on the NPU. The scorer now keeps the tokenizer it already loads in load() for the special-token ids.

Measured on strixhalo with laya_classify_eval on the 200 routing cases (typed-decisions checkpoint, CPU scorer):

accuracy ms per decision
before 191/200 = 95.5% 411
after 191/200 = 95.5% 352

Scorer gets an out-of-line constructor and destructor, because it now holds a std::unique_ptr to the forward-declared npu::Tokenizer. It was never copied. score() was already not re-entrant (it writes err_), so sharing one tokenizer adds no new constraint.

Local ctest passes 12/12. The laya_gate golden tests aren't configured on this box because golden.json isn't present.

🤖 Generated with Claude Code

Scorer::score() read tokenizer.json again on every call, ~48 ms of each
routing decision on strixhalo. The scorer now keeps the tokenizer it loads
in load(). CPU scorer on the 200 routing cases: 411 -> 352 ms a decision,
191/200 either way.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@context7

context7 Bot commented Sep 29, 2026

Copy link
Copy Markdown

Docs7 for 1bit-monster/engine

Result Status Action
Deployment ➖ Not used —
Content review ➖ Did not run. This site has no agent runs available this month. Wait for the monthly reset or check your Docs7 plan. —

Commit c4fc9e8

@github-actions

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Tokenizer Loading

The tokenizer is now loaded once during load() and stored in tok_. However, the score() method now checks if (!tok_) and returns false if the tokenizer hasn't been loaded, which is good. But there's no explicit handling for the case where load() fails to initialize tok_, which could leave tok_ in a null state and cause a crash or undefined behavior if score() is called afterward. This could be mitigated by ensuring load() properly initializes tok_ or sets an error state if it fails.

if (!tok_) {
    err_ = "scorer not loaded";
    return false;
}
Forward Declaration

The Tokenizer class is forward-declared in scorer.h, which is correct for header-only usage. However, the implementation in scorer.cpp relies on the full definition of onebit::npu::Tokenizer to create a std::unique_ptr to it. This is fine, but it's important to ensure that the full definition is available in the compilation unit where Scorer is instantiated to avoid undefined behavior.

namespace onebit::npu {
class Tokenizer;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant