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.
- Loading branch information
1 parent
0aa486f
commit d62f2b0
Showing
21 changed files
with
283 additions
and
48 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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use crate::backend::backend::Backend; | ||
use crate::tensor::ops::*; | ||
|
||
macro_rules! define_impl { | ||
( | ||
$backend:ty, | ||
$backend_inner:ty, | ||
$element:ident | ||
) => { | ||
impl<E: $element, const D: usize> TensorOpsArg<$backend, D> | ||
for <$backend as Backend>::TensorPrimitive<D> | ||
{ | ||
fn argmax(&self, dim: usize) -> <$backend as Backend>::IndexTensorPrimitive<D> { | ||
TensorOpsArg::argmax(&self.tensor(), dim) | ||
} | ||
|
||
fn argmin(&self, dim: usize) -> <$backend as Backend>::IndexTensorPrimitive<D> { | ||
TensorOpsArg::argmin(&self.tensor(), dim) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
crate::register_tch!(); | ||
crate::register_ndarray!(); |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
mod add; | ||
mod aggregation; | ||
mod arg; | ||
mod cat; | ||
mod creation; | ||
mod device; | ||
|
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,73 @@ | ||
use crate::backend::ndarray::NdArrayBackend; | ||
use crate::tensor::{backend::ndarray::NdArrayTensor, ops::*}; | ||
use crate::ElementValue; | ||
use crate::NdArrayElement; | ||
use std::cmp::Ordering; | ||
|
||
impl<E, const D: usize> TensorOpsArg<NdArrayBackend<E>, D> for NdArrayTensor<E, D> | ||
where | ||
E: NdArrayElement, | ||
{ | ||
fn argmax(&self, dim: usize) -> NdArrayTensor<i64, D> { | ||
arg(self, dim, cmp_max) | ||
} | ||
|
||
fn argmin(&self, dim: usize) -> NdArrayTensor<i64, D> { | ||
arg(self, dim, cmp_min) | ||
} | ||
} | ||
|
||
fn arg<E: NdArrayElement, F, const D: usize>( | ||
tensor: &NdArrayTensor<E, D>, | ||
dim: usize, | ||
cmp: F, | ||
) -> NdArrayTensor<i64, D> | ||
where | ||
F: Fn(&f64, &f64) -> Ordering, | ||
{ | ||
let mut data = tensor.to_data(); | ||
let mut start = 1; | ||
|
||
for i in 0..dim { | ||
start = start * tensor.shape.dims[i]; | ||
} | ||
let end = start + tensor.shape.dims[dim]; | ||
|
||
let data_dim = &mut data.value[start..end]; | ||
let mut sorted: Vec<f64> = data_dim.iter().map(|a| a.to_elem()).collect(); | ||
sorted.sort_by(cmp); | ||
|
||
let max = sorted[0]; | ||
for elem in data_dim { | ||
*elem = <E as ElementValue>::zero(); | ||
} | ||
|
||
let data_dim = &mut data.value[start..end]; | ||
for elem in data_dim { | ||
let as_float: f64 = elem.to_elem(); | ||
if as_float == max { | ||
*elem = <E as ElementValue>::one(); | ||
break; | ||
} | ||
} | ||
|
||
NdArrayTensor::from_data(data.convert()) | ||
} | ||
|
||
fn cmp_max(a: &f64, b: &f64) -> Ordering { | ||
if a < b { | ||
return Ordering::Less; | ||
} else if a > b { | ||
return Ordering::Greater; | ||
} | ||
return Ordering::Equal; | ||
} | ||
|
||
fn cmp_min(a: &f64, b: &f64) -> Ordering { | ||
if a > b { | ||
return Ordering::Less; | ||
} else if a < b { | ||
return Ordering::Greater; | ||
} | ||
return Ordering::Equal; | ||
} |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
mod add; | ||
mod aggregation; | ||
mod arg; | ||
mod cat; | ||
mod creation; | ||
mod device; | ||
|
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,33 @@ | ||
use crate::backend::tch::TchBackend; | ||
use crate::tensor::TchElement; | ||
use crate::tensor::{ | ||
backend::tch::{TchKind, TchTensor}, | ||
ops::*, | ||
}; | ||
|
||
impl<E, const D: usize> TensorOpsArg<TchBackend<E>, D> for TchTensor<E, D> | ||
where | ||
E: TchElement, | ||
{ | ||
fn argmax(&self, dim: usize) -> TchTensor<i64, D> { | ||
let tensor = self.tensor.argmax(dim as i64, true); | ||
let shape = self.shape.clone(); | ||
|
||
TchTensor { | ||
tensor, | ||
kind: TchKind::<i64>::new(), | ||
shape, | ||
} | ||
} | ||
|
||
fn argmin(&self, dim: usize) -> TchTensor<i64, D> { | ||
let tensor = self.tensor.argmin(dim as i64, true); | ||
let shape = self.shape.clone(); | ||
|
||
TchTensor { | ||
tensor, | ||
kind: TchKind::<i64>::new(), | ||
shape, | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
mod add; | ||
mod aggregation; | ||
mod arg; | ||
mod cat; | ||
mod creation; | ||
mod device; | ||
|
Oops, something went wrong.