use crate::domain::scalar::Scalar;
use eunomia::{Bf16, F16};
mod sealed {
pub trait Sealed {}
}
pub trait ExecutionStrategy: Send + Sync + 'static {}
pub struct ScalarStrategy;
impl ExecutionStrategy for ScalarStrategy {}
pub struct SimdStrategy;
impl ExecutionStrategy for SimdStrategy {}
#[cfg(feature = "parallel")]
pub struct ParallelStrategy;
#[cfg(feature = "parallel")]
impl ExecutionStrategy for ParallelStrategy {}
impl sealed::Sealed for SimdStrategy {}
pub trait SimdOperations<T: Scalar>: sealed::Sealed {
fn add_slice(a: &[T], b: &[T], out: &mut [T]) -> Result<(), &'static str>;
fn sub_slice(a: &[T], b: &[T], out: &mut [T]) -> Result<(), &'static str>;
fn mul_slice(a: &[T], b: &[T], out: &mut [T]) -> Result<(), &'static str>;
fn div_slice(a: &[T], b: &[T], out: &mut [T]) -> Result<(), &'static str>;
fn scale_slice(data: &mut [T], scalar: T) -> Result<(), &'static str>;
fn sum_slice(s: &[T]) -> Option<T>;
fn dot_slice(a: &[T], b: &[T]) -> Option<T>;
fn axpy_slice(alpha: T, x: &[T], out: &mut [T]) -> Result<(), &'static str>;
fn axpy_rows(
alphas: &[T],
x: &[T],
out: &mut [T],
row_stride: usize,
rows: usize,
cols: usize,
) -> Result<(), &'static str>;
fn axpy_rows_batch(
alphas: &[T],
x_panel: &[T],
out: &mut [T],
row_stride: usize,
rows: usize,
depth: usize,
cols: usize,
) -> Result<(), &'static str>;
fn tiled_gemm(
a: &[T],
b: &[T],
c: &mut [T],
m: usize,
n: usize,
k: usize,
) -> Result<(), &'static str>;
fn gemv_strided(
a: &[T],
x: &[T],
y: &mut [T],
nrows: usize,
ncols: usize,
lda: usize,
) -> Result<(), &'static str>;
fn gemv_transpose_strided(
a: &[T],
x: &[T],
y: &mut [T],
nrows: usize,
ncols: usize,
lda: usize,
) -> Result<(), &'static str>;
fn abs_sum_slice(s: &[T]) -> Option<T>;
fn abs_max_slice(s: &[T]) -> Option<T>;
fn min_slice(s: &[T]) -> Option<T>;
fn max_slice(s: &[T]) -> Option<T>;
fn jaccard_distance(a: &[T], b: &[T]) -> Option<f64>;
fn hamming_distance(a: &[T], b: &[T]) -> Option<u64>;
}
macro_rules! impl_simd_ops_native {
($t:ty) => {
impl SimdOperations<$t> for SimdStrategy {
#[inline(always)]
fn add_slice(a: &[$t], b: &[$t], out: &mut [$t]) -> Result<(), &'static str> {
hermes_simd::elementwise_add::<$t>(a, b, out).map_err(|_| "simd add failed")
}
#[inline(always)]
fn sub_slice(a: &[$t], b: &[$t], out: &mut [$t]) -> Result<(), &'static str> {
hermes_simd::elementwise_sub::<$t>(a, b, out).map_err(|_| "simd sub failed")
}
#[inline(always)]
fn mul_slice(a: &[$t], b: &[$t], out: &mut [$t]) -> Result<(), &'static str> {
hermes_simd::elementwise_mul::<$t>(a, b, out).map_err(|_| "simd mul failed")
}
#[inline(always)]
fn div_slice(a: &[$t], b: &[$t], out: &mut [$t]) -> Result<(), &'static str> {
hermes_simd::elementwise_div::<$t>(a, b, out).map_err(|_| "simd div failed")
}
#[inline(always)]
fn scale_slice(data: &mut [$t], scalar: $t) -> Result<(), &'static str> {
hermes_simd::scale::<$t>(data, scalar);
Ok(())
}
#[inline(always)]
fn sum_slice(s: &[$t]) -> Option<$t> {
Some(hermes_simd::sum::<$t>(s))
}
#[inline(always)]
fn dot_slice(a: &[$t], b: &[$t]) -> Option<$t> {
hermes_simd::dot::<$t>(a, b).ok()
}
#[inline(always)]
fn axpy_slice(alpha: $t, x: &[$t], out: &mut [$t]) -> Result<(), &'static str> {
hermes_simd::axpy::<$t>(alpha, x, out).map_err(|_| "simd axpy failed")
}
#[inline(always)]
fn axpy_rows(
alphas: &[$t],
x: &[$t],
out: &mut [$t],
row_stride: usize,
rows: usize,
cols: usize,
) -> Result<(), &'static str> {
hermes_simd::axpy_rows::<$t>(alphas, x, out, row_stride, rows, cols)
.map_err(|_| "simd axpy rows failed")
}
#[inline(always)]
fn axpy_rows_batch(
alphas: &[$t],
x_panel: &[$t],
out: &mut [$t],
row_stride: usize,
rows: usize,
depth: usize,
cols: usize,
) -> Result<(), &'static str> {
hermes_simd::axpy_rows_batch::<$t>(
alphas, x_panel, out, row_stride, rows, depth, cols,
)
.map_err(|_| "simd axpy rows batch failed")
}
#[inline(always)]
fn tiled_gemm(
a: &[$t],
b: &[$t],
c: &mut [$t],
m: usize,
n: usize,
k: usize,
) -> Result<(), &'static str> {
hermes_simd::tiled_gemm::<$t>(a, b, c, m, n, k)
.map_err(|_| "simd tiled gemm failed")
}
#[inline(always)]
fn gemv_strided(
a: &[$t],
x: &[$t],
y: &mut [$t],
nrows: usize,
ncols: usize,
lda: usize,
) -> Result<(), &'static str> {
hermes_simd::gemv_strided::<$t>(a, x, y, nrows, ncols, lda)
.map_err(|_| "simd gemv_strided failed")
}
#[inline(always)]
fn gemv_transpose_strided(
a: &[$t],
x: &[$t],
y: &mut [$t],
nrows: usize,
ncols: usize,
lda: usize,
) -> Result<(), &'static str> {
hermes_simd::gemv_transpose_strided::<$t>(a, x, y, nrows, ncols, lda)
.map_err(|_| "simd gemv_transpose_strided failed")
}
#[inline(always)]
fn abs_sum_slice(s: &[$t]) -> Option<$t> {
Some(hermes_simd::abs_sum::<$t>(s))
}
#[inline(always)]
fn abs_max_slice(s: &[$t]) -> Option<$t> {
Some(hermes_simd::abs_max::<$t>(s))
}
#[inline(always)]
fn min_slice(s: &[$t]) -> Option<$t> {
Some(hermes_simd::min::<$t>(s))
}
#[inline(always)]
fn max_slice(s: &[$t]) -> Option<$t> {
Some(hermes_simd::max::<$t>(s))
}
#[inline(always)]
fn jaccard_distance(a: &[$t], b: &[$t]) -> Option<f64> {
let intersection = hermes_simd::reduce_popcount_and(a, b).ok()?;
let union = hermes_simd::reduce_popcount_or(a, b).ok()?;
if union == 0 {
Some(0.0)
} else {
Some(1.0 - (intersection as f64) / (union as f64))
}
}
#[inline(always)]
fn hamming_distance(a: &[$t], b: &[$t]) -> Option<u64> {
let dist = hermes_simd::reduce_popcount_xor(a, b).ok()?;
Some(dist as u64)
}
}
};
}
impl_simd_ops_native!(f32);
impl_simd_ops_native!(f64);
impl_simd_ops_native!(F16);
impl_simd_ops_native!(Bf16);