use anyhow::{anyhow, Context, Result};
use mlx_native::MlxBuffer;
pub(crate) struct SlotRows {
heads: usize,
capacity: usize,
row_bytes: usize,
head_stride: usize,
slot_stride: usize,
}
impl SlotRows {
pub(crate) fn of(buffer: &MlxBuffer, n_seqs: usize, name: &str) -> Result<Self> {
let shape = buffer.shape();
let (heads, capacity, inner) = match *shape {
[seqs, heads, capacity] if seqs == n_seqs => (heads, capacity, 1),
[seqs, heads, capacity, inner] if seqs == n_seqs => (heads, capacity, inner),
_ => anyhow::bail!(
"slot spill ({name}): expected [n_seqs={n_seqs}, heads, capacity, inner], got {shape:?}"
),
};
let row_bytes = inner
.checked_mul(buffer.dtype().size_of())
.ok_or_else(|| anyhow!("slot spill ({name}): row extent overflow"))?;
let head_stride = capacity
.checked_mul(row_bytes)
.ok_or_else(|| anyhow!("slot spill ({name}): head stride overflow"))?;
let slot_stride = heads
.checked_mul(head_stride)
.ok_or_else(|| anyhow!("slot spill ({name}): slot stride overflow"))?;
let extent = slot_stride
.checked_mul(n_seqs)
.ok_or_else(|| anyhow!("slot spill ({name}): buffer extent overflow"))?;
anyhow::ensure!(
extent <= buffer.byte_len(),
"slot spill ({name}): shape covers {extent} bytes but the buffer holds {}",
buffer.byte_len()
);
Ok(Self {
heads,
capacity,
row_bytes,
head_stride,
slot_stride,
})
}
#[cfg(test)]
pub(crate) fn capacity(&self) -> usize {
self.capacity
}
pub(crate) fn spilled_len(&self, rows: usize, name: &str) -> Result<usize> {
anyhow::ensure!(
rows <= self.capacity,
"slot spill ({name}): cursor {rows} exceeds capacity {}",
self.capacity
);
Ok(self.heads * rows * self.row_bytes)
}
fn head_start(&self, slot_idx: usize, head: usize) -> usize {
slot_idx * self.slot_stride + head * self.head_stride
}
}
pub(crate) fn read_rows(
buffer: &MlxBuffer,
slot_idx: usize,
n_seqs: usize,
rows: usize,
name: &str,
) -> Result<Vec<u8>> {
let layout = SlotRows::of(buffer, n_seqs, name)?;
let mut out = Vec::with_capacity(layout.spilled_len(rows, name)?);
let bytes = buffer
.as_slice::<u8>()
.with_context(|| format!("slot spill ({name}) as_slice"))?;
let copy = rows * layout.row_bytes;
for head in 0..layout.heads {
let start = layout.head_start(slot_idx, head);
out.extend_from_slice(&bytes[start..start + copy]);
}
Ok(out)
}
pub(crate) fn write_rows(
src: &[u8],
buffer: &mut MlxBuffer,
slot_idx: usize,
n_seqs: usize,
rows: usize,
name: &str,
) -> Result<()> {
let layout = SlotRows::of(buffer, n_seqs, name)?;
anyhow::ensure!(
src.len() == layout.spilled_len(rows, name)?,
"slot spill ({name}): spilled {} bytes for {rows} rows, expected {}",
src.len(),
layout.heads * rows * layout.row_bytes
);
let copy = rows * layout.row_bytes;
if copy == 0 {
return Ok(());
}
let bytes = buffer
.as_mut_slice::<u8>()
.with_context(|| format!("slot spill ({name}) as_mut_slice"))?;
for (head, chunk) in src.chunks_exact(copy).enumerate() {
let start = layout.head_start(slot_idx, head);
bytes[start..start + copy].copy_from_slice(chunk);
}
Ok(())
}
pub(crate) fn copy_rows_between_slots(
buffer: &mut MlxBuffer,
src_slot: usize,
dst_slot: usize,
n_seqs: usize,
rows: usize,
name: &str,
) -> Result<()> {
let layout = SlotRows::of(buffer, n_seqs, name)?;
layout.spilled_len(rows, name)?;
anyhow::ensure!(
src_slot < n_seqs && dst_slot < n_seqs,
"slot spill ({name}): slots {src_slot}->{dst_slot} outside n_seqs={n_seqs}"
);
let copy = rows * layout.row_bytes;
if copy == 0 || src_slot == dst_slot {
return Ok(());
}
let bytes = buffer
.as_mut_slice::<u8>()
.with_context(|| format!("slot spill ({name}) as_mut_slice"))?;
for head in 0..layout.heads {
let src = layout.head_start(src_slot, head);
bytes.copy_within(src..src + copy, layout.head_start(dst_slot, head));
}
Ok(())
}