Seqpacker — 11 bin-packing algorithms in Rust for LLM sequence packing (my first Rust project)
⚓ Rust 📅 2026-03-29 👤 surdeus 👁️ 7Hi all,
I just published seqpacker, a bin-packing library for packing variable-length sequences into fixed-capacity bins. The primary use case is LLM training — reducing padding waste when batching sequences of different lengths — but the crate is general-purpose.
This is my first Rust project, so I'd genuinely appreciate feedback on the code, API design, and idiomatic Rust usage. Feature requests are also welcome.
The problem
LLM training pipelines pad every sequence to the longest in the batch. For skewed length distributions, 30–70% of compute goes to padding tokens that contribute nothing. Sequence packing reframes this as bin-packing: given a fixed capacity, fit variable-length items into as few bins as possible.
What seqpacker does
11 algorithms across three operating constraints:
| Algorithm | Approx. Ratio | Notes | |
|---|---|---|---|
| Offline | FFD, BFD, FFS, MFFD, OBFD | 1.18–1.30 | See all input, sort first |
| Online | NF, FF, BF, WF, Harmonic-K | 1.69–2.0 | One item at a time |
| Parallel | OBFDP | 1.22 | Rayon work-stealing |
Three modes: offline (batch), buffered (chunked), and streaming (NF and HK only — bounded state, emit bins incrementally).
Performance
| Package | Language | Runtime (10K seq) | Efficiency |
|---|---|---|---|
| seqpacker | Rust (PyO3) | ~5 ms | 98.76% |
| LightBinPack | C++ (pybind11) | ~6–7 ms | 98.76% |
| Pure Python libs | Python | 2–9.5 seconds | 98.76% |
Efficiency converges across implementations — the differentiator is runtime.
Usage
use seqpacker::{Packer, PackStrategy};
let packer = Packer::new(1024)
.with_strategy(PackStrategy::OptimizedBestFitDecreasing);
let result = packer.pack_lengths(&[1000, 800, 600, 500, 400]).unwrap();
println!("Efficiency: {:.2}%", result.metrics.efficiency * 100.0);
cargo add seqpacker
Links
GitHub | crates.io | docs.rs | Blog post
The blog post covers the problem, algorithms, and benchmarks in detail. A second article on profiling and optimisation in Rust is coming soon.
Any feedback on the code, API surface, or features you'd want is very welcome.
1 post - 1 participant
🏷️ Rust_feed