⚡️ Speed up method BeamSearchDecoder.finalize by 41% - #3
Open
codeflash-ai[bot] wants to merge 1 commit into
Open
⚡️ Speed up method BeamSearchDecoder.finalize by 41%#3codeflash-ai[bot] wants to merge 1 commit into
BeamSearchDecoder.finalize by 41%#3codeflash-ai[bot] wants to merge 1 commit into
Conversation
Here's a faster version of your code. Main changes. - Replace `np.argsort` with PyTorch's `torch.argsort` to avoid unnecessary conversion between numpy arrays and torch tensors (which can be expensive on GPU). - Avoid repeated `.tolist()` and multiple conversions between data types. - Pre-allocate lists with list comprehensions instead of updating dicts with tuple keys (where not required). - Simplify loops to reduce the number of Python-side operations. - Remove unnecessary variables and avoid repeated lookups. **Key optimization details:** - Uses torch's `topk` for quick sorting and indexing instead of numpy. - Reduces nested for loops and conversions. - Keeps your logic and signatures identical. If you **must** have unique sequence keys (as before) and the sequences could possibly repeat, the original dict usage is retained. However, for even more speed, consider using a list if uniqueness is not critical or cannot be violated in your context. Let me know if your `finished_sequences` can be a list-of-lists and you're only interested in speed at the expense of removing uniqueness enforcement! This version, though, already avoids numpy and should be considerably faster in batched GPU/CPU settings.
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.
📄 41% (0.41x) speedup for
BeamSearchDecoder.finalizeinwhisper/decoding.py⏱️ Runtime :
2.66 milliseconds→1.88 milliseconds(best of570runs)📝 Explanation and details
Here's a faster version of your code.
Main changes.
np.argsortwith PyTorch'storch.argsortto avoid unnecessary conversion between numpy arrays and torch tensors (which can be expensive on GPU)..tolist()and multiple conversions between data types.Key optimization details:
topkfor quick sorting and indexing instead of numpy.If you must have unique sequence keys (as before) and the sequences could possibly repeat, the original dict usage is retained. However, for even more speed, consider using a list if uniqueness is not critical or cannot be violated in your context.
Let me know if your
finished_sequencescan be a list-of-lists and you're only interested in speed at the expense of removing uniqueness enforcement! This version, though, already avoids numpy and should be considerably faster in batched GPU/CPU settings.✅ Correctness verification report:
🌀 Generated Regression Tests Details
To edit these changes
git checkout codeflash/optimize-BeamSearchDecoder.finalize-mayry8p6and push.