Skip to content

feat: re-introduce pretokens scratch buffers \(thread_local\) - #2284

Open
McPatate wants to merge 1 commit into
feat/train_encode_splitfrom
feat/reintroduce_pre_tokens_scratch
Open

feat: re-introduce pretokens scratch buffers \(thread_local\)#2284
McPatate wants to merge 1 commit into
feat/train_encode_splitfrom
feat/reintroduce_pre_tokens_scratch

Conversation

@McPatate

@McPatate McPatate commented Aug 4, 2026

Copy link
Copy Markdown
Member

No description provided.

Signed-off-by: Luc Georges <luc.sydney.georges@gmail.com>
@McPatate
McPatate requested review from ArthurZucker and SBrandeis and a lite review from Copilot August 4, 2026 17:13
@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

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.

Pull request overview

Re-introduces a persistent (per-thread) scratch buffer for pre-token spans in the PipelineTokenizer encode path to reduce repeated allocations during pre-tokenization.

Changes:

  • Add a module-level thread_local! PRE_TOKENS_SCRATCH: RefCell<Vec<Span>>.
  • Update encode_generic to reuse PRE_TOKENS_SCRATCH instead of allocating a fresh pre_tokens Vec per call.
  • Keep output allocation local while removing the local pre_tokens allocation.

馃挕 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 847 to 849
input: &str,
add_special_tokens: bool,
) -> Result<Vec<PipelineToken>> {
Comment on lines +883 to +886
let mut pre_tokens = cell.borrow_mut();
pre_tokens.clear();
self.pre_tokenizer
.pre_tokenize(normalized_chunk, &mut pre_tokens)?;
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.

3 participants