tokenizers, Oniguruma वाले Hugging Face tokenizers का शुद्ध Rust विकल्प है
C regex के बिना Hugging Face tokenizers build करें: कौन से Cargo features Oniguruma और C++ खींचते हैं, Remade with Rust का fork rusty_expressions की ओर कैसे जाता है, और पहले क्या test करें।

Oniguruma के बिना Hugging Face tokenizers कैसे build करूँ?
default features बंद करें। tokenizers crate default में onig (FFI के ज़रिए Oniguruma) और esaxx_fast (C++ suffix arrays) enable करता है। default-features = false के साथ fancy-regex feature जोड़ने पर regex Rust में चलता है और unigram trainer अपने Rust fallback का इस्तेमाल करता है। Remade with Rust के fork की बताई दिशा Oniguruma को rusty_expressions से बदलना है।
Related reading
tokenizers: The Hugging Face Tokenizer Library and the House Forktokenizers is Hugging Face's Rust library for BPE, WordPiece and Unigram tokenization with offset tracking. The house fork aims to drop the Oniguruma C engine for wasm builds.
tokenizers वह Rust लाइब्रेरी है जो Hugging Face के fast tokenizers के पीछे है: BPE, WordPiece और Unigram models, alignment tracking वाले normalisers, pre-tokenizers, truncation, padding और special tokens। Remade with Rust Remade-With-Rust/tokenizers पर एक fork बनाए रखता है, जिसके repository description में इसका उद्देश्य लिखा है: Oniguruma को rusty_expressions से बदलना ताकि लाइब्रेरी WebAssembly पर deploy हो सके। यह पृष्ठ बताता है कि stock build में C कहाँ से आता है, आज इसे कैसे हटाएँ, और जब fork ऐसे Rust regex engine पर जाए जो Oniguruma का semantics बनाए रखता है तो क्या बदलता है।
सामान्य परिचय tokenizers field note में है, और pins repo landing पर हैं।
C कोड कहाँ से आता है
दो default features
fork की main branch पर crate का Cargo.toml खोलें, version 0.23.2-dev.0, तो default feature set progressbar, onig और esaxx_fast है। onig onig crate को लाता है, जो libonig को compile और link करता है, यानी C में लिखी Oniguruma regex library को। esaxx_fast esaxx-rs/cpp को चालू करता है, जो Unigram trainer द्वारा इस्तेमाल की जाने वाली C++ suffix-array routine है। इसलिए साधारण tokenizers = "..." dependency हर encode call पर एक C regex engine और training में एक C++ routine डाल देती है।
regex क्यों मायने रखता है
tokenizer जो भी text देखता है, उस हर हिस्से पर regex चलता है। Split जैसे pre-tokenizers tokenizer.json से pattern लेते हैं, और hub से downloaded models में किसी और के लिखे patterns होते हैं। Oniguruma का असली CVE record है: CVE-2019-13224 Oniguruma 6.9.2 में onig_new_deluxe() में use-after-free है, जिसे crafted regular expression से reach किया जा सकता है, और code execution संभव है। rusty_expressions README बताता है कि C project 2025-04-24 को archive हो गया, इसलिए नए bugs upstream patch नहीं होंगे।
कोड में seam
crate के अंदर src/utils/mod.rs एक type expose करता है, SysRegex। onig enabled होने पर यह Oniguruma को wrap करता है। fancy-regex enabled और onig off होने पर यह Rust fancy-regex crate को wrap करता है। अगर दोनों में से कोई enabled नहीं है तो build compile_error के साथ रुक जाता है। लाइब्रेरी में बाकी सब कुछ SysRegex को call करता है, इसलिए backend एक build decision है, आपकी तरफ़ code change नहीं।
इसे हटाना, कदम दर कदम
आज Oniguruma के बिना build करें
dependency को default-features = false के साथ declare करें और जो features वापस चाहिए उन्हें list करें, जैसे features = ["fancy-regex", "progressbar"]। अगर models from_pretrained से load करते हैं तो http जोड़ें, और hub client को Rust TLS stack इस्तेमाल कराने के लिए rustls-tls। browser builds के लिए unstable_wasm इस्तेमाल करें, जो fancy-regex और wasm getrandom backend चुनता है। esaxx_fast के बिना Unigram trainer अपने Rust code path पर fallback करता है; encoding पर असर नहीं पड़ता।
साबित करें कि token IDs नहीं बदले
- जो tokenizers आप ship करते हैं, उन्हें उनके
tokenizer.jsonके आधार पर चुनें, और असली input का corpus लें: chat logs, code, mixed scripts, emoji। - corpus को default build और C-free build से encode करें और सिर्फ़ token strings नहीं, token IDs और offsets की तुलना करें।
- look-around, possessive quantifiers या Unicode classes वाले patterns पर ध्यान दें; engines यहीं अलग होते हैं।
cargo tree -e featuresचलाएँ और पुष्टि करें कि कोईonig-sysयाesaxxC++ build नहीं बचा है।
rusty_expressions कहाँ फिट होता है
rusty_expressions Oniguruma का behaviour है जिसे Rust में फिर से बनाया गया है, बिना C, बिना FFI और बिना onig-sys। इसका README live libonig के खिलाफ 5 million से ज़्यादा differential cases पर Oniguruma 6.9.10 के साथ match equivalence बताता है, अपने 23 benchmark cases में लगभग 3x तेज़ search, no_std support और wasm32-unknown-unknown पर असली execution। बारह ज्ञात अंतर सूचीबद्ध हैं। क्योंकि यह fancy-regex semantics के बजाय Oniguruma semantics रखता है, default build के लिए लिखे patterns के लिए यह ज़्यादा करीबी match है। Pins इसके repo landing पर हैं।
Trade-offs और अगले कदम
आपके लिए क्या बदलता है
fancy-regexके तहत regex behaviour थोड़ा बदल सकता है, इसीलिए ऊपर का token-ID diff किसी भी benchmark से ज़्यादा मायने रखता है।- C++ suffix array के बिना Unigram models train करना धीमा हो सकता है; अगर अक्सर train करते हैं तो measure करें।
unstable_wasmका नाम upstream में unstable है; जिस version को test करते हैं उसे pin करें।
यह fork क्या नहीं बदलता
tokenizer model formats, Python और Node bindings और कुल API upstream से inherited हैं; README अब भी upstream Hugging Face वाला है, जो server CPU पर एक gigabyte text tokenize करने के लिए 20 seconds से कम बताता है। main-branch code अब भी default में onig इस्तेमाल करता है, इसलिए swap आने तक C-free build आपकी Cargo.toml में लिया गया decision है।
Fork regex काम के अलावा अपने additions भी रखता है। इसका Cargo.toml parity-aware-bpe feature define करता है, और हाल के एक commit में ParityBpeTrainer example जोड़ा गया और trainer API docs में उसे list किया गया। बाकी हाल के commits maintenance हैं: हर CI action एक ही SHA पर pinned, पुराने audit suppressions हटाए गए, और Node binding security alerts 33 से 0 पर लाए गए। अगर आप upstream releases को करीब से track करते हैं, pin करने से पहले दोनों का diff लें।
संबंधित पढ़ाई
यहाँ का अपना tokenizer crate rusty_tokens है, और model runtimes candle में रहते हैं। C में regex compilation गलत होने का एक अलग मामला the GitLab regex compiler overflow में कवर किया गया है। Mind band on Vision AI crates list करता है, और आपके अपने hardware पर चलने वाले models mata.network पर Disco Party के ज़रिए रखे जा सकते हैं।
FAQ
Oniguruma से आख़िर क्यों हटें?
rusty_expressions README के अनुसार Oniguruma C project 2025-04-24 को archive हो गया, इसलिए आगे कोई security patches नहीं आएँगे। इसका CVE इतिहास है, जिसमें CVE-2019-13224 शामिल है, जो onig_new_deluxe में use-after-free है और crafted pattern से trigger होता है।
कौन से Cargo features tokenizers में C या C++ लाते हैं?
fork की main branch के Cargo.toml में, version 0.23.2-dev.0, default set progressbar, onig और esaxx_fast है। onig libonig link करता है; esaxx_fast unigram trainer के suffix array के लिए esaxx-rs/cpp enable करता है।
अगर मैं onig और fancy-regex दोनों disable कर दूँ तो क्या होता है?
build compile_error के साथ रुक जाता है जो दोनों में से एक माँगता है। crate का regex type, SysRegex, को backend चाहिए, इसलिए C-free build के लिए fancy-regex चुनें।
क्या मेरी tokenizer.json files अब भी काम करेंगी?
file format regex backend के साथ नहीं बदलता। जोखिम pattern behaviour का है: Split या कोई और regex pre-tokenizer दूसरे engine में अलग match कर सकता है, इसलिए switch करने से पहले असली corpus पर token IDs का diff लें।
क्या यह WebAssembly में चलता है?
हाँ। crate का unstable_wasm feature fancy-regex और wasm getrandom backend चुनता है। Oniguruma का onig crate wasm32 के लिए build नहीं होता, और यही fork होने की मुख्य वजह है।