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 LpPool1dDescriptor {
pub batch: i32,
pub channels: i32,
pub l_in: i32,
pub window: i32,
pub stride: i32,
pub p: f32,
pub ceil_mode: bool,
pub element: ElementKind,
}
impl LpPool1dDescriptor {
pub fn new(
batch: i32,
channels: i32,
l_in: i32,
window: i32,
p: f32,
element: ElementKind,
) -> Self {
Self {
batch,
channels,
l_in,
window,
stride: window,
p,
ceil_mode: false,
element,
}
}
#[inline]
pub fn with_stride(mut self, stride: i32) -> Self {
self.stride = stride;
self
}
#[inline]
pub fn with_ceil_mode(mut self, ceil_mode: bool) -> Self {
self.ceil_mode = ceil_mode;
self
}
}
pub struct LpPool1dFwArgs<'a, T: Element> {
pub x: TensorRef<'a, T, 3>,
pub y: TensorMut<'a, T, 3>,
}
pub struct LpPool1dPlan<T: Element> {
pub(super) desc: LpPool1dDescriptor,
pub(super) l_out: i32,
sku: KernelSku,
_marker: PhantomData<T>,
}
impl<T: Element> LpPool1dPlan<T> {
pub fn select(
_stream: &Stream,
desc: &LpPool1dDescriptor,
_pref: PlanPreference,
) -> Result<Self> {
validate_lp1d::<T>(desc)?;
let l_out = compute_l_out(desc)?;
let sku = build_lp1d_sku::<T>(PoolKind::LpPool1d);
Ok(Self {
desc: *desc,
l_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_length(&self) -> i32 {
self.l_out
}
pub fn run_fw(
&self,
stream: &Stream,
_workspace: Workspace<'_>,
args: LpPool1dFwArgs<'_, T>,
) -> Result<()> {
check_fw_args_lp1d(&self.desc, self.l_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_1d_f32_run(
x_ptr, y_ptr, self.desc.batch, self.desc.channels, self.desc.l_in,
self.desc.window, self.desc.stride, self.l_out,
self.desc.p, ceil_flag, stream_ptr,
)
},
ElementKind::F64 => unsafe {
baracuda_kernels_sys::baracuda_kernels_lp_pool_1d_f64_run(
x_ptr, y_ptr, self.desc.batch, self.desc.channels, self.desc.l_in,
self.desc.window, self.desc.stride, self.l_out,
self.desc.p, ceil_flag, stream_ptr,
)
},
ElementKind::F16 => unsafe {
baracuda_kernels_sys::baracuda_kernels_lp_pool_1d_f16_run(
x_ptr, y_ptr, self.desc.batch, self.desc.channels, self.desc.l_in,
self.desc.window, self.desc.stride, self.l_out,
self.desc.p, ceil_flag, stream_ptr,
)
},
ElementKind::Bf16 => unsafe {
baracuda_kernels_sys::baracuda_kernels_lp_pool_1d_bf16_run(
x_ptr, y_ptr, self.desc.batch, self.desc.channels, self.desc.l_in,
self.desc.window, self.desc.stride, self.l_out,
self.desc.p, ceil_flag, stream_ptr,
)
},
_ => {
return Err(Error::Unsupported(
"baracuda-kernels::LpPool1dPlan: unexpected dtype after select()",
));
}
};
super::map_lp_pool_status(status)
}
}
pub(super) fn validate_lp1d<T: Element>(d: &LpPool1dDescriptor) -> Result<()> {
if d.element != T::KIND {
return Err(Error::Unsupported(
"baracuda-kernels::LpPool1dPlan: descriptor.element != T::KIND",
));
}
if !matches!(
T::KIND,
ElementKind::F32 | ElementKind::F64 | ElementKind::F16 | ElementKind::Bf16
) {
return Err(Error::Unsupported(
"baracuda-kernels::LpPool1dPlan: dtype must be f32 / f64 / f16 / bf16",
));
}
if d.batch <= 0 || d.channels <= 0 || d.l_in <= 0 {
return Err(Error::InvalidProblem(
"baracuda-kernels::LpPool1dPlan: batch/channels/l_in must be > 0",
));
}
if d.window <= 0 || d.stride <= 0 {
return Err(Error::InvalidProblem(
"baracuda-kernels::LpPool1dPlan: window/stride must be > 0",
));
}
if d.window > d.l_in {
return Err(Error::InvalidProblem(
"baracuda-kernels::LpPool1dPlan: window > l_in produces zero-sized output",
));
}
if !d.p.is_finite() || d.p <= 0.0 {
return Err(Error::Unsupported(
"baracuda-kernels::LpPool1dPlan: p must be finite and > 0 \
(use MaxPool1dPlan for the p=∞ case)",
));
}
Ok(())
}
pub(super) fn compute_l_out(d: &LpPool1dDescriptor) -> Result<i32> {
let diff = d.l_in - d.window;
let out = if d.ceil_mode {
(diff + d.stride - 1) / d.stride + 1
} else {
diff / d.stride + 1
};
if out <= 0 {
return Err(Error::InvalidProblem(
"baracuda-kernels::LpPool1dPlan: computed l_out <= 0",
));
}
Ok(out)
}
pub(super) fn check_fw_args_lp1d<T: Element>(
d: &LpPool1dDescriptor,
l_out: i32,
args: &LpPool1dFwArgs<'_, T>,
) -> Result<()> {
let want_x = [d.batch, d.channels, d.l_in];
let want_y = [d.batch, d.channels, l_out];
if args.x.shape != want_x {
return Err(Error::InvalidProblem(
"baracuda-kernels::LpPool1dPlan: x shape != [N, C, L_in]",
));
}
if args.y.shape != want_y {
return Err(Error::InvalidProblem(
"baracuda-kernels::LpPool1dPlan: y shape != [N, C, L_out]",
));
}
Ok(())
}
pub(super) fn build_lp1d_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,
}
}