-
Notifications
You must be signed in to change notification settings - Fork 311
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
Broadcasting limitations / Custom strides? #437
Comments
Currently, #[macro_use]
extern crate ndarray;
fn main() {
let a = array![[[1, 2], [3, 4]]];
let b = array![[[5, 6]], [[7, 8]]];
let c = &a.broadcast((2, 2, 2)).unwrap() * &b;
assert_eq!(a.shape(), [1, 2, 2]);
assert_eq!(b.shape(), [2, 1, 2]);
assert_eq!(c.shape(), [2, 2, 2]);
println!("{}\n*\n{}\n=\n{}", a, b, c);
} which prints
which is the same result NumPy gives. Basically, Note that Having to manually call |
Thank you @jturner314! That solves my problems with broadcasting. Now, this could be another issue, but I have compared the performance of ndarray and Numpy and the results are very surprising to me: i7 6700k 32GB 1867Mhz
The benchmark code is here, if you wish to check it. In this benchmark I multiplied two arrays with 2^{n} elements. I added n/2 axes with length 1 on each array. In one array, the axes were added at the start. In the other array, the axes were added at the end. The expected result is going to have 2^{n + n / 2} elements. Of course, the problem is exponential with n. Python's solution is way, way more performant. I know that Numpy is really mature and it is a great piece of work (with huge amounts of C code), but the difference seems like black magic coming from the Numpy devs. Maybe am I doing something wrong? I added blas support in the rust project. However, it does not make any difference in the performance results (not worse, not better). That is weird to me, because blas should be pretty optimized I guess. Thank you again for your help and support! |
Well, NumPy is mature and highly optimized, while Some of the changes I made to your benchmarks were to make the NumPy and For what it's worth, the benchmark you're using is probably a worst-case for the way arithmetic operators are currently implemented in A couple of notes:
|
Thank you again @jturner314. Your suggestions are always good. I didn't prepare the benchmark to compare ndarray and Numpy in general. The benchmark was created because I need that functionality on one of my projects. I understand that the library and the language itself should be more mature to compete against Numpy. So thanks again, and keep the good work! |
@davenza You're welcome. I hope It seems like this issue is resolved, so I'm closing it. If there's anything else, please feel free to reopen this issue or open a new one. |
Hi, I have a problem that I don't know how to solve with the current broacast implementation.
I want to multiply two matrices where some axis in both matrices could have length 1. For example:
shape [1,2,2] * shape [2,1,2]
The expected matrix would be a
shape [2, 2, 2]
. #265 illustrates this behaviour better than me. This is totally natural in Numpy. Of course, the current implementation of ndarray does not allow this type of operations and @bluss commented that was not keen to implement it.Custom strides?
I have been thinking how to solve this problem, and the unique (efficient) solution that I could come up is using custom strides where the stride is 0 in the axis with length 1. So, our original matrices:
shape [1,2,2], strides [4,2,1] * shape [2,1,2], strides[4,2,1]
would be converted to:
shape [2,2,2], strides [0,2,1] * shape [2,1,2], strides[4,2,1]
Note that only the left matrix needs to be customized. However, the current ArrayBase implementation does not allow changes on the strides. The unique way to customize strides is: from_shape_vec. However, as the documentation states:
Errors if strides allow multiple indices to point to the same element
, so this type of changes are not allowed. How would you solve this problem?The text was updated successfully, but these errors were encountered: