Skip to content

Commit

Permalink
Add optional toktrie_hf_tokenizers dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
mmoskal committed Jan 16, 2025
1 parent aa8a846 commit 7057c3a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion python_ext/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"

[dependencies]
llguidance = { workspace = true }
# toktrie_hf_tokenizers = { workspace = true }
toktrie_hf_tokenizers = { workspace = true, optional = true }
bytemuck = "1.19.0"
pyo3 = {version = "0.21.2", features = ["extension-module", "abi3-py39"]}
serde = { version = "1.0.210", features = ["derive"] }
Expand All @@ -16,3 +16,7 @@ serde_json = "1.0.132"
name = "_lib"
crate-type = ["cdylib"]
path = "src/lib.rs"

[features]
default = []
tokenizers = ["toktrie_hf_tokenizers"]
22 changes: 15 additions & 7 deletions python_ext/src/py.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,21 @@ impl LLTokenizer {
slices: Option<Vec<String>>,
) -> PyResult<Self> {
let tok_env: TokEnv = if let Some(_tokenizer_str) = tokenizer.extract::<String>().ok() {
let _ = n_vocab;
return Err(PyValueError::new_err(
"Expecting a TokenizerWrapper() class, not a string",
));
// let tok = toktrie_hf_tokenizers::ByteTokenizerEnv::from_name(&tokenizer_str, n_vocab)
// .map_err(val_error)?;
// tok.to_env()
#[cfg(feature = "tokenizers")]
{
let tok =
toktrie_hf_tokenizers::ByteTokenizerEnv::from_name(&_tokenizer_str, n_vocab)
.map_err(val_error)?;
tok.to_env()
}

#[cfg(not(feature = "tokenizers"))]
{
let _ = n_vocab;
return Err(PyValueError::new_err(
"Expecting a TokenizerWrapper() class, not a string",
));
}
} else {
Arc::new(PyTokenizer::py_new(tokenizer)?)
};
Expand Down

0 comments on commit 7057c3a

Please sign in to comment.