-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tensor sorting operations (#1488)
* Add sort, sort_with_indices and argsort * Fix wasm build * Add sort ops autodiff * Fix TODO parallel comment * Fix sort_with_indices 1d and add descending options * Fix clippy * Fix ascending comment (configurable)
- Loading branch information
Showing
23 changed files
with
1,525 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ mod module; | |
mod tensor; | ||
|
||
pub(crate) mod maxmin; | ||
pub(crate) mod sort; | ||
|
||
pub use backward::*; | ||
pub use base::*; |
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,25 @@ | ||
use super::{unary, Backward, Ops}; | ||
use crate::{checkpoint::base::Checkpointer, grads::Gradients}; | ||
use burn_tensor::{backend::Backend, Shape}; | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct SortDim; | ||
|
||
impl<B: Backend, const D: usize> Backward<B, D, 1> for SortDim { | ||
type State = (B::IntTensorPrimitive<D>, Shape<D>); | ||
|
||
fn backward( | ||
self, | ||
ops: Ops<Self::State, 1>, | ||
grads: &mut Gradients, | ||
_checkpointer: &mut Checkpointer, | ||
) { | ||
unary::<B, D, D, _>(ops.parents, ops.node, grads, |grad| { | ||
let (indices, shape) = ops.state; | ||
let device = B::float_device(&grad); | ||
let zeros = B::float_zeros(shape, &device); | ||
|
||
B::float_scatter(D - 1, zeros, indices, grad) | ||
}); | ||
} | ||
} |
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,51 @@ | ||
#[burn_tensor_testgen::testgen(ad_sort)] | ||
mod tests { | ||
use super::*; | ||
use burn_tensor::Data; | ||
|
||
#[test] | ||
fn should_diff_sort() { | ||
let device = Default::default(); | ||
let tensor_1 = | ||
TestAutodiffTensor::from_floats([[1.0, 7.0], [-2.0, -3.0]], &device).require_grad(); | ||
let tensor_2 = | ||
TestAutodiffTensor::from_floats([[4.0, -7.0], [2.0, 3.0]], &device).require_grad(); | ||
|
||
let tensor_3 = tensor_1.clone().matmul(tensor_2.clone()); | ||
let tensor_4 = tensor_1.clone().mul(tensor_3.sort(1)); | ||
let grads = tensor_4.backward(); | ||
|
||
let grad_1 = tensor_1.grad(&grads).unwrap(); | ||
let grad_2 = tensor_2.grad(&grads).unwrap(); | ||
|
||
grad_1 | ||
.to_data() | ||
.assert_approx_eq(&Data::from([[35.0, 35.0], [-1.0, -8.0]]), 5); | ||
grad_2 | ||
.to_data() | ||
.assert_approx_eq(&Data::from([[11.0, 7.0], [55.0, 16.0]]), 5); | ||
} | ||
|
||
#[test] | ||
fn should_diff_sort_with_indices() { | ||
let device = Default::default(); | ||
let tensor_1 = | ||
TestAutodiffTensor::from_floats([[1.0, 7.0], [-2.0, -3.0]], &device).require_grad(); | ||
let tensor_2 = | ||
TestAutodiffTensor::from_floats([[4.0, -7.0], [2.0, 3.0]], &device).require_grad(); | ||
|
||
let tensor_3 = tensor_1.clone().matmul(tensor_2.clone()); | ||
let tensor_4 = tensor_1.clone().mul(tensor_3.sort_with_indices(1).0); | ||
let grads = tensor_4.backward(); | ||
|
||
let grad_1 = tensor_1.grad(&grads).unwrap(); | ||
let grad_2 = tensor_2.grad(&grads).unwrap(); | ||
|
||
grad_1 | ||
.to_data() | ||
.assert_approx_eq(&Data::from([[35.0, 35.0], [-1.0, -8.0]]), 5); | ||
grad_2 | ||
.to_data() | ||
.assert_approx_eq(&Data::from([[11.0, 7.0], [55.0, 16.0]]), 5); | ||
} | ||
} |
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
Oops, something went wrong.