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

Use string_view::find() to search for tokenization to speed up #12706

Merged
merged 1 commit into from
Apr 3, 2025

Conversation

yumeyao
Copy link
Contributor

@yumeyao yumeyao commented Apr 2, 2025

The old implementation using std::string::find() consumes a lot of time if the string is very long, because it searches beyond the fragment range, looking up for tokens after the fragment end (and later drop the match result if it is beyond the range).

O(n) wasted.

Furthermore, a long string produces more fragments and for each fragment, then a token is searched again and again in the fragments, i.e. It searches within [offset1, end) [offset2, end) [offset3, end) ... where offsetn is the nth of the fragments.

So actually O(n^2) wasted.

Hope this pic helps understand the issue more easily.
image

By limiting the search area (esp. the end) using std::string_view::find(), we can avoid such unnecessary looking up.

This PR contains the minimal code change to solve the performance pitfall, to at least make it work.
Maybe someone should refactor the whole fragment stuff with std::string_view for code readability in the future.

@ggerganov
Copy link
Member

Could you give some sample numbers to get a feeling about the perf impact of this change?

@yumeyao
Copy link
Contributor Author

yumeyao commented Apr 3, 2025

Could you give some sample numbers to get a feeling about the perf impact of this change?

@KiruyaMomochi shared the issue to me to ask for help with the information 'std::string::find() is the hotspot in a profile result' and I instantly noticed the performance pitfall.
image

In my test on WSL2 Ubuntu 24.04 with gcc 13.3.0 @ AMD 5950x, building with only cmake without any extra flags, this fixes the issue:
@267c139 (before the fix): 2.7s
@63c6042 (the fix commit): 0.426s

To reproduce it:

  1. prepare the test case
    printf '<start_of_turn>user\nhello<end_of_turn>\n%.0s' {1..200} > prompt.txt
  2. run it with google gemma (in my case 3.4)
    time llama-tokenize --model google_gemma-3-4b-it-IQ2_M.gguf --file prompt.txt

I also tried changing {1..200} to {1..500}, to make the string longer, the result is more obvious:
14.249s vs 0.450s
this also confirms the theoretical O(n^2) 'wasted' time complexity.

@KiruyaMomochi
Copy link

KiruyaMomochi commented Apr 3, 2025

My bad, it works like a charm. I didn't recompile after applying the patch.

Also, @LostRuins from LostRuins#1453.

Comment on lines -2228 to -2230
// check if match is within bounds of offset <-> length
if (match + text.length() > raw_text_base_offset + raw_text_base_length) break;

Copy link
Member

Choose a reason for hiding this comment

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

I'm not following why this check is no longer needed.

If someone can confirm the correctness - feel free to merge. Otherwise I'll take a detailed look later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it's checking if the match exceeds the length and drop the match if it does, as I have pointed out, it's doing this only because it searches for a match in [offset, end_of_string) instead of the expected range [offset, offset + length), so this extra check is to guarantee the behavior is same as searching within the expected range.

now we use string_view::find() to search the expected range directly hence we dont need this extra check.

@ggerganov ggerganov merged commit 5dd5d1a into ggml-org:master Apr 3, 2025
47 of 48 checks passed
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