winapiadmin's picture
Upload folder using huggingface_hub (part 16)
d517dc9 verified
|
Raw History Blame
3.79 kB

Rust STS tokenizer trainer

This is the tokenizer-training stage of the Python STS pipeline, implemented in Rust with Hugging Face tokenizers 0.21.4. It writes a standard tokenizer.json.

Build

cargo build --release

Example

cargo run --release -- `
  --data_dir .\traces `
  --out_dir .\out `
  --tokenizer bpe `
  --vocab_size 16000 `
  --tok_workers 8 `
  --input_workers 8

Other algorithms:

cargo run --release -- --data_dir .\traces --out_dir .\out --tokenizer unigram --vocab_size 16000
cargo run --release -- --data_dir .\traces --out_dir .\out --tokenizer wordpiece --vocab_size 16000

Pre-tokenization only:

cargo run --release -- --data_dir .\traces --out_dir .\out --pretokenize_only

This writes pretokenized_words.jsonl and pretokenized_meta.json, then exits. Otherwise the program trains and saves tokenizer.json; this executable does not create model-training .bin datasets, so --tokenize_only is accepted as a compatibility flag and is effectively the normal mode.

CLI

The tokenizer-stage options mirror the corresponding Python arguments:

  • --data_dir, --out_dir, --tokenizer, --vocab_size
  • --tok_max_messages, --tok_max_chars, --no_reasoning
  • --bpe_max_unique_words, --bpe_train_words, --bpe_max_word_chars, --bpe_max_token_length, --bpe_min_frequency
  • --unigram_max_piece_length, --unigram_max_unique_words, --unigram_train_words, --unigram_memory_total_mb, --unigram_stats_batch, --unigram_db_cache_mb, --unigram_max_word_chars
  • --wordpiece_max_unique_words, --wordpiece_train_words, --wordpiece_max_word_chars
  • --tok_batch_mb, --tok_queue_mb, --tok_batch_size, --input_chunk_mb, --zstd_read_mb, --max_json_line_mb, --tok_stats_dir
  • --tok_workers, --input_workers, --rayon_threads
  • --pretokenize_only, --tokenize_only

The exact frequency map starts in RAM and spills to a temporary SQLite database at 85% of the configured memory budget. Word-frequency additions are checked against SQLite's signed 64-bit integer limit. The bounded trainer sample keeps the most frequent 80% of selected words and a deterministic BLAKE2b-ranked tail, matching the Python selection strategy. Counts are exact; the trainer receives a frequency-weighted bounded sample.

Input workers process separate files concurrently and send bounded batches to one statistics accumulator. --tok_workers and --input_workers jointly cap this worker count. When corpus caps are enabled, workers are forced to one and the quotas are distributed over input files to keep the run deterministic.

Current limitations versus Python

  • Inputs: .jsonl and .jsonl.zst files, including recursive directory discovery. .tar, .tar.zst, nested archives, and --hf_dataset are not implemented yet. --max_archive_depth is reserved for archive support.
  • This is the tokenizer stage only. It does not tokenize the full corpus to model-training .bin files, build session train/validation indices, cache/reuse tokenized shards, or train the language model.
  • The input pipeline uses bounded Rust threads, not the Python version's separate reader-thread plus spawned-process stages. --input_workers is a cap on concurrent source workers, not a separate reader pool.
  • --hash_inputs, --verify_cache_hashes, --retokenize, and model/DataLoader options are omitted because this executable does not implement the downstream cache/model pipeline.
  • WordPiece with ByteLevel is not byte-safe; the program warns about this and skips byte round-trip assertions for WordPiece.

The tokenizer algorithms themselves are delegated to Hugging Face's Rust tokenizers crate. BPE, Unigram, and WordPiece are not reimplemented here.