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 AdaptivePool1dDescriptor {
pub batch: i32,
pub channels: i32,
pub l_in: i32,
pub l_out: i32,
pub element: ElementKind,
}
impl AdaptivePool1dDescriptor {
pub fn new(
batch: i32,
channels: i32,
l_in: i32,
l_out: i32,
element: ElementKind,
) -> Self {
Self {
batch,
channels,
l_in,
l_out,
element,
}
}
}
pub struct AdaptivePool1dFwArgs<'a, T: Element> {
pub x: TensorRef<'a, T, 3>,
pub y: TensorMut<'a, T, 3>,
}
pub struct AdaptivePool1dBwArgs<'a, T: Element> {
pub y: TensorRef<'a, T, 3>,
pub dy: TensorRef<'a, T, 3>,
pub x: TensorRef<'a, T, 3>,
pub dx: TensorMut<'a, T, 3>,
}
pub struct AdaptiveAvgPool1dPlan<T: Element> {
desc: AdaptivePool1dDescriptor,
sku: KernelSku,
_marker: PhantomData<T>,
}
impl<T: Element> AdaptiveAvgPool1dPlan<T> {
pub fn select(
_stream: &Stream,
desc: &AdaptivePool1dDescriptor,
_pref: PlanPreference,
) -> Result<Self> {
validate_descriptor::<T>(desc)?;
let sku = build_sku::<T>(PoolKind::AdaptiveAvgPool1d);
Ok(Self {
desc: *desc,
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]
#[deprecated(
since = "0.0.1-alpha.33",
note = "Phase 16.1 uses bit-exact per-output-cell windows; no single (kernel, stride) pair applies."
)]
pub fn derived_kernel_stride(&self) -> (i32, i32) {
(0, 0)
}
pub fn run_fw(
&self,
stream: &Stream,
_workspace: Workspace<'_>,
args: AdaptivePool1dFwArgs<'_, T>,
) -> Result<()> {
check_fw_args(&self.desc, &args)?;
let stream_ptr = stream.as_raw() as *mut c_void;
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 nc = self.desc.batch * self.desc.channels;
let status = dispatch_avg_fw::<T>(
x_ptr,
y_ptr,
nc,
1, 1, 1, self.desc.l_in,
1, 1, self.desc.l_out,
stream_ptr,
);
map_status(status)
}
pub fn run_bw(
&self,
stream: &Stream,
_workspace: Workspace<'_>,
args: AdaptivePool1dBwArgs<'_, T>,
) -> Result<()> {
check_bw_args(&self.desc, &args)?;
let stream_ptr = stream.as_raw() as *mut c_void;
let dy_ptr = args.dy.data.as_raw().0 as *const c_void;
let dx_ptr = args.dx.data.as_raw().0 as *mut c_void;
let nc = self.desc.batch * self.desc.channels;
let status = dispatch_avg_bw::<T>(
dy_ptr,
dx_ptr,
nc,
1,
1, 1, self.desc.l_in,
1, 1, self.desc.l_out,
stream_ptr,
);
map_status(status)
}
}
pub(crate) fn validate_descriptor<T: Element>(
desc: &AdaptivePool1dDescriptor,
) -> Result<()> {
validate_dtype::<T>()?;
if desc.element != T::KIND {
return Err(Error::Unsupported(
"baracuda-kernels::AdaptivePool1dPlan: descriptor.element != T::KIND",
));
}
if desc.batch <= 0 || desc.channels <= 0 || desc.l_in <= 0 || desc.l_out <= 0 {
return Err(Error::InvalidProblem(
"baracuda-kernels::AdaptivePool1dPlan: extents must be > 0",
));
}
if desc.l_out > desc.l_in {
return Err(Error::InvalidProblem(
"baracuda-kernels::AdaptivePool1dPlan: l_out must be <= l_in (upsampling \
is not a pooling op)",
));
}
Ok(())
}
pub(crate) fn check_fw_args<T: Element>(
desc: &AdaptivePool1dDescriptor,
args: &AdaptivePool1dFwArgs<'_, T>,
) -> Result<()> {
let x_shape = [desc.batch, desc.channels, desc.l_in];
let y_shape = [desc.batch, desc.channels, desc.l_out];
if args.x.shape != x_shape {
return Err(Error::InvalidProblem(
"baracuda-kernels::AdaptivePool1dPlan: x shape != [N, C, L_in]",
));
}
if args.y.shape != y_shape {
return Err(Error::InvalidProblem(
"baracuda-kernels::AdaptivePool1dPlan: y shape != [N, C, L_out]",
));
}
Ok(())
}
pub(crate) fn check_bw_args<T: Element>(
desc: &AdaptivePool1dDescriptor,
args: &AdaptivePool1dBwArgs<'_, T>,
) -> Result<()> {
let x_shape = [desc.batch, desc.channels, desc.l_in];
let y_shape = [desc.batch, desc.channels, desc.l_out];
if args.x.shape != x_shape || args.dx.shape != x_shape {
return Err(Error::InvalidProblem(
"baracuda-kernels::AdaptivePool1dPlan: x/dx shape != [N, C, L_in]",
));
}
if args.y.shape != y_shape || args.dy.shape != y_shape {
return Err(Error::InvalidProblem(
"baracuda-kernels::AdaptivePool1dPlan: y/dy shape != [N, C, L_out]",
));
}
Ok(())
}
pub(crate) fn validate_dtype<T: Element>() -> Result<()> {
if !matches!(
T::KIND,
ElementKind::F32 | ElementKind::F64 | ElementKind::F16 | ElementKind::Bf16
) {
return Err(Error::Unsupported(
"baracuda-kernels::AdaptivePoolPlan: adaptive pooling supports f32 / f64 / f16 / bf16",
));
}
Ok(())
}
pub(crate) fn build_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,
}
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn dispatch_avg_fw<T: Element>(
x: *const c_void,
y: *mut c_void,
nc: i32,
spatial_rank: i32,
in_d: i32,
in_h: i32,
in_w: i32,
out_d: i32,
out_h: i32,
out_w: i32,
stream: *mut c_void,
) -> i32 {
match T::KIND {
ElementKind::F16 => unsafe {
baracuda_kernels_sys::baracuda_kernels_adaptive_avg_pool_f16_fw_run(
x, y, nc, spatial_rank, in_d, in_h, in_w, out_d, out_h, out_w, stream,
)
},
ElementKind::Bf16 => unsafe {
baracuda_kernels_sys::baracuda_kernels_adaptive_avg_pool_bf16_fw_run(
x, y, nc, spatial_rank, in_d, in_h, in_w, out_d, out_h, out_w, stream,
)
},
ElementKind::F32 => unsafe {
baracuda_kernels_sys::baracuda_kernels_adaptive_avg_pool_f32_fw_run(
x, y, nc, spatial_rank, in_d, in_h, in_w, out_d, out_h, out_w, stream,
)
},
ElementKind::F64 => unsafe {
baracuda_kernels_sys::baracuda_kernels_adaptive_avg_pool_f64_fw_run(
x, y, nc, spatial_rank, in_d, in_h, in_w, out_d, out_h, out_w, stream,
)
},
_ => 3, }
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn dispatch_avg_bw<T: Element>(
dy: *const c_void,
dx: *mut c_void,
nc: i32,
spatial_rank: i32,
in_d: i32,
in_h: i32,
in_w: i32,
out_d: i32,
out_h: i32,
out_w: i32,
stream: *mut c_void,
) -> i32 {
match T::KIND {
ElementKind::F16 => unsafe {
baracuda_kernels_sys::baracuda_kernels_adaptive_avg_pool_f16_bw_run(
dy, dx, nc, spatial_rank, in_d, in_h, in_w, out_d, out_h, out_w, stream,
)
},
ElementKind::Bf16 => unsafe {
baracuda_kernels_sys::baracuda_kernels_adaptive_avg_pool_bf16_bw_run(
dy, dx, nc, spatial_rank, in_d, in_h, in_w, out_d, out_h, out_w, stream,
)
},
ElementKind::F32 => unsafe {
baracuda_kernels_sys::baracuda_kernels_adaptive_avg_pool_f32_bw_run(
dy, dx, nc, spatial_rank, in_d, in_h, in_w, out_d, out_h, out_w, stream,
)
},
ElementKind::F64 => unsafe {
baracuda_kernels_sys::baracuda_kernels_adaptive_avg_pool_f64_bw_run(
dy, dx, nc, spatial_rank, in_d, in_h, in_w, out_d, out_h, out_w, stream,
)
},
_ => 3,
}
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn dispatch_max_fw<T: Element>(
x: *const c_void,
y: *mut c_void,
nc: i32,
spatial_rank: i32,
in_d: i32,
in_h: i32,
in_w: i32,
out_d: i32,
out_h: i32,
out_w: i32,
stream: *mut c_void,
) -> i32 {
match T::KIND {
ElementKind::F16 => unsafe {
baracuda_kernels_sys::baracuda_kernels_adaptive_max_pool_f16_fw_run(
x, y, nc, spatial_rank, in_d, in_h, in_w, out_d, out_h, out_w, stream,
)
},
ElementKind::Bf16 => unsafe {
baracuda_kernels_sys::baracuda_kernels_adaptive_max_pool_bf16_fw_run(
x, y, nc, spatial_rank, in_d, in_h, in_w, out_d, out_h, out_w, stream,
)
},
ElementKind::F32 => unsafe {
baracuda_kernels_sys::baracuda_kernels_adaptive_max_pool_f32_fw_run(
x, y, nc, spatial_rank, in_d, in_h, in_w, out_d, out_h, out_w, stream,
)
},
ElementKind::F64 => unsafe {
baracuda_kernels_sys::baracuda_kernels_adaptive_max_pool_f64_fw_run(
x, y, nc, spatial_rank, in_d, in_h, in_w, out_d, out_h, out_w, stream,
)
},
_ => 3,
}
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn dispatch_max_bw<T: Element>(
x: *const c_void,
dy: *const c_void,
dx: *mut c_void,
nc: i32,
spatial_rank: i32,
in_d: i32,
in_h: i32,
in_w: i32,
out_d: i32,
out_h: i32,
out_w: i32,
stream: *mut c_void,
) -> i32 {
match T::KIND {
ElementKind::F16 => unsafe {
baracuda_kernels_sys::baracuda_kernels_adaptive_max_pool_f16_bw_run(
x, dy, dx, nc, spatial_rank, in_d, in_h, in_w, out_d, out_h, out_w, stream,
)
},
ElementKind::Bf16 => unsafe {
baracuda_kernels_sys::baracuda_kernels_adaptive_max_pool_bf16_bw_run(
x, dy, dx, nc, spatial_rank, in_d, in_h, in_w, out_d, out_h, out_w, stream,
)
},
ElementKind::F32 => unsafe {
baracuda_kernels_sys::baracuda_kernels_adaptive_max_pool_f32_bw_run(
x, dy, dx, nc, spatial_rank, in_d, in_h, in_w, out_d, out_h, out_w, stream,
)
},
ElementKind::F64 => unsafe {
baracuda_kernels_sys::baracuda_kernels_adaptive_max_pool_f64_bw_run(
x, dy, dx, nc, spatial_rank, in_d, in_h, in_w, out_d, out_h, out_w, stream,
)
},
_ => 3,
}
}
pub(crate) fn map_status(code: i32) -> Result<()> {
match code {
0 => Ok(()),
1 => Err(Error::MisalignedOperand),
2 => Err(Error::InvalidProblem(
"baracuda-kernels-sys reported invalid problem (adaptive pool)",
)),
3 => Err(Error::Unsupported(
"baracuda-kernels-sys reported unsupported configuration (adaptive pool)",
)),
4 => Err(Error::WorkspaceTooSmall { needed: 0, got: 0 }),
n => Err(Error::CutlassInternal(n)),
}
}