Avoiding np.diag 2.4.0 memory leak bug #30862 in unscented_transform.py#327
Open
devvaibhav455 wants to merge 1 commit intorlabbe:masterfrom
Open
Avoiding np.diag 2.4.0 memory leak bug #30862 in unscented_transform.py#327devvaibhav455 wants to merge 1 commit intorlabbe:masterfrom
devvaibhav455 wants to merge 1 commit intorlabbe:masterfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Hi Roger,
Thank you for creating this wonderful repository and for sharing your Kalman Filter knowledge through the book and tutorials. I actually learned Kalman Filters primarily through your material.
While working on a project, I ran into an issue when using the UKF implementation with NumPy 2.4.0, where np.diag causes a memory leak:
https://github.com/numpy/numpy/issues/30862
The issue originates from this line:
P = np.dot(y.T, np.dot(np.diag(Wc), y))I replaced it with a broadcasting-based implementation that produces the same result but avoids constructing the diagonal matrix, uses less memory, less computations overall, and faster :
P = y.T @ (y * Wc[:, None])This avoids the memory leak triggered by np.diag in NumPy 2.4.0.
I understand this is a NumPy-specific bug, but it took me a couple of days to trace and diagnose the issue. I hope this small change helps others avoid running into the same problem.
Thanks again for your excellent contribution to the community.