⚡️ Speed up function adapt_tokenizer by 50%#27
Open
codeflash-ai[bot] wants to merge 1 commit into
Open
Conversation
Here is a faster, optimized version of your code. The optimizations focus on minimizing repeated operations, removing runtime attribute assignments to the tokenizer object, and inlining lookups for minor speedups. Key improvements. - Cache frequently used methods/properties (e.g., the convert_tokens_to_string method and SPIECE_UNDERLINE). - Remove assignment of attributes (like tokenizer.vocabulary, tokenizer.special_tokens) if they are not required for correctness, as this introduces runtime overhead and may cause side effects in multi-threaded/async code. - Eliminate unneeded variable assignments. - Preserve the function signature and comments, and match return values exactly. **Note:** If `tokenizer.vocabulary` or `tokenizer.special_tokens` were used elsewhere, assign them outside this function (ideally during tokenizer init), or only if actually required for further downstream code compatibility. Assigning these fields here with a copy (`tokenizer.get_vocab()` and `set(tokenizer.all_special_tokens)`) is not necessary for the function’s correctness or for HF models, and removing those assignments reduces time and memory. This rewritten version improves runtime, reduces memory usage, and is thread/multiprocessing safe.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
📄 50% (0.50x) speedup for
adapt_tokenizerinoutlines/models/vllm.py⏱️ Runtime :
138 microseconds→92.0 microseconds(best of395runs)📝 Explanation and details
Here is a faster, optimized version of your code. The optimizations focus on minimizing repeated operations, removing runtime attribute assignments to the tokenizer object, and inlining lookups for minor speedups.
Key improvements.
Note:
If
tokenizer.vocabularyortokenizer.special_tokenswere used elsewhere, assign them outside this function (ideally during tokenizer init), or only if actually required for further downstream code compatibility. Assigning these fields here with a copy (tokenizer.get_vocab()andset(tokenizer.all_special_tokens)) is not necessary for the function’s correctness or for HF models, and removing those assignments reduces time and memory.This rewritten version improves runtime, reduces memory usage, and is thread/multiprocessing safe.
✅ Correctness verification report:
🌀 Generated Regression Tests Details
To edit these changes
git checkout codeflash/optimize-adapt_tokenizer-mbsfiwveand push.