pub struct RaggedBatch {
pub token_ids: Vec<u32>,
pub cumulative_offsets: Vec<u32>,
pub max_seq_len: usize,
}Expand description
A ragged (unpadded) batch for efficient ModernBERT inference.
ModernBERT achieves its speed advantage by avoiding padding tokens entirely.
Instead of [batch, max_seq_len], it uses a single contiguous 1D sequence
with offset indices to track document boundaries.
§Memory Layout
Traditional (padded):
[doc1_tok1, doc1_tok2, PAD, PAD, PAD] <- wasted compute
[doc2_tok1, doc2_tok2, doc2_tok3, PAD, PAD]
Ragged (unpadded):
[doc1_tok1, doc1_tok2, doc2_tok1, doc2_tok2, doc2_tok3]
cumulative_offsets: [0, 2, 5] <- doc1 is [0..2], doc2 is [2..5]Fields§
§token_ids: Vec<u32>Token IDs flattened into a single contiguous array.
Shape: [total_tokens] (1D, no padding)
cumulative_offsets: Vec<u32>Cumulative sequence lengths. Length: batch_size + 1 Document i spans tokens [offsets[i]..offsets[i+1])
max_seq_len: usizeMaximum sequence length in this batch (for kernel bounds).
Implementations§
Source§impl RaggedBatch
impl RaggedBatch
Sourcepub fn from_sequences(sequences: &[Vec<u32>]) -> RaggedBatch
pub fn from_sequences(sequences: &[Vec<u32>]) -> RaggedBatch
Create a new ragged batch from sequences.
Sourcepub fn batch_size(&self) -> usize
pub fn batch_size(&self) -> usize
Get the number of documents in this batch.
Sourcepub fn total_tokens(&self) -> usize
pub fn total_tokens(&self) -> usize
Get the total number of tokens (no padding).
Sourcepub fn doc_range(&self, doc_idx: usize) -> Option<Range<usize>>
pub fn doc_range(&self, doc_idx: usize) -> Option<Range<usize>>
Get token range for a specific document.
Sourcepub fn doc_tokens(&self, doc_idx: usize) -> Option<&[u32]>
pub fn doc_tokens(&self, doc_idx: usize) -> Option<&[u32]>
Get tokens for a specific document.
Sourcepub fn padding_savings(&self) -> f64
pub fn padding_savings(&self) -> f64
Calculate memory saved vs padded batch.
Trait Implementations§
Source§impl Clone for RaggedBatch
impl Clone for RaggedBatch
Source§fn clone(&self) -> RaggedBatch
fn clone(&self) -> RaggedBatch
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for RaggedBatch
impl RefUnwindSafe for RaggedBatch
impl Send for RaggedBatch
impl Sync for RaggedBatch
impl Unpin for RaggedBatch
impl UnsafeUnpin for RaggedBatch
impl UnwindSafe for RaggedBatch
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more