⚡️ Speed up function is_continuous_axis by 83%
#199
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.
📄 83% (0.83x) speedup for
is_continuous_axisinkeras/src/utils/python_utils.py⏱️ Runtime :
688 microseconds→376 microseconds(best of172runs)📝 Explanation and details
The optimized code achieves an 82% speedup by eliminating redundant computation and leveraging early exit optimization.
Key optimization: The original code redundantly checked the same condition twice with separate
positive_order_flagandnegative_order_flagloops. Both loops were testingaxis[i + 1] - axis[i] != 1, making one of them completely unnecessary since the function only needs to determine if consecutive elements differ by exactly 1.What changed:
Falseimmediately when the first non-consecutive pair is foundTrueonly if all consecutive differences equal 1Why it's faster:
Performance impact: Based on the function references,
is_continuous_axisis called in critical neural network operations like RMS normalization and layer normalization in PyTorch backend optimizations. The test results show particularly strong gains (90%+ speedup) for large continuous arrays and arrays with early discontinuities, which are common in tensor dimension checking scenarios. This optimization will significantly benefit deep learning workloads that frequently validate tensor axis continuity during model execution.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-is_continuous_axis-mja5qf5cand push.