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, IndexElement, IndexElementKind, IndexingKind,
KernelSku, MathPrecision, OpCategory, PlanPreference, PrecisionGuarantee, TensorMut, TensorRef,
Workspace,
};
use super::gather::map_status;
#[derive(Copy, Clone, Debug)]
pub struct NonzeroDescriptor<const N: usize> {
pub shape: [i32; N],
pub max_nz: i32,
pub element: ElementKind,
}
pub struct NonzeroArgs<'a, T: Element, const N: usize, I: IndexElement = i32> {
pub x: TensorRef<'a, T, N>,
pub out_coords: TensorMut<'a, I, 1>,
pub counter: TensorMut<'a, I, 1>,
}
pub struct NonzeroPlan<T: Element, const N: usize> {
desc: NonzeroDescriptor<N>,
sku: KernelSku,
_marker: PhantomData<T>,
}
impl<T: Element, const N: usize> NonzeroPlan<T, N> {
pub fn select(
_stream: &Stream,
desc: &NonzeroDescriptor<N>,
_pref: PlanPreference,
) -> Result<Self> {
if desc.element != T::KIND {
return Err(Error::Unsupported(
"baracuda-kernels::NonzeroPlan: descriptor element != type parameter T",
));
}
if N == 0 {
return Err(Error::InvalidProblem(
"baracuda-kernels::NonzeroPlan: rank-0 tensors not supported",
));
}
if desc.max_nz < 0 {
return Err(Error::InvalidProblem(
"baracuda-kernels::NonzeroPlan: max_nz must be non-negative",
));
}
for &d in desc.shape.iter() {
if d < 0 {
return Err(Error::InvalidProblem(
"baracuda-kernels::NonzeroPlan: shape dims must be non-negative",
));
}
}
let supported = matches!(
T::KIND,
ElementKind::F32 | ElementKind::F64 | ElementKind::I32 | ElementKind::Bool
);
if !supported {
return Err(Error::Unsupported(
"baracuda-kernels::NonzeroPlan: today only input dtypes \
`f32`, `f64`, `i32`, `bool` wired",
));
}
let precision_guarantee = PrecisionGuarantee {
math_precision: MathPrecision::F32,
accumulator: ElementKind::F32,
bit_stable_on_same_hardware: false,
deterministic: false,
};
let sku = KernelSku {
category: OpCategory::Indexing,
op: IndexingKind::Nonzero 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<I: IndexElement>(&self, args: &NonzeroArgs<'_, T, N, I>) -> Result<()> {
if args.x.shape != self.desc.shape {
return Err(Error::InvalidProblem(
"baracuda-kernels::NonzeroPlan: x shape mismatch with descriptor",
));
}
if N > 8 {
return Err(Error::Unsupported(
"baracuda-kernels::NonzeroPlan: tensor rank > 8 not supported",
));
}
let max_nz = self.desc.max_nz as i64;
let rank = N as i64;
let needed_coords = max_nz.saturating_mul(rank);
let coords_len = args.out_coords.data.len() as i64;
let counter_len = args.counter.data.len() as i64;
if coords_len < needed_coords {
return Err(Error::BufferTooSmall {
needed: needed_coords as usize,
got: coords_len as usize,
});
}
if counter_len < 1 {
return Err(Error::BufferTooSmall {
needed: 1,
got: counter_len as usize,
});
}
let x_numel = args.x.numel();
let x_len = args.x.data.len() as i64;
if x_len < x_numel {
return Err(Error::BufferTooSmall {
needed: x_numel as usize,
got: x_len as usize,
});
}
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<I: IndexElement>(
&self,
stream: &Stream,
_workspace: Workspace<'_>,
args: NonzeroArgs<'_, T, N, I>,
) -> Result<()> {
self.can_implement(&args)?;
let numel = args.x.numel();
let x_ptr = args.x.data.as_raw().0 as *const c_void;
let coords_ptr = args.out_coords.data.as_raw().0 as *mut c_void;
let counter_ptr = args.counter.data.as_raw().0 as *mut c_void;
let stream_ptr = stream.as_raw() as *mut c_void;
let shape = self.desc.shape;
let stride_x = args.x.stride;
let rank = N as i32;
let status = match (T::KIND, I::KIND) {
(ElementKind::F32, IndexElementKind::I32) => unsafe {
baracuda_kernels_sys::baracuda_kernels_nonzero_f32_run(
numel,
rank,
self.desc.max_nz,
shape.as_ptr(),
stride_x.as_ptr(),
x_ptr,
coords_ptr,
counter_ptr,
core::ptr::null_mut(),
0,
stream_ptr,
)
},
(ElementKind::F64, IndexElementKind::I32) => unsafe {
baracuda_kernels_sys::baracuda_kernels_nonzero_f64_run(
numel,
rank,
self.desc.max_nz,
shape.as_ptr(),
stride_x.as_ptr(),
x_ptr,
coords_ptr,
counter_ptr,
core::ptr::null_mut(),
0,
stream_ptr,
)
},
(ElementKind::I32, IndexElementKind::I32) => unsafe {
baracuda_kernels_sys::baracuda_kernels_nonzero_i32_run(
numel,
rank,
self.desc.max_nz,
shape.as_ptr(),
stride_x.as_ptr(),
x_ptr,
coords_ptr,
counter_ptr,
core::ptr::null_mut(),
0,
stream_ptr,
)
},
(ElementKind::Bool, IndexElementKind::I32) => unsafe {
baracuda_kernels_sys::baracuda_kernels_nonzero_bool_run(
numel,
rank,
self.desc.max_nz,
shape.as_ptr(),
stride_x.as_ptr(),
x_ptr,
coords_ptr,
counter_ptr,
core::ptr::null_mut(),
0,
stream_ptr,
)
},
(ElementKind::F32, IndexElementKind::I64) => unsafe {
baracuda_kernels_sys::baracuda_kernels_nonzero_i64idx_f32_run(
numel,
rank,
self.desc.max_nz,
shape.as_ptr(),
stride_x.as_ptr(),
x_ptr,
coords_ptr,
counter_ptr,
core::ptr::null_mut(),
0,
stream_ptr,
)
},
(ElementKind::F64, IndexElementKind::I64) => unsafe {
baracuda_kernels_sys::baracuda_kernels_nonzero_i64idx_f64_run(
numel,
rank,
self.desc.max_nz,
shape.as_ptr(),
stride_x.as_ptr(),
x_ptr,
coords_ptr,
counter_ptr,
core::ptr::null_mut(),
0,
stream_ptr,
)
},
(ElementKind::I32, IndexElementKind::I64) => unsafe {
baracuda_kernels_sys::baracuda_kernels_nonzero_i64idx_i32_run(
numel,
rank,
self.desc.max_nz,
shape.as_ptr(),
stride_x.as_ptr(),
x_ptr,
coords_ptr,
counter_ptr,
core::ptr::null_mut(),
0,
stream_ptr,
)
},
(ElementKind::Bool, IndexElementKind::I64) => unsafe {
baracuda_kernels_sys::baracuda_kernels_nonzero_i64idx_bool_run(
numel,
rank,
self.desc.max_nz,
shape.as_ptr(),
stride_x.as_ptr(),
x_ptr,
coords_ptr,
counter_ptr,
core::ptr::null_mut(),
0,
stream_ptr,
)
},
_ => {
return Err(Error::Unsupported(
"baracuda-kernels::NonzeroPlan::run reached an unimplemented dtype \
— select() should have caught this",
));
}
};
map_status(status)
}
}