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, ImageKind, KernelSku, MathPrecision, OpCategory,
PlanPreference, PrecisionGuarantee, TensorMut, TensorRef, Workspace,
};
use super::map_status;
#[derive(Copy, Clone, Debug)]
pub struct RoiPoolDescriptor {
pub n: i32,
pub c: i32,
pub h: i32,
pub w: i32,
pub num_rois: i32,
pub pooled_h: i32,
pub pooled_w: i32,
pub spatial_scale: f32,
pub element: ElementKind,
}
pub struct RoiPoolArgs<'a, T: Element> {
pub input: TensorRef<'a, T, 4>,
pub rois: TensorRef<'a, T, 2>,
pub output: TensorMut<'a, T, 4>,
pub argmax: TensorMut<'a, i32, 4>,
}
pub struct RoiPoolPlan<T: Element> {
desc: RoiPoolDescriptor,
sku: KernelSku,
_marker: PhantomData<T>,
}
impl<T: Element> RoiPoolPlan<T> {
pub fn select(
_stream: &Stream,
desc: &RoiPoolDescriptor,
_pref: PlanPreference,
) -> Result<Self> {
if desc.element != T::KIND {
return Err(Error::Unsupported(
"baracuda-kernels::RoiPoolPlan: descriptor element != T",
));
}
if desc.n < 0
|| desc.c < 0
|| desc.h < 0
|| desc.w < 0
|| desc.num_rois < 0
|| desc.pooled_h < 0
|| desc.pooled_w < 0
{
return Err(Error::InvalidProblem(
"baracuda-kernels::RoiPoolPlan: extents must be non-negative",
));
}
if !matches!(T::KIND, ElementKind::F32 | ElementKind::F64) {
return Err(Error::Unsupported(
"baracuda-kernels::RoiPoolPlan: only `f32`, `f64` wired",
));
}
let precision_guarantee = PrecisionGuarantee {
math_precision: if T::KIND == ElementKind::F64 {
MathPrecision::F64
} else {
MathPrecision::F32
},
accumulator: T::KIND,
bit_stable_on_same_hardware: true,
deterministic: true,
};
let sku = KernelSku {
category: OpCategory::Image,
op: ImageKind::RoiPool as u16,
element: T::KIND,
aux_element: Some(ElementKind::I32),
layout: None,
epilogue: None,
arch: ArchSku::Sm80,
backend: BackendKind::Bespoke,
precision_guarantee,
};
Ok(Self {
desc: *desc,
sku,
_marker: PhantomData,
})
}
pub fn can_implement(&self, args: &RoiPoolArgs<'_, T>) -> Result<()> {
if args.input.shape != [self.desc.n, self.desc.c, self.desc.h, self.desc.w] {
return Err(Error::InvalidProblem(
"baracuda-kernels::RoiPoolPlan: input shape mismatch",
));
}
if args.rois.shape != [self.desc.num_rois, 5] {
return Err(Error::InvalidProblem(
"baracuda-kernels::RoiPoolPlan: rois must be [num_rois, 5]",
));
}
let out_shape = [self.desc.num_rois, self.desc.c, self.desc.pooled_h, self.desc.pooled_w];
if args.output.shape != out_shape || args.argmax.shape != out_shape {
return Err(Error::InvalidProblem(
"baracuda-kernels::RoiPoolPlan: output / argmax shape mismatch",
));
}
Ok(())
}
#[inline]
pub fn workspace_size(&self) -> usize {
0
}
#[inline]
pub fn sku(&self) -> KernelSku {
self.sku
}
#[inline]
pub fn precision_guarantee(&self) -> PrecisionGuarantee {
self.sku.precision_guarantee
}
pub fn run(
&self,
stream: &Stream,
_workspace: Workspace<'_>,
args: RoiPoolArgs<'_, T>,
) -> Result<()> {
self.can_implement(&args)?;
if args.output.numel() == 0 {
return Ok(());
}
let input_ptr = args.input.data.as_raw().0 as *const c_void;
let rois_ptr = args.rois.data.as_raw().0 as *const c_void;
let out_ptr = args.output.data.as_raw().0 as *mut c_void;
let arg_ptr = args.argmax.data.as_raw().0 as *mut c_void;
let stream_ptr = stream.as_raw() as *mut c_void;
let status = match T::KIND {
ElementKind::F32 => unsafe {
baracuda_kernels_sys::baracuda_kernels_roi_pool_f32_run(
self.desc.n, self.desc.c, self.desc.h, self.desc.w,
self.desc.num_rois, self.desc.pooled_h, self.desc.pooled_w,
self.desc.spatial_scale,
input_ptr, rois_ptr, out_ptr, arg_ptr,
core::ptr::null_mut(), 0, stream_ptr,
)
},
ElementKind::F64 => unsafe {
baracuda_kernels_sys::baracuda_kernels_roi_pool_f64_run(
self.desc.n, self.desc.c, self.desc.h, self.desc.w,
self.desc.num_rois, self.desc.pooled_h, self.desc.pooled_w,
self.desc.spatial_scale,
input_ptr, rois_ptr, out_ptr, arg_ptr,
core::ptr::null_mut(), 0, stream_ptr,
)
},
_ => {
return Err(Error::Unsupported(
"baracuda-kernels::RoiPoolPlan::run reached unimplemented dtype",
));
}
};
map_status(status)
}
}