Skip to content

Commit

Permalink
Merge pull request #46 from utilityai/batch-add-error-handeling
Browse files Browse the repository at this point in the history
Batch add error handeling
  • Loading branch information
MarcusDunn authored Jan 25, 2024
2 parents 3b741af + 23cdc63 commit fff5031
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions llama-cpp-2/examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ either reduce n_len or increase n_ctx")
for (i, token) in (0_i32..).zip(tokens_list.into_iter()) {
// llama_decode will output logits only for the last token of the prompt
let is_last = i == last_index;
batch.add(token, i, &[0], is_last);
batch.add(token, i, &[0], is_last)?;
}

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

batch.clear();
batch.add(new_token_id, n_cur, &[0], true);
batch.add(new_token_id, n_cur, &[0], true)?;
}

n_cur += 1;
Expand Down
16 changes: 14 additions & 2 deletions llama-cpp-2/src/llama_batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ pub struct LlamaBatch {
pub(crate) llama_batch: llama_batch,
}

/// Errors that can occur when adding a token to a batch.
#[derive(thiserror::Error, Debug)]
pub enum BatchAddError {
/// There was not enough space in the batch to add the token.
#[error("Insufficient Space of {0}")]
InsufficientSpace(usize),
}

impl LlamaBatch {
/// Clear the batch. This does not free the memory associated with the batch, but it does reset
/// the number of tokens to 0.
Expand All @@ -35,8 +43,10 @@ impl LlamaBatch {
pos: llama_pos,
seq_ids: &[i32],
logits: bool,
) {
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());
) -> Result<(), BatchAddError> {
if self.allocated < usize::try_from(self.n_tokens() + 1).expect("cannot fit n_tokens into a usize") {
return Err(BatchAddError::InsufficientSpace(self.allocated))
}
let offset = self.llama_batch.n_tokens;
let offset_usize = usize::try_from(offset).expect("cannot fit n_tokens into a usize");
unsafe {
Expand Down Expand Up @@ -66,6 +76,8 @@ impl LlamaBatch {

// batch.n_tokens++;
self.llama_batch.n_tokens += 1;

Ok(())
}
/// Create a new `LlamaBatch` that cab contain up to `n_tokens` tokens.
///
Expand Down

0 comments on commit fff5031

Please sign in to comment.