numr 0.16.0

Numerical computing and tensor library for Rust, in the role of NumPy: n-dimensional arrays, linear algebra, FFT, statistics and autograd on CPU, CUDA and WebGPU
Documentation
//! conv2d CUDA kernel launcher.
//!
//! One kernel, one output element per thread, summing over `ic` then `ky`
//! then `kx`. No variant, so nothing about the launch shape can move a bit.
//! The kernel indexes the output and both operands in 64 bits, so an output
//! past 2^32 elements is addressed in full.

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_MODULE, conv_arg_u32, conv_output_numel};
use crate::dtype::DType;
use crate::error::{Error, Result};
use crate::runtime::cuda::GuardedStream;

/// Launch conv2d kernel.
///
/// Performs 2D convolution with optional groups support.
///
/// # Arguments
///
/// * `input_ptr` - Input tensor (N, C_in, H, W)
/// * `weight_ptr` - Weight tensor (C_out, C_in/groups, K_h, K_w)
/// * `bias_ptr` - Optional bias tensor (C_out,)
/// * `output_ptr` - Output tensor (N, C_out, H_out, W_out)
///
/// # Errors
///
/// Returns `Error::InvalidArgument` when an extent does not fit the kernel's
/// 32-bit arguments, when the output element count overflows `usize`, or when
/// the output needs more blocks than one grid axis holds.
///
/// # Safety
///
/// All pointers must be valid device memory with sufficient size.
#[allow(clippy::too_many_arguments)]
pub unsafe fn launch_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,
    c_in: usize,
    height: usize,
    width: usize,
    c_out: 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,
    groups: usize,
) -> Result<()> {
    const OP: &str = "conv2d";
    let total = conv_output_numel(&[batch, c_out, output_h, output_w], OP)?;
    if total == 0 {
        return Ok(());
    }

    unsafe {
        let module = get_or_load_module(context, device_index, CONV_MODULE)?;
        let func_name = kernel_name("conv2d", dtype);
        let func = get_kernel_function(&module, &func_name)?;

        let grid = elementwise_launch_config(total)?;
        let block = (BLOCK_SIZE, 1, 1);
        let cfg = launch_config(grid, block, 0);

        let batch_u32 = conv_arg_u32(batch, "batch", OP)?;
        let c_in_u32 = conv_arg_u32(c_in, "c_in", OP)?;
        let height_u32 = conv_arg_u32(height, "height", OP)?;
        let width_u32 = conv_arg_u32(width, "width", OP)?;
        let c_out_u32 = conv_arg_u32(c_out, "c_out", 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 groups_u32 = conv_arg_u32(groups, "groups", 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(&c_in_u32);
        builder.arg(&height_u32);
        builder.arg(&width_u32);
        builder.arg(&c_out_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(&groups_u32);
        builder.arg(&has_bias_u32);

        builder
            .launch(cfg)
            .map_err(|e| Error::Internal(format!("CUDA conv2d kernel launch failed: {:?}", e)))?;

        Ok(())
    }
}