⚡️ Speed up function convert_sam2_segmentation_response_to_inference_instances_seg_response by 51%
#585
+10
−9
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.
📄 51% (0.51x) speedup for
convert_sam2_segmentation_response_to_inference_instances_seg_responseininference/core/workflows/core_steps/models/foundation/segment_anything2/v1.py⏱️ Runtime :
30.0 milliseconds→19.8 milliseconds(best of209runs)📝 Explanation and details
The optimized code achieves a 51% speedup through two key NumPy-based optimizations:
1. Vectorized coordinate operations: Instead of using Python list comprehensions to extract x/y coordinates (
[coord[0] for coord in mask]), the code converts each mask to a NumPy array once withnp.asarray(mask)and then uses vectorized slicing (mask_coords[:, 0]andmask_coords[:, 1]) and NumPy's optimizedmin()/max()methods. This eliminates expensive Python loops for coordinate processing.2. Early confidence filtering: The confidence threshold check (
prediction.confidence < threshold) is moved outside the mask loop, so when a prediction fails the threshold test, all its masks are skipped immediately rather than processing each mask before the confidence check.Performance characteristics from tests:
These optimizations are particularly effective for computer vision workloads where masks typically contain many coordinate points, making the vectorized NumPy operations significantly faster than Python list processing.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-convert_sam2_segmentation_response_to_inference_instances_seg_response-mh9th7dpand push.