2023-03-25 19:26:40 +01:00
|
|
|
#include "common.h"
|
|
|
|
#include "llama.h"
|
|
|
|
|
2023-04-16 12:13:00 +02:00
|
|
|
#include <ctime>
|
|
|
|
|
2023-06-16 20:23:53 +02:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning(disable: 4244 4267) // possible loss of data
|
|
|
|
#endif
|
|
|
|
|
2024-02-13 13:06:58 +01:00
|
|
|
static std::vector<std::string> split_lines(const std::string & s) {
|
|
|
|
std::string line;
|
|
|
|
std::vector<std::string> lines;
|
|
|
|
std::stringstream ss(s);
|
|
|
|
while (std::getline(ss, line)) {
|
|
|
|
lines.push_back(line);
|
|
|
|
}
|
|
|
|
return lines;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void batch_add_seq(llama_batch & batch, const std::vector<int32_t> & tokens, int seq_id) {
|
|
|
|
for (size_t i = 0; i < tokens.size(); i++) {
|
2024-03-04 21:31:20 +01:00
|
|
|
llama_batch_add(batch, tokens[i], i, { seq_id }, i == tokens.size() - 1);
|
2024-02-13 13:06:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void batch_decode(llama_context * ctx, llama_batch & batch, float * output, int n_seq, int n_embd) {
|
|
|
|
// clear previous kv_cache values (irrelevant for embeddings)
|
|
|
|
llama_kv_cache_clear(ctx);
|
|
|
|
|
|
|
|
// run model
|
|
|
|
fprintf(stderr, "%s: n_tokens = %d, n_seq = %d\n", __func__, batch.n_tokens, n_seq);
|
|
|
|
if (llama_decode(ctx, batch) < 0) {
|
|
|
|
fprintf(stderr, "%s : failed to decode\n", __func__);
|
|
|
|
}
|
|
|
|
|
2024-03-04 21:31:20 +01:00
|
|
|
for (int i = 0; i < batch.n_tokens; i++) {
|
|
|
|
if (!batch.logits[i]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to get sequence embeddings - supported only when pooling_type is not NONE
|
|
|
|
const float * embd = llama_get_embeddings_seq(ctx, batch.seq_id[i][0]);
|
|
|
|
if (embd == NULL) {
|
|
|
|
embd = llama_get_embeddings_ith(ctx, i);
|
|
|
|
if (embd == NULL) {
|
|
|
|
fprintf(stderr, "%s: failed to get embeddings for token %d\n", __func__, i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float * out = output + batch.seq_id[i][0] * n_embd;
|
2024-03-09 13:27:58 +01:00
|
|
|
llama_embd_normalize(embd, out, n_embd);
|
2024-02-13 13:06:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-25 19:26:40 +01:00
|
|
|
int main(int argc, char ** argv) {
|
|
|
|
gpt_params params;
|
|
|
|
|
2023-09-07 19:22:29 +02:00
|
|
|
if (!gpt_params_parse(argc, argv, params)) {
|
2023-03-25 19:26:40 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
params.embedding = true;
|
2024-03-26 10:11:46 +01:00
|
|
|
// For non-causal models, batch size must be equal to ubatch size
|
|
|
|
params.n_ubatch = params.n_batch;
|
2023-03-25 19:26:40 +01:00
|
|
|
|
2023-09-15 22:59:49 +02:00
|
|
|
print_build_info();
|
2023-05-01 18:23:47 +02:00
|
|
|
|
2023-06-29 15:15:15 +02:00
|
|
|
if (params.seed == LLAMA_DEFAULT_SEED) {
|
2023-03-25 19:26:40 +01:00
|
|
|
params.seed = time(NULL);
|
|
|
|
}
|
|
|
|
|
2023-06-29 15:15:15 +02:00
|
|
|
fprintf(stderr, "%s: seed = %u\n", __func__, params.seed);
|
2023-03-25 19:26:40 +01:00
|
|
|
|
|
|
|
std::mt19937 rng(params.seed);
|
|
|
|
if (params.random_prompt) {
|
|
|
|
params.prompt = gpt_random_prompt(rng);
|
|
|
|
}
|
|
|
|
|
2024-02-16 10:31:07 +01:00
|
|
|
llama_backend_init();
|
|
|
|
llama_numa_init(params.numa);
|
2023-05-20 10:06:11 +02:00
|
|
|
|
2023-06-24 10:47:58 +02:00
|
|
|
llama_model * model;
|
2023-03-25 19:26:40 +01:00
|
|
|
llama_context * ctx;
|
|
|
|
|
|
|
|
// load the model
|
2023-06-24 10:47:58 +02:00
|
|
|
std::tie(model, ctx) = llama_init_from_gpt_params(params);
|
|
|
|
if (model == NULL) {
|
2023-05-02 22:39:51 +02:00
|
|
|
fprintf(stderr, "%s: error: unable to load model\n", __func__);
|
|
|
|
return 1;
|
2023-03-25 19:26:40 +01:00
|
|
|
}
|
|
|
|
|
2023-09-28 21:42:38 +02:00
|
|
|
const int n_ctx_train = llama_n_ctx_train(model);
|
|
|
|
const int n_ctx = llama_n_ctx(ctx);
|
|
|
|
|
|
|
|
if (n_ctx > n_ctx_train) {
|
2023-09-08 17:43:35 +02:00
|
|
|
fprintf(stderr, "%s: warning: model was trained on only %d context tokens (%d specified)\n",
|
2023-09-28 21:42:38 +02:00
|
|
|
__func__, n_ctx_train, n_ctx);
|
2023-09-08 17:43:35 +02:00
|
|
|
}
|
|
|
|
|
2023-03-25 19:26:40 +01:00
|
|
|
// print system information
|
|
|
|
{
|
|
|
|
fprintf(stderr, "\n");
|
2023-09-28 21:42:38 +02:00
|
|
|
fprintf(stderr, "%s\n", get_system_info(params).c_str());
|
2023-03-25 19:26:40 +01:00
|
|
|
}
|
|
|
|
|
2024-02-13 13:06:58 +01:00
|
|
|
// split the prompt into lines
|
|
|
|
std::vector<std::string> prompts = split_lines(params.prompt);
|
2023-03-25 19:26:40 +01:00
|
|
|
|
2024-02-13 13:06:58 +01:00
|
|
|
// max batch size
|
|
|
|
const uint64_t n_batch = params.n_batch;
|
2024-03-13 18:54:21 +01:00
|
|
|
GGML_ASSERT(params.n_batch >= params.n_ctx);
|
2023-03-25 19:26:40 +01:00
|
|
|
|
2024-02-13 13:06:58 +01:00
|
|
|
// tokenize the prompts and trim
|
|
|
|
std::vector<std::vector<int32_t>> inputs;
|
|
|
|
for (const auto & prompt : prompts) {
|
2024-03-14 14:14:14 +01:00
|
|
|
auto inp = ::llama_tokenize(ctx, prompt, true, false);
|
2024-02-13 13:06:58 +01:00
|
|
|
if (inp.size() > n_batch) {
|
2024-03-26 10:11:46 +01:00
|
|
|
fprintf(stderr, "%s: error: number of tokens in input line (%lld) exceeds batch size (%lld), increase batch size and re-run\n",
|
|
|
|
__func__, (long long int) inp.size(), (long long int) n_batch);
|
|
|
|
return 1;
|
2023-03-25 19:26:40 +01:00
|
|
|
}
|
2024-02-13 13:06:58 +01:00
|
|
|
inputs.push_back(inp);
|
2023-03-25 19:26:40 +01:00
|
|
|
}
|
|
|
|
|
2024-03-14 14:14:14 +01:00
|
|
|
// add eos if not present
|
|
|
|
for (auto & inp : inputs) {
|
|
|
|
if (inp.empty() || inp.back() != llama_token_eos(model)) {
|
|
|
|
inp.push_back(llama_token_eos(model));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-13 13:06:58 +01:00
|
|
|
// tokenization stats
|
|
|
|
if (params.verbose_prompt) {
|
|
|
|
for (int i = 0; i < (int) inputs.size(); i++) {
|
|
|
|
fprintf(stderr, "%s: prompt %d: '%s'\n", __func__, i, prompts[i].c_str());
|
|
|
|
fprintf(stderr, "%s: number of tokens in prompt = %zu\n", __func__, inputs[i].size());
|
|
|
|
for (int j = 0; j < (int) inputs[i].size(); j++) {
|
|
|
|
fprintf(stderr, "%6d -> '%s'\n", inputs[i][j], llama_token_to_piece(ctx, inputs[i][j]).c_str());
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n\n");
|
2023-03-25 19:26:40 +01:00
|
|
|
}
|
2023-08-22 16:03:12 +02:00
|
|
|
}
|
2023-03-25 19:26:40 +01:00
|
|
|
|
2024-02-13 13:06:58 +01:00
|
|
|
// initialize batch
|
|
|
|
const int n_prompts = prompts.size();
|
2024-03-04 21:31:20 +01:00
|
|
|
struct llama_batch batch = llama_batch_init(n_batch, 0, 1);
|
2024-02-13 13:06:58 +01:00
|
|
|
|
|
|
|
// allocate output
|
2023-09-28 21:42:38 +02:00
|
|
|
const int n_embd = llama_n_embd(model);
|
2024-02-13 13:06:58 +01:00
|
|
|
std::vector<float> embeddings(n_prompts * n_embd, 0);
|
|
|
|
float * emb = embeddings.data();
|
|
|
|
|
|
|
|
// break into batches
|
|
|
|
int p = 0; // number of prompts processed already
|
|
|
|
int s = 0; // number of prompts in current batch
|
|
|
|
for (int k = 0; k < n_prompts; k++) {
|
|
|
|
// clamp to n_batch tokens
|
|
|
|
auto & inp = inputs[k];
|
2024-03-04 21:31:20 +01:00
|
|
|
|
2024-02-13 13:06:58 +01:00
|
|
|
const uint64_t n_toks = inp.size();
|
|
|
|
|
|
|
|
// encode if at capacity
|
|
|
|
if (batch.n_tokens + n_toks > n_batch) {
|
|
|
|
float * out = emb + p * n_embd;
|
|
|
|
batch_decode(ctx, batch, out, s, n_embd);
|
|
|
|
llama_batch_clear(batch);
|
|
|
|
p += s;
|
|
|
|
s = 0;
|
|
|
|
}
|
2024-02-11 17:21:38 +01:00
|
|
|
|
2024-02-13 13:06:58 +01:00
|
|
|
// add to batch
|
|
|
|
batch_add_seq(batch, inp, s);
|
|
|
|
s += 1;
|
2024-02-11 17:21:38 +01:00
|
|
|
}
|
2023-03-25 19:26:40 +01:00
|
|
|
|
2024-02-13 13:06:58 +01:00
|
|
|
// final batch
|
|
|
|
float * out = emb + p * n_embd;
|
|
|
|
batch_decode(ctx, batch, out, s, n_embd);
|
|
|
|
|
2024-03-27 12:15:44 +01:00
|
|
|
// print the first part of the embeddings or for a single prompt, the full embedding
|
2024-03-14 09:12:29 +01:00
|
|
|
fprintf(stdout, "\n");
|
2024-03-14 11:37:20 +01:00
|
|
|
for (int j = 0; j < n_prompts; j++) {
|
2024-03-14 09:12:29 +01:00
|
|
|
fprintf(stdout, "embedding %d: ", j);
|
2024-03-27 12:15:44 +01:00
|
|
|
for (int i = 0; i < (n_prompts > 1 ? std::min(16, n_embd) : n_embd); i++) {
|
2024-03-14 14:14:14 +01:00
|
|
|
fprintf(stdout, "%9.6f ", emb[j * n_embd + i]);
|
2024-02-13 13:06:58 +01:00
|
|
|
}
|
2024-03-14 09:12:29 +01:00
|
|
|
fprintf(stdout, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
// print cosine similarity matrix
|
2024-03-27 12:15:44 +01:00
|
|
|
if (n_prompts > 1) {
|
2024-03-14 09:12:29 +01:00
|
|
|
fprintf(stdout, "\n");
|
2024-03-27 12:15:44 +01:00
|
|
|
printf("cosine similarity matrix:\n\n");
|
|
|
|
for (int i = 0; i < n_prompts; i++) {
|
|
|
|
for (int j = 0; j < n_prompts; j++) {
|
|
|
|
float sim = llama_embd_similarity_cos(emb + i * n_embd, emb + j * n_embd, n_embd);
|
|
|
|
fprintf(stdout, "%6.2f ", sim);
|
|
|
|
}
|
|
|
|
fprintf(stdout, "\n");
|
|
|
|
}
|
2023-03-25 19:26:40 +01:00
|
|
|
}
|
|
|
|
|
2024-02-13 13:06:58 +01:00
|
|
|
// clean up
|
2023-03-25 19:26:40 +01:00
|
|
|
llama_print_timings(ctx);
|
|
|
|
llama_free(ctx);
|
2023-06-24 10:47:58 +02:00
|
|
|
llama_free_model(model);
|
2023-07-10 17:49:56 +02:00
|
|
|
llama_backend_free();
|
|
|
|
|
2023-03-25 19:26:40 +01:00
|
|
|
return 0;
|
|
|
|
}
|