-
Notifications
You must be signed in to change notification settings - Fork 271
Feature: Speed up MatrixVariable.sum(axis=None)
via quicksum
#1078
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Added a test to verify that summing a matrix variable with axis=0 returns a MatrixExpr with the expected shape. This improves coverage for matrix sum operations with axis specified.
Introduced a new test to compare performance of matrix sum versus element-wise sum. Refactored imports for clarity and consistency. Renamed performance test for better description.
Renamed the Model instance from 'm' to 'model' to avoid confusion with the integer variable 'm' and improve code clarity in the test_sum_performance function.
Replaces incorrect usage of 'm' with 'model' in the assertion within test_sum_performance to ensure the correct object is referenced.
Updated the assertion in test_matrix_sum_argument to expect shape (1,) instead of (1, 1) when summing along axis 0. This aligns the test with the actual output of the sum operation.
Do you mind extending the performance tests for matrix variables and reporting the speed improvement this PR brings please? And thank you very much, as always! |
In my local environment, this change is 50x faster than before for the |
Sorry, I didn't get it. |
Modified the assertion in test_sum_performance to compare orig_time + 1 with matrix_time instead of orig_time. This may address timing precision or test flakiness.
Oh sorry @Zeroto521, I hadn't looked at the changes yet, thank you! Can you think of other things that people using matrix variables would want to be sped up? Things like adding big matrix variables, etc. If so, would you mind adding such performance tests? That's what I meant, if you think it's a reasonable request. Great work with all your PRs, by the way! 💪 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don0t know how to feel about having a test that may stochastically break, but it shouldn't be a big deal. if a problem appears down the line, we just make it optional. Thank you!
Yeah, it seems like the tests are not passing, I'm gonna skip the test. Are you sure this is faster? |
My bad, I show use import numpy as np
def quicksum(a: np.ndarray):
res = 0
for i in a.flat:
res += i
return res
class Matrix(np.ndarray):
def sum(self, *args, **kwargs):
if kwargs.get("axis") is None:
print("sum via quicksum")
return quicksum(self)
else:
print("sum via np.sum")
return super().sum(*args, **kwargs)
m = Matrix((2, 2))
m.flat = [[1, 1], [1, 1]]
print(m.sum())
print("----")
print(np.ndarray.sum(m))
print("-----")
print(np.sum(m)) Output:
|
MatrixVariable.sum(axis=None)
viaquicksum
MatrixVariable.sum
is slower thanquicksum
#1070.axis=None
.