-
Notifications
You must be signed in to change notification settings - Fork 340
Implement co-broadcasting in operator overloading #898
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
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
f7a9491
Fix co_broadcast in operator overloading
SparrowLii 5e77eed
Add BroadCastShape<Ix0> to the Dimension definition
SparrowLii e8b29e1
add BroadCastShape<<Self as Dimension>::Smaller>
SparrowLii f7c9da1
use uninitialized to avoid traversing or cloning
SparrowLii 0561c13
Use MaybeUninitSubst and Zip to avoid uninitialized(), rename BroadCa…
SparrowLii fc969bd
treat zero dimension like numpy does
SparrowLii a19149b
rebase and use map_collect_owned in impl_ops.rs
SparrowLii b1f4f95
use izip in index loop in broadcast.rs
SparrowLii c9eb88e
Update documentation and function names
SparrowLii e3b73cc
Update documentation and function names
SparrowLii b623239
Add function broadcast_with
SparrowLii c24e602
Modify the docs and visibility of broadcast_with
SparrowLii 16f382b
TEST: Remove unused imports in tests/broadcast.rs
bluss b39593e
FIX: Rename BroadcastShape to DimMax
bluss 38f7341
FIX: Remove broadcast_shape from the DimMax trait
bluss 03cfdfc
MAINT: Fix clippy warnings for broadcast_with
bluss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
use uninitialized to avoid traversing or cloning
- Loading branch information
commit f7c9da16b483d6b4909f43eb055acf1a896e96ab
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,20 +56,22 @@ macro_rules! impl_binary_op( | |
/// between `self` and `rhs`, | ||
/// and return the result. | ||
/// | ||
/// `self` must be an `Array` or `ArcArray`. | ||
/// | ||
/// If their shapes disagree, `self` is broadcast to their broadcast shape, | ||
/// cloning the data if needed. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cloning is not necessary to mention here IMO. It's more appropriate to address this in general in the arithmetic ops section (docs for struct ArrayBase). |
||
/// | ||
/// **Panics** if broadcasting isn’t possible. | ||
impl<A, B, S, S2, D, E> $trt<ArrayBase<S2, E>> for ArrayBase<S, D> | ||
where | ||
A: Clone + $trt<B, Output=A>, | ||
A: Copy + $trt<B, Output=A>, | ||
B: Clone, | ||
S: Data<Elem=A>, | ||
S: DataOwned<Elem=A> + DataMut, | ||
S2: Data<Elem=B>, | ||
D: Dimension + BroadcastShape<E>, | ||
E: Dimension, | ||
{ | ||
type Output = Array<A, <D as BroadcastShape<E>>::BroadcastOutput>; | ||
type Output = ArrayBase<S, <D as BroadcastShape<E>>::BroadcastOutput>; | ||
fn $mth(self, rhs: ArrayBase<S2, E>) -> Self::Output | ||
{ | ||
self.$mth(&rhs) | ||
|
@@ -79,25 +81,46 @@ where | |
/// Perform elementwise | ||
#[doc=$doc] | ||
/// between reference `self` and `rhs`, | ||
/// and return the result as a new `Array`. | ||
/// and return the result. | ||
/// | ||
/// `rhs` must be an `Array` or `ArcArray`. | ||
/// | ||
/// If their shapes disagree, `self` is broadcast to their broadcast shape, | ||
/// cloning the data if needed. | ||
/// | ||
/// **Panics** if broadcasting isn’t possible. | ||
impl<'a, A, B, S, S2, D, E> $trt<ArrayBase<S2, E>> for &'a ArrayBase<S, D> | ||
where | ||
A: Clone + $trt<B, Output=A>, | ||
B: Clone, | ||
A: Clone + $trt<B, Output=B>, | ||
B: Copy, | ||
S: Data<Elem=A>, | ||
S2: Data<Elem=B>, | ||
D: Dimension + BroadcastShape<E>, | ||
E: Dimension, | ||
S2: DataOwned<Elem=B> + DataMut, | ||
D: Dimension, | ||
E: Dimension + BroadcastShape<D>, | ||
{ | ||
type Output = Array<A, <D as BroadcastShape<E>>::BroadcastOutput>; | ||
type Output = ArrayBase<S2, <E as BroadcastShape<D>>::BroadcastOutput>; | ||
fn $mth(self, rhs: ArrayBase<S2, E>) -> Self::Output | ||
bluss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
E864 | self.$mth(&rhs) | |
let shape = rhs.dim.broadcast_shape(&self.dim).unwrap(); | ||
if shape.slice() == rhs.dim.slice() { | ||
let mut out = rhs.into_dimensionality::<<E as BroadcastShape<D>>::BroadcastOutput>().unwrap(); | ||
out.zip_mut_with(self, |x, y| { | ||
*x = y.clone() $operator x.clone(); | ||
}); | ||
out | ||
} else { | ||
// SAFETY: Overwrite all the elements in the array after | ||
// it is created via `zip_mut_from_pair`. | ||
let mut out = unsafe { | ||
Self::Output::uninitialized(shape.clone().into_pattern()) | ||
}; | ||
let lhs = self.broadcast(shape.clone()).unwrap(); | ||
let rhs = rhs.broadcast(shape).unwrap(); | ||
out.zip_mut_from_pair(&lhs, &rhs, |x, y| { | ||
x.clone() $operator y.clone() | ||
}); | ||
out | ||
} | ||
} | ||
} | ||
|
||
|
@@ -106,32 +129,44 @@ where | |
/// between `self` and reference `rhs`, | ||
/// and return the result. | ||
/// | ||
/// `rhs` must be an `Array` or `ArcArray`. | ||
/// | ||
/// If their shapes disagree, `self` is broadcast to their broadcast shape, | ||
/// cloning the data if needed. | ||
/// | ||
/// **Panics** if broadcasting isn’t possible. | ||
impl<'a, A, B, S, S2, D, E> $trt<&'a ArrayBase<S2, E>> for ArrayBase<S, D> | ||
where | ||
A: Clone + $trt<B, Output=A>, | ||
A: Copy + $trt<B, Output=A>, | ||
B: Clone, | ||
S: Data<Elem=A>, | ||
S: DataOwned<Elem=A> + DataMut, | ||
S2: Data<Elem=B>, | ||
D: Dimension + BroadcastShape<E>, | ||
E: Dimension, | ||
{ | ||
type Output = Array<A, <D as BroadcastShape<E>>::BroadcastOutput>; | ||
type Output = ArrayBase<S, <D as BroadcastShape<E>>::BroadcastOutput>; | ||
fn $mth(self, rhs: &ArrayBase<S2, E>) -> Self::Output | ||
{ | ||
let shape = self.dim.broadcast_shape(&rhs.dim).unwrap(); | ||
let mut self_ = if shape.slice() == self.dim.slice() { | ||
self.into_owned().into_dimensionality::<<D as BroadcastShape<E>>::BroadcastOutput>().unwrap() | ||
if shape.slice() == self.dim.slice() { | ||
let mut out = self.into_dimensionality::<<D as BroadcastShape<E>>::BroadcastOutput>().unwrap(); | ||
out.zip_mut_with(rhs, |x, y| { | ||
*x = x.clone() $operator y.clone(); | ||
}); | ||
out | ||
} else { | ||
self.broadcast(shape).unwrap().to_owned() | ||
}; | ||
self_.zip_mut_with(rhs, |x, y| { | ||
*x = x.clone() $operator y.clone(); | ||
}); | ||
self_ | ||
// SAFETY: Overwrite all the elements in the array after | ||
// it is created via `zip_mut_from_pair`. | ||
let mut out = unsafe { | ||
Self::Output::uninitialized(shape.clone().into_pattern()) | ||
}; | ||
let lhs = self.broadcast(shape.clone()).unwrap(); | ||
let rhs = rhs.broadcast(shape).unwrap(); | ||
out.zip_mut_from_pair(&lhs, &rhs, |x, y| { | ||
x.clone() $operator y.clone() | ||
}); | ||
out | ||
} | ||
} | ||
} | ||
|
||
|
@@ -140,13 +175,13 @@ where | |
/// between references `self` and `rhs`, | ||
/// and return the result as a new `Array`. | ||
/// | ||
/// If their shapes disagree, `self` is broadcast to their broadcast shape, | ||
/// If their shapes disagree, `self` and `rhs` is broadcast to their broadcast shape, | ||
/// cloning the data if needed. | ||
/// | ||
/// **Panics** if broadcasting isn’t possible. | ||
impl<'a, A, B, S, S2, D, E> $trt<&'a ArrayBase<S2, E>> for &'a ArrayBase<S, D> | ||
where | ||
A: Clone + $trt<B, Output=A>, | ||
A: Copy + $trt<B, Output=A>, | ||
bluss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
B: Clone, | ||
S: Data<Elem=A>, | ||
S2: Data<Elem=B>, | ||
|
@@ -156,15 +191,17 @@ where | |
type Output = Array<A, <D as BroadcastShape<E>>::BroadcastOutput>; | ||
fn $mth(self, rhs: &'a ArrayBase<S2, E>) -> Self::Output { | ||
let shape = self.dim.broadcast_shape(&rhs.dim).unwrap(); | ||
let mut self_ = if shape.slice() == self.dim.slice() { | ||
self.to_owned().into_dimensionality::<<D as BroadcastShape<E>>::BroadcastOutput>().unwrap() | ||
} else { | ||
self.broadcast(shape).unwrap().to_owned() | ||
// SAFETY: Overwrite all the elements in the array after | ||
// it is created via `zip_mut_from_pair`. | ||
let mut out = unsafe { | ||
Self::Output::uninitialized(shape.clone().into_pattern()) | ||
}; | ||
bluss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self_.zip_mut_with(rhs, |x, y| { | ||
*x = x.clone() $operator y.clone(); | ||
let lhs = self.broadcast(shape.clone()).unwrap(); | ||
let rhs = rhs.broadcast(shape).unwrap(); | ||
out.zip_mut_from_pair(&lhs, &rhs, |x, y| { | ||
x.clone() $operator y.clone() | ||
}); | ||
self_ | ||
out | ||
} | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Why not just use
Zip
in this method (is it slower?)?Zip::map_assign_into
should do it (assign into self).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.
Right, we can use Zip. This function does not need to be added.