use crate::znx_base::ZnxInfos; use crate::{Backend, DataView, DataViewMut, Module, ZnxSliceSize, ZnxView, ZnxViewMut, alloc_aligned}; use rand::seq::SliceRandom; use rand_core::RngCore; use rand_distr::{Distribution, weighted::WeightedIndex}; use sampling::source::Source; pub struct Scalar { data: D, n: usize, cols: usize, } impl ZnxInfos for Scalar { fn cols(&self) -> usize { self.cols } fn rows(&self) -> usize { 1 } fn n(&self) -> usize { self.n } fn size(&self) -> usize { 1 } } impl ZnxSliceSize for Scalar { fn sl(&self) -> usize { self.n() } } impl DataView for Scalar { type D = D; fn data(&self) -> &Self::D { &self.data } } impl DataViewMut for Scalar { fn data_mut(&mut self) -> &mut Self::D { &mut self.data } } impl> ZnxView for Scalar { type Scalar = i64; } impl + AsRef<[u8]>> Scalar { pub fn fill_ternary_prob(&mut self, col: usize, prob: f64, source: &mut Source) { let choices: [i64; 3] = [-1, 0, 1]; let weights: [f64; 3] = [prob / 2.0, 1.0 - prob, prob / 2.0]; let dist: WeightedIndex = WeightedIndex::new(&weights).unwrap(); self.at_mut(col, 0) .iter_mut() .for_each(|x: &mut i64| *x = choices[dist.sample(source)]); } pub fn fill_ternary_hw(&mut self, col: usize, hw: usize, source: &mut Source) { assert!(hw <= self.n()); self.at_mut(col, 0)[..hw] .iter_mut() .for_each(|x: &mut i64| *x = (((source.next_u32() & 1) as i64) << 1) - 1); self.at_mut(col, 0).shuffle(source); } } impl>> Scalar { pub(crate) fn bytes_of(n: usize, cols: usize) -> usize { n * cols * size_of::() } pub(crate) fn new(n: usize, cols: usize) -> Self { let data = alloc_aligned::(Self::bytes_of::(n, cols)); Self { data: data.into(), n, cols, } } pub(crate) fn new_from_bytes(n: usize, cols: usize, bytes: impl Into>) -> Self { let data: Vec = bytes.into(); assert!(data.len() == Self::bytes_of::(n, cols)); Self { data: data.into(), n, cols, } } } pub type ScalarOwned = Scalar>; pub trait ScalarAlloc { fn bytes_of_scalar(&self, cols: usize) -> usize; fn new_scalar(&self, cols: usize) -> ScalarOwned; fn new_scalar_from_bytes(&self, cols: usize, bytes: Vec) -> ScalarOwned; } impl ScalarAlloc for Module { fn bytes_of_scalar(&self, cols: usize) -> usize { ScalarOwned::bytes_of::(self.n(), cols) } fn new_scalar(&self, cols: usize) -> ScalarOwned { ScalarOwned::new::(self.n(), cols) } fn new_scalar_from_bytes(&self, cols: usize, bytes: Vec) -> ScalarOwned { ScalarOwned::new_from_bytes::(self.n(), cols, bytes) } } pub trait ScalarToRef { fn to_ref(&self) -> Scalar<&[u8]>; } pub trait ScalarToMut { fn to_mut(&mut self) -> Scalar<&mut [u8]>; } impl ScalarToMut for Scalar> { fn to_mut(&mut self) -> Scalar<&mut [u8]> { Scalar { data: self.data.as_mut_slice(), n: self.n, cols: self.cols, } } } impl ScalarToRef for Scalar> { fn to_ref(&self) -> Scalar<&[u8]> { Scalar { data: self.data.as_slice(), n: self.n, cols: self.cols, } } } impl ScalarToMut for Scalar<&mut [u8]> { fn to_mut(&mut self) -> Scalar<&mut [u8]> { Scalar { data: self.data, n: self.n, cols: self.cols, } } } impl ScalarToRef for Scalar<&mut [u8]> { fn to_ref(&self) -> Scalar<&[u8]> { Scalar { data: self.data, n: self.n, cols: self.cols, } } } impl ScalarToRef for Scalar<&[u8]> { fn to_ref(&self) -> Scalar<&[u8]> { Scalar { data: self.data, n: self.n, cols: self.cols, } } }