Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/llama-batch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,20 @@ bool llama_batch_ext::set_token_pos(int32_t idx, const llama_pos * pos_in) {
return true;
}

bool llama_batch_ext::reserve_embd(size_t n_tokens, size_t n_embd_row) {
if (n_tokens == 0 || n_tokens > n_tokens_max) {
return false;
}
if (n_embd_row == 0) {
return false;
}
if (n_embd_row > embd.max_size() / n_tokens) {
return false;
}
embd.reserve(n_tokens * n_embd_row);
return true;
}

bool llama_batch_ext::set_output(int32_t idx, bool output_last) {
if (idx < 0 || idx >= (int32_t) tokens.size()) {
return false;
Expand Down Expand Up @@ -1541,6 +1555,10 @@ bool llama_batch_ext_set_decision_group(llama_batch_ext * batch, int32_t idx, in
return batch->set_decision_group(idx, group);
}

bool llama_batch_ext_reserve_embd(llama_batch_ext * batch, size_t n_tokens, size_t n_embd) {
return batch->reserve_embd(n_tokens, n_embd);
}

// llama_batch_compat

void llama_batch_compat::init(llama_batch_ext & dst, const llama_batch & batch_inp, size_t n_embd_row) {
Expand Down
1 change: 1 addition & 0 deletions src/llama-batch.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ struct llama_batch_ext {
bool set_token_embd_row(int32_t idx, int32_t row);
bool set_token_state_row(int32_t idx, int32_t row);
bool set_token_pos(int32_t idx, const llama_pos * pos_in);
bool reserve_embd(size_t n_tokens, size_t n_embd_row);
bool set_output(int32_t idx, bool output_last);
bool set_decision_order(int32_t idx, int32_t order);
bool set_decision_group(int32_t idx, int32_t group);
Expand Down
3 changes: 3 additions & 0 deletions src/llama-ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ LLAMA_API bool llama_batch_ext_set_decision_order(struct llama_batch_ext * batch
// 0 (default) is the state, k > 0 the k-th question; full attention layers hide other questions from a question
LLAMA_API bool llama_batch_ext_set_decision_group(struct llama_batch_ext * batch, int32_t idx, int32_t group);

// Reserve storage for n_tokens embedding rows of n_embd floats without adding entries.
LLAMA_API bool llama_batch_ext_reserve_embd(struct llama_batch_ext * batch, size_t n_tokens, size_t n_embd);

// Label of option i of a decision model ("<arch>.decision.labels"), nullptr past the last one
LLAMA_API const char * llama_model_decision_label(const struct llama_model * model, uint32_t i);

Expand Down
2 changes: 2 additions & 0 deletions tools/mtmd/mtmd-helper-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "llama.h"
#include "llama-cpp.h"
#include "mtmd.h"
#include "../../src/llama-ext.h"

#include <cstdarg>
#include <cstdio>
Expand Down Expand Up @@ -161,6 +162,7 @@ struct decode_embd_batch {
batch.reset(llama_batch_ext_init(lctx));
}
llama_batch_ext_clear(batch.get());
llama_batch_ext_reserve_embd(batch.get(), n, n_mmproj_embd);
for (int i = offset; i < offset + n; i++) {
const llama_embd e = { embd + (size_t) i * n_mmproj_embd, 1, (size_t) n_mmproj_embd };
const int32_t idx = llama_batch_ext_add_embd(batch.get(), seq_id, e);
Expand Down