⚡️ Speed up method KalmanFilterXYWH.update by 6%
#42
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.
📄 6% (0.06x) speedup for
KalmanFilterXYWH.updateinultralytics/trackers/utils/kalman_filter.py⏱️ Runtime :
12.8 milliseconds→12.0 milliseconds(best of163runs)📝 Explanation and details
The optimization improves performance by 6% through three key linear algebra optimizations in the Kalman filter update step:
What optimizations were applied:
np.dot(covariance, self._update_mat.T)into a reusablecov_updatevariable instead of computing it inline within thecho_solvecallmulti_dotwith directmatmulcalls: Split the three-matrix multiplicationnp.linalg.multi_dot((kalman_gain, projected_cov, kalman_gain.T))into two sequentialnp.matmuloperationsoverwrite_b=Trueinscipy.linalg.cho_solveto allow in-place operations on the input arrayWhy these optimizations provide speedup:
cov_updateeliminates duplicate matrix multiplication that was happening insidecho_solvenp.matmulcalls are faster thannp.linalg.multi_dot, which has overhead for handling variable numbers of matrices and additional checksoverwrite_b=Trueparameter reduces memory allocations by allowing SciPy to modify the input array in-place during the solve operationPerformance characteristics from test results:
The optimization shows consistent 6-7% speedups on large-scale test cases (batch processing, repeated updates, randomized inputs) while maintaining identical numerical results. Single-operation tests show more variable performance due to measurement noise, but the optimization particularly benefits workloads that perform many Kalman filter updates, which is typical in object tracking scenarios where this filter would be applied frame-by-frame to multiple tracked objects.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-KalmanFilterXYWH.update-mir9nm9gand push.