use core::ffi::c_void;
use core::marker::PhantomData;
use baracuda_cutlass::{Error, Result};
use baracuda_driver::Stream;
use baracuda_kernels_types::{
Element, ElementKind, IntElement, KernelSku, PlanPreference, PrecisionGuarantee, QuantizeKind,
TensorMut, TensorRef, Workspace,
};
use super::map_status;
use super::per_group::build_sku_group;
use super::{validate_input_element, validate_output_element};
#[derive(Copy, Clone, Debug)]
pub struct DequantizePerGroupDescriptor {
pub outer_size: i32,
pub axis_size: i32,
pub group_size: i32,
pub input_element: ElementKind,
pub output_element: ElementKind,
}
impl DequantizePerGroupDescriptor {
#[inline]
pub fn num_groups(&self) -> i32 {
if self.group_size <= 0 {
0
} else {
self.axis_size / self.group_size
}
}
}
pub struct DequantizePerGroupArgs<'a, TIn: Element, TOut: IntElement> {
pub input: TensorRef<'a, TOut, 2>,
pub scale: TensorRef<'a, TIn, 2>,
pub zero_point: TensorRef<'a, i32, 2>,
pub output: TensorMut<'a, TIn, 2>,
}
pub struct DequantizePerGroupPlan<TIn: Element, TOut: IntElement> {
desc: DequantizePerGroupDescriptor,
sku: KernelSku,
_marker: PhantomData<(TIn, TOut)>,
}
impl<TIn: Element, TOut: IntElement> DequantizePerGroupPlan<TIn, TOut> {
pub fn select(
_stream: &Stream,
desc: &DequantizePerGroupDescriptor,
_pref: PlanPreference,
) -> Result<Self> {
if desc.input_element != TIn::KIND {
return Err(Error::Unsupported(
"DequantizePerGroupPlan: descriptor input_element != TIn",
));
}
if desc.output_element != TOut::KIND {
return Err(Error::Unsupported(
"DequantizePerGroupPlan: descriptor output_element != TOut",
));
}
validate_input_element(TIn::KIND, "DequantizePerGroupPlan: unsupported TIn dtype")?;
validate_output_element(TOut::KIND, "DequantizePerGroupPlan: unsupported TOut dtype")?;
if desc.outer_size < 0 || desc.axis_size < 0 {
return Err(Error::InvalidProblem(
"DequantizePerGroupPlan: outer_size and axis_size must be non-negative",
));
}
if desc.group_size <= 0 {
return Err(Error::InvalidProblem(
"DequantizePerGroupPlan: group_size must be > 0",
));
}
if desc.axis_size % desc.group_size != 0 {
return Err(Error::InvalidProblem(
"DequantizePerGroupPlan: axis_size must be a multiple of group_size",
));
}
let sku = build_sku_group::<TIn, TOut>(QuantizeKind::DequantizePerGroup);
Ok(Self {
desc: *desc,
sku,
_marker: PhantomData,
})
}
pub fn can_implement(&self, args: &DequantizePerGroupArgs<'_, TIn, TOut>) -> Result<()> {
let expect_io = [self.desc.outer_size, self.desc.axis_size];
if args.input.shape != expect_io || args.output.shape != expect_io {
return Err(Error::InvalidProblem(
"DequantizePerGroupPlan: I/O tensor shape != [outer, axis_size]",
));
}
let expect_sg = [self.desc.outer_size, self.desc.num_groups()];
if args.scale.shape != expect_sg || args.zero_point.shape != expect_sg {
return Err(Error::InvalidProblem(
"DequantizePerGroupPlan: scale / zp shape != [outer, num_groups]",
));
}
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: DequantizePerGroupArgs<'_, TIn, TOut>,
) -> Result<()> {
self.can_implement(&args)?;
let total = (self.desc.outer_size as i64) * (self.desc.axis_size as i64);
if total == 0 {
return Ok(());
}
let in_ptr = args.input.data.as_raw().0 as *const c_void;
let sc_ptr = args.scale.data.as_raw().0 as *const c_void;
let zp_ptr = args.zero_point.data.as_raw().0 as *const c_void;
let out_ptr = args.output.data.as_raw().0 as *mut c_void;
let stream_ptr = stream.as_raw() as *mut c_void;
let (outer, axis, g) = (
self.desc.outer_size,
self.desc.axis_size,
self.desc.group_size,
);
let status = match (TIn::KIND, TOut::KIND) {
(ElementKind::F32, ElementKind::S8) => unsafe {
baracuda_kernels_sys::baracuda_kernels_dequantize_per_group_f32_s8_run(
outer, axis, g, in_ptr, sc_ptr, zp_ptr, out_ptr,
core::ptr::null_mut(), 0, stream_ptr,
)
},
(ElementKind::F32, ElementKind::U8) => unsafe {
baracuda_kernels_sys::baracuda_kernels_dequantize_per_group_f32_u8_run(
outer, axis, g, in_ptr, sc_ptr, zp_ptr, out_ptr,
core::ptr::null_mut(), 0, stream_ptr,
)
},
(ElementKind::F64, ElementKind::S8) => unsafe {
baracuda_kernels_sys::baracuda_kernels_dequantize_per_group_f64_s8_run(
outer, axis, g, in_ptr, sc_ptr, zp_ptr, out_ptr,
core::ptr::null_mut(), 0, stream_ptr,
)
},
(ElementKind::F64, ElementKind::U8) => unsafe {
baracuda_kernels_sys::baracuda_kernels_dequantize_per_group_f64_u8_run(
outer, axis, g, in_ptr, sc_ptr, zp_ptr, out_ptr,
core::ptr::null_mut(), 0, stream_ptr,
)
},
(ElementKind::F16, ElementKind::S8) => unsafe {
baracuda_kernels_sys::baracuda_kernels_dequantize_per_group_f16_s8_run(
outer, axis, g, in_ptr, sc_ptr, zp_ptr, out_ptr,
core::ptr::null_mut(), 0, stream_ptr,
)
},
(ElementKind::F16, ElementKind::U8) => unsafe {
baracuda_kernels_sys::baracuda_kernels_dequantize_per_group_f16_u8_run(
outer, axis, g, in_ptr, sc_ptr, zp_ptr, out_ptr,
core::ptr::null_mut(), 0, stream_ptr,
)
},
(ElementKind::Bf16, ElementKind::S8) => unsafe {
baracuda_kernels_sys::baracuda_kernels_dequantize_per_group_bf16_s8_run(
outer, axis, g, in_ptr, sc_ptr, zp_ptr, out_ptr,
core::ptr::null_mut(), 0, stream_ptr,
)
},
(ElementKind::Bf16, ElementKind::U8) => unsafe {
baracuda_kernels_sys::baracuda_kernels_dequantize_per_group_bf16_u8_run(
outer, axis, g, in_ptr, sc_ptr, zp_ptr, out_ptr,
core::ptr::null_mut(), 0, stream_ptr,
)
},
_ => {
return Err(Error::Unsupported(
"DequantizePerGroupPlan::run unsupported (TIn, TOut)",
))
}
};
map_status(status)
}
}