Skip to content

Commit 1756c4b

Browse files
committed
find result_norm/result_embd tensors properly; update output allocation logic
1 parent 0105714 commit 1756c4b

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

examples/embedding/embedding.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ static bool needs_logit(enum llama_pooling_type pooling_type, int pos, int n_tok
3131
}
3232
}
3333

34-
static void batch_add_seq(llama_batch & batch, const std::vector<int32_t> & tokens, int seq_id, enum llama_pooling_type pooling_type) {
35-
int n_tokens = tokens.size();
34+
static void batch_add_seq(llama_batch & batch, const std::vector<int32_t> & tokens, llama_seq_id seq_id, enum llama_pooling_type pooling_type) {
35+
size_t n_tokens = tokens.size();
3636
for (size_t i = 0; i < n_tokens; i++) {
3737
bool logit = needs_logit(pooling_type, i, n_tokens);
3838
llama_batch_add(batch, tokens[i], i, { seq_id }, logit);

examples/retrieval/retrieval.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ static bool needs_logit(enum llama_pooling_type pooling_type, int pos, int n_tok
8787
}
8888
}
8989

90-
static void batch_add_seq(llama_batch & batch, const std::vector<int32_t> & tokens, int seq_id, enum llama_pooling_type pooling_type) {
91-
int n_tokens = tokens.size();
92-
for (size_t i = 0; i < tokens.size(); i++) {
90+
static void batch_add_seq(llama_batch & batch, const std::vector<int32_t> & tokens, llama_seq_id seq_id, enum llama_pooling_type pooling_type) {
91+
size_t n_tokens = tokens.size();
92+
for (size_t i = 0; i < n_tokens; i++) {
9393
bool logit = needs_logit(pooling_type, i, n_tokens);
9494
llama_batch_add(batch, tokens[i], i, { seq_id }, logit);
9595
}

llama.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7436,11 +7436,17 @@ struct llm_build_context {
74367436
}
74377437

74387438
struct ggml_cgraph * append_pooling(struct ggml_cgraph * gf) {
7439-
struct ggml_tensor * inp = gf->nodes[gf->n_nodes - 1];
7440-
if (strcmp(inp->name, "result_embd") != 0) {
7441-
inp = gf->nodes[gf->n_nodes - 2];
7442-
GGML_ASSERT(strcmp(inp->name, "result_norm") == 0 && "embeddings tensor not found");
7439+
// find result_norm tensor for input
7440+
struct ggml_tensor * inp = nullptr;
7441+
for (int i = gf->n_nodes - 1; i >= 0; --i) {
7442+
inp = gf->nodes[i];
7443+
if (strcmp(inp->name, "result_norm") == 0 || strcmp(inp->name, "result_embd") == 0) {
7444+
break;
7445+
} else {
7446+
inp = nullptr;
7447+
}
74437448
}
7449+
GGML_ASSERT(inp != nullptr && "missing result_norm/result_embd tensor");
74447450

74457451
struct ggml_tensor * cur;
74467452

@@ -12029,8 +12035,8 @@ static size_t llama_output_reserve(llama_context & lctx, size_t n_outputs) {
1202912035
const auto n_embd = hparams.n_embd;
1203012036

1203112037
// TODO: use a per-batch flag for logits presence instead
12032-
const bool has_logits = cparams.causal_attn;
12033-
const bool has_embd = cparams.embeddings && (hparams.causal_attn || cparams.pooling_type == LLAMA_POOLING_TYPE_NONE);
12038+
const bool has_logits = !cparams.embeddings;
12039+
const bool has_embd = cparams.embeddings;
1203412040

1203512041
const size_t logits_size = has_logits ? n_vocab*n_outputs_max : 0;
1203612042
const size_t embd_size = has_embd ? n_embd*n_outputs_max : 0;

0 commit comments

Comments
 (0)