use core::ffi::c_void;
use core::marker::PhantomData;
use baracuda_cutlass::{Error, Result};
use baracuda_driver::Stream;
use baracuda_kernels_sys::{
baracuda_kernels_fractional_max_pool_2d_bw_bf16_run,
baracuda_kernels_fractional_max_pool_2d_bw_f16_run,
baracuda_kernels_fractional_max_pool_2d_bw_f32_run,
baracuda_kernels_fractional_max_pool_2d_bw_f64_run,
baracuda_kernels_fractional_max_pool_2d_fw_bf16_run,
baracuda_kernels_fractional_max_pool_2d_fw_f16_run,
baracuda_kernels_fractional_max_pool_2d_fw_f32_run,
baracuda_kernels_fractional_max_pool_2d_fw_f64_run,
};
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 FractionalMaxPool2dDescriptor {
pub batch: i32,
pub channels: i32,
pub h_in: i32,
pub w_in: i32,
pub window_h: i32,
pub window_w: i32,
pub h_out: i32,
pub w_out: i32,
pub seed: u64,
pub element: ElementKind,
}
impl FractionalMaxPool2dDescriptor {
#[allow(clippy::too_many_arguments)]
pub fn new(
batch: i32,
channels: i32,
h_in: i32,
w_in: i32,
window_h: i32,
window_w: i32,
h_out: i32,
w_out: i32,
element: ElementKind,
) -> Self {
Self {
batch,
channels,
h_in,
w_in,
window_h,
window_w,
h_out,
w_out,
seed: 0,
element,
}
}
#[inline]
pub fn with_seed(mut self, seed: u64) -> Self {
self.seed = seed;
self
}
}
pub struct FractionalMaxPool2dFwArgs<'a, T: Element> {
pub x: TensorRef<'a, T, 4>,
pub y: TensorMut<'a, T, 4>,
pub indices: TensorMut<'a, i64, 4>,
pub random_samples: TensorRef<'a, f32, 3>,
}
pub struct FractionalMaxPool2dBwArgs<'a, T: Element> {
pub dy: TensorRef<'a, T, 4>,
pub indices: TensorRef<'a, i64, 4>,
pub dx: TensorMut<'a, T, 4>,
}
pub struct FractionalMaxPool2dPlan<T: Element> {
desc: FractionalMaxPool2dDescriptor,
sku: KernelSku,
_marker: PhantomData<T>,
}
impl<T: Element> FractionalMaxPool2dPlan<T> {
pub fn select(
_stream: &Stream,
desc: &FractionalMaxPool2dDescriptor,
_pref: PlanPreference,
) -> Result<Self> {
validate_descriptor::<T>(desc)?;
let sku = build_sku::<T>(PoolKind::FractionalMaxPool2d, true);
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
}
pub fn run_fw(
&self,
stream: &Stream,
_workspace: Workspace<'_>,
args: FractionalMaxPool2dFwArgs<'_, T>,
) -> Result<()> {
check_fw_args(&self.desc, &args)?;
let stream_ptr = stream.as_raw() as *mut c_void;
let x = args.x.data.as_raw().0 as *const c_void;
let y = args.y.data.as_raw().0 as *mut c_void;
let indices = args.indices.data.as_raw().0 as *mut c_void;
let rs = args.random_samples.data.as_raw().0 as *const f32;
let status = unsafe {
match T::KIND {
ElementKind::F32 => baracuda_kernels_fractional_max_pool_2d_fw_f32_run(
x, y, indices, rs,
self.desc.batch, self.desc.channels,
self.desc.h_in, self.desc.w_in,
self.desc.h_out, self.desc.w_out,
self.desc.window_h, self.desc.window_w,
stream_ptr,
),
ElementKind::F64 => baracuda_kernels_fractional_max_pool_2d_fw_f64_run(
x, y, indices, rs,
self.desc.batch, self.desc.channels,
self.desc.h_in, self.desc.w_in,
self.desc.h_out, self.desc.w_out,
self.desc.window_h, self.desc.window_w,
stream_ptr,
),
ElementKind::F16 => baracuda_kernels_fractional_max_pool_2d_fw_f16_run(
x, y, indices, rs,
self.desc.batch, self.desc.channels,
self.desc.h_in, self.desc.w_in,
self.desc.h_out, self.desc.w_out,
self.desc.window_h, self.desc.window_w,
stream_ptr,
),
ElementKind::Bf16 => baracuda_kernels_fractional_max_pool_2d_fw_bf16_run(
x, y, indices, rs,
self.desc.batch, self.desc.channels,
self.desc.h_in, self.desc.w_in,
self.desc.h_out, self.desc.w_out,
self.desc.window_h, self.desc.window_w,
stream_ptr,
),
_ => return Err(Error::Unsupported(
"baracuda-kernels::FractionalMaxPool2dPlan: dtype not in {f16, bf16, f32, f64}",
)),
}
};
ffi_status(status)
}
pub fn run_bw(
&self,
stream: &Stream,
_workspace: Workspace<'_>,
args: FractionalMaxPool2dBwArgs<'_, T>,
) -> Result<()> {
check_bw_args(&self.desc, &args)?;
let stream_ptr = stream.as_raw() as *mut c_void;
let dy = args.dy.data.as_raw().0 as *const c_void;
let indices = args.indices.data.as_raw().0 as *const c_void;
let dx = args.dx.data.as_raw().0 as *mut c_void;
let status = unsafe {
match T::KIND {
ElementKind::F32 => baracuda_kernels_fractional_max_pool_2d_bw_f32_run(
dy, indices, dx,
self.desc.batch, self.desc.channels,
self.desc.h_in, self.desc.w_in,
self.desc.h_out, self.desc.w_out,
stream_ptr,
),
ElementKind::F64 => baracuda_kernels_fractional_max_pool_2d_bw_f64_run(
dy, indices, dx,
self.desc.batch, self.desc.channels,
self.desc.h_in, self.desc.w_in,
self.desc.h_out, self.desc.w_out,
stream_ptr,
),
ElementKind::F16 => baracuda_kernels_fractional_max_pool_2d_bw_f16_run(
dy, indices, dx,
self.desc.batch, self.desc.channels,
self.desc.h_in, self.desc.w_in,
self.desc.h_out, self.desc.w_out,
stream_ptr,
),
ElementKind::Bf16 => baracuda_kernels_fractional_max_pool_2d_bw_bf16_run(
dy, indices, dx,
self.desc.batch, self.desc.channels,
self.desc.h_in, self.desc.w_in,
self.desc.h_out, self.desc.w_out,
stream_ptr,
),
_ => return Err(Error::Unsupported(
"baracuda-kernels::FractionalMaxPool2dPlan: dtype not in {f16, bf16, f32, f64}",
)),
}
};
ffi_status(status)
}
}
pub(crate) fn validate_descriptor<T: Element>(
desc: &FractionalMaxPool2dDescriptor,
) -> Result<()> {
if desc.element != T::KIND {
return Err(Error::Unsupported(
"baracuda-kernels::FractionalMaxPool2dPlan: descriptor.element != T::KIND",
));
}
if !matches!(
T::KIND,
ElementKind::F32 | ElementKind::F64 | ElementKind::F16 | ElementKind::Bf16
) {
return Err(Error::Unsupported(
"baracuda-kernels::FractionalMaxPool2dPlan: dtype not in {f16, bf16, f32, f64}",
));
}
if desc.batch <= 0 || desc.channels <= 0 {
return Err(Error::InvalidProblem(
"baracuda-kernels::FractionalMaxPool2dPlan: batch / channels must be > 0",
));
}
if desc.h_in <= 0 || desc.w_in <= 0 {
return Err(Error::InvalidProblem(
"baracuda-kernels::FractionalMaxPool2dPlan: h_in / w_in must be > 0",
));
}
if desc.h_out <= 0 || desc.w_out <= 0 {
return Err(Error::InvalidProblem(
"baracuda-kernels::FractionalMaxPool2dPlan: h_out / w_out must be > 0",
));
}
if desc.window_h <= 0 || desc.window_w <= 0 {
return Err(Error::InvalidProblem(
"baracuda-kernels::FractionalMaxPool2dPlan: window extents must be > 0",
));
}
if desc.window_h > desc.h_in || desc.window_w > desc.w_in {
return Err(Error::InvalidProblem(
"baracuda-kernels::FractionalMaxPool2dPlan: window must fit within input",
));
}
Ok(())
}
pub(crate) fn build_sku<T: Element>(op: PoolKind, deterministic_fw_only: bool) -> 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: deterministic_fw_only,
};
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,
}
}
fn check_fw_args<T: Element>(
desc: &FractionalMaxPool2dDescriptor,
args: &FractionalMaxPool2dFwArgs<'_, 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::FractionalMaxPool2dPlan: x shape != [N, C, H_in, W_in]",
));
}
if args.y.shape != y_shape {
return Err(Error::InvalidProblem(
"baracuda-kernels::FractionalMaxPool2dPlan: y shape != [N, C, H_out, W_out]",
));
}
if args.indices.shape != y_shape {
return Err(Error::InvalidProblem(
"baracuda-kernels::FractionalMaxPool2dPlan: indices shape != [N, C, H_out, W_out]",
));
}
if args.random_samples.shape != [desc.batch, desc.channels, 2] {
return Err(Error::InvalidProblem(
"baracuda-kernels::FractionalMaxPool2dPlan: random_samples shape != [N, C, 2]",
));
}
Ok(())
}
fn check_bw_args<T: Element>(
desc: &FractionalMaxPool2dDescriptor,
args: &FractionalMaxPool2dBwArgs<'_, 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.dy.shape != y_shape {
return Err(Error::InvalidProblem(
"baracuda-kernels::FractionalMaxPool2dPlan: dy shape != [N, C, H_out, W_out]",
));
}
if args.indices.shape != y_shape {
return Err(Error::InvalidProblem(
"baracuda-kernels::FractionalMaxPool2dPlan: indices shape != [N, C, H_out, W_out]",
));
}
if args.dx.shape != x_shape {
return Err(Error::InvalidProblem(
"baracuda-kernels::FractionalMaxPool2dPlan: dx shape != [N, C, H_in, W_in]",
));
}
Ok(())
}
pub(crate) fn ffi_status(status: i32) -> Result<()> {
match status {
0 => Ok(()),
2 => Err(Error::InvalidProblem(
"baracuda-kernels::FractionalMaxPool*dPlan: invalid problem (ffi status 2)",
)),
_ => Err(Error::CutlassInternal(-status)),
}
}