⚡️ Speed up function validate_and_format_image_pairs by 28%
#384
+28
−12
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.
📄 28% (0.28x) speedup for
validate_and_format_image_pairsinsrc/transformers/models/lightglue/image_processing_lightglue.py⏱️ Runtime :
2.58 milliseconds→2.02 milliseconds(best of57runs)📝 Explanation and details
The optimized code achieves a 27% speedup by eliminating costly Python overhead from nested
all()calls and generator expressions, which were the primary bottleneck consuming 91.4% of execution time.Key optimizations:
Short-circuit validation in
_is_valid_image: The function now returns early for PIL images and invalid images, avoiding unnecessaryget_image_type()calls and shape checks.Replaced nested
all()with explicit loops: The original code usedall(isinstance(image_pair, list) and len(image_pair) == 2 and all(_is_valid_image(image) for image in image_pair) for image_pair in images)which creates multiple generator objects and has significant function call overhead. The optimized version uses a simple for-loop with early breaking, reducing the overhead by directly checking conditions.Optimized list flattening: Replaced the list comprehension
[image for image_pair in images for image in image_pair]with in-placelist.extend()calls, which avoids creating intermediate lists and reduces memory allocations.Direct indexing for pairs: For the simple case of exactly 2 images, the code now directly accesses
images[0]andimages[1]instead of using generators withall().Impact on workloads: Since this function is called in
preprocess()andvisualize_keypoint_matching()- both likely to be in hot paths for image processing pipelines - this optimization will provide meaningful performance improvements, especially for:The optimization is particularly effective for the transformer's LightGlue model preprocessing, where image validation happens before every inference or visualization operation.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-validate_and_format_image_pairs-mia63r9fand push.