Metadata-Version: 2.4
Name: bbpe_tokenizer
Version: 0.5.4
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Rust
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Text Processing :: Linguistic
Requires-Dist: jinja2>=3.1.0
Requires-Dist: torch>=2.0.0 ; extra == 'logits'
Requires-Dist: transformers>=4.30.0 ; extra == 'logits'
Requires-Dist: numpy>=1.24.4 ; extra == 'numpy'
Provides-Extra: logits
Provides-Extra: numpy
License-File: LICENSE-APACHE
License-File: LICENSE-MIT
Summary: A high-performance byte-level BPE tokenizer with a Rust core and Python wrapper.
Keywords: bpe,tokenizer,nlp,text-processing
Home-Page: https://github.com/hoare211/bbpe_rs
Author: bbpe_rs contributors
License: MIT OR Apache-2.0
Requires-Python: >=3.8
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Homepage, https://github.com/hoare211/bbpe_rs
Project-URL: Issues, https://github.com/hoare211/bbpe_rs/issues
Project-URL: PyPI, https://pypi.org/project/bbpe_tokenizer/
Project-URL: Repository, https://github.com/hoare211/bbpe_rs

# bbpe_rs

[![CI](https://github.com/hoare211/bbpe_rs/actions/workflows/quality.yml/badge.svg)](https://github.com/hoare211/bbpe_rs/actions/workflows/quality.yml)
[![PyPI](https://img.shields.io/pypi/v/bbpe_tokenizer.svg)](https://pypi.org/project/bbpe_tokenizer/)
[![License: MIT OR Apache-2.0](https://img.shields.io/badge/license-MIT%20OR%20Apache--2.0-blue.svg)](#license)

A byte-level BPE tokenizer trainer and encoder written in Rust, with Python
bindings. It is built for corpora that do not fit in memory and for pipelines
that need a reproducible model artifact instead of a black box.

`bbpe_rs` trains a vocabulary from raw JSONL or plain text, exports tokenizers
at any smaller size without retraining, and encodes/decodes with a
deterministic Rust core. A model is a plain JSON file carrying its own
provenance (git commit, corpus statistics, pre-tokenizer pattern) and is
validated for self-consistency on load.

> **Status: 0.5.4 (pre-1.0).** The model format is stable; the public API may
> still change between minor releases.

中文说明见 [README.zh-CN.md](https://github.com/hoare211/bbpe_rs/blob/main/README.zh-CN.md)。

## Highlights

- **Streaming corpus reader** — JSONL or plain text is processed line by line,
  so corpus size is limited by disk, not by RAM.
- **Out-of-core training** — add `--sample-rate` to train on a random sample of
  a corpus much larger than memory, with an independent held-out split for
  acceptance checks.
- **Export without retraining** — train once at `K_max`, then `bbpe cut` to
  derive any smaller vocabulary with identical merge order.
- **Deterministic** — same corpus and parameters produce the same merges and
  token ids, independent of thread count.
- **Reproducible artifacts** — each model records the build commit, corpus
  counters, sampling parameters and pre-tokenizer pattern; `bbpe inspect`
  re-checks them and reports failures with a non-zero exit code.
- **Python bindings** — training and long encodes release the GIL, support
  `Ctrl+C`, `CancellationToken` and `timeout`.
- **TokenBuffer / DLPack** — consume large encoded outputs as a read-only
  contiguous buffer or transfer ownership to a CPU tensor without a Python list.
- **Rust core without `unsafe`** — FFI-specific `unsafe` is isolated in the
  Python binding layer.

## Quick start

Requirements: Rust 1.85+ (edition 2024). Python 3.8+ only for the bindings.

```bash
git clone https://github.com/hoare211/bbpe_rs.git
cd bbpe_rs

# CLI only: does not pull in pyo3, builds faster
cargo build --release --no-default-features
```

Create a tiny corpus and train a vocabulary:

```bash
mkdir -p data output
for _ in $(seq 1 100); do
  printf '{"text": "hello world 你好世界"}\n{"text": "byte pair encoding merges frequent pairs"}\n{"text": "分词器需要足够多样的语料"}\n'
done > data/train.jsonl

./target/release/bbpe train \
    --input data/train.jsonl --format jsonl \
    --vocab 300 --output output/vocab.json

./target/release/bbpe inspect output/vocab.json
./target/release/bbpe encode --model output/vocab.json --text 'hello world'
./target/release/bbpe decode --model output/vocab.json --ids 104,101,108   # hel
```

`--vocab 300` means 256 base bytes plus 44 merges. The three repeated lines
above are only a smoke-test corpus; train on your own data for real use.

The same commands are available as standalone binaries (`train`, `inspect`,
`encode`, `decode`, …) and as `bbpe <subcommand>`.

### Large corpora

```bash
./target/release/bbpe train \
    --input data/large.jsonl --format jsonl \
    --vocab 100256 --output output/vocab_100k.json \
    --sample-rate 0.2 --heldout-rate 0.005 --heldout-out output/heldout.jsonl
```

Without `--sample-rate` the trainer runs an exact full-corpus pass. With it, a
line-level random sample is used and the held-out split is written separately.
Peak RSS is approximately `9.2 × training-corpus bytes`; the trainer performs a
memory pre-check and refuses to start with a suggested down-sampling command
instead of running into OOM.

### Python

```bash
pip install bbpe_tokenizer

# From a source checkout (Rust 1.85+ required):
pip install maturin
maturin develop --release        # or: pip install .
```

```python
from bbpe_tokenizer import BBPETokenizer, BBPETrainer, TrainerConfig, JsonlFileReader

# Train
trainer = BBPETrainer(TrainerConfig(vocab_size=100256))
trainer.train(JsonlFileReader.static_text("data/train.jsonl"))
trainer.save("output/vocab.json")

# Encode / decode
tok = BBPETokenizer("output/vocab.json")
ids = tok.encode_ordinary("hello world")
print(ids, tok.decode(ids))
```

`torch` and `transformers` are optional and only needed for
`UTF8StrictLogitsProcessor`:

```bash
pip install "bbpe_tokenizer[logits]"
```

For large outputs, avoid materializing a Python `list[int]`:

```python
buf = tok.encode_ordinary_parallel_buffer(text, num_threads=8)
view = memoryview(buf)  # read-only, contiguous int32
```

`encode_ordinary_dlpack(text)` provides a one-shot CPU int32 handoff for
`torch.from_dlpack(...)`. NumPy is optional: `pip install "bbpe_tokenizer[numpy]"`.

## Performance

Indicative numbers from a single 43 GB RAM machine with an SSD, Chinese
web text, 100K vocabulary. They are measurements, not guarantees.

| Workload | Throughput |
|---|---|
| Training (8.28 GB JSONL corpus) | ~3.7 MB/s |
| Corpus sampling | ~56 MB/s |
| Peak RSS during training | ≈ 9.2 × corpus bytes |

Run the bundled benchmark to measure your own hardware:

```bash
cargo run --release --bin bench -- data/corpus.jsonl 2256 2
```

## Model format

A model is a single JSON document (`format = "bbpe-rs-model"`,
`format_version = 1`) containing the byte vocabulary, the ordered merge list,
the pre-tokenizer pattern, special tokens and a `meta` block. Legacy v0 models
(without `format`/`pre_tokenizer_pattern`) can be migrated:

```bash
bbpe inspect legacy_tokenizer.json --migrate vocab_v1.json --pattern "<training pattern>"
```

## Project layout

```text
src/            Rust library, CLI dispatch and subcommands
python/         Python package (thin wrapper over the Rust extension)
tests/          Rust integration tests and pytest suites
bench/          large-corpus preparation and encoding benchmarks
examples/       small Rust probes
scripts/        shared quality gates and release tooling
docs/           usage manual (Chinese)
```

## Documentation

- [docs/使用说明.md](https://github.com/hoare211/bbpe_rs/blob/main/docs/%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E.md) — full usage manual: CLI reference,
  Python API, model contract, troubleshooting (Chinese).
- [README.zh-CN.md](https://github.com/hoare211/bbpe_rs/blob/main/README.zh-CN.md) — Chinese README.

## Publishing (maintainers)

Pushing an exact product tag such as `v0.5.4` triggers
[`.github/workflows/pypi.yml`](.github/workflows/pypi.yml). The workflow runs the
strict release gates, builds platform wheels and an sdist, then publishes
`bbpe_tokenizer` to PyPI through Trusted Publishing. Before the first release,
open **Publishing** in the PyPI account sidebar and add a pending publisher for
project `bbpe_tokenizer` with owner `hoare211`, repository `bbpe_rs`, workflow
`pypi.yml`, and environment `pypi`. The first upload creates the project. Create a
GitHub environment named `pypi` as well; protection rules can be added there later.

For a local current-platform wheel and sdist, run:

```bash
scripts/publish-pypi.sh --dry-run   # validate and build without uploading
scripts/publish-pypi.sh             # upload using PYPI_TOKEN from .env
```

Local publishing requires a clean worktree and an exact `v<version>` tag at
`HEAD`. Never move an already-published tag or reuse a PyPI version.

## Contributing

See [CONTRIBUTING.md](https://github.com/hoare211/bbpe_rs/blob/main/CONTRIBUTING.md). Security reports: [SECURITY.md](https://github.com/hoare211/bbpe_rs/blob/main/SECURITY.md).

## License

Licensed under either of

- Apache License, Version 2.0 ([LICENSE-APACHE](https://github.com/hoare211/bbpe_rs/blob/main/LICENSE-APACHE))
- MIT license ([LICENSE-MIT](https://github.com/hoare211/bbpe_rs/blob/main/LICENSE-MIT))

at your option. This is the usual Rust dual-license arrangement: you may pick
whichever license suits your project.

Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in this work by you, as defined in the Apache-2.0 license, shall
be dual licensed as above, without any additional terms or conditions.

