use core::marker::PhantomData;
use baracuda_cutlass::{Error, Result};
use baracuda_driver::Stream;
use baracuda_kernels_types::{
ArchSku, AttentionKind, BackendKind, Element, ElementKind, KernelSku, MathPrecision,
OpCategory, PlanPreference, PrecisionGuarantee, TensorMut, TensorRef, Workspace,
};
pub const SDPA_BLOCK_SPARSE_MAX_BLOCK: i32 = 64;
pub const SDPA_BLOCK_SPARSE_MAX_D: i32 = 128;
#[derive(Copy, Clone, Debug)]
pub struct SdpaBlockSparseDescriptor {
pub batch_size: i32,
pub num_heads: i32,
pub query_len: i32,
pub key_len: i32,
pub d_k: i32,
pub d_v: i32,
pub block_size: i32,
pub scale: f32,
pub is_causal: bool,
pub element: ElementKind,
}
pub struct SdpaBlockSparseArgs<'a, T: Element> {
pub q: TensorRef<'a, T, 4>,
pub k: TensorRef<'a, T, 4>,
pub v: TensorRef<'a, T, 4>,
pub block_pattern: TensorRef<'a, u8, 3>,
pub y: TensorMut<'a, T, 4>,
pub lse: TensorMut<'a, T, 3>,
}
pub struct SdpaBlockSparsePlan<T: Element> {
desc: SdpaBlockSparseDescriptor,
sku: KernelSku,
num_blocks_q: i32,
num_blocks_k: i32,
_marker: PhantomData<T>,
}
impl<T: Element> SdpaBlockSparsePlan<T> {
pub fn select(
_stream: &Stream,
desc: &SdpaBlockSparseDescriptor,
_pref: PlanPreference,
) -> Result<Self> {
if desc.element != T::KIND {
return Err(Error::Unsupported(
"baracuda-kernels::SdpaBlockSparsePlan: descriptor element != T",
));
}
if desc.batch_size < 0
|| desc.num_heads < 0
|| desc.query_len < 0
|| desc.key_len < 0
|| desc.d_k < 0
|| desc.d_v < 0
{
return Err(Error::InvalidProblem(
"baracuda-kernels::SdpaBlockSparsePlan: extents must be non-negative",
));
}
if desc.d_k != desc.d_v {
return Err(Error::Unsupported(
"baracuda-kernels::SdpaBlockSparsePlan: Tier 1 requires d_k == d_v",
));
}
if desc.d_k > SDPA_BLOCK_SPARSE_MAX_D {
return Err(Error::Unsupported(
"baracuda-kernels::SdpaBlockSparsePlan: d_k > 128 unsupported in Tier 1",
));
}
if desc.block_size <= 0 || desc.block_size > SDPA_BLOCK_SPARSE_MAX_BLOCK {
return Err(Error::Unsupported(
"baracuda-kernels::SdpaBlockSparsePlan: block_size must be in 1..=64",
));
}
if !desc.scale.is_finite() {
return Err(Error::InvalidProblem(
"baracuda-kernels::SdpaBlockSparsePlan: scale must be finite",
));
}
let dtype_in_scope = matches!(
T::KIND,
ElementKind::F32 | ElementKind::F16 | ElementKind::Bf16 | ElementKind::F64
);
if !dtype_in_scope {
return Err(Error::Unsupported(
"baracuda-kernels::SdpaBlockSparsePlan: wired today: `{f32, f16, bf16, f64}`",
));
}
#[cfg(feature = "xformers_blocksparse")]
{
let probe = unsafe {
match T::KIND {
ElementKind::F32 =>
baracuda_kernels_sys::baracuda_kernels_sdpa_f32_block_sparse_can_implement(
desc.batch_size, desc.num_heads, desc.query_len, desc.key_len,
desc.d_k, desc.d_v, desc.block_size,
),
ElementKind::F16 =>
baracuda_kernels_sys::baracuda_kernels_sdpa_f16_block_sparse_can_implement(
desc.batch_size, desc.num_heads, desc.query_len, desc.key_len,
desc.d_k, desc.d_v, desc.block_size,
),
ElementKind::Bf16 =>
baracuda_kernels_sys::baracuda_kernels_sdpa_bf16_block_sparse_can_implement(
desc.batch_size, desc.num_heads, desc.query_len, desc.key_len,
desc.d_k, desc.d_v, desc.block_size,
),
ElementKind::F64 =>
baracuda_kernels_sys::baracuda_kernels_sdpa_f64_block_sparse_can_implement(
desc.batch_size, desc.num_heads, desc.query_len, desc.key_len,
desc.d_k, desc.d_v, desc.block_size,
),
_ => 3,
}
};
map_status(probe)?;
}
let num_blocks_q = (desc.query_len + desc.block_size - 1) / desc.block_size;
let num_blocks_k = (desc.key_len + desc.block_size - 1) / desc.block_size;
let precision_guarantee = PrecisionGuarantee {
math_precision: MathPrecision::F32,
accumulator: ElementKind::F32,
bit_stable_on_same_hardware: true,
deterministic: true,
};
let sku = KernelSku {
category: OpCategory::Attention,
op: AttentionKind::BlockSparseAttention 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,
num_blocks_q,
num_blocks_k,
_marker: PhantomData,
})
}
pub fn can_implement(&self, args: &SdpaBlockSparseArgs<'_, T>) -> Result<()> {
let shape_q = [
self.desc.batch_size,
self.desc.num_heads,
self.desc.query_len,
self.desc.d_k,
];
let shape_k = [
self.desc.batch_size,
self.desc.num_heads,
self.desc.key_len,
self.desc.d_k,
];
let shape_v = [
self.desc.batch_size,
self.desc.num_heads,
self.desc.key_len,
self.desc.d_v,
];
let shape_y = [
self.desc.batch_size,
self.desc.num_heads,
self.desc.query_len,
self.desc.d_v,
];
let shape_lse = [
self.desc.batch_size,
self.desc.num_heads,
self.desc.query_len,
];
let shape_bp = [
self.desc.batch_size,
self.desc.num_heads,
self.num_blocks_q * self.num_blocks_k,
];
if args.q.shape != shape_q {
return Err(Error::InvalidProblem(
"baracuda-kernels::SdpaBlockSparsePlan: Q shape mismatch",
));
}
if args.k.shape != shape_k {
return Err(Error::InvalidProblem(
"baracuda-kernels::SdpaBlockSparsePlan: K shape mismatch",
));
}
if args.v.shape != shape_v {
return Err(Error::InvalidProblem(
"baracuda-kernels::SdpaBlockSparsePlan: V shape mismatch",
));
}
if args.y.shape != shape_y {
return Err(Error::InvalidProblem(
"baracuda-kernels::SdpaBlockSparsePlan: y shape mismatch",
));
}
if args.lse.shape != shape_lse {
return Err(Error::InvalidProblem(
"baracuda-kernels::SdpaBlockSparsePlan: lse shape mismatch",
));
}
if args.block_pattern.shape != shape_bp {
return Err(Error::InvalidProblem(
"baracuda-kernels::SdpaBlockSparsePlan: block_pattern shape mismatch \
(expected [B, H, num_blocks_q * num_blocks_k])",
));
}
if !args.q.is_contiguous()
|| !args.k.is_contiguous()
|| !args.v.is_contiguous()
|| !args.y.is_contiguous()
|| !args.lse.is_contiguous()
|| !args.block_pattern.is_contiguous()
{
return Err(Error::Unsupported(
"baracuda-kernels::SdpaBlockSparsePlan: all tensors must be contiguous in Tier 1",
));
}
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
}
#[inline]
pub fn num_blocks_q(&self) -> i32 {
self.num_blocks_q
}
#[inline]
pub fn num_blocks_k(&self) -> i32 {
self.num_blocks_k
}
pub fn run(
&self,
stream: &Stream,
_workspace: Workspace<'_>,
args: SdpaBlockSparseArgs<'_, T>,
) -> Result<()> {
self.can_implement(&args)?;
if args.y.numel() == 0 {
return Ok(());
}
#[cfg(feature = "xformers_blocksparse")]
{
let stream_ptr = stream.as_raw() as *mut c_void;
let q_ptr = args.q.data.as_raw().0 as *const c_void;
let k_ptr = args.k.data.as_raw().0 as *const c_void;
let v_ptr = args.v.data.as_raw().0 as *const c_void;
let bp_ptr = args.block_pattern.data.as_raw().0 as *const c_void;
let y_ptr = args.y.data.as_raw().0 as *mut c_void;
let lse_ptr = args.lse.data.as_raw().0 as *mut c_void;
let is_causal_flag = if self.desc.is_causal { 1 } else { 0 };
let status = unsafe {
match T::KIND {
ElementKind::F32 =>
baracuda_kernels_sys::baracuda_kernels_sdpa_f32_block_sparse_run(
self.desc.batch_size, self.desc.num_heads,
self.desc.query_len, self.desc.key_len,
self.desc.d_k, self.desc.d_v, self.desc.block_size,
self.desc.scale, is_causal_flag,
q_ptr, k_ptr, v_ptr, bp_ptr,
y_ptr, lse_ptr,
core::ptr::null_mut(), 0, stream_ptr,
),
ElementKind::F16 =>
baracuda_kernels_sys::baracuda_kernels_sdpa_f16_block_sparse_run(
self.desc.batch_size, self.desc.num_heads,
self.desc.query_len, self.desc.key_len,
self.desc.d_k, self.desc.d_v, self.desc.block_size,
self.desc.scale, is_causal_flag,
q_ptr, k_ptr, v_ptr, bp_ptr,
y_ptr, lse_ptr,
core::ptr::null_mut(), 0, stream_ptr,
),
ElementKind::Bf16 =>
baracuda_kernels_sys::baracuda_kernels_sdpa_bf16_block_sparse_run(
self.desc.batch_size, self.desc.num_heads,
self.desc.query_len, self.desc.key_len,
self.desc.d_k, self.desc.d_v, self.desc.block_size,
self.desc.scale, is_causal_flag,
q_ptr, k_ptr, v_ptr, bp_ptr,
y_ptr, lse_ptr,
core::ptr::null_mut(), 0, stream_ptr,
),
ElementKind::F64 =>
baracuda_kernels_sys::baracuda_kernels_sdpa_f64_block_sparse_run(
self.desc.batch_size, self.desc.num_heads,
self.desc.query_len, self.desc.key_len,
self.desc.d_k, self.desc.d_v, self.desc.block_size,
self.desc.scale, is_causal_flag,
q_ptr, k_ptr, v_ptr, bp_ptr,
y_ptr, lse_ptr,
core::ptr::null_mut(), 0, stream_ptr,
),
_ => return Err(Error::Unsupported(
"baracuda-kernels::SdpaBlockSparsePlan::run reached an unimplemented dtype",
)),
}
};
map_status(status)
}
#[cfg(not(feature = "xformers_blocksparse"))]
{
let _ = stream;
Err(Error::Unsupported(
"baracuda-kernels::SdpaBlockSparsePlan: build with the `xformers_blocksparse` cargo feature",
))
}
}
}