Skip to content

refactor: extract pretok algo#2134

Merged
McPatate merged 1 commit into
feat/train_encode_splitfrom
refactor/extract_pretok_algo
Jul 5, 2026
Merged

refactor: extract pretok algo#2134
McPatate merged 1 commit into
feat/train_encode_splitfrom
refactor/extract_pretok_algo

Conversation

@McPatate

@McPatate McPatate commented Jul 1, 2026

Copy link
Copy Markdown
Member

When going through the punctuation pretokenizer implementation, I noticed that I would have to reimplement the same kind of algo for each variant of its inner behavior field, which is also the same as the Whitespace and Bert algo.

Extracting this into an easy to adapt API for each pretokenizer, while maintaining the perf gains shown by the other two variants.

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

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.

@ArthurZucker ArthurZucker left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

feels like policy is not needed and classify alone can do this.
Otherwise makes a lot of sense.its super impressive that this just gives 8x.
Pushing to out is fine as long as out is pre allocated for worst case IMO!

Comment on lines +46 to +47
classify: impl Fn(char) -> C,
policy: impl Fn(C) -> SplitPolicy,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we want generics instead here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

What do you have in mind?

Comment on lines +43 to +48
pub fn split<C: Copy + PartialEq>(
text: &str,
out: &mut Vec<Split>,
classify: impl Fn(char) -> C,
policy: impl Fn(C) -> SplitPolicy,
) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could be a blanket implementation in the pipeline::PreTokenizer trait, wdyt?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Meh, the fn knows nothing about PreTokenizer, doesn't need self so I'd rather leave it as its own separate thing.


impl pipeline::PreTokenizer for Whitespace {
// XXX: surprisingly, inlining here yields 10-15% slower performance
#[inline(never)]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why not letting the compiler handle this?

@McPatate McPatate Jul 5, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We're not sure it'll pick the right choice. Here I've tested and observed that if this code is inlined, perf degrades, so we tell the compiler to make sure it always makes the right decision.

Signed-off-by: Luc Georges <luc.sydney.georges@gmail.com>
@McPatate McPatate force-pushed the refactor/extract_pretok_algo branch from 2961689 to 5fc5280 Compare July 5, 2026 08:10
@McPatate McPatate merged commit 3ce145c into feat/train_encode_split Jul 5, 2026
33 of 42 checks passed
@McPatate McPatate deleted the refactor/extract_pretok_algo branch July 5, 2026 08:36
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.

4 participants