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
BUG: spatial: Rotation.apply
is slow - replace np.einsum
with @
?
#20532
Comments
Rotation.apply
is slow - replace np.einsum
with @
?
Can confirm that the matrix multiplication approaches are faster on my machine @cosama updated comment with timing:
|
feel free to submit a PR @cosama ! |
@scottshambaugh @lucascolley Thanks for the feedback: I had recently a quick look into this and I am not sure if this actually hits some catch somewhere. def create_xyz():
return np.random.default_rng().random((100000, 3), dtype=float) * 100
%timeit create_xyz()
def mat():
xyz = create_xyz()
return (matrix @ xyz.T).T
%timeit mat()
def mat():
xyz = create_xyz()
return rot.apply(xyz)
%timeit mat() no speedup here:
|
I think that the similar timing in your comment is largely in the Here's what I get if I pull out xyz = np.random.default_rng().random((100000, 3), dtype=float) * 100
def mat1():
return (matrix @ xyz.T).T
%timeit mat1()
def mat2():
return rot.apply(xyz)
%timeit mat2()
|
@scottshambaugh Thanks for the reply. I just can't wrap my head around this, have been playing with it over the last 30min again. |
Describe your issue.
I just did a bit of benchmarking and
scipy.spatial.transform.Rotation.apply
is quite slow. It turned out the reason for this is thatnumpy.einsum
is quite a bit slower than direct matrix multiplication using the@
operator. This seems to be a known issue: https://stackoverflow.com/questions/20149201/why-is-numpys-einsum-slower-than-numpys-built-in-functions.Replacing the
np.einsum('ijk,ik->ij', matrix[None, :, :], vector)
call with((matrix[None, :, :] @ vector.T))[0].T
, should fix this. By the way turning on and off optimization with theoptimize=True
flag tonumpy.einsum
did not change anything for me. I can submit a pull request, just wanted to see if others can reproduce this.Here is the faulty line: https://github.com/scipy/scipy/blob/main/scipy/spatial/transform/_rotation.pyx#L2537. This would require some thought about the
inverse
kwarg, butself.inverse().as_matrix()
, would be one way of addressing that.Reproducing Code Example
Message
SciPy/NumPy/Python version and system information
The text was updated successfully, but these errors were encountered: