Skip to content

Commit fff5031

Browse files
authored
Merge pull request #46 from utilityai/batch-add-error-handeling
Batch add error handeling
2 parents 3b741af + 23cdc63 commit fff5031

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

llama-cpp-2/examples/simple.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ either reduce n_len or increase n_ctx")
9696
for (i, token) in (0_i32..).zip(tokens_list.into_iter()) {
9797
// llama_decode will output logits only for the last token of the prompt
9898
let is_last = i == last_index;
99-
batch.add(token, i, &[0], is_last);
99+
batch.add(token, i, &[0], is_last)?;
100100
}
101101

102102
ctx.decode(&mut batch)
@@ -129,7 +129,7 @@ either reduce n_len or increase n_ctx")
129129
std::io::stdout().flush()?;
130130

131131
batch.clear();
132-
batch.add(new_token_id, n_cur, &[0], true);
132+
batch.add(new_token_id, n_cur, &[0], true)?;
133133
}
134134

135135
n_cur += 1;

llama-cpp-2/src/llama_batch.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ pub struct LlamaBatch {
1414
pub(crate) llama_batch: llama_batch,
1515
}
1616

17+
/// Errors that can occur when adding a token to a batch.
18+
#[derive(thiserror::Error, Debug)]
19+
pub enum BatchAddError {
20+
/// There was not enough space in the batch to add the token.
21+
#[error("Insufficient Space of {0}")]
22+
InsufficientSpace(usize),
23+
}
24+
1725
impl LlamaBatch {
1826
/// Clear the batch. This does not free the memory associated with the batch, but it does reset
1927
/// the number of tokens to 0.
@@ -35,8 +43,10 @@ impl LlamaBatch {
3543
pos: llama_pos,
3644
seq_ids: &[i32],
3745
logits: bool,
38-
) {
39-
assert!(self.allocated > (usize::try_from(self.n_tokens() + 1).expect("self.n_tokens does not fit into a usize")), "there are only {} tokens allocated for the batch, but {} tokens in the batch when you tried to add one", self.allocated, self.n_tokens());
46+
) -> Result<(), BatchAddError> {
47+
if self.allocated < usize::try_from(self.n_tokens() + 1).expect("cannot fit n_tokens into a usize") {
48+
return Err(BatchAddError::InsufficientSpace(self.allocated))
49+
}
4050
let offset = self.llama_batch.n_tokens;
4151
let offset_usize = usize::try_from(offset).expect("cannot fit n_tokens into a usize");
4252
unsafe {
@@ -66,6 +76,8 @@ impl LlamaBatch {
6676

6777
// batch.n_tokens++;
6878
self.llama_batch.n_tokens += 1;
79+
80+
Ok(())
6981
}
7082
/// Create a new `LlamaBatch` that cab contain up to `n_tokens` tokens.
7183
///

0 commit comments

Comments
 (0)