Skip to content

Avoiding np.diag 2.4.0 memory leak bug #30862 in unscented_transform.py#327

Open
devvaibhav455 wants to merge 1 commit intorlabbe:masterfrom
devvaibhav455:stable_unscented_tf
Open

Avoiding np.diag 2.4.0 memory leak bug #30862 in unscented_transform.py#327
devvaibhav455 wants to merge 1 commit intorlabbe:masterfrom
devvaibhav455:stable_unscented_tf

Conversation

@devvaibhav455
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant