use diskann_utils::views::rowmajor::{self, Matrix};
use crate::matrix_kernels::test_util::TestDistr;
pub(super) fn generate_f32(
m: usize,
k: usize,
n: usize,
rng: &mut impl rand::Rng,
) -> (rowmajor::Owned<f32>, rowmajor::Owned<f32>, Vec<f32>) {
let ref_a = TestDistr::matrix::<f32>(m, k, rng);
let ref_b = TestDistr::matrix::<f32>(k, n, rng);
let ref_c: Vec<f32> = ref_a
.rows()
.map(|a_row| {
let mut max_ip = f32::NEG_INFINITY;
for b_col in 0..n {
let mut ip = 0.0;
for (k, a) in a_row.iter().enumerate() {
ip = a.mul_add(*ref_b.element(k, b_col), ip);
}
max_ip = max_ip.max(ip);
}
max_ip
})
.collect();
(ref_a, ref_b, ref_c)
}
pub(super) fn generate_i8(
m: usize,
k: usize,
n: usize,
rng: &mut impl rand::Rng,
) -> (rowmajor::Owned<i8>, rowmajor::Owned<i8>, Vec<i32>) {
let ref_a = TestDistr::matrix::<i8>(m, k, rng);
let ref_b = TestDistr::matrix::<i8>(k, n, rng);
let ref_c: Vec<i32> = ref_a
.rows()
.map(|a_row| {
let mut max_ip = i32::MIN;
for b_col in 0..n {
let mut ip = 0;
for (k, a) in a_row.iter().enumerate() {
ip += i32::from(*a) * i32::from(*ref_b.element(k, b_col));
}
max_ip = max_ip.max(ip);
}
max_ip
})
.collect();
(ref_a, ref_b, ref_c)
}
#[derive(Debug, Clone)]
pub(super) struct TestDims {
pub(super) a_panels_per_tile: usize,
pub(super) total_a_rows: usize,
pub(super) b_cols_per_tile: usize,
pub(super) total_b_cols: usize,
pub(super) k: usize,
}
impl TestDims {
fn from_tuple(
[
a_panels_per_tile,
total_a_rows,
b_cols_per_tile,
total_b_cols,
k,
]: [usize; 5],
) -> Self {
Self {
a_panels_per_tile,
total_a_rows,
b_cols_per_tile,
total_b_cols,
k,
}
}
}
pub(super) fn packed_x_unpacked_test_dims(mr: usize, nr: usize) -> Vec<TestDims> {
[
[1, 1, 1, 1, 1], [1, mr / 2, 1, 1, 1], [1, mr - 1, 2 * nr, nr, 3], [2, mr + 1, 2 * nr, nr, 3], [2, 2 * mr - 1, 2 * nr, 2 * nr + 1, 5], [2, 2 * mr + 1, 2 * nr, 2 * nr + 1, 5], [1, mr * 3, 1, 3, 1], [2, mr * 2, 2 * nr, 2 * nr, 3], [2, mr * 3, 2 * nr, nr, 3],
[2, mr, 2 * nr, 2 * nr + 1, 3],
[2, mr * 3, 2 * nr, 2 * nr + 1, 5],
[2, mr * 5, 2 * nr, 4 * nr + 1, 1],
]
.map(TestDims::from_tuple)
.into_iter()
.collect()
}