| 34 | } |
| 35 | |
| 36 | static void batch_decode(llama_context * ctx, llama_batch & batch, float * output, int n_seq, int n_embd, int embd_norm) { |
| 37 | const enum llama_pooling_type pooling_type = llama_pooling_type(ctx); |
| 38 | |
| 39 | // clear previous kv_cache values (irrelevant for embeddings) |
| 40 | llama_kv_self_clear(ctx); |
| 41 | |
| 42 | // run model |
| 43 | LOG_INF("%s: n_tokens = %d, n_seq = %d\n", __func__, batch.n_tokens, n_seq); |
| 44 | if (llama_decode(ctx, batch) < 0) { |
| 45 | LOG_ERR("%s : failed to process\n", __func__); |
| 46 | } |
| 47 | |
| 48 | for (int i = 0; i < batch.n_tokens; i++) { |
| 49 | if (!batch.logits[i]) { |
| 50 | continue; |
| 51 | } |
| 52 | |
| 53 | const float * embd = nullptr; |
| 54 | int embd_pos = 0; |
| 55 | |
| 56 | if (pooling_type == LLAMA_POOLING_TYPE_NONE) { |
| 57 | // try to get token embeddings |
| 58 | embd = llama_get_embeddings_ith(ctx, i); |
| 59 | embd_pos = i; |
| 60 | GGML_ASSERT(embd != NULL && "failed to get token embeddings"); |
| 61 | } else { |
| 62 | // try to get sequence embeddings - supported only when pooling_type is not NONE |
| 63 | embd = llama_get_embeddings_seq(ctx, batch.seq_id[i][0]); |
| 64 | embd_pos = batch.seq_id[i][0]; |
| 65 | GGML_ASSERT(embd != NULL && "failed to get sequence embeddings"); |
| 66 | } |
| 67 | |
| 68 | float * out = output + embd_pos * n_embd; |
| 69 | common_embd_normalize(embd, out, n_embd, embd_norm); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | int main(int argc, char ** argv) { |
| 74 | common_params params; |
no test coverage detected