Skip to content

broadcasting in af_arith for binary operations #2871

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

Merged
merged 10 commits into from
Jul 5, 2022

Conversation

syurkevi
Copy link
Contributor

@syurkevi syurkevi commented May 5, 2020

Will prepend 1s until dimension sizes match, then 1s are treated as wildcards and are broadcasted to their matching sizes.

array A = range(dim4(32, 32, 3), 2);  // [32 32 3]
array B = -range(dim4(3)); //    [ 3 ] -> [1* 1* 3]

array C = A + B; // [32 32 3]

In cases where one of the arguments is a vector, prepending until dimensions match may not align well:

 array A = constant(-1, dim4(15, 3, 5)); // [15 3 5]
 array B = constant(1, dim4(3, 1));        // [ 1  1 3] - misaligned,
 // even though 3 intended to broadcast with second dimension of A

Upon a mismatch, the prepended vectors will be shifted left until there is a match. In the above example the broadcast will be a success after a single additional shift.

This manner of treating vectors is a workaround due to no difference between.

af::dim4 d1(3);
af::dim4 d2(3, 1);
af::dim4 d3(3, 1, 1);
af::dim4 d4(3, 1, 1, 1);
// d1 == d2 == d3 == d4

TODO:

  • n-ary operations?
  • complex values

@syurkevi syurkevi force-pushed the broadcast branch 2 times, most recently from a945c6f to 59bea35 Compare June 11, 2022 00:37
@syurkevi
Copy link
Contributor Author

As per the discussion during the maintainers meeting, support for matching vectors to corresponding matrix dimensions was dropped due to ambiguous cases with multiple matching dimensions. Instead, only the 1s wild-card style broadcasting will be supported:

array a = randu(5, 3) + randu(5); // Okay
array b = randu(5, 3) + randu(3); // Fails
array c = randu(5, 1, 3) + randu(5); // Okay
array d = randu(5, 1, 3) + randu(1, 4); // Okay
array e = randu(5, 1, 3) + randu(1, 1, 3); // Okay
array f = randu(5, 1, 3) + randu(5, 5, 3); // Okay
array g = randu(5, 1, 3) + randu(5, 3, 3); // Okay

array i = randu(3, 255, 255) + randu(3); // Okay

@syurkevi syurkevi changed the title broadcasting in af_arith for binary operations [WIP] broadcasting in af_arith for binary operations Jun 15, 2022
@syurkevi syurkevi force-pushed the broadcast branch 6 times, most recently from 9320ad0 to ef77042 Compare June 29, 2022 20:14
@umar456 umar456 merged commit c115cbc into arrayfire:master Jul 5, 2022
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.

3 participants