Skip to content
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

allow noop tensor expand #67

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
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
19 changes: 18 additions & 1 deletion src/hl_ops/movement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ impl<S: Shape> GraphTensor<S> {
S: BroadcastShapeTo<Dst, Ax>,
{
let new_dims = Dst::realized_shape();
if !new_dims.is_empty() {
let src_dims = S::realized_shape();
let is_noop = src_dims.len() == new_dims.len()
&& src_dims
.iter()
.zip(new_dims.iter())
.all(|(src_dim, new_dim)| src_dim.as_num() == new_dim.as_num());

if !new_dims.is_empty() && !is_noop {
for (i, dim) in Ax::as_array().into_iter().map(|i| (i, new_dims[i])) {
self.shape.expand(i, dim);
}
Expand Down Expand Up @@ -514,4 +521,14 @@ mod tests {

assert_close(&c.data(), &d_c.as_vec());
}

#[test]
fn test_noop_expand() {
type S = R1<1>;
type Tensor = GraphTensor<S>;
let mut cx = Graph::new();
let a: Tensor = cx.tensor();
let noop_expanded: Tensor = a.expand::<S, LAxis<0>>();
assert_eq!(a.shape, noop_expanded.shape);
}
}
6 changes: 3 additions & 3 deletions src/hl_ops/reduction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ mod tests {
let a_data = random_vec(6);
let a = cx.tensor::<R2<2, 3>>();
a.set(a_data.clone());
let b = a.sum_reduce::<_, LAxis<1>>();
let b = a.sum_reduce::<R1<2>, LAxis<1>>();
b.retrieve();

cx.execute();
Expand All @@ -114,7 +114,7 @@ mod tests {
let a_data = random_vec(6);
let a = cx.tensor::<R2<2, 3>>();
a.set(a_data.clone());
let b = a.max_reduce::<_, LAxis<1>>();
let b = a.max_reduce::<R1<2>, LAxis<1>>();
b.retrieve();

cx.execute();
Expand All @@ -132,7 +132,7 @@ mod tests {
let a_data = random_vec(6);
let a = cx.tensor::<R2<2, 3>>();
a.set(a_data.clone());
let b = a.mean_reduce::<_, LAxis<1>>();
let b = a.mean_reduce::<R1<2>, LAxis<1>>();
b.retrieve();

cx.execute();
Expand Down
2 changes: 1 addition & 1 deletion src/shape/broadcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub trait ReduceShape<Ax>: Sized + HasAxes<Ax> + ReduceShapeTo<Self::Reduced, Ax
type Reduced: Shape + BroadcastShapeTo<Self, Ax>;
}

impl ReduceShapeTo<(), Axis<0>> for () {}
impl<S: Shape + HasAxes<AnyAxes>, AnyAxes> ReduceShapeTo<S, AnyAxes> for S {}
impl ReduceShape<Axis<0>> for () {
type Reduced = ();
}
Expand Down
28 changes: 21 additions & 7 deletions src/tests/test_prim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,15 @@ fn test_sum_reduce() {
let a = cx
.tensor::<R3<2, 2, 3>>()
.set([[[1., 2., 3.], [1., 2., 3.]], [[1., 2., 3.], [1., 2., 3.]]]);
let b = a.sum_reduce::<_, crate::prelude::Axis<1>>().retrieve();
let c = a.sum_reduce::<_, crate::prelude::Axis<0>>().retrieve();
let d = a.sum_reduce::<_, crate::prelude::Axis<2>>().retrieve();
let b = a
.sum_reduce::<R2<2, 3>, crate::prelude::Axis<1>>()
.retrieve();
let c = a
.sum_reduce::<R2<2, 3>, crate::prelude::Axis<0>>()
.retrieve();
let d = a
.sum_reduce::<R2<2, 2>, crate::prelude::Axis<2>>()
.retrieve();
cx.execute();

let d_dev = Cpu::default();
Expand All @@ -290,7 +296,9 @@ fn test_sum_reduce2() {
[[34.4, -96.0, 144.0], [43.0, 560.0, 180.0]],
[[39.6, -120.0, 180.0], [49.5, 700.0, 225.0]],
]]);
let b = a.sum_reduce::<_, crate::prelude::Axis<3>>().retrieve();
let b = a
.sum_reduce::<R3<1, 2, 2>, crate::prelude::Axis<3>>()
.retrieve();
cx.execute();

let d_dev = Cpu::default();
Expand All @@ -309,9 +317,15 @@ fn test_max_reduce() {
let a = cx
.tensor::<R3<2, 2, 3>>()
.set([[[1., 2., 3.], [1., 2., 3.]], [[1., 2., 3.], [1., 2., 3.]]]);
let b = a.max_reduce::<_, crate::prelude::Axis<1>>().retrieve();
let c = a.max_reduce::<_, crate::prelude::Axis<0>>().retrieve();
let d = a.max_reduce::<_, crate::prelude::Axis<2>>().retrieve();
let b = a
.max_reduce::<R2<2, 3>, crate::prelude::Axis<1>>()
.retrieve();
let c = a
.max_reduce::<R2<2, 3>, crate::prelude::Axis<0>>()
.retrieve();
let d = a
.max_reduce::<R2<2, 2>, crate::prelude::Axis<2>>()
.retrieve();
cx.execute();

let d_dev = Cpu::default();
Expand Down