tokenizers v1: encode, decode and scaling, measured

2026年9月21日 00:00
站內 AI 整理稿

Back to Articles tokenizers v1: encode, decode and scaling, measured Published September 21, 2026 Update on GitHub Upvote 1 Arthur Zucker ArthurZ Follow Simon Brandeis sbrandeis Follow Luc Georges mcpotato Follow Lysandre lysandre Follow The tokenizer has not historically been the bottleneck within ML workflows.

Compute-wise, tokenization is light compared to the heavy modeling happening in the rest of the pipeline.Yet, in some cases, it has rapidly become key to accelerating (or slowing down) your machine learning work.As models become faster and workloads scale, that balance begins to shift.

Training on massive datasets, serving many concurrent requests, or repeatedly processing long inputs can put enough pressure on the tokenizer that it starves the model of data.This is why we have chosen to heavily focus on performance for the upcoming version 1 of tokenizers.

Tokenization should be light and should scale with your workflow.Your GPUs should never sit idle waiting for the CPU to complete its tokenization.In this article, we look at what makes v1 faster than v0.23, often by tens of times.This work was entirely possible thanks to the rest of the ecosystem.

Tokenization is a very active area of open source work, and libraries such as gigatoken, tiktoken, kitoken, tokie, fastokens, wordchipper and ai-tokenizer, as well as many others, have each pushed on what a fast tokenizer can be.

We read that work, and several of the ideas below reached us because another project showed they were worth trying.Before this refactor, tokenizers was nowhere near the performance it could have had, so contributing to it may not have seemed worth it.

With this refactor, we hope to make clear that we intend tokenizers to be a library worth contributing to.We also thank IBM, NVIDIA, and the ExecuTorch team for contributing patches and helping us test across a wide range of hardware to broaden platform support.

Results We showcase results for the release candidate of tokenizers v1 against other widely used alternatives.We go over single-threaded, multi-threaded, scaling across threads, per-model comparison, per-language comparison, latency, decoding throughput, memory heap, as well as crate size.

We run this from the tokbench repository, and add a command to rerun the benchmarks on your hardware if you would like to do so.What V1 Is v1 will produce the same token IDs as v0.23.

The goal was to preserve the output, the API, the vocabulary and the merge ranks, and improve everything that can be improved.That includes breadth.The library stays general across tokenizer families rather than specialising on BPE, so v1 loads everything v0.23 loaded.

A tokenizer converts text into the list of integers a model reads.tokenizers runs that conversion in four stages.Normalization applies operations such as lowercasing or Unicode normalization to the raw text.Pre-tokenization splits the text into smaller pieces called pre-tokens.

The model turns each pre-token into tokens and maps them to IDs in its vocabulary.Post-processing adds any special tokens the model expects.The model stage is where most of the work described here happens.Eight of the ten model families measured in this article use byte pair encoding, or BPE.

BPE starts from the bytes of a pre-token and repeatedly joins the highest ranked adjacent pair until no ranked pair remains.The ranking is learned when the tokenizer is trained and ships with it, so the same text always produces the same IDs.A merge never crosses a pre-token boundary.

The other two families use WordPiece and Unigram, the two other model types the library supports.The tokenization pipeline page documents the four stages.Tokenization algorithms documents BPE, WordPiece and Unigram.Each stage was worked on.

