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

Allocate output from pool in Gather, NonZero ops #168

Merged
merged 1 commit into from
May 12, 2024
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
2 changes: 1 addition & 1 deletion src/ops/gather.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn gather<T: Copy + Default>(
if let (0, Some(index)) = (indices.ndim(), indices.item()) {
let mut slice_range = full_range(input.ndim());
slice_range[axis] = SliceItem::Index(*index as isize);
let output = input.slice_dyn(slice_range.as_slice()).to_tensor();
let output = input.slice_dyn(slice_range.as_slice()).to_tensor_in(pool);
return Ok(output);
}

Expand Down
18 changes: 10 additions & 8 deletions src/ops/reduce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ impl Operator for CumSum {
}

/// Return the indices of nonzero elements in `input` as a `(dim, index)` tensor.
pub fn nonzero<T: Default + PartialEq>(input: TensorView<T>) -> Tensor<i32> {
pub fn nonzero<T: Default + PartialEq>(pool: &TensorPool, input: TensorView<T>) -> Tensor<i32> {
// Special case for scalar inputs.
if let (Some(item), 0) = (input.item(), input.ndim()) {
return Tensor::zeros(&[0, if *item != T::default() { 1 } else { 0 }]);
Expand All @@ -186,7 +186,7 @@ pub fn nonzero<T: Default + PartialEq>(input: TensorView<T>) -> Tensor<i32> {
// Transpose from `(index, dim)` to `(dim, index)`.
Tensor::from_data(&[nonzeros.len() / input.ndim(), input.ndim()], nonzeros)
.transposed()
.to_tensor()
.to_tensor_in(pool)
}

#[derive(Debug)]
Expand All @@ -197,11 +197,11 @@ impl Operator for NonZero {
"NonZero"
}

fn run(&self, _pool: &TensorPool, inputs: InputList) -> Result<Vec<Output>, OpError> {
fn run(&self, pool: &TensorPool, inputs: InputList) -> Result<Vec<Output>, OpError> {
let input = inputs.require(0)?;
match input {
Input::IntTensor(input) => nonzero(input).into_op_result(),
Input::FloatTensor(input) => nonzero(input).into_op_result(),
Input::IntTensor(input) => nonzero(pool, input).into_op_result(),
Input::FloatTensor(input) => nonzero(pool, input).into_op_result(),
}
}
}
Expand Down Expand Up @@ -889,8 +889,9 @@ mod tests {

#[test]
fn test_nonzero() {
let pool = new_pool();
let input = tensor!((2, 2); [0., 1., 1., 1.]);
let result = nonzero(input.view());
let result = nonzero(&pool, input.view());
assert_eq!(result.shape(), &[2, 3]);

// (dim, index) => (index, dim)
Expand All @@ -909,12 +910,13 @@ mod tests {

#[test]
fn test_nonzero_scalar() {
let pool = new_pool();
let input = tensor!(3.);
let result = nonzero(input.view());
let result = nonzero(&pool, input.view());
assert_eq!(result.shape(), &[0, 1]);

let input = tensor!(0.);
let result = nonzero(input.view());
let result = nonzero(&pool, input.view());
assert_eq!(result.shape(), &[0, 0]);
}

Expand Down
Loading