Skip to content

Commit b5d9df6

Browse files
authored
Merge pull request #10 from SamBroomy/python-bindings-feature-flag
feat: python feature flag
2 parents 5d376ce + 298c2b6 commit b5d9df6

4 files changed

Lines changed: 37 additions & 36 deletions

File tree

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ crate-type = ["cdylib", "rlib"]
1616

1717
[features]
1818
default = []
19+
python = ["dep:pyo3"]
1920
pcre2 = ["dep:pcre2"]
2021

2122
[dependencies]
@@ -28,13 +29,13 @@ rustc-hash = "2.0"
2829
# Error handling
2930
thiserror = "2.0"
3031
# Python bindings
31-
pyo3 = { version = "0.23", features = ["extension-module"] }
32+
pyo3 = { version = "0.27", features = ["extension-module"], optional = true }
3233
# Base64 decoding for tiktoken vocab files
3334
base64 = "0.22"
3435
# Aho-Corasick for fast multi-pattern special token matching
3536
aho-corasick = "1.1"
3637
# LRU cache for frequent token sequences
37-
lru = "0.12"
38+
lru = "0.16"
3839
# regexr regex engine (default backend)
3940
regexr = { version = "0.1.0-beta.4", features = ["jit", "simd"] }
4041

pyproject.toml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ name = "splintr-rs"
77
version = "0.6.0"
88
description = "Fast Rust BPE tokenizer with Python bindings"
99
readme = "README.md"
10-
license = {text = "MIT"}
10+
license = { text = "MIT" }
1111
requires-python = ">=3.8"
1212
keywords = ["tokenizer", "bpe", "tiktoken", "gpt", "llm"]
13-
authors = [
14-
{name = "Farhan"}
15-
]
13+
authors = [{ name = "Farhan" }]
1614
classifiers = [
1715
"Development Status :: 4 - Beta",
1816
"Intended Audience :: Developers",
@@ -38,4 +36,4 @@ Documentation = "https://github.com/farhan/splintr#readme"
3836
python-source = "python"
3937
module-name = "splintr._core"
4038
bindings = "pyo3"
41-
features = ["pyo3/extension-module"]
39+
features = ["python"]

src/lib.rs

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,8 @@
11
pub mod core;
2+
#[cfg(feature = "python")]
23
mod python;
34

4-
use pyo3::prelude::*;
5-
65
pub use core::{
76
ByteLevelStreamingDecoder, StreamingDecoder, Tokenizer, TokenizerError, CL100K_BASE_PATTERN,
87
LLAMA3_PATTERN, O200K_BASE_PATTERN,
98
};
10-
11-
/// Splintr - Fast Rust BPE tokenizer with Python bindings
12-
///
13-
/// A high-performance tokenizer featuring:
14-
/// - Regexr with JIT and SIMD (default, pure Rust)
15-
/// - Optional PCRE2 with JIT (requires `pcre2` feature)
16-
/// - Rayon parallelism for multi-core encoding
17-
/// - Linked-list BPE algorithm (avoids O(N²) on pathological inputs)
18-
/// - FxHashMap for fast lookups
19-
/// - Aho-Corasick for fast special token matching
20-
/// - LRU cache for frequently encoded chunks
21-
/// - UTF-8 streaming decoder for LLM output
22-
/// - Agent tokens for chat/reasoning/tool-use applications
23-
#[pymodule]
24-
fn _core(m: &Bound<'_, PyModule>) -> PyResult<()> {
25-
m.add_class::<python::PyTokenizer>()?;
26-
m.add_class::<python::PyStreamingDecoder>()?;
27-
m.add_class::<python::PyByteLevelStreamingDecoder>()?;
28-
m.add_class::<python::PyCL100KAgentTokens>()?;
29-
m.add_class::<python::PyO200KAgentTokens>()?;
30-
m.add_class::<python::PyLlama3AgentTokens>()?;
31-
m.add_class::<python::PyDeepSeekV3AgentTokens>()?;
32-
m.add("CL100K_BASE_PATTERN", CL100K_BASE_PATTERN)?;
33-
m.add("O200K_BASE_PATTERN", O200K_BASE_PATTERN)?;
34-
m.add("LLAMA3_PATTERN", LLAMA3_PATTERN)?;
35-
Ok(())
36-
}

src/python/mod.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,36 @@
11
mod bindings;
22

3+
use crate::core::{CL100K_BASE_PATTERN, LLAMA3_PATTERN, O200K_BASE_PATTERN};
34
pub use bindings::{
45
PyByteLevelStreamingDecoder, PyCL100KAgentTokens, PyDeepSeekV3AgentTokens, PyLlama3AgentTokens,
56
PyO200KAgentTokens, PyStreamingDecoder, PyTokenizer,
67
};
8+
9+
use pyo3::prelude::*;
10+
11+
/// Splintr - Fast Rust BPE tokenizer with Python bindings
12+
///
13+
/// A high-performance tokenizer featuring:
14+
/// - Regexr with JIT and SIMD (default, pure Rust)
15+
/// - Optional PCRE2 with JIT (requires `pcre2` feature)
16+
/// - Rayon parallelism for multi-core encoding
17+
/// - Linked-list BPE algorithm (avoids O(N²) on pathological inputs)
18+
/// - FxHashMap for fast lookups
19+
/// - Aho-Corasick for fast special token matching
20+
/// - LRU cache for frequently encoded chunks
21+
/// - UTF-8 streaming decoder for LLM output
22+
/// - Agent tokens for chat/reasoning/tool-use applications
23+
#[pymodule]
24+
fn _core(m: &Bound<'_, PyModule>) -> PyResult<()> {
25+
m.add_class::<PyTokenizer>()?;
26+
m.add_class::<PyStreamingDecoder>()?;
27+
m.add_class::<PyByteLevelStreamingDecoder>()?;
28+
m.add_class::<PyCL100KAgentTokens>()?;
29+
m.add_class::<PyO200KAgentTokens>()?;
30+
m.add_class::<PyLlama3AgentTokens>()?;
31+
m.add_class::<PyDeepSeekV3AgentTokens>()?;
32+
m.add("CL100K_BASE_PATTERN", CL100K_BASE_PATTERN)?;
33+
m.add("O200K_BASE_PATTERN", O200K_BASE_PATTERN)?;
34+
m.add("LLAMA3_PATTERN", LLAMA3_PATTERN)?;
35+
Ok(())
36+
}

0 commit comments

Comments
 (0)