Expand description
Safe wrapper around llama_batch_ext, llama.cpp’s entry-wise batch.
LlamaBatch wraps the original
llama_batch: six parallel arrays, one entry per token, and a single flat
embd block for the whole batch. That shape cannot describe a batch where
different entries carry differently-shaped inputs, which is what multimodal
and speculative decoding need. llama_batch_ext
(llama.cpp #24669)
replaces the arrays with a list of entries that each own their token id,
positions, input embedding and “state” embedding.
Reach for this instead of LlamaBatch
when you need one of the things only an entry can express:
- an input embedding on some entries but not others — a vision encoder’s
output interleaved with text tokens, where
llama_batchforces the whole batch to be one or the other; - M-RoPE positions, which are
[t, y, x, z]per embedding entry rather than one scalar — seeLlamaBatchExt::set_positions; - a state embedding (
llama_batch_ext_set_embd_state), the hidden state an MTP draft context reads from the target model.
For plain token decoding there is nothing to gain:
LlamaBatch is the same work with less
ceremony, and llama_decode is where this crate’s decode lifecycle hooks
live — see LlamaContext::process.
§Example
use llama_cpp_4::context::LlamaContext;
use llama_cpp_4::llama_batch_ext::{LlamaBatchExt, LlamaProcessType};
use llama_cpp_4::token::LlamaToken;
let mut batch = LlamaBatchExt::new(ctx)?;
let mut pos = 0;
let mut last = None;
for &token in tokens {
let entry = batch.add_token(token, 0)?;
batch.set_pos(entry, pos)?;
pos += 1;
last = Some(entry);
}
if let Some(entry) = last {
batch.set_output(entry, true)?;
}
ctx.process(&mut batch, LlamaProcessType::Decode)?;Structs§
- Batch
Entry - A handle to one entry of a
LlamaBatchExt, returned by theadd_*methods and accepted by theset_*methods. - Embedding
Rows - A borrowed, row-major block of embedding values, mirroring
llama_embd. - Llama
Batch Ext - A safe wrapper around
llama_batch_ext.
Enums§
- Batch
ExtError - Errors from building a
LlamaBatchExt. - Llama
Decision Order - What a joint decision head (
clefand friends) reads an entry as. - Llama
Process Type - Which graph to run a batch through.
Constants§
- MROPE_
SECTIONS - Positions an embedding entry carries on an M-RoPE or interleaved-M-RoPE
model:
[t, y, x, z], wheretdoubles as the KV-cache position.