⚡️ Speed up function pad by 22%
#150
Open
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.
📄 22% (0.22x) speedup for
padinkeras/src/backend/openvino/numpy.py⏱️ Runtime :
54.1 microseconds→44.4 microseconds(best of5runs)📝 Explanation and details
The optimization achieves a 21% speedup through two main improvements:
1. Optimized
get_ov_outputfunction:KerasVariable,OpenVINOKerasTensor,Tensor) to the top with early returns, eliminating unnecessary downstream checks for 70% of calls based on the profiler databfloat16_dtype = np.dtype("bfloat16")at module level, avoiding repeated dtype object creation (reduces 26.5% time hotspot by ~32%)x = list(x)conversion sinceov_opset.constant()accepts both tuples and lists directly2. Optimized
padfunction:zip(*pad_width): The original code used explicit loops and.append()calls to buildpads_beginandpads_endlists. The optimized version useszip(*pad_width)to unpack all pairs at once, which is significantly faster in Python due to reduced interpreter overhead and fewer function callsThe profiler shows the
zipoptimization reduced the pad extraction time from ~1.8% to negligible, while theget_ov_outputimprovements reduced per-hit time from 1457ns to 1171ns for the first isinstance check.Impact on workloads: Since
get_ov_outputis called frequently in tensor operations andpadis commonly used in neural network layers (convolutions, pooling), these optimizations will provide cumulative benefits in model training and inference pipelines. The test results show consistent 9-49% improvements across error cases, indicating the optimizations are effective across different input patterns.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pad-mir9ozc4and push.