⚡️ Speed up function filter_tensors_by_objectness by 20%
#641
+16
−7
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.
📄 20% (0.20x) speedup for
filter_tensors_by_objectnessininference/models/owlv2/owlv2.py⏱️ Runtime :
486 microseconds→406 microseconds(best of20runs)📝 Explanation and details
The optimized code achieves a 19% speedup through two key improvements:
1. Simplified tensor squeeze operations:
logit_shift.squeeze(0).squeeze(1)andlogit_scale.squeeze(0).squeeze(1)logit_shift.squeeze()andlogit_scale.squeeze()2. Replaced basic indexing with
index_select():boxes[objectness_indices],image_class_embeds[objectness_indices], etc.boxes.index_select(0, indices),image_class_embeds.index_select(0, indices), etc.index_select()is more efficient for first-axis indexing in PyTorch, providing better memory locality and reduced overheadPerformance characteristics from tests:
The changes maintain identical functionality while reducing both computational overhead and memory allocations during the tensor filtering process.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-filter_tensors_by_objectness-mhc8u5ieand push.