Skip to content

Add left to right broadcast for Standard binary ops #362

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 8 commits into from
Jul 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 112 additions & 24 deletions rust/moose/src/standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,16 @@ where
{
type Output = StandardTensor<T>;
fn add(self, other: StandardTensor<T>) -> Self::Output {
StandardTensor::<T>(
self.0 + other.0,
HostPlacement {
owner: "TODO".into(),
let placement = HostPlacement {
owner: "TODO".into(),
}
.into();
match self.0.broadcast(other.0.dim()) {
Some(self_broadcasted) => {
StandardTensor::<T>(self_broadcasted.to_owned() + other.0, placement)
}
.into(),
)
None => StandardTensor::<T>(self.0 + other.0, placement),
}
}
}

Expand All @@ -261,13 +264,16 @@ where
{
type Output = StandardTensor<T>;
fn sub(self, other: StandardTensor<T>) -> Self::Output {
StandardTensor::<T>(
self.0 - other.0,
HostPlacement {
owner: "TODO".into(),
let placement = HostPlacement {
owner: "TODO".into(),
}
.into();
match self.0.broadcast(other.0.dim()) {
Some(self_broadcasted) => {
StandardTensor::<T>(self_broadcasted.to_owned() - other.0, placement)
}
.into(),
)
None => StandardTensor::<T>(self.0 - other.0, placement),
}
}
}

Expand All @@ -277,13 +283,16 @@ where
{
type Output = StandardTensor<T>;
fn mul(self, other: StandardTensor<T>) -> Self::Output {
StandardTensor::<T>(
self.0 * other.0,
HostPlacement {
owner: "TODO".into(),
let placement = HostPlacement {
owner: "TODO".into(),
}
.into();
match self.0.broadcast(other.0.dim()) {
Some(self_broadcasted) => {
StandardTensor::<T>(self_broadcasted.to_owned() * other.0, placement)
}
.into(),
)
None => StandardTensor::<T>(self.0 * other.0, placement),
}
}
}

Expand All @@ -293,13 +302,16 @@ where
{
type Output = StandardTensor<T>;
fn div(self, other: StandardTensor<T>) -> Self::Output {
StandardTensor::<T>(
self.0 / other.0,
HostPlacement {
owner: "TODO".into(),
let placement = HostPlacement {
owner: "TODO".into(),
}
.into();
match self.0.broadcast(other.0.dim()) {
Some(self_broadcasted) => {
StandardTensor::<T>(self_broadcasted.to_owned() / other.0, placement)
}
.into(),
)
None => StandardTensor::<T>(self.0 / other.0, placement),
}
}
}

Expand Down Expand Up @@ -489,4 +501,80 @@ mod tests {
assert_eq!(cx, c_exp);
assert_eq!(dx, d_exp);
}

#[test]
fn test_add_broadcasting() {
let x_1 = StandardTensor::<f32>::from(array![2.0].into_dimensionality::<IxDyn>().unwrap());
let y_1 =
StandardTensor::<f32>::from(array![1.0, 2.0].into_dimensionality::<IxDyn>().unwrap());
let z_1 = x_1.add(y_1);
let z_1_exp =
StandardTensor::<f32>::from(array![3.0, 4.0].into_dimensionality::<IxDyn>().unwrap());
let x_2 =
StandardTensor::<f32>::from(array![1.0, 2.0].into_dimensionality::<IxDyn>().unwrap());
let y_2 = StandardTensor::<f32>::from(array![2.0].into_dimensionality::<IxDyn>().unwrap());
let z_2 = x_2.add(y_2);
let z_2_exp =
StandardTensor::<f32>::from(array![3.0, 4.0].into_dimensionality::<IxDyn>().unwrap());

assert_eq!(z_1, z_1_exp);
assert_eq!(z_2, z_2_exp);
}

#[test]
fn test_sub_broadcasting() {
let x_1 = StandardTensor::<f32>::from(array![2.0].into_dimensionality::<IxDyn>().unwrap());
let y_1 =
StandardTensor::<f32>::from(array![1.0, 2.0].into_dimensionality::<IxDyn>().unwrap());
let z_1 = x_1.sub(y_1);
let z_1_exp =
StandardTensor::<f32>::from(array![1.0, 0.0].into_dimensionality::<IxDyn>().unwrap());
let x_2 =
StandardTensor::<f32>::from(array![1.0, 2.0].into_dimensionality::<IxDyn>().unwrap());
let y_2 = StandardTensor::<f32>::from(array![2.0].into_dimensionality::<IxDyn>().unwrap());
let z_2 = x_2.sub(y_2);
let z_2_exp =
StandardTensor::<f32>::from(array![-1.0, 0.0].into_dimensionality::<IxDyn>().unwrap());

assert_eq!(z_1, z_1_exp);
assert_eq!(z_2, z_2_exp);
}

#[test]
fn test_mul_broadcasting() {
let x_1 = StandardTensor::<f32>::from(array![2.0].into_dimensionality::<IxDyn>().unwrap());
let y_1 =
StandardTensor::<f32>::from(array![1.0, 2.0].into_dimensionality::<IxDyn>().unwrap());
let z_1 = x_1.mul(y_1);
let z_1_exp =
StandardTensor::<f32>::from(array![2.0, 4.0].into_dimensionality::<IxDyn>().unwrap());
let x_2 =
StandardTensor::<f32>::from(array![1.0, 2.0].into_dimensionality::<IxDyn>().unwrap());
let y_2 = StandardTensor::<f32>::from(array![2.0].into_dimensionality::<IxDyn>().unwrap());
let z_2 = x_2.mul(y_2);
let z_2_exp =
StandardTensor::<f32>::from(array![2.0, 4.0].into_dimensionality::<IxDyn>().unwrap());

assert_eq!(z_1, z_1_exp);
assert_eq!(z_2, z_2_exp);
}

#[test]
fn test_div_broadcasting() {
let x_1 = StandardTensor::<f32>::from(array![1.0].into_dimensionality::<IxDyn>().unwrap());
let y_1 =
StandardTensor::<f32>::from(array![2.0, 4.0].into_dimensionality::<IxDyn>().unwrap());
let z_1 = x_1.div(y_1);
let z_1_exp =
StandardTensor::<f32>::from(array![0.5, 0.25].into_dimensionality::<IxDyn>().unwrap());
let x_2 =
StandardTensor::<f32>::from(array![2.0, 4.0].into_dimensionality::<IxDyn>().unwrap());
let y_2 = StandardTensor::<f32>::from(array![2.0].into_dimensionality::<IxDyn>().unwrap());
let z_2 = x_2.div(y_2);
let z_2_exp =
StandardTensor::<f32>::from(array![1.0, 2.0].into_dimensionality::<IxDyn>().unwrap());

assert_eq!(z_1, z_1_exp);
assert_eq!(z_2, z_2_exp);
}
}