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

Decode stream python #1678

Merged
merged 2 commits into from
Nov 15, 2024
Merged

Decode stream python #1678

merged 2 commits into from
Nov 15, 2024

Conversation

Narsil
Copy link
Collaborator

@Narsil Narsil commented Nov 7, 2024

Follow up of #1677

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Copy link
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, I am a bit curious about the rational behind not using / creating a rust DecodeStream

///
#[pyclass(module = "tokenizers.decoders", name = "DecodeStream")]
#[derive(Clone)]
pub struct PyDecodeStream {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

intersting, so we don't use the rust DecodeStream object. I am guessing it's for ownership reasons? Otherwise we needs to wrap the PyDecodeStream {stream: DecodeStream} with arc?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arc doesn't save us. The issue is with the borrow of the tokenizer's lifetime.

You can technically reborrow in every call in rust too, it's just not very "rusty".
For Python, we need to get access to the tokenizer on every call, and cloning into an Arc feels super wasteful (and breaks every update you might do on the tokenizer afterwards).

This seems innocuous enough since currently users have to hold already a reference to the tokenizer anyway.

}
}

/// Internal function exposed only to bypass python limitations
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what were the limitations?

@Narsil
Copy link
Collaborator Author

Narsil commented Nov 15, 2024

Sounds good, I am a bit curious about the rational behind not using / creating a rust DecodeStream

Lifetimes cannot cross the boundary. Stream<'a> is not possible to use directly in the bindings, so reborrowing the tokenizer on every call is the only way for python.

Different API because Python cannot handle lifetimes properly.
@Narsil Narsil merged commit cc5fb01 into main Nov 15, 2024
30 checks passed
@Narsil Narsil deleted the decode_stream_python branch November 15, 2024 11:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants