use core::ffi::c_void;
use core::marker::PhantomData;
use baracuda_cutlass::{Error, Result};
use baracuda_driver::Stream;
use baracuda_kernels_types::{
Element, ElementKind, KernelSku, PlanPreference, PoolKind, PrecisionGuarantee, TensorMut,
TensorRef, Workspace,
};
use super::adaptive_avg_pool1d::{
build_sku, dispatch_avg_bw, dispatch_avg_fw, map_status, validate_dtype,
};
#[derive(Copy, Clone, Debug)]
#[non_exhaustive]
pub struct AdaptivePool2dDescriptor {
pub batch: i32,
pub channels: i32,
pub h_in: i32,
pub w_in: i32,
pub h_out: i32,
pub w_out: i32,
pub element: ElementKind,
}
impl AdaptivePool2dDescriptor {
pub fn new(
batch: i32,
channels: i32,
h_in: i32,
w_in: i32,
h_out: i32,
w_out: i32,
element: ElementKind,
) -> Self {
Self {
batch,
channels,
h_in,
w_in,
h_out,
w_out,
element,
}
}
}
pub struct AdaptivePool2dFwArgs<'a, T: Element> {
pub x: TensorRef<'a, T, 4>,
pub y: TensorMut<'a, T, 4>,
}
pub struct AdaptivePool2dBwArgs<'a, T: Element> {
pub y: TensorRef<'a, T, 4>,
pub dy: TensorRef<'a, T, 4>,
pub x: TensorRef<'a, T, 4>,
pub dx: TensorMut<'a, T, 4>,
}
pub struct AdaptiveAvgPool2dPlan<T: Element> {
desc: AdaptivePool2dDescriptor,
sku: KernelSku,
_marker: PhantomData<T>,
}
impl<T: Element> AdaptiveAvgPool2dPlan<T> {
pub fn select(
_stream: &Stream,
desc: &AdaptivePool2dDescriptor,
_pref: PlanPreference,
) -> Result<Self> {
validate_descriptor::<T>(desc)?;
let sku = build_sku::<T>(PoolKind::AdaptiveAvgPool2d);
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), (i32, i32)) {
((0, 0), (0, 0))
}
pub fn run_fw(
&self,
stream: &Stream,
_workspace: Workspace<'_>,
args: AdaptivePool2dFwArgs<'_, 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,
2, 1, self.desc.h_in, self.desc.w_in,
1, self.desc.h_out, self.desc.w_out,
stream_ptr,
);
map_status(status)
}
pub fn run_bw(
&self,
stream: &Stream,
_workspace: Workspace<'_>,
args: AdaptivePool2dBwArgs<'_, 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,
2,
1, self.desc.h_in, self.desc.w_in,
1, self.desc.h_out, self.desc.w_out,
stream_ptr,
);
map_status(status)
}
}
pub(crate) fn validate_descriptor<T: Element>(
desc: &AdaptivePool2dDescriptor,
) -> Result<()> {
validate_dtype::<T>()?;
if desc.element != T::KIND {
return Err(Error::Unsupported(
"baracuda-kernels::AdaptivePool2dPlan: descriptor.element != T::KIND",
));
}
if desc.batch <= 0
|| desc.channels <= 0
|| desc.h_in <= 0
|| desc.w_in <= 0
|| desc.h_out <= 0
|| desc.w_out <= 0
{
return Err(Error::InvalidProblem(
"baracuda-kernels::AdaptivePool2dPlan: extents must be > 0",
));
}
if desc.h_out > desc.h_in || desc.w_out > desc.w_in {
return Err(Error::InvalidProblem(
"baracuda-kernels::AdaptivePool2dPlan: output extents must be <= input \
extents (upsampling is not a pooling op)",
));
}
Ok(())
}
pub(crate) fn check_fw_args<T: Element>(
desc: &AdaptivePool2dDescriptor,
args: &AdaptivePool2dFwArgs<'_, T>,
) -> Result<()> {
let x_shape = [desc.batch, desc.channels, desc.h_in, desc.w_in];
let y_shape = [desc.batch, desc.channels, desc.h_out, desc.w_out];
if args.x.shape != x_shape {
return Err(Error::InvalidProblem(
"baracuda-kernels::AdaptivePool2dPlan: x shape != [N, C, H_in, W_in]",
));
}
if args.y.shape != y_shape {
return Err(Error::InvalidProblem(
"baracuda-kernels::AdaptivePool2dPlan: y shape != [N, C, H_out, W_out]",
));
}
Ok(())
}
pub(crate) fn check_bw_args<T: Element>(
desc: &AdaptivePool2dDescriptor,
args: &AdaptivePool2dBwArgs<'_, T>,
) -> Result<()> {
let x_shape = [desc.batch, desc.channels, desc.h_in, desc.w_in];
let y_shape = [desc.batch, desc.channels, desc.h_out, desc.w_out];
if args.x.shape != x_shape || args.dx.shape != x_shape {
return Err(Error::InvalidProblem(
"baracuda-kernels::AdaptivePool2dPlan: x/dx shape != [N, C, H_in, W_in]",
));
}
if args.y.shape != y_shape || args.dy.shape != y_shape {
return Err(Error::InvalidProblem(
"baracuda-kernels::AdaptivePool2dPlan: y/dy shape != [N, C, H_out, W_out]",
));
}
Ok(())
}