Skip to content

Commit dcba4fc

Browse files
save all this
1 parent a366c59 commit dcba4fc

18 files changed

Lines changed: 38031 additions & 25 deletions

.direnv/flake-profile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
flake-profile-2-link

.direnv/flake-profile-2-link

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/nix/store/6iabdxd5vr4sgrqsclls76f5qd9yqrym-nix-shell-env

CLAUDE.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# CLAUDE.md
2+
3+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4+
5+
## Project Overview
6+
7+
This is a Rust/Python hybrid project that implements a Byte Pair Encoding (BPE) tokenizer. The core tokenization algorithms are implemented in Rust for performance, with Python bindings provided via PyO3. The project focuses on efficient text tokenization with support for special tokens and multiprocessing.
8+
9+
## Key Components Architecture
10+
11+
- **Rust Core (`src/lib.rs`)**: Contains the main BPE merge algorithm (`rusty_merge`) and helper functions. Uses PyO3 to expose functions to Python. The merge function implements an optimized version of the BPE algorithm with efficient pair counting and token updating.
12+
13+
- **Python Interface (`test.py`)**: Main training script that orchestrates the BPE training process. Handles file chunking, multiprocessing for pre-tokenization, and calls into Rust for the merge operations. Uses regex for tokenization patterns and implements heap-based optimization.
14+
15+
- **Build System**: Uses maturin for building the Rust-Python extension, configured for stable ABI (abi3-py38) for compatibility across Python versions.
16+
17+
## Development Commands
18+
19+
### Building the Extension
20+
```bash
21+
# Build the Rust extension and install in development mode
22+
maturin develop
23+
```
24+
25+
### Running Tests/Training
26+
```bash
27+
# Run the main BPE training script
28+
python test.py
29+
```
30+
31+
### Dependencies Management
32+
```bash
33+
# Install Python dependencies
34+
uv sync
35+
36+
# Add new dependencies
37+
uv add <package>
38+
```
39+
40+
### Development Environment
41+
```bash
42+
# Enter Nix development shell (if using Nix)
43+
nix develop
44+
```
45+
46+
## Key Implementation Details
47+
48+
- The Rust implementation maintains two key data structures: `pair_to_count` (frequency of byte pairs) and `pair_to_toks` (which tokens contain each pair)
49+
- The Python side handles file I/O, chunking for parallel processing, and pre-tokenization using regex patterns
50+
- Special tokens like `<|endoftext|>` are handled separately in the tokenization pipeline
51+
- The merge algorithm efficiently updates pair counts when tokens are merged, avoiding full recomputation

Cargo.lock

Lines changed: 154 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,11 @@ name = "rusty_tokey"
99
crate-type = ["cdylib"]
1010

1111
[dependencies]
12-
pyo3 = "0.25.0"
12+
once_cell = "1.21.3"
13+
rand = "0.9.0"
14+
regex = "1.11.1"
15+
16+
[dependencies.pyo3]
17+
version = "0.25.0"
18+
# "abi3-py38" tells pyo3 (and maturin) to build using the stable ABI with minimum Python version 3.8
19+
features = ["abi3-py38"]

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# rusty-tokey
2+
3+
```
4+
_ _ _
5+
_ __ _ _ ___ | |_ _ _| |_ ___ | | _____ _ _
6+
| '__| | | / __|| __| | | | __/ _ \| |/ / _ \ | | |
7+
| | | |_| \__ \| |_| |_| | || (_) | < __/ |_| |
8+
|_| \__,_|___/ \__|\__, |\__\___/|_|\_\___|\__, |
9+
|___/ |___/
10+
```
11+
12+
Byte Pair Encoding tokenizer. Rust core with Python bindings.
13+
14+
## Build
15+
16+
```bash
17+
maturin develop
18+
```
19+
20+
## Run
21+
22+
```bash
23+
python test.py
24+
```
25+
26+
## What it does
27+
28+
- Pre-tokenizes text using regex patterns
29+
- Counts byte pair frequencies
30+
- Merges most frequent pairs iteratively
31+
- Outputs vocabulary and merge operations
32+
33+
Core algorithm in Rust for speed. Python handles I/O and orchestration.

flake.lock

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
outputs = { self, nixpkgs, flake-utils, ... }:
1010
flake-utils.lib.eachDefaultSystem (system:
1111
let
12-
pkgs = import nixpkgs { inherit system; };
12+
pkgs = import nixpkgs { inherit system; config.allowUnfree = true; };
1313
in {
14+
pkgs.config.allowUnfree = true;
1415
devShells.default = pkgs.mkShell {
1516
buildInputs = [
1617
pkgs.python312
1718
pkgs.uv
19+
pkgs.claude-code
1820
];
1921
shellHook = ''
2022
echo "Python: $(python --version)"

pyproject.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,10 @@ classifiers = [
1111
"Programming Language :: Python :: Implementation :: PyPy",
1212
]
1313
dynamic = ["version"]
14+
dependencies = [
15+
"maturin>=1.8.7",
16+
"regex>=2024.11.6",
17+
]
1418
[tool.maturin]
1519
features = ["pyo3/extension-module"]
20+
python-source = "python"

python/rusty_tokey/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)