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

fix/ndarray: remove reversed axes check #1058

Merged
merged 4 commits into from
Dec 14, 2023
Merged
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
43 changes: 42 additions & 1 deletion burn-ndarray/src/ops/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ where
.unwrap()
.into_shared();

NdArrayTensor { array }
// Transform column-major layout into row-major (standard) layout. (fix #1053)
let array = NdArrayTensor { array };
Self::reshape(array.clone(), array.shape())
}

fn to_slice_args<const D1: usize, const D2: usize>(
Expand Down Expand Up @@ -491,3 +493,42 @@ fn arg<E: NdArrayElement, const D: usize>(
array: output.into_shared(),
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn should_generate_row_major_layout_for_cat() {
let expected_shape: &[usize] = &[4, 6, 2];
let expected_strides: &[isize] = &[12, 2, 1];
let expected_array = NdArrayTensor::from_data(Data::<i32, 3>::from([
[[1, 0], [2, 0], [3, 0], [4, 0], [5, 0], [6, 0]],
[[7, 0], [8, 0], [9, 0], [10, 0], [11, 0], [12, 0]],
[[13, 0], [14, 0], [15, 0], [16, 0], [17, 0], [18, 0]],
[[19, 0], [20, 0], [21, 0], [22, 0], [23, 0], [24, 0]],
]));

// unsqueeze dim on the outermost axis
let array = NdArrayOps::reshape(
NdArrayTensor::from_data(Data::<i32, 2>::from([
[1, 2, 3, 4, 5, 6],
[7, 8, 9, 10, 11, 12],
[13, 14, 15, 16, 17, 18],
[19, 20, 21, 22, 23, 24],
])),
Shape::from([4, 6, 1]),
);
let zeros = NdArrayTensor::<i32, 3>::from_data(Data::zeros([4, 6, 1]));
// make `ndarray` concatenates array on the outermost axis
let array = NdArrayOps::cat([array, zeros].to_vec(), 2);

assert!(array.array.is_standard_layout());
assert_eq!(array.array.shape(), expected_shape);
assert_eq!(array.array.strides(), expected_strides);
assert_eq!(
array.array.into_iter().collect::<Vec<_>>(),
expected_array.array.into_iter().collect::<Vec<_>>(),
);
}
}
9 changes: 1 addition & 8 deletions burn-ndarray/src/tensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,7 @@ macro_rules! reshape {
array $array:expr
) => {{
let dim = $crate::to_typed_dims!($n, $shape.dims, justdim);
let safe_into_shape =
$array.is_standard_layout() ||
(
$array.ndim() > 1 &&
$array.raw_view().reversed_axes().is_standard_layout()
);

let array: ndarray::ArcArray<$ty, Dim<[usize; $n]>> = match safe_into_shape {
let array: ndarray::ArcArray<$ty, Dim<[usize; $n]>> = match $array.is_standard_layout() {
true => $array
.into_shape(dim)
.expect("Safe to change shape without relayout")
Expand Down
17 changes: 17 additions & 0 deletions burn-tensor/src/tests/ops/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,21 @@ mod tests {

let output: Tensor<TestBackend, 4> = TestTensor::stack(vec![tensor_1, tensor_2], 3);
}

#[test]
fn should_generate_row_major_layout() {
let data_expected = Data::from([
[1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0],
[7, 0, 8, 0, 9, 0, 10, 0, 11, 0, 12, 0],
[13, 0, 14, 0, 15, 0, 16, 0, 17, 0, 18, 0],
[19, 0, 20, 0, 21, 0, 22, 0, 23, 0, 24, 0],
]);

let tensor = Tensor::<TestBackend, 1, Int>::arange(1..25).reshape([4, 6]);
let zeros: Tensor<TestBackend, 2, Int> = Tensor::zeros([4, 6]);
let intersperse =
Tensor::stack::<3>([tensor.clone(), zeros.clone()].to_vec(), 2).reshape([4, 12]);

assert_eq!(data_expected, intersperse.into_data());
}
}