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

[Tokenizers] Question regarding performance #7143

Open
r-Larch opened this issue Apr 27, 2024 · 0 comments
Open

[Tokenizers] Question regarding performance #7143

r-Larch opened this issue Apr 27, 2024 · 0 comments
Labels
untriaged New issue has not been triaged

Comments

@r-Larch
Copy link

r-Larch commented Apr 27, 2024

Hi, thanks for the effort put into the Microsoft.ML.Tokenizers!

I'm the author of the last performance improvements in SharpToken library.
Since MLTokenizers are faster now than SharpToken I looked into the sources to understand where this performance comes from.

Now I have a question (out of curiosity)

Why is it required to copy a ReadOnlySpan<char> to a buffer, when the rest of the code just uses ReadOnlySpan<char> again?

TiktokenPreTokenizer.cs line: 104

public override IEnumerable<(int Offset, int Length)> PreTokenize(ReadOnlySpan<char> text)
{
if (text.IsEmpty)
{
return [];
}
#if NET7_0_OR_GREATER
char[] buffer = ArrayPool<char>.Shared.Rent(text.Length);
text.CopyTo(buffer);
return SplitText(buffer, _regex, _specialTokensRegex, text.Length);
static IEnumerable<(int Offset, int Length)> SplitText(char[] text, Regex regex, Regex? specialTokensRegex, int textLength)

PreTokenizer.cs line: 74

internal static IEnumerable<(int Offset, int Length)> SplitText(ReadOnlySpan<char> text, Regex regex)
{
#if NET7_0_OR_GREATER
char[] buffer = ArrayPool<char>.Shared.Rent(text.Length);
text.CopyTo(buffer);
return SplitText(buffer, regex, text.Length);
static IEnumerable<(int Offset, int Length)> SplitText(char[] text, Regex regex, int textLength)
{
(int Offset, int Length) match;
int beginning = 0;
while (TryGetMatch(regex, text, beginning, textLength - beginning, out match))

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged label Apr 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
untriaged New issue has not been triaged
Projects
None yet
Development

No branches or pull requests

1 participant