use crate::dtype::DType;
use crate::error::{Error, Result};
use crate::runtime::Device;
use crate::runtime::cuda::tune::{time_launches, tuned};
use crate::runtime::cuda::{CudaClient, CudaRuntime};
use crate::tensor::Tensor;
use super::matmul::launch_matmul_kernel_bt;
use super::matmul_f32_smallm::{launch_matmul_smallm_bt_f32_ungated, smallm_block_count};
pub const SMALLM_MAX_WAVES_FALLBACK: usize = 12;
pub const SMALLM_MAX_WAVES_KEY: &str = "matmul_f32_smallm_bt.max_waves";
pub const SMALLM_MAX_WAVES_CEILING: usize = 64;
const PROBE_K: usize = 5120;
const PROBE_ITERS: usize = 3;
const PROBE_RUNGS: [(usize, usize); 10] = [
(48, 8),
(48, 16),
(48, 32),
(48, 64),
(256, 4),
(256, 8),
(256, 16),
(1024, 1),
(1024, 2),
(1024, 4),
];
pub fn smallm_max_waves(client: &CudaClient) -> usize {
tuned(
client,
SMALLM_MAX_WAVES_KEY,
SMALLM_MAX_WAVES_FALLBACK,
|| probe_max_waves(client),
)
}
#[derive(Clone, Copy, Debug, PartialEq)]
struct Rung {
waves: f64,
wins: bool,
}
fn probe_max_waves(client: &CudaClient) -> Result<usize> {
let sm_count = client.device.profile().compute_units as usize;
if sm_count == 0 {
return Err(Error::Internal(
"small-M wave probe: device profile reports 0 compute units".into(),
));
}
let max_n = PROBE_RUNGS.iter().map(|r| r.0).max().unwrap_or(0);
let max_m = PROBE_RUNGS.iter().map(|r| r.1).max().unwrap_or(0);
let device = &client.device;
let a = Tensor::<CudaRuntime>::empty(&[max_m, PROBE_K], DType::F32, device)?;
let w = Tensor::<CudaRuntime>::empty(&[max_n, PROBE_K], DType::F32, device)?;
let c = Tensor::<CudaRuntime>::empty(&[max_m, max_n], DType::F32, device)?;
let index = device.index;
let mut rungs = Vec::with_capacity(PROBE_RUNGS.len());
for &(n, m) in &PROBE_RUNGS {
let small = time_launches(client, PROBE_ITERS, || unsafe {
launch_matmul_smallm_bt_f32_ungated(
client.context(),
client.stream(),
index,
a.ptr(),
w.ptr(),
c.ptr(),
m,
n,
PROBE_K,
)
})?;
let tiled = time_launches(client, PROBE_ITERS, || {
let ran = unsafe {
launch_matmul_kernel_bt(
client.context(),
client.stream(),
index,
DType::F32,
a.ptr(),
w.ptr(),
c.ptr(),
m,
n,
PROBE_K,
)
}?;
if ran {
Ok(())
} else {
Err(Error::Internal(format!(
"small-M wave probe: tiled bt launcher declined M={m} N={n} K={PROBE_K}"
)))
}
})?;
rungs.push(Rung {
waves: smallm_block_count(m, n) as f64 / sm_count as f64,
wins: small <= tiled,
});
}
Ok(max_waves_from(rungs))
}
fn max_waves_from(mut rungs: Vec<Rung>) -> usize {
rungs.sort_by(|x, y| x.waves.total_cmp(&y.waves));
let Some(loss) = rungs.iter().position(|r| !r.wins) else {
return SMALLM_MAX_WAVES_CEILING;
};
let loss_waves = rungs[loss].waves;
let last_win = rungs[..loss]
.iter()
.map(|r| r.waves)
.filter(|&w| w < loss_waves)
.fold(None, |best: Option<f64>, w| {
Some(best.map_or(w, |b| b.max(w)))
});
match last_win {
None => 1,
Some(w) => (w.floor() as usize).clamp(1, SMALLM_MAX_WAVES_CEILING),
}
}
#[cfg(test)]
mod tests {
use super::*;
fn rung(waves: f64, wins: bool) -> Rung {
Rung { waves, wins }
}
#[test]
fn every_rung_winning_returns_the_ceiling() {
let rungs = vec![rung(1.7, true), rung(13.7, true), rung(82.3, true)];
assert_eq!(max_waves_from(rungs), SMALLM_MAX_WAVES_CEILING);
}
#[test]
fn first_rung_losing_returns_one() {
let rungs = vec![rung(1.7, false), rung(4.6, true)];
assert_eq!(max_waves_from(rungs), 1);
}
#[test]
fn bound_is_the_floor_of_the_last_win_before_the_first_loss() {
let rungs = vec![
rung(18.3, false),
rung(4.6, true),
rung(13.7, true),
rung(9.1, true),
rung(82.3, true),
];
assert_eq!(max_waves_from(rungs), 13);
}
#[test]
fn a_loss_at_the_same_wave_count_excludes_that_rung() {
let rungs = vec![rung(4.6, true), rung(9.1, true), rung(9.1, false)];
assert_eq!(max_waves_from(rungs), 4);
}
#[test]
fn a_sub_wave_win_clamps_to_one() {
let rungs = vec![rung(0.4, true), rung(1.7, false)];
assert_eq!(max_waves_from(rungs), 1);
}
#[test]
fn rungs_span_the_wave_range_and_fit_the_buffers() {
let blocks: Vec<usize> = PROBE_RUNGS
.iter()
.map(|&(n, m)| smallm_block_count(m, n))
.collect();
assert_eq!(blocks.iter().min(), Some(&48));
assert_eq!(blocks.iter().max(), Some(&512));
let max_n = PROBE_RUNGS.iter().map(|r| r.0).max().unwrap_or(0);
let max_m = PROBE_RUNGS.iter().map(|r| r.1).max().unwrap_or(0);
let bytes = 4 * (max_m * PROBE_K + max_n * PROBE_K + max_m * max_n);
assert!(bytes < 100 << 20, "probe buffers are {bytes} bytes");
}
}