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.
Add
topk
tensor operation (tracel-ai#1497)
* Add topk and topk_with_indices * Change topk_with_indices test to guarantee order (previously equal elements)
- Loading branch information
Showing
7 changed files
with
215 additions
and
10 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
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 |
---|---|---|
|
@@ -51,6 +51,7 @@ mod squeeze; | |
mod stack; | ||
mod sub; | ||
mod tanh; | ||
mod topk; | ||
mod transpose; | ||
mod tri; | ||
mod tri_mask; |
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,76 @@ | ||
#[burn_tensor_testgen::testgen(topk)] | ||
mod tests { | ||
use super::*; | ||
use burn_tensor::{Data, Shape, Tensor}; | ||
|
||
#[test] | ||
fn test_topk_1d() { | ||
// Int | ||
let tensor = TestTensorInt::from([1, 2, 3, 4, 5]); | ||
|
||
let values = tensor.topk(3, /*dim*/ 0); | ||
let values_actual = values.into_data(); | ||
|
||
let values_expected = Data::from([5, 4, 3]); | ||
assert_eq!(values_expected, values_actual); | ||
|
||
// Float | ||
let tensor = TestTensor::from([1., 2., 3., 4., 5.]); | ||
|
||
let values = tensor.topk(3, /*dim*/ 0); | ||
let values_actual = values.into_data(); | ||
|
||
let values_expected = Data::from([5., 4., 3.]); | ||
values_expected.assert_approx_eq(&values_actual, 5); | ||
} | ||
|
||
#[test] | ||
fn test_topk() { | ||
// 2D Int | ||
let tensor = TestTensorInt::from([[[1, 4, 7], [2, 5, 6]], [[3, 0, 9], [8, 2, 8]]]); | ||
|
||
let values = tensor.topk(2, /*dim*/ 2); | ||
let values_actual = values.into_data(); | ||
|
||
let values_expected = Data::from([[[7, 4], [6, 5]], [[9, 3], [8, 8]]]); | ||
assert_eq!(values_expected, values_actual); | ||
|
||
// 2D Float | ||
let tensor = TestTensor::from([[[1., 4., 7.], [2., 5., 6.]], [[3., 0., 9.], [8., 2., 8.]]]); | ||
|
||
let values = tensor.topk(2, /*dim*/ 2); | ||
let values_actual = values.into_data(); | ||
|
||
let values_expected = Data::from([[[7., 4.], [6., 5.]], [[9., 3.], [8., 8.]]]); | ||
values_expected.assert_approx_eq(&values_actual, 5); | ||
} | ||
|
||
#[test] | ||
fn test_topk_with_indices() { | ||
// 1D | ||
let tensor = TestTensorInt::from([1, 2, 3, 4, 5]); | ||
|
||
let (values, indices) = tensor.topk_with_indices(3, /*dim*/ 0); | ||
let values_actual = values.into_data(); | ||
let indices_actual = indices.into_data(); | ||
|
||
let values_expected = Data::from([5, 4, 3]); | ||
assert_eq!(values_expected, values_actual); | ||
|
||
let indices_expected = Data::from([4, 3, 2]); | ||
assert_eq!(indices_expected, indices_actual); | ||
|
||
// 2D | ||
let tensor = TestTensor::from([[[1., 4., 7.], [2., 5., 6.]], [[3., 0., 9.], [8., 2., 7.]]]); | ||
|
||
let (values, indices) = tensor.topk_with_indices(2, /*dim*/ 2); | ||
let values_actual = values.into_data(); | ||
let indices_actual = indices.into_data(); | ||
|
||
let values_expected = Data::from([[[7., 4.], [6., 5.]], [[9., 3.], [8., 7.]]]); | ||
values_expected.assert_approx_eq(&values_actual, 5); | ||
|
||
let indices_expected = Data::from([[[2, 1], [2, 1]], [[2, 0], [0, 2]]]); | ||
assert_eq!(indices_expected, indices_actual); | ||
} | ||
} |