These are the changes that mattered: change what it does workspace split one crate became a workspace: tk-encode is the required runtime, and tk-serialize, tk-convert and tk-train are linked only when an application needs them no-alloc model the merge working set lives in a caller-owned scratch buffer; the loop never touches the allocator bitcannon the split pattern becomes Boolean operations over bitstreams, using SIMD instructions to find splits instead of a regex engine merge-loop rewrite the pieces being merged form an intrusive doubly-linked list inside one preallocated buffer, so a merge updates two indices instead of moving data word cache a thread-local memo from pre-token bytes to finished ids, so a repeated word is merged once native parallelism one shared tokenizer encodes from many threads at once; each thread draws its scratch buffer and word cache from its own sub-pool, so threads no longer queue on a single lock (#2365) The Split: Bitstreams Instead Of A Regex BPE models use a regular expression to split the input text into smaller, easier to process chunks called pre-tokens.

Merges happen inside a pre-token and never across the boundary between two of them, so this split decides what the rest of the pipeline sees.That regular expression is a fixed parameter of the model.

It ships with the tokenizer and never changes at runtime, so there is no need for a general-purpose regex engine to interpret it on every encode.An equivalent splitting function can be written by hand, once, for the pattern a given model actually uses.

A hand-written function can then use the SIMD instructions (single instruction, multiple data) of a modern CPU, which apply one operation to many bytes at once and suit UTF-8 text well.

bitcannon views the input's bytes as parallel streams of bits, so boundaries fall out of boolean operations across whole registers instead of a scan that advances one character at a time.It decides 64 bytes per register operation.

The same idea drives Parabix for text processing and simdjson for JSON.This depends on recognising the pattern.A handful of grammars cover most byte-level BPE models, and a tokenizer whose pattern is not among them keeps the regex path and none of this speed-up.

That is why the gains above vary as much as they do.The Word Cache Real text contains many repeated words.Because BPE always produces the same token IDs for a given pre-token, v1 can save the result after processing it once.

A thread-local cache maps each pre-token's bytes to its token IDs, allowing later occurrences to skip the merge process.Naturally, as the input grows, the number of unique words can grow more slowly than the total number of words.Repeated words then account for an increasing share of the input.

New words still appear, which accounts for the occasional misses in the animation below.

Reproduce the shared-prefix result with: tokbench measure prefix-sharing \ --engine pipeline \ --engine hf-tokenizers \ --compare-to pipeline-no-cache \ --corpus agenticswe Caching works best when the input contains repeated pre-tokens.

Input with few repeated pre-tokens can pay for lookups without receiving many hits.The Merge Loop The next major cost comes from the BPE merge loop.For each pre-token, the loop repeatedly finds the highest-priority adjacent pair and merges it.

The previous implementation allocated new memory for every call and built a new priority queue for every pre-token.v1 reuses a scratch buffer owned by the caller, removing those repeated allocations.

It stores symbols in a flat array and links adjacent symbols by their positions in that array, which makes updates during merging cheaper.It also processes a batch of pre-tokens in a single model call.

Each candidate pair is also packed into a single 64-bit value, with the merge rank in the high bits.Comparing two candidates is then just comparing two integers, and "no merge here" is the largest possible value, so the loop finds its next merge without a branch.

Method Small differences in benchmark design can produce large differences in tokenizer performance.We used the following rules to keep the comparison consistent across engines.

rule why one timing loop every engine runs the identical loop; no per-engine fast path load excluded vocabulary load is timed separately, never inside encode id-hash verified FNV-1a over the output ids must match the baseline exactly common cells only medians are over cells every engine ran and verified complete sweep per process each repeat starts in a new process and retains every cell physical-core pinning workers are pinned to eight distinct physical cores, never sibling SMT threads independent Jobs separate Jobs measure host-to-host variation Repeatedly encoding one document can be faster than encoding a stream of distinct documents on the same build.

The first approach measures performance when the entire document is already represented in the cache.The second measures performance on new input while allowing previously seen pre-tokens to remain cached.

Both conditions are sometimes described as "warm," even though they measure different workloads.Our headline results use distinct documents, and the complete corpus is too large to fit in the cache.

Tokenizer benchmarks should identify which workload they use because the choice can dominate the result.What This Adds Up To Across the ten model families v1's encode path covers, it encodes text 3 to 30 times faster than v0.23 with one thread on an Apple M4 Max.

The low end is t5-base, the high end gpt2.It scales at 76% of linear across eight workers.Throughout these changes, v1 produces exactly the same token IDs as the released library.

The overall improvement comes from several changes working together: a hand-written splitter in place of a regex engine, a cache that answers a repeated word without merging it again, a merge loop that never touches the allocator, and one model call per batch of pre-tokens instead of one per pre-token.

Each reduces the work done at a different point in the pipeline.The next priority is support for more model families.We will move additional models onto the new merge loop before 1.0.0.

Once the release candidates stabilize, the next step will be bringing about the improvements within the transformers library and the rest of the ecosystem which depend on the tokenizers library.This post is generated from tokbench results and will be updated as support expands.

Getting It A release candidate for v1 is on crates.io.The API you call is the one you already call, so the only thing that changes is which build you install.It is the ordinary install: cargo add tokenizers --pre Training is behind a default-on feature that pulls a C++ dependency with it.

If you only need to encode, turn it off to exclude the training implementation: cargo add tokenizers --pre --no-default-features --features http Encoding is unchanged: same call, same ids.

use tokenizers::tokenizer::{Result, Tokenizer}; fn main() -> Result<()> { let tokenizer = Tokenizer::frompretrained("deepseek-ai/DeepSeek-V4-Flash", None)?; let encoding = tokenizer.encode("The tokenizer is no longer the bottleneck.", false)?; println!("{:?}", encoding.

getids()); // [671, 17840, 9160, 344, 1119, 5827, 270, 111127, 16] println!("{:?}", encoding.gettokens()); // ["The", "Ġtoken", "izer", "Ġis", "Ġno", "Ġlonger", "Ġthe", "Ġbottleneck", "."] Ok(()) } ` For a batch, encodebatch is what scales across cores.It is the call the scaling view above measures.

`rust let encodings = tokenizer.encodebatch(documents, false)?; Every figure in this post was measured against this crate.The Python bindings wrap the same code and are built from bindings/python, but they add per-call overhead that none of these measurements include.

Progress Towards V1 The benchmarks in this post cover the completed release-candidate work listed first.The remaining sections show what is still required for 1.0.0 and what we plan to explore afterward.Release Candidate: Implemented This work is in the Rust pre-release on crates.

io: cargo add tokenizers --pre workspace split: divide the single crate into tk-encode, tk-serialize, tk-conve

Related

相關文章

雷峰網模型更新

從賣配置到鋪自研技術,理想i6的第二年

上市一年、累計銷量近19萬輛之後,理想給i6提出了一個更大的目標。“我們希望理想i6成為理想汽車在全球銷量最高的一款車。”理想汽車董事長兼CEO李想在新車發佈時表示。10月28日,2026款理想i6正式發佈,全國統一零售價24.98萬元,首銷期限時權益價23.98萬元,同步開啟試駕、定購與交付。這次改款涉及燈光、儲物、座椅、屏幕、續航和芯片六個方面。其中,兩項配置值得單獨拿出來看:自研連山5C電池和馬赫M100輔助駕駛芯片,均成為新車標配。一款已有較大銷量基礎的產品,開始集中搭載理想的自研技術。

8 小時前

蓋茨炮轟白宮 AI 監管僵局:生物武器風險或超新冠

他稱 AI 模型展現出的能力"驚人",無論造福還是作惡的潛力都"不容低估"。最讓他警惕的,是惡意行為者借 AI 製造生物武器、或撕開各領域網絡安全缺口——而部分"開放"模型本就免費可用,其安全護欄與監控還能被直接卸掉。他直言,一旦 AI 捲入生物安全事件,損害規模"可能比新冠嚴重得多"。

14 小時前6700

月之暗面開放 2.8 萬億參數 Kimi K3.1 Pro 權重,強勢登頂 Agent Arena 開源榜

隨著此次權重的正式開源,該模型在由 Arena 運營的智能體排行榜 Agent Arena 中表現亮眼,一舉奪得開放權重模型的第一名,在全部參評模型中位列第13位,並在工作、編程與聊天三大核心類別中均穩居開放模型首位。基於真實長週期會話的嚴苛評測與傳統的成對盲測不同,Agent Arena 擁有獨特的評測方式。

15 小時前5900
雷峰網模型更新

360亮相北大AI+X青年人才實踐發展論壇,聯合發佈“AI原生人才標準”

近日,青年人才能力數字服務平臺與北大青年AI+創新勞動實踐平臺發佈暨AI+X青年人才實踐發展論壇隆重舉辦。本次會議由中國技術交易所、北京大學信息科學技術學院聯合主辦,錨定AI時代青年人才培育、產教深度協同等創新方向,匯聚職上青雲等共建夥伴、高校科研力量與產業生態資源,共尋智能浪潮下青年人才發展的創新範式。其中,360集團作為承辦企業受邀參會,360集團副總裁卜思南以《智能體時代,培養AI原生人才》為題發表主旨演講,深度解讀了智能體變革浪潮下的AI原生人才評價邏輯,探尋產教協同賦能新質生產力的實踐路徑。

15 小時前

Meta收購AI編碼初創公司Conductor,加碼智能體編程賽道

該消息最初由Axios報道,Conductor創始人兼CEO Charlie Holtz同步在博客中宣佈這一進展,並表示公司將於“明年年初”為客戶帶來“一些新東西”。Meta向TechCrunch證實了此次收購,但拒絕透露交易細節。Holtz還提到,期待與Meta首席人工智能官Alexander Wang及超級智能實驗室產品負責人Nat Friedman合作,稱MSL“是所有實驗室中人才密度最高的”。

16 小時前4900