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

Add support for negative range indexing on tensors #783

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
59 changes: 51 additions & 8 deletions src/tensor/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,22 +304,65 @@ impl Tensor {
curr_idx, // not advanced because select() squeezes dimension
),
Narrow(start, end) => {

let positive_index = |i: &i64, dim_len| {
if *i < 0 { dim_len + *i } else { *i }
};

if let Some((start, length)) = match (start, end) {
(Unbounded, Unbounded) => None,
(Included(start), Unbounded) => {
let dim_len = curr_tensor.size()[curr_idx as usize];
Some((*start, dim_len - *start))
let start = positive_index(start, dim_len);

Some((start, dim_len - start))
}
(Excluded(start), Unbounded) => {
let dim_len = curr_tensor.size()[curr_idx as usize];
Some((*start + 1, dim_len - *start - 1))
let start = positive_index(start, dim_len);

Some((start + 1, dim_len - start - 1))
}
(Unbounded, Included(end)) => Some((0, *end + 1)),
(Unbounded, Excluded(end)) => Some((0, *end)),
(Included(start), Included(end)) => Some((*start, *end - *start + 1)),
(Included(start), Excluded(end)) => Some((*start, *end - *start)),
(Excluded(start), Included(end)) => Some((*start + 1, *end - *start)),
(Excluded(start), Excluded(end)) => Some((*start + 1, *end - *start - 1)),
(Unbounded, Included(end)) => {
let dim_len = curr_tensor.size()[curr_idx as usize];
let end = positive_index(end, dim_len);

Some((0, end + 1))
},
(Unbounded, Excluded(end)) => {
let dim_len = curr_tensor.size()[curr_idx as usize];
let end = positive_index(end, dim_len);

Some((0, end))
},
(Included(start), Included(end)) => {
let dim_len = curr_tensor.size()[curr_idx as usize];
let start = positive_index(start, dim_len);
let end = positive_index(end, dim_len);

Some((start, end - start + 1))
},
(Included(start), Excluded(end)) => {
let dim_len = curr_tensor.size()[curr_idx as usize];
let start = positive_index(start, dim_len);
let end = positive_index(end, dim_len);

Some((start, end - start))
},
(Excluded(start), Included(end)) => {
let dim_len = curr_tensor.size()[curr_idx as usize];
let start = positive_index(start, dim_len);
let end = positive_index(end, dim_len);

Some((start + 1, end - start))
},
(Excluded(start), Excluded(end)) => {
let dim_len = curr_tensor.size()[curr_idx as usize];
let start = positive_index(start, dim_len);
let end = positive_index(end, dim_len);

Some((start + 1, end - start - 1))
},
} {
(curr_tensor.narrow(curr_idx, start, length.max(0)), curr_idx + 1)
} else {
Expand Down
21 changes: 21 additions & 0 deletions tests/tensor_indexing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ fn range_index() {
assert_eq!(result.size(), &[2, 3]);
assert_eq!(vec_i64_from(&result), &[3, 4, 5, 6, 7, 8]);

let result = tensor.i(-4..-2);
assert_eq!(result.size(), &[2, 3]);
assert_eq!(vec_i64_from(&result), &[0, 1, 2, 3, 4, 5]);

// RangeFull
let tensor = Tensor::arange_start(0, 2 * 3, opt).view([2, 3]);
let result = tensor.i(..);
Expand All @@ -45,18 +49,31 @@ fn range_index() {
assert_eq!(result.size(), &[2, 3]);
assert_eq!(vec_i64_from(&result), &[6, 7, 8, 9, 10, 11]);

let result = tensor.i(-1..);
assert_eq!(result.size(), &[1, 3]);
assert_eq!(vec_i64_from(&result), &[9, 10, 11]);

// RangeTo
let tensor = Tensor::arange_start(0, 4 * 3, opt).view([4, 3]);
let result = tensor.i(..2);
assert_eq!(result.size(), &[2, 3]);
assert_eq!(vec_i64_from(&result), &[0, 1, 2, 3, 4, 5]);

let result = tensor.i(..-3);
assert_eq!(result.size(), &[1, 3]);
assert_eq!(vec_i64_from(&result), &[0, 1, 2]);

// RangeInclusive
let tensor = Tensor::arange_start(0, 4 * 3, opt).view([4, 3]);
let result = tensor.i(1..=2);
assert_eq!(result.size(), &[2, 3]);
assert_eq!(vec_i64_from(&result), &[3, 4, 5, 6, 7, 8]);

let (start, end) = (1, -2);
let result = tensor.i(start..=end);
assert_eq!(result.size(), &[2, 3]);
assert_eq!(vec_i64_from(&result), &[3, 4, 5, 6, 7, 8]);

// RangeTo
let tensor = Tensor::arange_start(0, 4 * 3, opt).view([4, 3]);
let result = tensor.i(..1);
Expand All @@ -69,6 +86,10 @@ fn range_index() {
assert_eq!(result.size(), &[2, 3]);
assert_eq!(vec_i64_from(&result), &[0, 1, 2, 3, 4, 5]);

let result = tensor.i(..=-3);
assert_eq!(result.size(), &[2, 3]);
assert_eq!(vec_i64_from(&result), &[0, 1, 2, 3, 4, 5]);

// Negative Cases
//
// Rust considers both `0..0` and `1..0` to be valid, empty ranges.
Expand Down