Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions bindings/python/src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ fn poisoned<G>(_: std::sync::PoisonError<G>) -> PyErr {
PyRuntimeError::new_err("tokenizer lock poisoned")
}

/// The input sequence(s) to [`encode`](`PyTokenizer::encode`)
/// Can be either a single str or a pair of (str, str)
#[derive(FromPyObject)]
enum InputSequence {
#[pyo3(annotation = "str")]
Single(PyBackedStr),
#[pyo3(annotation = "(str, str)")]
Pair(PyBackedStr, PyBackedStr),
}

/// A tokenizer: a model plus its optional normalizer and pre-tokenizer.
///
/// Create one from a model (`Tokenizer(models.BPE())`), a file
Expand Down Expand Up @@ -173,7 +183,7 @@ impl PyTokenizer {
fn run_encode(
&self,
py: Python<'_>,
text: &str,
text: &InputSequence,
add_special_tokens: bool,
) -> PyResult<Vec<u32>> {
self.inner.with(py, |lock| {
Expand All @@ -197,7 +207,7 @@ impl PyTokenizer {
fn run_encode_batch(
&self,
py: Python<'_>,
texts: &[PyBackedStr],
texts: &[InputSequence],
add_special_tokens: bool,
) -> PyResult<Vec<Vec<u32>>> {
self.inner.with(py, |lock| {
Expand Down Expand Up @@ -285,7 +295,7 @@ fn get_or_compile(lock: &Detached<'_, Inner>) -> PyResult<Arc<PipelineTokenizer>

fn encode_one(
pipe: &PipelineTokenizer,
text: &str,
text: InputSequence,
pre_tokens: &mut Vec<Span>,
add_special_tokens: bool,
scratch: &mut PipelineModelScratch,
Expand All @@ -308,7 +318,7 @@ fn encode_one(
/// and the batch is worth splitting; the caller has already released the GIL.
fn encode_batch_core(
pipe: &PipelineTokenizer,
texts: &[PyBackedStr],
texts: &[InputSequence],
add_special_tokens: bool,
) -> PyResult<Vec<Vec<u32>>> {
if get_parallelism() && texts.len() > 1 {
Expand Down Expand Up @@ -428,7 +438,7 @@ impl PyTokenizer {
fn encode_batch_ids<'py>(
&self,
py: Python<'py>,
texts: Vec<PyBackedStr>,
texts: Vec<InputSequence>,
add_special_tokens: bool,
) -> PyResult<Bound<'py, PyList>> {
let batches = self.run_encode_batch(py, &texts, add_special_tokens)?;
Expand Down Expand Up @@ -479,7 +489,7 @@ impl PyTokenizer {
#[pyo3(signature = (texts, *, add_special_tokens = true) -> "EncodingBatch")]
fn encode_batch(
slf: &Bound<'_, Self>,
texts: Vec<PyBackedStr>,
texts: Vec<InputSequence>,
add_special_tokens: bool,
) -> PyResult<PyEncodingBatch> {
let rows = slf
Expand Down Expand Up @@ -764,3 +774,8 @@ impl Iterator for BufferedPyIterator {
self.buffer.pop_front()
}
}


struct PyEncodeInput {

}
Loading
Loading