use crate::dtype::DType;
use crate::error::{Error, Result};
use crate::runtime::traits::profile::DeviceCaps;
use super::matmul_wmma_policy::WMMA_STAGE_HALVES;
#[inline]
pub(super) fn use_wmma_grouped(dtype: DType, caps: DeviceCaps, n: usize, k: usize) -> bool {
let dtype_ok = match dtype {
DType::F16 => caps.f16_mma,
DType::BF16 => caps.bf16,
_ => false,
};
dtype_ok && n.is_multiple_of(WMMA_STAGE_HALVES) && k.is_multiple_of(WMMA_STAGE_HALVES)
}
#[inline]
pub(super) fn grouped_row_hint(total_rows: usize, num_groups: usize) -> usize {
if num_groups == 0 {
return total_rows;
}
total_rows.div_ceil(num_groups)
}
pub(super) fn grouped_tile(row_hint: usize, n: usize) -> (usize, usize, &'static str) {
if n >= 128 && row_hint >= 128 {
(128, 128, "128x128x8_8x8")
} else {
(64, 64, "64x64x32_8x4")
}
}
pub(super) fn grouped_block_dim(suffix: &str) -> (u32, u32, u32) {
match suffix {
"128x128x8_8x8" => (16, 16, 1),
_ => (16, 8, 1),
}
}
pub(super) fn grouped_dtype_suffix(dtype: DType) -> Result<&'static str> {
match dtype {
DType::F32 => Ok("f32"),
DType::F16 => Ok("f16"),
DType::BF16 => Ok("bf16"),
other => Err(Error::Internal(format!(
"grouped matmul supports F32/F16/BF16, got {other:?}"
))),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn dtype_suffixes_cover_the_instantiated_kernels() {
assert_eq!(grouped_dtype_suffix(DType::F32).unwrap(), "f32");
assert_eq!(grouped_dtype_suffix(DType::F16).unwrap(), "f16");
assert_eq!(grouped_dtype_suffix(DType::BF16).unwrap(), "bf16");
assert!(grouped_dtype_suffix(DType::I32).is_err());
}
#[test]
fn wide_output_takes_the_large_tile() {
assert_eq!(grouped_tile(128, 4096).2, "128x128x8_8x8");
}
#[test]
fn narrow_output_takes_the_small_tile() {
assert_eq!(grouped_tile(128, 48).2, "64x64x32_8x4");
}
#[test]
fn many_small_groups_with_wide_n_take_the_small_tile() {
let hint = grouped_row_hint(32 * 64, 32);
assert_eq!(grouped_tile(hint, 4096).2, "64x64x32_8x4");
}
#[test]
fn few_large_groups_with_wide_n_take_the_large_tile() {
let hint = grouped_row_hint(2 * 256, 2);
assert_eq!(grouped_tile(hint, 4096).2, "128x128x8_8x8");
}
#[test]
fn hint_exact_division() {
assert_eq!(grouped_row_hint(256, 4), 64);
}
#[test]
fn hint_ragged_division_rounds_up() {
assert_eq!(grouped_row_hint(100, 3), 34);
}
#[test]
fn hint_single_group_is_total_rows() {
assert_eq!(grouped_row_hint(777, 1), 777);
}
#[test]
fn hint_zero_groups_falls_back_to_total_rows() {
assert_eq!(grouped_row_hint(512, 0), 512);
}
#[test]
fn block_dims_match_the_instantiated_tiles() {
assert_eq!(grouped_block_dim("128x128x8_8x8"), (16, 16, 1));
assert_eq!(grouped_block_dim("64x64x32_8x4"), (16, 8, 1));
}
fn turing_caps() -> DeviceCaps {
DeviceCaps {
dp4a: true,
int8_mma: true,
int8_mma_m16n8k32: false,
f16_mma: true,
bf16: false,
fp8: false,
}
}
fn ampere_caps() -> DeviceCaps {
DeviceCaps {
dp4a: true,
int8_mma: true,
int8_mma_m16n8k32: true,
f16_mma: true,
bf16: true,
fp8: true,
}
}
fn no_caps() -> DeviceCaps {
DeviceCaps::default()
}
#[test]
fn f16_takes_wmma_only_with_f16_mma() {
assert!(use_wmma_grouped(DType::F16, turing_caps(), 32, 32));
assert!(!use_wmma_grouped(DType::F16, no_caps(), 32, 32));
}
#[test]
fn bf16_takes_wmma_only_with_bf16_cap() {
assert!(use_wmma_grouped(DType::BF16, ampere_caps(), 32, 32));
assert!(!use_wmma_grouped(DType::BF16, turing_caps(), 32, 32));
}
#[test]
fn f32_never_takes_wmma() {
assert!(!use_wmma_grouped(DType::F32, ampere_caps(), 32, 32));
}
#[test]
fn stage_aligned_but_not_16_takes_wmma() {
assert!(use_wmma_grouped(DType::F16, ampere_caps(), 40, 24));
assert!(use_wmma_grouped(DType::BF16, ampere_caps(), 8, 8));
}
#[test]
fn ragged_n_or_k_rejected() {
assert!(!use_wmma_grouped(DType::F16, ampere_caps(), 35, 32));
assert!(!use_wmma_grouped(DType::F16, ampere_caps(), 32, 21));
assert!(!use_wmma_grouped(DType::F16, ampere_caps(), 33, 32));
assert!(!use_wmma_grouped(DType::F16, ampere_caps(), 32, 33));
}
}