use burn::{
Tensor,
prelude,
prelude::Backend,
tensor::{
DType,
TensorKind,
},
};
use strum;
#[derive(
Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, strum::EnumString, strum::Display,
)]
#[non_exhaustive]
pub enum TensorKindDesc {
Bool,
Float,
Int,
}
impl TensorKindDesc {
pub const fn for_kind<K: ParamKindBinding>() -> Self {
K::KIND
}
pub fn kind<B, const R: usize, K>(_tensor: &Tensor<B, R, K>) -> Self
where
B: Backend,
K: TensorKind<B> + ParamKindBinding,
{
Self::for_kind::<K>()
}
}
impl From<DType> for TensorKindDesc {
fn from(dtype: DType) -> Self {
if dtype.is_float() {
TensorKindDesc::Float
} else if dtype.is_int() {
TensorKindDesc::Int
} else if dtype.is_bool() {
TensorKindDesc::Bool
} else {
panic!("Unsupported dtype: {dtype:?}")
}
}
}
pub trait ParamKindBinding {
const KIND: TensorKindDesc;
}
impl ParamKindBinding for prelude::Bool {
const KIND: TensorKindDesc = TensorKindDesc::Bool;
}
impl ParamKindBinding for prelude::Float {
const KIND: TensorKindDesc = TensorKindDesc::Float;
}
impl ParamKindBinding for prelude::Int {
const KIND: TensorKindDesc = TensorKindDesc::Int;
}
#[cfg(test)]
mod tests {
use burn::{
prelude::{
Bool,
Float,
Int,
Tensor,
},
tensor::{
BoolStore,
DType,
},
};
use crate::{
burner::descriptors::TensorKindDesc,
support::testing::CpuBackend,
};
type B = CpuBackend;
#[test]
fn test_tensor_kinds() {
let device = Default::default();
assert_eq!(TensorKindDesc::for_kind::<Bool>(), TensorKindDesc::Bool);
assert_eq!(
TensorKindDesc::kind(&Tensor::<B, 1, Bool>::zeros(&[1], &device)),
TensorKindDesc::Bool
);
assert_eq!(TensorKindDesc::for_kind::<Float>(), TensorKindDesc::Float);
assert_eq!(
TensorKindDesc::kind(&Tensor::<B, 1, Float>::zeros(&[1], &device)),
TensorKindDesc::Float
);
assert_eq!(TensorKindDesc::for_kind::<Int>(), TensorKindDesc::Int);
assert_eq!(
TensorKindDesc::kind(&Tensor::<B, 1, Int>::zeros(&[1], &device)),
TensorKindDesc::Int
);
}
#[test]
fn test_from_dtype() {
assert_eq!(
TensorKindDesc::from(DType::Bool(BoolStore::Native)),
TensorKindDesc::Bool
);
assert_eq!(TensorKindDesc::from(DType::F64), TensorKindDesc::Float);
assert_eq!(TensorKindDesc::from(DType::I64), TensorKindDesc::Int);
}
}