use cudarc::driver::PushKernelArg;
use cudarc::driver::safe::CudaContext;
use std::sync::Arc;
use super::super::loader::{
BLOCK_SIZE, elementwise_launch_config, get_kernel_function, get_or_load_module, kernel_name,
launch_config,
};
use super::tuning::{
CONV_BLOCK_THREADS, CONV_MODULE, CUDA_MAX_GRID_YZ, DEPTHWISE_CONV2D_OX_BLOCK,
DEPTHWISE_CONV2D_OX_MIN_OUTPUT_WIDTH, DEPTHWISE_CONV2D_OX_MIN_WAVES,
DEPTHWISE_CONV2D_OX_MODULE, conv_arg_u32, conv_output_numel, fills_device,
};
use crate::dtype::DType;
use crate::error::{Error, Result};
use crate::runtime::Device;
use crate::runtime::cuda::CudaDevice;
use crate::runtime::cuda::GuardedStream;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum DepthwiseConv2dVariant {
Flat,
Ox,
}
fn depthwise_conv2d_variant(
device_index: usize,
dtype: DType,
batch: usize,
channels: usize,
output_h: usize,
output_w: usize,
) -> DepthwiseConv2dVariant {
if matches!(dtype, DType::FP8E4M3 | DType::FP8E5M2) {
return DepthwiseConv2dVariant::Flat;
}
let compute_units = CudaDevice::new(device_index).profile().compute_units as usize;
let x_extent = output_w.div_ceil(DEPTHWISE_CONV2D_OX_BLOCK);
let x_work = output_h.saturating_mul(x_extent);
let ox_threads = batch.saturating_mul(channels).saturating_mul(x_work);
let blocked = output_w >= DEPTHWISE_CONV2D_OX_MIN_OUTPUT_WIDTH
&& fills_device(ox_threads, compute_units, DEPTHWISE_CONV2D_OX_MIN_WAVES)
&& channels <= CUDA_MAX_GRID_YZ
&& batch <= CUDA_MAX_GRID_YZ
&& u32::try_from(x_work).is_ok();
if blocked {
DepthwiseConv2dVariant::Ox
} else {
DepthwiseConv2dVariant::Flat
}
}
#[allow(clippy::too_many_arguments)]
pub unsafe fn launch_depthwise_conv2d(
context: &Arc<CudaContext>,
stream: &GuardedStream,
device_index: usize,
dtype: DType,
input_ptr: u64,
weight_ptr: u64,
bias_ptr: Option<u64>,
output_ptr: u64,
batch: usize,
channels: usize,
height: usize,
width: usize,
kernel_h: usize,
kernel_w: usize,
output_h: usize,
output_w: usize,
stride_h: usize,
stride_w: usize,
pad_h: usize,
pad_w: usize,
dilation_h: usize,
dilation_w: usize,
) -> Result<()> {
let variant =
depthwise_conv2d_variant(device_index, dtype, batch, channels, output_h, output_w);
unsafe {
launch_depthwise_conv2d_variant(
context,
stream,
device_index,
dtype,
input_ptr,
weight_ptr,
bias_ptr,
output_ptr,
batch,
channels,
height,
width,
kernel_h,
kernel_w,
output_h,
output_w,
stride_h,
stride_w,
pad_h,
pad_w,
dilation_h,
dilation_w,
variant,
)
}
}
#[allow(clippy::too_many_arguments)]
unsafe fn launch_depthwise_conv2d_variant(
context: &Arc<CudaContext>,
stream: &GuardedStream,
device_index: usize,
dtype: DType,
input_ptr: u64,
weight_ptr: u64,
bias_ptr: Option<u64>,
output_ptr: u64,
batch: usize,
channels: usize,
height: usize,
width: usize,
kernel_h: usize,
kernel_w: usize,
output_h: usize,
output_w: usize,
stride_h: usize,
stride_w: usize,
pad_h: usize,
pad_w: usize,
dilation_h: usize,
dilation_w: usize,
variant: DepthwiseConv2dVariant,
) -> Result<()> {
const OP: &str = "depthwise_conv2d";
let total = conv_output_numel(&[batch, channels, output_h, output_w], OP)?;
if total == 0 {
return Ok(());
}
unsafe {
let ox_blocked = variant == DepthwiseConv2dVariant::Ox;
let x_extent = output_w.div_ceil(DEPTHWISE_CONV2D_OX_BLOCK);
let x_work = output_h.saturating_mul(x_extent);
let base = if ox_blocked {
"depthwise_conv2d_ox"
} else {
"depthwise_conv2d"
};
let module_name = if ox_blocked {
DEPTHWISE_CONV2D_OX_MODULE
} else {
CONV_MODULE
};
let module = get_or_load_module(context, device_index, module_name)?;
let func_name = kernel_name(base, dtype);
let func = get_kernel_function(&module, &func_name)?;
let cfg = if ox_blocked {
let x_work_u32 = conv_arg_u32(x_work, "output_h * output column blocks", OP)?;
let grid = (
x_work_u32.div_ceil(CONV_BLOCK_THREADS),
conv_arg_u32(channels, "channels", OP)?,
conv_arg_u32(batch, "batch", OP)?,
);
launch_config(grid, (CONV_BLOCK_THREADS, 1, 1), 0)
} else {
let grid = elementwise_launch_config(total)?;
launch_config(grid, (BLOCK_SIZE, 1, 1), 0)
};
let batch_u32 = conv_arg_u32(batch, "batch", OP)?;
let channels_u32 = conv_arg_u32(channels, "channels", OP)?;
let height_u32 = conv_arg_u32(height, "height", OP)?;
let width_u32 = conv_arg_u32(width, "width", OP)?;
let kernel_h_u32 = conv_arg_u32(kernel_h, "kernel_h", OP)?;
let kernel_w_u32 = conv_arg_u32(kernel_w, "kernel_w", OP)?;
let output_h_u32 = conv_arg_u32(output_h, "output_h", OP)?;
let output_w_u32 = conv_arg_u32(output_w, "output_w", OP)?;
let stride_h_u32 = conv_arg_u32(stride_h, "stride_h", OP)?;
let stride_w_u32 = conv_arg_u32(stride_w, "stride_w", OP)?;
let pad_h_u32 = conv_arg_u32(pad_h, "pad_h", OP)?;
let pad_w_u32 = conv_arg_u32(pad_w, "pad_w", OP)?;
let dilation_h_u32 = conv_arg_u32(dilation_h, "dilation_h", OP)?;
let dilation_w_u32 = conv_arg_u32(dilation_w, "dilation_w", OP)?;
let has_bias_u32: u32 = if bias_ptr.is_some() { 1 } else { 0 };
let bias_ptr_val = bias_ptr.unwrap_or(0);
let mut builder = stream.launch_builder(&func);
builder.arg(&input_ptr);
builder.arg(&weight_ptr);
builder.arg(&bias_ptr_val);
builder.arg(&output_ptr);
builder.arg(&batch_u32);
builder.arg(&channels_u32);
builder.arg(&height_u32);
builder.arg(&width_u32);
builder.arg(&kernel_h_u32);
builder.arg(&kernel_w_u32);
builder.arg(&output_h_u32);
builder.arg(&output_w_u32);
builder.arg(&stride_h_u32);
builder.arg(&stride_w_u32);
builder.arg(&pad_h_u32);
builder.arg(&pad_w_u32);
builder.arg(&dilation_h_u32);
builder.arg(&dilation_w_u32);
builder.arg(&has_bias_u32);
builder.launch(cfg).map_err(|e| {
Error::Internal(format!(
"CUDA depthwise_conv2d kernel launch failed: {:?}",
e
))
})?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ops::RandomOps;
use crate::runtime::Runtime;
use crate::runtime::cuda::{CudaClient, CudaRuntime};
use crate::tensor::Tensor;
fn setup() -> Option<CudaClient> {
if !crate::runtime::cuda::is_cuda_available() {
return None;
}
Some(CudaRuntime::default_client(&CudaDevice::new(0)))
}
#[test]
fn flat_and_column_blocked_agree_bitwise() {
let Some(client) = setup() else {
return;
};
let (batch, channels, h, w, k) = (2usize, 6usize, 17usize, 23usize, 3usize);
let (out_h, out_w) = (h, w);
let input = client
.rand(&[batch, channels, h, w], DType::F32)
.expect("input");
let weight = client
.rand(&[channels, 1, k, k], DType::F32)
.expect("weight");
let bias = client.rand(&[channels], DType::F32).expect("bias");
let run = |variant: DepthwiseConv2dVariant| -> Vec<f32> {
let out = Tensor::<CudaRuntime>::empty(
&[batch, channels, out_h, out_w],
DType::F32,
&client.device,
)
.expect("output");
unsafe {
launch_depthwise_conv2d_variant(
&client.context,
&client.stream,
client.device.index,
DType::F32,
input.ptr(),
weight.ptr(),
Some(bias.ptr()),
out.ptr(),
batch,
channels,
h,
w,
k,
k,
out_h,
out_w,
1,
1,
1,
1,
1,
1,
variant,
)
.expect("launch");
}
out.to_vec()
};
let flat = run(DepthwiseConv2dVariant::Flat);
let blocked = run(DepthwiseConv2dVariant::Ox);
assert_eq!(flat.len(), blocked.len());
for (i, (a, b)) in flat.iter().zip(&blocked).enumerate() {
assert_eq!(
a.to_bits(),
b.to_bits(),
"element {i}: flat {a} vs column-blocked {b}"
);
}
}
}