use crate::dtype::DType;
use super::conv_implicit_gemm_staging::ConvStaging;
use super::launch_dims::{LaunchConfig, MAX_GRID_DIM_X, MAX_GRID_DIM_YZ};
use super::names::dtype_suffix;
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub(super) enum ConvTile {
Tile128x64,
Tile256x32,
Tile256x64,
}
impl ConvTile {
const fn tile_m(self) -> u32 {
match self {
Self::Tile256x32 | Self::Tile256x64 => 256,
Self::Tile128x64 => 128,
}
}
const fn tile_n(self) -> u32 {
match self {
Self::Tile128x64 | Self::Tile256x64 => 64,
Self::Tile256x32 => 32,
}
}
const fn threads(self) -> u32 {
128
}
const fn suffix(self) -> &'static str {
match self {
Self::Tile128x64 => "128x64",
Self::Tile256x32 => "256x32",
Self::Tile256x64 => "256x64",
}
}
pub(super) const fn patch(self) -> (usize, usize) {
match self {
Self::Tile128x64 => (8, 16),
Self::Tile256x32 | Self::Tile256x64 => (8, 32),
}
}
pub(super) const fn halo_capacity(self) -> usize {
match self {
Self::Tile128x64 => 192,
Self::Tile256x32 | Self::Tile256x64 => 352,
}
}
pub(super) const fn nchw_run_capacity(self) -> usize {
match self {
Self::Tile128x64 => 64,
Self::Tile256x32 | Self::Tile256x64 => 104,
}
}
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub(super) struct ConvWindow {
pub batch: usize,
pub out_h: usize,
pub out_w: usize,
pub kernel: (usize, usize),
pub stride: (usize, usize),
pub dilation: (usize, usize),
pub pad_left: usize,
}
pub(super) fn conv_kernel_name(
dtype: DType,
half_accumulate: bool,
tile: ConvTile,
staging: ConvStaging,
) -> String {
let acc = if half_accumulate {
"f16acc"
} else {
dtype_suffix(dtype)
};
format!(
"conv2d_implicit_gemm_{acc}_{}_{}",
tile.suffix(),
staging.suffix()
)
}
pub(super) fn select_conv_tile(c_out: usize, half_accumulate: bool) -> ConvTile {
if c_out <= 32 {
ConvTile::Tile256x32
} else if half_accumulate {
ConvTile::Tile256x64
} else {
ConvTile::Tile128x64
}
}
pub(super) fn conv_launch_config(
window: &ConvWindow,
c_out: usize,
tile: ConvTile,
staging: ConvStaging,
) -> Option<LaunchConfig> {
let grid_x = if staging.is_halo() {
let (ph, pw) = tile.patch();
(window.batch as u64)
.checked_mul((window.out_h as u64).div_ceil(ph as u64))?
.checked_mul((window.out_w as u64).div_ceil(pw as u64))?
} else {
(window.batch as u64)
.checked_mul(window.out_h as u64)?
.checked_mul(window.out_w as u64)?
.div_ceil(u64::from(tile.tile_m()))
};
let grid_y = (c_out as u64).div_ceil(u64::from(tile.tile_n()));
if grid_x == 0 || grid_y == 0 {
return None;
}
if grid_x > u64::from(MAX_GRID_DIM_X) || grid_y > u64::from(MAX_GRID_DIM_YZ) {
return None;
}
Some(LaunchConfig {
grid_dim: (grid_x as u32, grid_y as u32, 1),
block_dim: (tile.threads(), 1, 1),
shared_mem_bytes: 0,
})
}
#[cfg(test)]
pub(super) fn test_window(
batch: usize,
h: usize,
w: usize,
k: usize,
s: usize,
d: usize,
) -> ConvWindow {
ConvWindow {
batch,
out_h: h,
out_w: w,
kernel: (k, k),
stride: (s, s),
dilation: (d, d),
pad_left: k / 2,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn narrow_channels_take_256x32() {
for c_out in [1, 3, 16, 32] {
assert_eq!(select_conv_tile(c_out, false), ConvTile::Tile256x32);
assert_eq!(select_conv_tile(c_out, true), ConvTile::Tile256x32);
}
}
#[test]
fn wide_channels_take_128x64() {
for c_out in [33, 48, 64, 96, 160] {
assert_eq!(select_conv_tile(c_out, false), ConvTile::Tile128x64);
}
}
#[test]
fn wide_channels_with_half_accumulation_take_256x64() {
for c_out in [33, 48, 64, 96, 160] {
assert_eq!(select_conv_tile(c_out, true), ConvTile::Tile256x64);
}
}
use super::test_window as window;
#[test]
fn launch_config_puts_pixels_on_x() {
let gather = ConvStaging::Vector;
let w = window(1, 1080, 1920, 3, 1, 1);
let cfg = conv_launch_config(&w, 64, ConvTile::Tile128x64, gather).unwrap();
assert_eq!(cfg.grid_dim, (16_200, 1, 1));
assert_eq!(cfg.block_dim, (128, 1, 1));
let cfg = conv_launch_config(
&window(2, 17, 23, 3, 1, 1),
48,
ConvTile::Tile128x64,
gather,
)
.unwrap();
assert_eq!(cfg.grid_dim, (7, 1, 1));
let cfg = conv_launch_config(
&window(1, 10, 10, 3, 1, 1),
130,
ConvTile::Tile128x64,
gather,
)
.unwrap();
assert_eq!(cfg.grid_dim, (1, 3, 1));
let w4k = window(1, 2160, 3840, 3, 1, 1);
let cfg = conv_launch_config(&w4k, 32, ConvTile::Tile256x32, gather).unwrap();
assert_eq!(cfg.grid_dim, (32_400, 1, 1));
let cfg = conv_launch_config(&w, 96, ConvTile::Tile256x64, gather).unwrap();
assert_eq!(cfg.grid_dim, (8_100, 2, 1));
assert_eq!(cfg.block_dim, (128, 1, 1));
}
#[test]
fn halo_launch_config_counts_patches() {
let halo = ConvStaging::Halo;
let w = window(1, 1080, 1920, 3, 1, 1);
let cfg = conv_launch_config(&w, 64, ConvTile::Tile128x64, halo).unwrap();
assert_eq!(cfg.grid_dim, (16_200, 1, 1));
let cfg = conv_launch_config(&w, 96, ConvTile::Tile256x64, halo).unwrap();
assert_eq!(cfg.grid_dim, (8_100, 2, 1));
let cfg = conv_launch_config(
&window(1, 540, 960, 3, 1, 1),
32,
ConvTile::Tile256x32,
halo,
)
.unwrap();
assert_eq!(cfg.grid_dim, (68 * 30, 1, 1));
let cfg = conv_launch_config(&window(2, 17, 23, 3, 1, 1), 32, ConvTile::Tile256x32, halo)
.unwrap();
assert_eq!(cfg.grid_dim, (6, 1, 1));
assert_eq!(cfg.block_dim, (128, 1, 1));
}
#[test]
fn launch_config_rejects_empty_and_oversized_grids() {
let gather = ConvStaging::Vector;
let t = ConvTile::Tile128x64;
assert!(conv_launch_config(&window(0, 4, 8, 3, 1, 1), 32, t, gather).is_none());
assert!(conv_launch_config(&window(1, 4, 8, 3, 1, 1), 0, t, gather).is_none());
assert!(conv_launch_config(&window(0, 4, 8, 3, 1, 1), 32, t, ConvStaging::Halo).is_none());
let too_many_channels = (MAX_GRID_DIM_YZ as usize + 1) * 32;
let w = window(1, 4, 8, 3, 1, 1);
assert!(conv_launch_config(&w, too_many_channels, ConvTile::Tile256x32, gather).is_none());
}
#[test]
fn kernel_names_match_the_cu_instantiations() {
assert_eq!(
conv_kernel_name(DType::F16, false, ConvTile::Tile256x32, ConvStaging::Scalar),
"conv2d_implicit_gemm_f16_256x32_scalar"
);
assert_eq!(
conv_kernel_name(
DType::BF16,
false,
ConvTile::Tile128x64,
ConvStaging::Scalar
),
"conv2d_implicit_gemm_bf16_128x64_scalar"
);
assert_eq!(
conv_kernel_name(DType::F16, false, ConvTile::Tile128x64, ConvStaging::Vector),
"conv2d_implicit_gemm_f16_128x64_vec"
);
assert_eq!(
conv_kernel_name(DType::F16, true, ConvTile::Tile256x32, ConvStaging::Vector),
"conv2d_implicit_gemm_f16acc_256x32_vec"
);
assert_eq!(
conv_kernel_name(DType::F16, true, ConvTile::Tile256x32, ConvStaging::Scalar),
"conv2d_implicit_gemm_f16acc_256x32_scalar"
);
assert_eq!(
conv_kernel_name(DType::F16, true, ConvTile::Tile256x64, ConvStaging::Vector),
"conv2d_implicit_gemm_f16acc_256x64_vec"
);
assert_eq!(
conv_kernel_name(DType::F16, true, ConvTile::Tile256x64, ConvStaging::Scalar),
"conv2d_implicit_gemm_f16acc_256x64_scalar"
);
assert_eq!(
conv_kernel_name(DType::F16, true, ConvTile::Tile256x64, ConvStaging::Halo),
"conv2d_implicit_gemm_f16acc_256x64_halo"
);
assert_eq!(
conv_kernel_name(DType::BF16, false, ConvTile::Tile128x64, ConvStaging::Halo),
"conv2d_implicit_gemm_bf16_128x64_halo"
);
assert_eq!(
conv_kernel_name(
DType::F16,
false,
ConvTile::Tile128x64,
ConvStaging::NchwHalo
),
"conv2d_implicit_gemm_f16_128x64_nchw_halo"
);
assert_eq!(
conv_kernel_name(
DType::BF16,
false,
ConvTile::Tile256x32,
ConvStaging::NchwGather
),
"conv2d_implicit_gemm_bf16_256x32_nchw_gather"
);
assert_eq!(
conv_kernel_name(
DType::F16,
true,
ConvTile::Tile256x64,
ConvStaging::NchwHalo
),
"conv2d_implicit_gemm_f16acc_256x64_nchw_halo"
);
assert_eq!(
conv_kernel_name(
DType::F16,
true,
ConvTile::Tile256x32,
ConvStaging::NchwGather
),
"conv2d_implicit_gemm_f16acc_256x32_nchw_gather"
);
}
#[test]
fn nchw_halo_launch_config_counts_patches() {
let w = window(1, 1080, 1920, 3, 1, 1);
let cfg = conv_launch_config(&w, 64, ConvTile::Tile128x64, ConvStaging::NchwHalo).unwrap();
assert_eq!(cfg.grid_dim, (16_200, 1, 1));
let cfg =
conv_launch_config(&w, 64, ConvTile::Tile128x64, ConvStaging::NchwGather).unwrap();
assert_eq!(cfg.grid_dim, (16_200, 1, 1));
let cfg = conv_launch_config(
&window(2, 17, 23, 3, 1, 1),
32,
ConvTile::Tile256x32,
ConvStaging::NchwHalo,
)
.unwrap();
assert_eq!(cfg.grid_dim, (6, 1, 1));
}
}