use ruda_core::ir::{ElemType, FloatKind, features::Features};
use ruda::runtime::{backend::Runtime, client::ComputeClient, throughput::ComputeCmmaConfig};
use crate::library::throughput::compute_direct;
pub(super) struct Arithmetic;
impl Arithmetic {
pub(super) fn dtypes<R: Runtime>(client: &ComputeClient<R>, dtype: ElemType) -> alloc::vec::Vec<ElemType> {
let mut dtypes = alloc::vec![dtype];
if let Some(accumulator) = Self::promoted(dtype)
&& client.properties().features.supports_type(accumulator)
{
dtypes.push(accumulator);
}
dtypes
}
pub(super) fn widths<R: Runtime>(client: &ComputeClient<R>, dtype: ElemType) -> alloc::vec::Vec<usize> {
client
.properties()
.vector_sizes_in_registers(dtype.size(), compute_direct::LIVE_VECTORS)
.collect()
}
fn promoted(dtype: ElemType) -> Option<ElemType> {
let accumulator = ElemType::Float(FloatKind::F32);
let narrower_float =
matches!(dtype, ElemType::Float(_)) && dtype.size() < accumulator.size();
narrower_float.then_some(accumulator)
}
}
pub(super) struct CooperativeMatrix;
impl CooperativeMatrix {
pub(super) fn implemented(
features: &Features,
dtype: ElemType,
config: ComputeCmmaConfig,
) -> bool {
features.matmul.cmma.iter().any(|it| {
it.a_type == dtype.into()
&& it.b_type == dtype.into()
&& it.cd_type == config.accumulator_type.into()
&& it.m as usize == config.cmma_dims.m
&& it.n as usize == config.cmma_dims.n
&& it.k as usize == config.cmma_dims.k
})
}
}