use cudarc::driver::PushKernelArg;
use cudarc::driver::safe::CudaContext;
use std::sync::Arc;
use crate::dtype::DType;
use crate::error::{Error, Result};
use crate::runtime::cuda::GuardedStream;
use super::matmul_wmma_tile::{select_wmma_tile, wmma_kernel_name, wmma_launch_config};
use super::module_cache::{get_kernel_function, get_or_load_module};
use super::names::kernel_names;
pub unsafe fn launch_matmul_wmma_kernel(
context: &Arc<CudaContext>,
stream: &GuardedStream,
device_index: usize,
dtype: DType,
a_ptr: u64,
b_ptr: u64,
c_ptr: u64,
m: usize,
n: usize,
k: usize,
) -> Result<()> {
let module = get_or_load_module(context, device_index, kernel_names::MATMUL_WMMA_MODULE)?;
let tile = select_wmma_tile(m, n, k, 1, device_index);
let func_name = wmma_kernel_name("matmul_wmma", dtype, tile);
let func = get_kernel_function(&module, &func_name)?;
let cfg = wmma_launch_config(m, n, 1, tile);
let m_u32 = m as u32;
let n_u32 = n as u32;
let k_u32 = k as u32;
unsafe {
let mut builder = stream.launch_builder(&func);
builder.arg(&a_ptr);
builder.arg(&b_ptr);
builder.arg(&c_ptr);
builder.arg(&m_u32);
builder.arg(&n_u32);
builder.arg(&k_u32);
builder.launch(cfg).map_err(|e| {
Error::Internal(format!("CUDA WMMA matmul kernel launch failed: {:?}", e))
})?;
}
Ok(())
}
pub unsafe fn launch_matmul_wmma_batched_kernel(
context: &Arc<CudaContext>,
stream: &GuardedStream,
device_index: usize,
dtype: DType,
a_ptr: u64,
b_ptr: u64,
c_ptr: u64,
batch: usize,
m: usize,
n: usize,
k: usize,
a_batch: usize,
b_batch: usize,
) -> Result<()> {
let module = get_or_load_module(context, device_index, kernel_names::MATMUL_WMMA_MODULE)?;
let tile = select_wmma_tile(m, n, k, batch, device_index);
let func_name = wmma_kernel_name("matmul_wmma_batched", dtype, tile);
let func = get_kernel_function(&module, &func_name)?;
let cfg = wmma_launch_config(m, n, batch, tile);
let batch_u32 = batch as u32;
let m_u32 = m as u32;
let n_u32 = n as u32;
let k_u32 = k as u32;
let a_batch_u32 = a_batch as u32;
let b_batch_u32 = b_batch as u32;
unsafe {
let mut builder = stream.launch_builder(&func);
builder.arg(&a_ptr);
builder.arg(&b_ptr);
builder.arg(&c_ptr);
builder.arg(&batch_u32);
builder.arg(&m_u32);
builder.arg(&n_u32);
builder.arg(&k_u32);
builder.arg(&a_batch_u32);
builder.arg(&b_batch_u32);
builder.launch(cfg).map_err(|e| {
Error::Internal(format!(
"CUDA WMMA batched matmul kernel launch failed: {:?}",
e
))
})?;
}
Ok(())
}
pub unsafe fn launch_matmul_bias_wmma_kernel(
context: &Arc<CudaContext>,
stream: &GuardedStream,
device_index: usize,
dtype: DType,
a_ptr: u64,
b_ptr: u64,
bias_ptr: u64,
c_ptr: u64,
m: usize,
n: usize,
k: usize,
) -> Result<()> {
let module = get_or_load_module(context, device_index, kernel_names::MATMUL_WMMA_MODULE)?;
let tile = select_wmma_tile(m, n, k, 1, device_index);
let func_name = wmma_kernel_name("matmul_bias_wmma", dtype, tile);
let func = get_kernel_function(&module, &func_name)?;
let cfg = wmma_launch_config(m, n, 1, tile);
let m_u32 = m as u32;
let n_u32 = n as u32;
let k_u32 = k as u32;
unsafe {
let mut builder = stream.launch_builder(&func);
builder.arg(&a_ptr);
builder.arg(&b_ptr);
builder.arg(&bias_ptr);
builder.arg(&c_ptr);
builder.arg(&m_u32);
builder.arg(&n_u32);
builder.arg(&k_u32);
builder.launch(cfg).map_err(|e| {
Error::Internal(format!(
"CUDA WMMA matmul_bias kernel launch failed: {:?}",
e
))
})?;
}
Ok(())
}
pub unsafe fn launch_matmul_bias_wmma_batched_kernel(
context: &Arc<CudaContext>,
stream: &GuardedStream,
device_index: usize,
dtype: DType,
a_ptr: u64,
b_ptr: u64,
bias_ptr: u64,
c_ptr: u64,
batch: usize,
m: usize,
n: usize,
k: usize,
a_batch: usize,
b_batch: usize,
) -> Result<()> {
let module = get_or_load_module(context, device_index, kernel_names::MATMUL_WMMA_MODULE)?;
let tile = select_wmma_tile(m, n, k, batch, device_index);
let func_name = wmma_kernel_name("matmul_bias_wmma_batched", dtype, tile);
let func = get_kernel_function(&module, &func_name)?;
let cfg = wmma_launch_config(m, n, batch, tile);
let batch_u32 = batch as u32;
let m_u32 = m as u32;
let n_u32 = n as u32;
let k_u32 = k as u32;
let a_batch_u32 = a_batch as u32;
let b_batch_u32 = b_batch as u32;
unsafe {
let mut builder = stream.launch_builder(&func);
builder.arg(&a_ptr);
builder.arg(&b_ptr);
builder.arg(&bias_ptr);
builder.arg(&c_ptr);
builder.arg(&batch_u32);
builder.arg(&m_u32);
builder.arg(&n_u32);
builder.arg(&k_u32);
builder.arg(&a_batch_u32);
builder.arg(&b_batch_u32);
builder.launch(cfg).map_err(|e| {
Error::Internal(format!(
"CUDA WMMA batched matmul_bias kernel launch failed: {:?}",
e
))
})?;
}
Ok(())
}