[Backport M84] Replace tiktoken with gpt-tokenizer #7667
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Changes
In my memory profiling and local stress testing I always see tiktoken leaking memory and contributing to a huge spikes in a CPU usage. If I keep hammering IDE with text changes (like keep pressed Enter for a minute) it can even lead into 100% CPU utilisation which never recovers.
I decided to give some alternatives a try, and I change is easy to see with a bare end.
Namely I replaced tiktoken with gpt-tokenizer which claims to be:
> most feature-complete, open-source GPT tokenizer on NPM. This package is a port of OpenAI's [tiktoken]
It is also much more performant and less memory hungry. Additionally because it initialises much faster I deleted a bunch of code for lazy initialization of tiktoken, and I see no downside.
In a practical test with Cody, I tried the scenario with enter key being pressed for a minute (adding 1-2k lines).
After the test I looked at CPU utilisation (if it persists) and memory usage:
Old version with tiktoken:
100% CPU utilisation, 1.5G+ of memory used by agent process
New version with gpt-tokenizer:
0% CPU utilisation, less than 400MB of memory used by agent process
Test plan
All unit tests still works without any changes to the logic or values.
Scenario 1:
Scenario 2:
Enter
pressed) until ~1000 lines are addedBackport 16c0d79 from Replace tiktoken with gpt-tokenizer #7662