use cudarc::driver::PushKernelArg;
use cudarc::driver::safe::CudaContext;
use std::sync::Arc;
use super::loader::{
BLOCK_SIZE, MAX_GRID_DIM_X, dtype_suffix, get_kernel_function, get_or_load_module,
launch_config,
};
use crate::dtype::DType;
use crate::error::{Error, Result};
use crate::runtime::cuda::GuardedStream;
pub const IMAGE_RESIZE_MODULE: &str = "image_resize";
#[derive(Debug, Clone, Copy)]
pub struct ResizeLaunch {
pub src_dtype: DType,
pub dst_dtype: DType,
pub dst_dims: [usize; 4],
pub src_strides: [isize; 4],
pub dst_strides: [isize; 4],
pub channels_last: bool,
pub axis: usize,
pub mode: u32,
pub in_size: usize,
pub out_size: usize,
}
fn float_suffix(dtype: DType) -> Option<&'static str> {
matches!(dtype, DType::F32 | DType::F64 | DType::F16 | DType::BF16).then(|| dtype_suffix(dtype))
}
fn kernel_name(src: DType, dst: DType) -> Option<String> {
let pair_exists = matches!(
(src, dst),
(DType::F32, DType::F32)
| (DType::F64, DType::F64)
| (DType::F16 | DType::BF16, DType::F32)
| (DType::F32, DType::F16 | DType::BF16)
| (DType::F16, DType::F16)
| (DType::BF16, DType::BF16)
);
if !pair_exists {
return None;
}
Some(format!(
"resize_pass_{}_{}",
float_suffix(src)?,
float_suffix(dst)?
))
}
pub unsafe fn launch_resize_pass(
context: &Arc<CudaContext>,
stream: &GuardedStream,
device_index: usize,
src_ptr: u64,
dst_ptr: u64,
launch: &ResizeLaunch,
) -> Result<()> {
let total: usize = launch.dst_dims.iter().product();
if total == 0 {
return Ok(());
}
let func_name =
kernel_name(launch.src_dtype, launch.dst_dtype).ok_or(Error::UnsupportedDType {
dtype: launch.src_dtype,
op: "resize",
})?;
let module = get_or_load_module(context, device_index, IMAGE_RESIZE_MODULE)?;
let func = get_kernel_function(&module, &func_name)?;
let blocks = (total as u64).div_ceil(u64::from(BLOCK_SIZE));
let grid = (blocks.min(u64::from(MAX_GRID_DIM_X)) as u32, 1, 1);
let cfg = launch_config(grid, (BLOCK_SIZE, 1, 1), 0);
let total_u64 = total as u64;
let [_, dim_c, dim_h, dim_w] = launch.dst_dims.map(|d| d as u64);
let ss = launch.src_strides.map(|s| s as i64);
let ds = launch.dst_strides.map(|s| s as i64);
let channels_last = u32::from(launch.channels_last);
let axis = launch.axis as u32;
let mode = launch.mode;
let in_size = launch.in_size as i64;
let out_size = launch.out_size as i64;
unsafe {
let mut builder = stream.launch_builder(&func);
builder.arg(&src_ptr);
builder.arg(&dst_ptr);
builder.arg(&total_u64);
builder.arg(&dim_c);
builder.arg(&dim_h);
builder.arg(&dim_w);
for s in &ss {
builder.arg(s);
}
for s in &ds {
builder.arg(s);
}
builder.arg(&channels_last);
builder.arg(&axis);
builder.arg(&mode);
builder.arg(&in_size);
builder.arg(&out_size);
builder.launch(cfg).map_err(|e| {
Error::Internal(format!("CUDA kernel '{func_name}' launch failed: {e:?}"))
})?;
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn kernel_names_cover_every_schedule_pair() {
assert_eq!(
kernel_name(DType::F16, DType::F32).as_deref(),
Some("resize_pass_f16_f32")
);
assert_eq!(
kernel_name(DType::F32, DType::BF16).as_deref(),
Some("resize_pass_f32_bf16")
);
assert_eq!(
kernel_name(DType::F64, DType::F64).as_deref(),
Some("resize_pass_f64_f64")
);
assert_eq!(kernel_name(DType::F64, DType::F32), None);
assert_eq!(kernel_name(DType::I32, DType::I32), None);
}
}