⚡️ Speed up function map_graph by 146%
#222
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.
📄 146% (1.46x) speedup for
map_graphinkeras/src/ops/function.py⏱️ Runtime :
13.9 milliseconds→5.64 milliseconds(best of36runs)📝 Explanation and details
The optimized code achieves a 146% speedup primarily through replacing an O(n²) algorithm with an O(n) algorithm for operation name uniqueness checking, which becomes critical for large models.
Key Optimization:
all_names.count(name)which scans the entire list, resulting in O(n²) complexityname_counts) to track occurrences in a single pass, then checks counts separately - reducing to O(n) complexityThe line profiler shows the dramatic impact: the original
all_names.count(name)took 8.47ms (20.6% of total time), while the optimized name counting takes only 0.42ms (1.3% of total time) - a 95% reduction in this section alone.Why this matters for Keras:
Based on the function references,
map_graphis called during model initialization in the Function constructor, which processes all operations in a neural network. Large models with hundreds or thousands of operations would experience quadratic slowdown in the original version, making model creation prohibitively slow.Test case performance:
The optimization preserves all existing behavior while dramatically improving scalability for real-world deep learning models where operation counts can be very large.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-map_graph-mjaqgp2dand push.