use core::ffi::c_void;
use core::marker::PhantomData;
use baracuda_cutlass::{Error, Result};
use baracuda_driver::Stream;
use baracuda_kernels_types::{
ArchSku, BackendKind, Element, ElementKind, KernelSku, MathPrecision, OpCategory,
PlanPreference, PoolKind, PrecisionGuarantee, TensorMut, TensorRef, Workspace,
};
#[derive(Copy, Clone, Debug)]
#[non_exhaustive]
pub struct LpPool2dDescriptor {
pub batch: i32,
pub channels: i32,
pub h_in: i32,
pub w_in: i32,
pub window_h: i32,
pub window_w: i32,
pub stride_h: i32,
pub stride_w: i32,
pub p: f32,
pub ceil_mode: bool,
pub element: ElementKind,
}
impl LpPool2dDescriptor {
pub fn new(
batch: i32,
channels: i32,
h_in: i32,
w_in: i32,
window_h: i32,
window_w: i32,
p: f32,
element: ElementKind,
) -> Self {
Self {
batch,
channels,
h_in,
w_in,
window_h,
window_w,
stride_h: window_h,
stride_w: window_w,
p,
ceil_mode: false,
element,
}
}
#[inline]
pub fn with_stride(mut self, stride_h: i32, stride_w: i32) -> Self {
self.stride_h = stride_h;
self.stride_w = stride_w;
self
}
#[inline]
pub fn with_ceil_mode(mut self, ceil_mode: bool) -> Self {
self.ceil_mode = ceil_mode;
self
}
}
pub struct LpPool2dFwArgs<'a, T: Element> {
pub x: TensorRef<'a, T, 4>,
pub y: TensorMut<'a, T, 4>,
}
pub struct LpPool2dPlan<T: Element> {
pub(super) desc: LpPool2dDescriptor,
pub(super) h_out: i32,
pub(super) w_out: i32,
sku: KernelSku,
_marker: PhantomData<T>,
}
impl<T: Element> LpPool2dPlan<T> {
pub fn select(
_stream: &Stream,
desc: &LpPool2dDescriptor,
_pref: PlanPreference,
) -> Result<Self> {
validate_lp2d::<T>(desc)?;
let (h_out, w_out) = compute_out_2d(desc)?;
let sku = build_lp2d_sku::<T>(PoolKind::LpPool2d);
Ok(Self {
desc: *desc,
h_out,
w_out,
sku,
_marker: PhantomData,
})
}
#[inline]
pub fn sku(&self) -> KernelSku {
self.sku
}
#[inline]
pub fn precision_guarantee(&self) -> PrecisionGuarantee {
self.sku.precision_guarantee
}
#[inline]
pub fn workspace_size(&self) -> usize {
0
}
#[inline]
pub fn output_dims(&self) -> (i32, i32) {
(self.h_out, self.w_out)
}
pub fn run_fw(
&self,
stream: &Stream,
_workspace: Workspace<'_>,
args: LpPool2dFwArgs<'_, T>,
) -> Result<()> {
check_fw_args_lp2d(&self.desc, self.h_out, self.w_out, &args)?;
let x_ptr = args.x.data.as_raw().0 as *const c_void;
let y_ptr = args.y.data.as_raw().0 as *mut c_void;
let stream_ptr = stream.as_raw() as *mut c_void;
let ceil_flag = if self.desc.ceil_mode { 1 } else { 0 };
let status = match T::KIND {
ElementKind::F32 => unsafe {
baracuda_kernels_sys::baracuda_kernels_lp_pool_2d_f32_run(
x_ptr, y_ptr, self.desc.batch, self.desc.channels,
self.desc.h_in, self.desc.w_in,
self.desc.window_h, self.desc.window_w,
self.desc.stride_h, self.desc.stride_w,
self.h_out, self.w_out, self.desc.p, ceil_flag, stream_ptr,
)
},
ElementKind::F64 => unsafe {
baracuda_kernels_sys::baracuda_kernels_lp_pool_2d_f64_run(
x_ptr, y_ptr, self.desc.batch, self.desc.channels,
self.desc.h_in, self.desc.w_in,
self.desc.window_h, self.desc.window_w,
self.desc.stride_h, self.desc.stride_w,
self.h_out, self.w_out, self.desc.p, ceil_flag, stream_ptr,
)
},
ElementKind::F16 => unsafe {
baracuda_kernels_sys::baracuda_kernels_lp_pool_2d_f16_run(
x_ptr, y_ptr, self.desc.batch, self.desc.channels,
self.desc.h_in, self.desc.w_in,
self.desc.window_h, self.desc.window_w,
self.desc.stride_h, self.desc.stride_w,
self.h_out, self.w_out, self.desc.p, ceil_flag, stream_ptr,
)
},
ElementKind::Bf16 => unsafe {
baracuda_kernels_sys::baracuda_kernels_lp_pool_2d_bf16_run(
x_ptr, y_ptr, self.desc.batch, self.desc.channels,
self.desc.h_in, self.desc.w_in,
self.desc.window_h, self.desc.window_w,
self.desc.stride_h, self.desc.stride_w,
self.h_out, self.w_out, self.desc.p, ceil_flag, stream_ptr,
)
},
_ => {
return Err(Error::Unsupported(
"baracuda-kernels::LpPool2dPlan: unexpected dtype after select()",
));
}
};
super::map_lp_pool_status(status)
}
}
pub(super) fn validate_lp2d<T: Element>(d: &LpPool2dDescriptor) -> Result<()> {
if d.element != T::KIND {
return Err(Error::Unsupported(
"baracuda-kernels::LpPool2dPlan: descriptor.element != T::KIND",
));
}
if !matches!(
T::KIND,
ElementKind::F32 | ElementKind::F64 | ElementKind::F16 | ElementKind::Bf16
) {
return Err(Error::Unsupported(
"baracuda-kernels::LpPool2dPlan: dtype must be f32 / f64 / f16 / bf16",
));
}
if d.batch <= 0 || d.channels <= 0 || d.h_in <= 0 || d.w_in <= 0 {
return Err(Error::InvalidProblem(
"baracuda-kernels::LpPool2dPlan: batch/channels/h_in/w_in must be > 0",
));
}
if d.window_h <= 0 || d.window_w <= 0 || d.stride_h <= 0 || d.stride_w <= 0 {
return Err(Error::InvalidProblem(
"baracuda-kernels::LpPool2dPlan: window/stride must be > 0",
));
}
if d.window_h > d.h_in || d.window_w > d.w_in {
return Err(Error::InvalidProblem(
"baracuda-kernels::LpPool2dPlan: window > input dimension produces zero-sized output",
));
}
if !d.p.is_finite() || d.p <= 0.0 {
return Err(Error::Unsupported(
"baracuda-kernels::LpPool2dPlan: p must be finite and > 0 \
(use MaxPool2dPlan for the p=∞ case)",
));
}
Ok(())
}
pub(super) fn compute_out_2d(d: &LpPool2dDescriptor) -> Result<(i32, i32)> {
let diff_h = d.h_in - d.window_h;
let diff_w = d.w_in - d.window_w;
let (h_out, w_out) = if d.ceil_mode {
(
(diff_h + d.stride_h - 1) / d.stride_h + 1,
(diff_w + d.stride_w - 1) / d.stride_w + 1,
)
} else {
(diff_h / d.stride_h + 1, diff_w / d.stride_w + 1)
};
if h_out <= 0 || w_out <= 0 {
return Err(Error::InvalidProblem(
"baracuda-kernels::LpPool2dPlan: computed (h_out, w_out) <= 0",
));
}
Ok((h_out, w_out))
}
pub(super) fn check_fw_args_lp2d<T: Element>(
d: &LpPool2dDescriptor,
h_out: i32,
w_out: i32,
args: &LpPool2dFwArgs<'_, T>,
) -> Result<()> {
let want_x = [d.batch, d.channels, d.h_in, d.w_in];
let want_y = [d.batch, d.channels, h_out, w_out];
if args.x.shape != want_x {
return Err(Error::InvalidProblem(
"baracuda-kernels::LpPool2dPlan: x shape != [N, C, H_in, W_in]",
));
}
if args.y.shape != want_y {
return Err(Error::InvalidProblem(
"baracuda-kernels::LpPool2dPlan: y shape != [N, C, H_out, W_out]",
));
}
Ok(())
}
pub(super) fn build_lp2d_sku<T: Element>(op: PoolKind) -> KernelSku {
let math_precision = match T::KIND {
ElementKind::F64 => MathPrecision::F64,
ElementKind::F16 => MathPrecision::F16,
ElementKind::Bf16 => MathPrecision::Bf16,
_ => MathPrecision::F32,
};
let accumulator = match T::KIND {
ElementKind::F64 => ElementKind::F64,
_ => ElementKind::F32,
};
let precision_guarantee = PrecisionGuarantee {
math_precision,
accumulator,
bit_stable_on_same_hardware: false,
deterministic: true,
};
KernelSku {
category: OpCategory::Pooling,
op: op as u16,
element: T::KIND,
aux_element: None,
layout: None,
epilogue: None,
arch: ArchSku::Sm80,
backend: BackendKind::Bespoke,
precision_guarantee,
}
}