Skip to content

Add optional Hugging Face Rust JSON tokenizer backend - #212

Open
JacobSzwejbka wants to merge 5 commits into
meta-pytorch:mainfrom
JacobSzwejbka:hf-rust-tokenizer-tok
Open

JacobSzwejbka wants to merge 5 commits into
meta-pytorch:mainfrom
JacobSzwejbka:hf-rust-tokenizer-tok

Conversation

@JacobSzwejbka

@JacobSzwejbka JacobSzwejbka commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This adds an opt-in Rust-backed Hugging Face tokenizer for standard tokenizer.json files. It uses only the published Hugging Face v1.0.0-rc.2 crates from crates.io; the Cargo lockfile contains no git dependencies.

The backend canonicalizes normal Hub JSON in memory, uses the upstream v1 inference and decoder pipelines, reads BOS/EOS roles from canonical metadata or sibling tokenizer configuration files, and falls back cleanly when the RC cannot load a tokenizer. It preserves ExecuTorch's independent and repeatable BOS/EOS-count contract.

The option remains fully off by default: without TOKENIZERS_BUILD_HF_RUST_TOKENIZER, CMake does not require Cargo or add the Rust archive.

Experimental .tok support has been split into draft meta-pytorch/tokenizers#218. That draft is blocked on the format landing in a protected Hugging Face branch or release.

Companion ExecuTorch integration: pytorch/executorch#22548.

Review order

Start with rust_tokenizer/Cargo.toml and rust_tokenizer/CMakeLists.txt, then rust_tokenizer/src/lib.rs for JSON loading and the narrow C ABI, followed by src/rust_hf_tokenizer.cpp for the existing C++ interface adaptation.

Upstream alignment

Hugging Face's v1 RC announcement publishes the Rust crates and identifies inference-only C/C++ bindings for ExecuTorch and llama.cpp as a v1 goal. Their experimental C bindings PR #2375 is not yet merged and does not expose all metadata required by the ExecuTorch Tokenizer interface. This PR keeps its local ABI intentionally small so it can migrate once the upstream ABI stabilizes.

Validation

Rust formatting, Clippy with warnings denied, and unit tests pass. The focused C++ adapter suite passes. A clean CMake build and installed-package consumer successfully round-trip a real Hub GPT-2 tokenizer.json; a real BERT tokenizer was also exercised. The default-off build does not invoke Cargo.

The current Apple arm64 minsize smoke executable measures 1,921,872 bytes stripped and 901,688 bytes with gzip-9.

AI-assisted by Codex.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 4, 2026
@JacobSzwejbka
JacobSzwejbka force-pushed the hf-rust-tokenizer-tok branch 4 times, most recently from 281ac35 to 228a1da Compare September 24, 2026 19:05
@JacobSzwejbka JacobSzwejbka changed the title Add optional Hugging Face .tok tokenizer backend Add optional byte-level Hugging Face .tok tokenizer backend Sep 24, 2026
Preserve independent BOS/EOS counts, avoid guessing a missing token ID, and reject .tok files whose decoder cannot yet be represented safely. Add C++ regression coverage and run the Rust checks in CI.

AI-assisted-by: Codex
@JacobSzwejbka JacobSzwejbka changed the title Add optional byte-level Hugging Face .tok tokenizer backend Add optional Hugging Face Rust tokenizer backend Sep 24, 2026
@JacobSzwejbka
JacobSzwejbka force-pushed the hf-rust-tokenizer-tok branch 2 times, most recently from b3c4f65 to 790e279 Compare September 24, 2026 20:30
@JacobSzwejbka
JacobSzwejbka marked this pull request as ready for review September 24, 2026 20:32
@JacobSzwejbka
JacobSzwejbka requested review from mergennachin and removed request for mergennachin September 24, 2026 20:32
Generated with assistance from Codex.
Generated with assistance from Codex.
@JacobSzwejbka JacobSzwejbka changed the title Add optional Hugging Face Rust tokenizer backend Add optional Hugging Face Rust JSON tokenizer backend Sep 24, 2026

This branch has not been deployed

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

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant