winapiadmin's picture
Upload folder using huggingface_hub (part 16)
d517dc9 verified
|
Raw History Blame
4.39 kB
# rust-sts-tokenizer
Tokenizer-training stage for STS JSONL traces. This replacement uses:
- `hashbrown` + `ahash` for the in-memory frequency maps.
- XXH3 for deterministic tail sampling and stable writer routing.
- Sonic-RS for SIMD JSON parsing and serialization.
- streaming `zstd` decompression and streaming `tar` traversal, including nested `.tar.zst` archives.
- RocksDB as a temporary spill backend with a custom checked `u64` merge operator and multiple bounded writer threads.
- Hugging Face `tokenizers` 0.21.4 for BPE, Unigram, and WordPiece training.
## Build on Windows (MSVC)
Use a Visual Studio 2022 Developer PowerShell/Command Prompt with the **Desktop development with C++** workload and Windows SDK installed. Install CMake and LLVM/Clang as well. RocksDB's Rust bindings use bindgen's runtime mode, so `LIBCLANG_PATH` must point to the directory containing `libclang.dll`.
Example (adjust the LLVM path to your installation):
```powershell
$env:LIBCLANG_PATH = "C:\Program Files\LLVM\bin"
$env:CARGO_BUILD_JOBS = "4"
rustup target add x86_64-pc-windows-msvc
cargo build --release --target x86_64-pc-windows-msvc
```
`CARGO_BUILD_JOBS` limits parallel native compilation too; increase it if you have ample RAM and CPU capacity. The crate requires Rust 1.88 or newer.
## Run
The original program uses verbatim CLI field names, so options use underscores:
```powershell
cargo run --release --target x86_64-pc-windows-msvc -- `
--data_dir .\traces `
--out_dir .\out `
--tokenizer bpe `
--vocab_size 16000 `
--stats_memory_gb 64 `
--db_writer_threads 8 `
--db_write_batch_size 16384 `
--tok_workers 8 `
--input_workers 8
```
`--stats_memory_gb` overrides `--stats_memory_mb`. The default threshold is 8 GiB. It is a spill threshold, not a request to preallocate that amount of RAM. The implementation switches to RocksDB at roughly 85% of the configured estimate.
RocksDB also has its own memtables. With the defaults, their configured maximum is approximately `512 MiB * 4 = 2 GiB`, in addition to worker batches, queues, parser buffers, tokenizer training data, and operating-system memory. Choose the statistics threshold based on actual available RAM, not installed RAM alone.
## Storage behavior and durability
RocksDB is created only after the in-memory statistics map reaches its spill threshold. It is stored under `--tok_stats_dir`, or under `--out_dir` by default. After successful selection, the temporary database is closed and deleted.
The statistics database disables the RocksDB WAL because the database is temporary and fully reproducible from the input corpus. A process or machine crash can therefore lose unflushed statistics and leave a stale `.sts_tokenizer_stats_*` directory. These directories are not automatically resumed; remove stale directories manually before rerunning if disk space matters.
The merge operator stores compact 8-byte little-endian `u64` counters and checks addition overflow. Total corpus occurrences are counted separately in `u128`.
## Selection behavior
The result keeps up to the configured unique-word cap: approximately 80% from the most frequent words and the remainder as a deterministic XXH3 tail sample. The RocksDB path scans its sorted iterator twice and keeps bounded heaps rather than materializing the entire unique-word set in RAM.
XXH3 replaces the previous BLAKE2 tail hash, so deterministic tail samples will differ from prior program versions, but are stable across runs with this version.
## Important limitations
- The memory estimate is deliberately conservative but is not an exact process-memory limit. Actual use also includes worker-local maps, queued batches, RocksDB memtables, parser/decompressor buffers, and allocator overhead.
- Multiple source files are read concurrently. A run using `--tok_max_messages` or `--tok_max_chars` uses one source worker so limits remain deterministic.
- A single `.tar.zst` stream is read sequentially; independent archive files can be processed concurrently, but members of one compressed archive are not decompressed in parallel.
- This source was generated and reviewed against the current crate APIs, but it has not been compiled or benchmarked in this environment because a Rust toolchain is unavailable here. Run `cargo check --target x86_64-pc-windows-msvc` in the MSVC developer environment before a long corpus run.