forked from tracel-ai/burn
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(tensor): Add chunk op (tracel-ai#998)
- Loading branch information
Showing
5 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#[burn_tensor_testgen::testgen(chunk)] | ||
mod tests { | ||
use super::*; | ||
use alloc::vec::Vec; | ||
use burn_tensor::{Data, Int, Shape, Tensor}; | ||
|
||
fn test_chunk_evenly_divisible() { | ||
let tensors: Vec<Tensor<TestBackend, 1, Int>> = Tensor::arange(0..12).chunk(6, 0); | ||
assert_eq!(tensors.len(), 6); | ||
|
||
let expected = vec![ | ||
Data::from([0, 1]), | ||
Data::from([2, 3]), | ||
Data::from([4, 5]), | ||
Data::from([6, 7]), | ||
Data::from([8, 9]), | ||
Data::from([10, 11]), | ||
]; | ||
|
||
for (index, tensor) in tensors.iter().enumerate() { | ||
assert_eq!(tensor.to_data(), expected[index]); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_chunk_not_evenly_divisible() { | ||
let tensors: Vec<Tensor<TestBackend, 1, Int>> = Tensor::arange(0..11).chunk(6, 0); | ||
assert_eq!(tensors.len(), 6); | ||
|
||
let expected = vec![ | ||
Data::from([0, 1]), | ||
Data::from([2, 3]), | ||
Data::from([4, 5]), | ||
Data::from([6, 7]), | ||
Data::from([8, 9]), | ||
Data::from([10]), | ||
]; | ||
|
||
for (index, tensor) in tensors.iter().enumerate() { | ||
assert_eq!(tensor.to_data(), expected[index]); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_chunk_not_divisible() { | ||
let tensors: Vec<Tensor<TestBackend, 1, Int>> = Tensor::arange(0..6).chunk(7, 0); | ||
assert_eq!(tensors.len(), 6); | ||
|
||
let expected = vec![ | ||
Data::from([0]), | ||
Data::from([1]), | ||
Data::from([2]), | ||
Data::from([3]), | ||
Data::from([4]), | ||
Data::from([5]), | ||
]; | ||
|
||
for (index, tensor) in tensors.iter().enumerate() { | ||
assert_eq!(tensor.to_data(), expected[index]); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_chunk_multi_dimension() { | ||
let tensors: Vec<Tensor<TestBackend, 2, Int>> = | ||
Tensor::from_data(Data::from([[0, 1, 2, 3]])).chunk(2, 1); | ||
assert_eq!(tensors.len(), 2); | ||
|
||
let expected = vec![Data::from([[0, 1]]), Data::from([[2, 3]])]; | ||
|
||
for (index, tensor) in tensors.iter().enumerate() { | ||
assert_eq!(tensor.to_data(), expected[index]); | ||
} | ||
} | ||
|
||
#[test] | ||
#[should_panic] | ||
fn test_invalid_dim() { | ||
let tensors: Vec<Tensor<TestBackend, 1, Int>> = Tensor::arange(0..12).chunk(6, 1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ mod arange_step; | |
mod arg; | ||
mod cast; | ||
mod cat; | ||
mod chunk; | ||
mod clamp; | ||
mod cos; | ||
mod create_like; | ||
|