use std::ops::Range;
use burn::{
Tensor,
prelude::{
Backend,
ElementConversion,
},
tensor::{
AsIndex,
BasicOps,
Bool,
Int,
},
};
pub trait TensorOpExt<B, const D: usize, K>
where
B: Backend,
K: BasicOps<B>,
{
fn swap(
&mut self,
other: &mut Self,
);
fn release(&mut self) -> Self;
fn select_dim<const D2: usize, I: AsIndex>(
self,
dim: usize,
index: I,
) -> Tensor<B, D2, K>;
}
impl<B, const D: usize, K> TensorOpExt<B, D, K> for Tensor<B, D, K>
where
B: Backend,
K: BasicOps<B>,
{
fn swap(
&mut self,
other: &mut Self,
) {
core::mem::swap(self, other);
}
fn release(&mut self) -> Self {
let mut z = Tensor::empty([0; D], &self.device());
self.swap(&mut z);
z
}
fn select_dim<const D2: usize, I: AsIndex>(
self,
dim: usize,
index: I,
) -> Tensor<B, D2, K> {
let index = index.as_index();
self.slice_dim(dim, index..index + 1).squeeze_dim::<D2>(dim)
}
}
pub trait TensorIntOpExt<B, const D: usize>
where
B: Backend,
{
fn square(self) -> Self;
fn bounded_elem<E: ElementConversion>(
self,
range: Range<E>,
) -> Tensor<B, D, Bool>;
}
impl<B, const D: usize> TensorIntOpExt<B, D> for Tensor<B, D, Int>
where
B: Backend,
{
fn square(self) -> Self {
self.powi_scalar(2)
}
fn bounded_elem<E: ElementConversion>(
self,
range: Range<E>,
) -> Tensor<B, D, Bool> {
self.clone()
.greater_equal_elem(range.start)
.bool_or(self.clone().lower_elem(range.end))
}
}
pub trait TensorBoolOpExt<B, const D: usize>
where
B: Backend,
{
fn count_dim<I: AsIndex>(
self,
dim: I,
) -> Tensor<B, D, Int>;
fn count_dims<I: AsIndex>(
self,
dims: &[I],
) -> Tensor<B, D, Int>;
}
impl<B, const D: usize> TensorBoolOpExt<B, D> for Tensor<B, D, Bool>
where
B: Backend,
{
fn count_dim<I: AsIndex>(
self,
dim: I,
) -> Tensor<B, D, Int> {
self.int().sum_dim(dim)
}
fn count_dims<I: AsIndex>(
self,
dims: &[I],
) -> Tensor<B, D, Int> {
self.int().sum_dims(dims)
}
}
#[cfg(test)]
mod tests {
use burn::tensor::{
Tensor,
TensorData,
};
use super::*;
use crate::support::testing::CpuBackend;
type B = CpuBackend;
#[test]
fn test_release_swap() {
let device = Default::default();
let mut tensor: Tensor<B, 1> =
Tensor::<B, 1>::from_data(TensorData::from([0.0, 1.0, 2.0, 3.0]), &device);
assert_eq!(tensor.dims(), [4]);
let mut old: Tensor<B, 1> = tensor.release();
assert_eq!(tensor.dims(), [0]);
assert_eq!(old.dims(), [4]);
tensor.swap(&mut old);
assert_eq!(tensor.dims(), [4]);
assert_eq!(old.dims(), [0]);
}
#[test]
fn test_select_dim() {
let device = Default::default();
let tensor: Tensor<B, 2> =
Tensor::from_data(TensorData::from([[0.0, 1.0], [2.0, 3.0]]), &device);
let r1: Tensor<B, 1> = tensor.clone().select_dim(0, 1);
r1.to_data().assert_eq(&TensorData::from([2.0, 3.0]), false);
let c1: Tensor<B, 1> = tensor.clone().select_dim(1, 1);
c1.to_data().assert_eq(&TensorData::from([1.0, 3.0]), false);
}
#[test]
fn test_int_square() {
let device = Default::default();
let x: Tensor<B, 1, Int> = Tensor::from_data([0, 1, 2, 3], &device);
x.square()
.to_data()
.assert_eq(&TensorData::from([0, 1, 4, 9]), false);
}
#[test]
fn test_bool_count_dim() {
let device = Default::default();
let x: Tensor<B, 2, Bool> =
Tensor::from_data([[true, true, false], [true, false, false]], &device);
x.clone()
.count_dim(0)
.squeeze_dim::<1>(0)
.to_data()
.assert_eq(&TensorData::from([2, 1, 0]), false);
x.clone()
.count_dim(1)
.squeeze_dim::<1>(1)
.to_data()
.assert_eq(&TensorData::from([2, 1]), false);
x.clone()
.count_dims(&[0])
.squeeze_dim::<1>(0)
.to_data()
.assert_eq(&TensorData::from([2, 1, 0]), false);
x.clone()
.count_dims(&[0, 1])
.squeeze_dim::<1>(0)
.to_data()
.assert_eq(&TensorData::from([3]), false);
}
}