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, KernelSku, LossKind, LossReduction, MathPrecision,
OpCategory, PlanPreference, PrecisionGuarantee, TensorMut, TensorRef, Workspace,
};
use super::common::{check_supported_dtype, map_status, unpack_workspace, validate_shape};
#[derive(Copy, Clone, Debug)]
pub struct L1LossDescriptor<const N: usize> {
pub input_shape: [i32; N],
pub reduction: LossReduction,
pub element: ElementKind,
}
pub struct L1LossArgs<'a, T: Element, const N: usize> {
pub pred: TensorRef<'a, T, N>,
pub target: TensorRef<'a, T, N>,
pub out: TensorMut<'a, T, N>,
}
pub struct L1LossPlan<T: Element, const N: usize> {
desc: L1LossDescriptor<N>,
sku: KernelSku,
_marker: PhantomData<T>,
}
impl<T: Element, const N: usize> L1LossPlan<T, N> {
pub fn select(
_stream: &Stream,
desc: &L1LossDescriptor<N>,
_pref: PlanPreference,
) -> Result<Self> {
if desc.element != T::KIND {
return Err(Error::Unsupported(
"baracuda-kernels::L1LossPlan: descriptor element != T",
));
}
check_supported_dtype::<T>()?;
validate_shape(&desc.input_shape, N)?;
let precision_guarantee = PrecisionGuarantee {
math_precision: MathPrecision::F32,
accumulator: if T::KIND == ElementKind::F64 {
ElementKind::F64
} else {
ElementKind::F32
},
bit_stable_on_same_hardware: true,
deterministic: true,
};
let sku = KernelSku {
category: OpCategory::Loss,
op: LossKind::L1 as u16,
element: T::KIND,
aux_element: None,
layout: None,
epilogue: None,
arch: ArchSku::Sm80,
backend: BackendKind::Bespoke,
precision_guarantee,
};
Ok(Self {
desc: *desc,
sku,
_marker: PhantomData,
})
}
#[inline]
pub fn workspace_size(&self) -> usize {
match self.desc.reduction {
LossReduction::None => 0,
LossReduction::Mean | LossReduction::Sum => {
let mut numel: i64 = 1;
for &d in self.desc.input_shape.iter() {
numel = numel.saturating_mul(d as i64);
}
(numel as usize).saturating_mul(core::mem::size_of::<T>())
}
}
}
#[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: L1LossArgs<'_, T, N>,
) -> Result<()> {
if args.pred.shape != self.desc.input_shape || args.target.shape != self.desc.input_shape {
return Err(Error::InvalidProblem(
"baracuda-kernels::L1LossPlan: pred / target shape mismatch",
));
}
let numel = args.pred.numel();
if numel == 0 {
return Ok(());
}
let (ws_ptr, ws_bytes) = unpack_workspace(workspace, self.workspace_size())?;
let stream_ptr = stream.as_raw() as *mut c_void;
let pred_ptr = args.pred.data.as_raw().0 as *const c_void;
let target_ptr = args.target.data.as_raw().0 as *const c_void;
let out_ptr = args.out.data.as_raw().0 as *mut c_void;
let mode = self.desc.reduction as i32;
let status = match T::KIND {
ElementKind::F32 => unsafe {
baracuda_kernels_sys::baracuda_kernels_loss_l1_f32_run(
numel, mode, pred_ptr, target_ptr, out_ptr, ws_ptr, ws_bytes, stream_ptr,
)
},
ElementKind::F16 => unsafe {
baracuda_kernels_sys::baracuda_kernels_loss_l1_f16_run(
numel, mode, pred_ptr, target_ptr, out_ptr, ws_ptr, ws_bytes, stream_ptr,
)
},
ElementKind::Bf16 => unsafe {
baracuda_kernels_sys::baracuda_kernels_loss_l1_bf16_run(
numel, mode, pred_ptr, target_ptr, out_ptr, ws_ptr, ws_bytes, stream_ptr,
)
},
ElementKind::F64 => unsafe {
baracuda_kernels_sys::baracuda_kernels_loss_l1_f64_run(
numel, mode, pred_ptr, target_ptr, out_ptr, ws_ptr, ws_bytes, stream_ptr,
)
},
_ => {
return Err(Error::Unsupported(
"baracuda-kernels::L1LossPlan::run unwired dtype",
));
}
};
map_status(status)
}
}
#[derive(Copy, Clone, Debug)]
pub struct L1LossBackwardDescriptor<const N: usize> {
pub input_shape: [i32; N],
pub reduction: LossReduction,
pub element: ElementKind,
}
pub struct L1LossBackwardArgs<'a, T: Element, const N: usize> {
pub pred: TensorRef<'a, T, N>,
pub target: TensorRef<'a, T, N>,
pub dy: TensorRef<'a, T, N>,
pub dpred: TensorMut<'a, T, N>,
}
pub struct L1LossBackwardPlan<T: Element, const N: usize> {
desc: L1LossBackwardDescriptor<N>,
sku: KernelSku,
_marker: PhantomData<T>,
}
impl<T: Element, const N: usize> L1LossBackwardPlan<T, N> {
pub fn select(
_stream: &Stream,
desc: &L1LossBackwardDescriptor<N>,
_pref: PlanPreference,
) -> Result<Self> {
if desc.element != T::KIND {
return Err(Error::Unsupported(
"baracuda-kernels::L1LossBackwardPlan: descriptor element != T",
));
}
check_supported_dtype::<T>()?;
validate_shape(&desc.input_shape, N)?;
let precision_guarantee = PrecisionGuarantee {
math_precision: MathPrecision::F32,
accumulator: if T::KIND == ElementKind::F64 {
ElementKind::F64
} else {
ElementKind::F32
},
bit_stable_on_same_hardware: true,
deterministic: true,
};
let sku = KernelSku {
category: OpCategory::Loss,
op: LossKind::L1 as u16,
element: T::KIND,
aux_element: None,
layout: None,
epilogue: None,
arch: ArchSku::Sm80,
backend: BackendKind::Bespoke,
precision_guarantee,
};
Ok(Self {
desc: *desc,
sku,
_marker: PhantomData,
})
}
#[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: L1LossBackwardArgs<'_, T, N>,
) -> Result<()> {
if args.pred.shape != self.desc.input_shape
|| args.target.shape != self.desc.input_shape
|| args.dpred.shape != self.desc.input_shape
{
return Err(Error::InvalidProblem(
"baracuda-kernels::L1LossBackwardPlan: shape mismatch",
));
}
let numel = args.pred.numel();
if numel == 0 {
return Ok(());
}
let mode = self.desc.reduction as i32;
let inv_n_or_one: f32 = match self.desc.reduction {
LossReduction::None => 0.0,
LossReduction::Mean => 1.0 / (numel as f32),
LossReduction::Sum => 1.0,
};
let stream_ptr = stream.as_raw() as *mut c_void;
let pred_ptr = args.pred.data.as_raw().0 as *const c_void;
let target_ptr = args.target.data.as_raw().0 as *const c_void;
let dy_ptr = args.dy.data.as_raw().0 as *const c_void;
let dpred_ptr = args.dpred.data.as_raw().0 as *mut c_void;
let status = match T::KIND {
ElementKind::F32 => unsafe {
baracuda_kernels_sys::baracuda_kernels_loss_l1_backward_f32_run(
numel, mode, inv_n_or_one, pred_ptr, target_ptr, dy_ptr, dpred_ptr,
core::ptr::null_mut(), 0, stream_ptr,
)
},
ElementKind::F16 => unsafe {
baracuda_kernels_sys::baracuda_kernels_loss_l1_backward_f16_run(
numel, mode, inv_n_or_one, pred_ptr, target_ptr, dy_ptr, dpred_ptr,
core::ptr::null_mut(), 0, stream_ptr,
)
},
ElementKind::Bf16 => unsafe {
baracuda_kernels_sys::baracuda_kernels_loss_l1_backward_bf16_run(
numel, mode, inv_n_or_one, pred_ptr, target_ptr, dy_ptr, dpred_ptr,
core::ptr::null_mut(), 0, stream_ptr,
)
},
ElementKind::F64 => unsafe {
baracuda_kernels_sys::baracuda_kernels_loss_l1_backward_f64_run(
numel, mode, inv_n_or_one, pred_ptr, target_ptr, dy_ptr, dpred_ptr,
core::ptr::null_mut(), 0, stream_ptr,
)
},
_ => {
return Err(Error::Unsupported(
"baracuda-kernels::L1LossBackwardPlan::run unwired dtype",
));
}
};
map_status(status)
}
}