⚡️ Speed up function select_multikrum by 287%
#30
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.
📄 287% (2.87x) speedup for
select_multikruminframework/py/flwr/serverapp/strategy/multikrum.py⏱️ Runtime :
31.7 milliseconds→8.18 milliseconds(best of186runs)📝 Explanation and details
The optimization achieves a 287% speedup by vectorizing key operations in the MultiKrum algorithm and eliminating redundant array operations.
Key optimizations applied:
Optimized array flattening in
compute_distances: Replaced the inefficient list comprehension[np.concatenate(rec.to_numpy_ndarrays(), axis=None).ravel() for rec in records]with a manual loop that handles single-array cases more efficiently. This avoids unnecessary concatenation when anArrayRecordcontains only one array (which is common), reducing from 50.5% to 27.3% of function time.Vectorized closest indices computation: Eliminated the expensive Python loop that called
np.argsort()for each distance row individually. The original code spent 22.3% of time in the loop callingnp.argsort(...).tolist()for each row. The optimization usesnp.argsort(distance_matrix, axis=1)once to sort all rows simultaneously, then slices to get closest indices.Vectorized score calculation: Replaced the list comprehension that computed scores row-by-row with
np.take_along_axis()followed by.sum(axis=1). This eliminates the expensive loop that was taking 50.1% of total time inselect_multikrum, using NumPy's optimized indexing instead of Python iteration.Performance characteristics: The optimizations are most effective for scenarios with:
The test results show consistent speedups across all scenarios, with the vectorized operations scaling better as the number of nodes and model size increase.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
To edit these changes
git checkout codeflash/optimize-select_multikrum-mh69burgand push.