Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to pass a tokenizer from Python into Rust? #1487

Closed
albertsgarde opened this issue Apr 5, 2024 · 2 comments
Closed

Is it possible to pass a tokenizer from Python into Rust? #1487

albertsgarde opened this issue Apr 5, 2024 · 2 comments
Labels

Comments

@albertsgarde
Copy link

I am implementing a part of my Python project in Rust. I need to use a tokenizer deep in the Rust code, but I want the Python code to decide what tokenizer to use. I would therefore like to pass a tokenizer from Python to Rust. Since the Python wrapper is supposed to be thin, I have hope that this is possible. If it is not, I'll probably just use Pyo3 to call Python from Rust, but that would both be uglier and feel silly to call Python code that only calls right back into Rust.

What I have tried looks something like this:

use tokenizers::Tokenizer;

#[pyfunction]
pub fn foo(tokenizer: &Tokenizer) {
	baz(tokenizer)
}

but Pyo3 gives me the error "the trait bound &tokenizers::Tokenizer: pyo3::PyClass is not satisfied". Looking at the source, this makes sense since it is the PyTokenizer struct in the crate tokenizers-python that wraps Tokenizer and is turned into a Python class. If I could access the PyTokenizer struct in Rust I might therefore be able to fix the problem, but since tokenizers-python is not available on crates.io this is difficult.

Is there some way to pass a tokenizer from Python to Rust?

Copy link

github-actions bot commented May 6, 2024

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot added the Stale label May 6, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale May 11, 2024
@ArthurZucker
Copy link
Collaborator

SOrry for the late reply here, I think pyo3 is required for this TBH. We have example of this, we use wrappers and match types to switch between model or decoder objects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants