Skip to main content

bin_pack

Function bin_pack 

Source
pub(crate) fn bin_pack(
    seq_lengths: &[usize],
    cost_model: &CostModel,
) -> Vec<Vec<usize>>
Expand description

Length-sorted greedy bin-packer.

Partitions seq_lengths (indexed 0..n) into contiguous groups (chunks) where each chunk satisfies cost_model.fits(chunk.len(), max_seq_in_chunk).

If a single text exceeds the budget on its own — which can happen when max_workspace_bytes is very small or the text is at MAX_SEQ_LENGTH and the budget is tighter than one text — it gets its own single-element chunk. The caller (ORT session) will either succeed or fail; we never silently truncate or discard inputs.

§Returns

Vec<Vec<usize>> where each inner Vec contains the original indices of texts in that chunk, sorted ascending by sequence length. The outer vec preserves the order chunks should be processed in. Callers scatter results back to the original positions using these indices.

§Complexity

O(n log n) sort + O(n) scan.