| 2540 | } |
| 2541 | |
| 2542 | void send_embedding(const server_slot & slot, const llama_batch & batch) { |
| 2543 | auto res = std::make_unique<server_task_result_embd>(); |
| 2544 | res->id = slot.id_task; |
| 2545 | res->index = slot.index; |
| 2546 | res->n_tokens = slot.n_prompt_tokens; |
| 2547 | res->oaicompat = slot.params.oaicompat; |
| 2548 | |
| 2549 | const int n_embd = llama_model_n_embd(model); |
| 2550 | |
| 2551 | std::vector<float> embd_res(n_embd, 0.0f); |
| 2552 | |
| 2553 | for (int i = 0; i < batch.n_tokens; ++i) { |
| 2554 | if (!batch.logits[i] || batch.seq_id[i][0] != slot.id) { |
| 2555 | continue; |
| 2556 | } |
| 2557 | |
| 2558 | const float * embd = llama_get_embeddings_seq(ctx, batch.seq_id[i][0]); |
| 2559 | if (embd == NULL) { |
| 2560 | embd = llama_get_embeddings_ith(ctx, i); |
| 2561 | } |
| 2562 | |
| 2563 | if (embd == NULL) { |
| 2564 | SLT_ERR(slot, "failed to get embeddings, token = %d, seq_id = %d\n", batch.token[i], batch.seq_id[i][0]); |
| 2565 | |
| 2566 | res->embedding.push_back(std::vector<float>(n_embd, 0.0f)); |
| 2567 | continue; |
| 2568 | } |
| 2569 | |
| 2570 | // normalize only when there is pooling |
| 2571 | // TODO: configurable |
| 2572 | if (llama_pooling_type(slot.ctx) != LLAMA_POOLING_TYPE_NONE) { |
| 2573 | common_embd_normalize(embd, embd_res.data(), n_embd, 2); |
| 2574 | res->embedding.push_back(embd_res); |
| 2575 | } else { |
| 2576 | res->embedding.push_back({ embd, embd + n_embd }); |
| 2577 | } |
| 2578 | } |
| 2579 | |
| 2580 | SLT_DBG(slot, "%s", "sending embeddings\n"); |
| 2581 | |
| 2582 | queue_results.send(std::move(res)); |
| 2583 | } |
| 2584 | |
| 2585 | void send_rerank(const server_slot & slot, const llama_batch & batch) { |
| 2586 | auto res = std::make_unique<server_task_result_rerank>(); |
nothing calls this directly
no test coverage detected