|
| 1 | +#include "common.h" |
| 2 | +#include "llama.h" |
| 3 | + |
| 4 | +#include <algorithm> |
| 5 | +#include <fstream> |
| 6 | + |
| 7 | +struct chunk { |
| 8 | + // filename |
| 9 | + std::string filename; |
| 10 | + // original file position |
| 11 | + int64_t filepos; |
| 12 | + // original text data |
| 13 | + std::string textdata = ""; |
| 14 | + // tokenized text data |
| 15 | + std::vector<std::int32_t> tokens; |
| 16 | + // embedding |
| 17 | + std::vector<float> embedding; |
| 18 | + // cosin similarity |
| 19 | + float similarity; |
| 20 | +}; |
| 21 | + |
| 22 | +// chunk file data to chunks of size >= chunk_size |
| 23 | +// chunk_separator is the separator between chunks |
| 24 | +static std::vector<chunk> chunk_file(const std::string filename, int chunk_size, std::string chunk_separator) { |
| 25 | + std::vector<chunk> chunks; |
| 26 | + std::ifstream f(filename.c_str()); |
| 27 | + |
| 28 | + if (!f.is_open()) { |
| 29 | + fprintf(stderr, "Error: could not open file %s\n", filename.c_str()); |
| 30 | + return chunks; |
| 31 | + } |
| 32 | + |
| 33 | + chunk current_chunk; |
| 34 | + char buffer[chunk_size]; |
| 35 | + int64_t filepos = 0; |
| 36 | + std::string current = ""; |
| 37 | + while (f.read(buffer, chunk_size)) { |
| 38 | + current += std::string(buffer, f.gcount()); |
| 39 | + size_t pos; |
| 40 | + while ((pos = current.find(chunk_separator)) != std::string::npos) { |
| 41 | + current_chunk.textdata += current.substr(0, pos + chunk_separator.size()); |
| 42 | + if ((int) current_chunk.textdata.size() > chunk_size) { |
| 43 | + // save chunk |
| 44 | + current_chunk.filepos = filepos; |
| 45 | + current_chunk.filename = filename; |
| 46 | + chunks.push_back(current_chunk); |
| 47 | + // update filepos |
| 48 | + filepos += (int) current_chunk.textdata.size(); |
| 49 | + // reset current_chunk |
| 50 | + current_chunk = chunk(); |
| 51 | + } |
| 52 | + current = current.substr(pos + chunk_separator.size()); |
| 53 | + } |
| 54 | + |
| 55 | + } |
| 56 | + // add leftover data to last chunk |
| 57 | + if (current_chunk.textdata.size() > 0) { |
| 58 | + if (chunks.empty()) { |
| 59 | + current_chunk.filepos = filepos; |
| 60 | + current_chunk.filename = filename; |
| 61 | + chunks.push_back(current_chunk); |
| 62 | + } else { |
| 63 | + chunks.back().textdata += current_chunk.textdata; |
| 64 | + } |
| 65 | + } |
| 66 | + f.close(); |
| 67 | + return chunks; |
| 68 | +} |
| 69 | + |
| 70 | +static void batch_add_seq(llama_batch & batch, const std::vector<int32_t> & tokens, int seq_id) { |
| 71 | + for (size_t i = 0; i < tokens.size(); i++) { |
| 72 | + llama_batch_add(batch, tokens[i], i, { seq_id }, i == tokens.size() - 1); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +static void batch_decode(llama_context * ctx, llama_batch & batch, float * output, int n_seq, int n_embd) { |
| 77 | + // clear previous kv_cache values (irrelevant for embeddings) |
| 78 | + llama_kv_cache_clear(ctx); |
| 79 | + |
| 80 | + // run model |
| 81 | + fprintf(stderr, "%s: n_tokens = %d, n_seq = %d\n", __func__, batch.n_tokens, n_seq); |
| 82 | + if (llama_decode(ctx, batch) < 0) { |
| 83 | + fprintf(stderr, "%s : failed to decode\n", __func__); |
| 84 | + } |
| 85 | + |
| 86 | + for (int i = 0; i < batch.n_tokens; i++) { |
| 87 | + if (!batch.logits[i]) { |
| 88 | + continue; |
| 89 | + } |
| 90 | + |
| 91 | + // try to get sequence embeddings - supported only when pooling_type is not NONE |
| 92 | + const float * embd = llama_get_embeddings_seq(ctx, batch.seq_id[i][0]); |
| 93 | + if (embd == NULL) { |
| 94 | + embd = llama_get_embeddings_ith(ctx, i); |
| 95 | + if (embd == NULL) { |
| 96 | + fprintf(stderr, "%s: failed to get embeddings for token %d\n", __func__, i); |
| 97 | + continue; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + float * out = output + batch.seq_id[i][0] * n_embd; |
| 102 | + llama_embd_normalize(embd, out, n_embd); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +int main(int argc, char ** argv) { |
| 107 | + gpt_params params; |
| 108 | + |
| 109 | + if (!gpt_params_parse(argc, argv, params)) { |
| 110 | + return 1; |
| 111 | + } |
| 112 | + |
| 113 | + if (params.chunk_size <= 0) { |
| 114 | + fprintf(stderr, "chunk_size must be positive\n"); |
| 115 | + return 1; |
| 116 | + } |
| 117 | + if (params.context_files.empty()) { |
| 118 | + fprintf(stderr, "context_files must be specified\n"); |
| 119 | + return 1; |
| 120 | + } |
| 121 | + params.embedding = true; |
| 122 | + |
| 123 | + print_build_info(); |
| 124 | + |
| 125 | + if (params.seed == LLAMA_DEFAULT_SEED) { |
| 126 | + params.seed = time(NULL); |
| 127 | + } |
| 128 | + |
| 129 | + printf("processing files:\n"); |
| 130 | + for (auto & context_file : params.context_files) { |
| 131 | + printf("%s\n", context_file.c_str()); |
| 132 | + } |
| 133 | + |
| 134 | + std::vector<chunk> chunks; |
| 135 | + for (auto & context_file : params.context_files) { |
| 136 | + std::vector<chunk> file_chunk = chunk_file(context_file, params.chunk_size, params.chunk_separator); |
| 137 | + chunks.insert(chunks.end(), file_chunk.begin(), file_chunk.end()); |
| 138 | + } |
| 139 | + printf("Number of chunks: %ld\n", chunks.size()); |
| 140 | + |
| 141 | + llama_backend_init(); |
| 142 | + llama_numa_init(params.numa); |
| 143 | + |
| 144 | + llama_model * model; |
| 145 | + llama_context * ctx; |
| 146 | + |
| 147 | + // load the model |
| 148 | + std::tie(model, ctx) = llama_init_from_gpt_params(params); |
| 149 | + if (model == NULL) { |
| 150 | + fprintf(stderr, "%s: error: unable to load model\n", __func__); |
| 151 | + return 1; |
| 152 | + } |
| 153 | + |
| 154 | + const int n_ctx_train = llama_n_ctx_train(model); |
| 155 | + const int n_ctx = llama_n_ctx(ctx); |
| 156 | + |
| 157 | + if (n_ctx > n_ctx_train) { |
| 158 | + fprintf(stderr, "%s: warning: model was trained on only %d context tokens (%d specified)\n", |
| 159 | + __func__, n_ctx_train, n_ctx); |
| 160 | + } |
| 161 | + |
| 162 | + // print system information |
| 163 | + { |
| 164 | + fprintf(stderr, "\n"); |
| 165 | + fprintf(stderr, "%s\n", get_system_info(params).c_str()); |
| 166 | + } |
| 167 | + |
| 168 | + // max batch size |
| 169 | + const uint64_t n_batch = params.n_batch; |
| 170 | + GGML_ASSERT(params.n_batch >= params.n_ctx); |
| 171 | + |
| 172 | + // tokenize the prompts and trim |
| 173 | + for (auto & chunk : chunks) { |
| 174 | + auto inp = ::llama_tokenize(ctx, chunk.textdata, true, false); |
| 175 | + if (inp.size() > n_batch) { |
| 176 | + inp.resize(n_batch); |
| 177 | + } |
| 178 | + // add eos if not present |
| 179 | + if (inp.empty() || inp.back() != llama_token_eos(model)) { |
| 180 | + inp.push_back(llama_token_eos(model)); |
| 181 | + } |
| 182 | + chunk.tokens = inp; |
| 183 | + } |
| 184 | + |
| 185 | + // tokenization stats |
| 186 | + if (params.verbose_prompt) { |
| 187 | + for (int i = 0; i < (int) chunks.size(); i++) { |
| 188 | + fprintf(stderr, "%s: prompt %d: '%s'\n", __func__, i, chunks[i].textdata.c_str()); |
| 189 | + fprintf(stderr, "%s: number of tokens in prompt = %zu\n", __func__, chunks[i].tokens.size()); |
| 190 | + for (int j = 0; j < (int) chunks[i].tokens.size(); j++) { |
| 191 | + fprintf(stderr, "%6d -> '%s'\n", chunks[i].tokens[j], llama_token_to_piece(ctx, chunks[i].tokens[j]).c_str()); |
| 192 | + } |
| 193 | + fprintf(stderr, "\n\n"); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + // initialize batch |
| 198 | + const int n_chunks = chunks.size(); |
| 199 | + struct llama_batch batch = llama_batch_init(n_batch, 0, 1); |
| 200 | + |
| 201 | + // allocate output |
| 202 | + const int n_embd = llama_n_embd(model); |
| 203 | + std::vector<float> embeddings(n_chunks * n_embd, 0); |
| 204 | + float * emb = embeddings.data(); |
| 205 | + |
| 206 | + // break into batches |
| 207 | + int p = 0; // number of prompts processed already |
| 208 | + int s = 0; // number of prompts in current batch |
| 209 | + for (int k = 0; k < n_chunks; k++) { |
| 210 | + // clamp to n_batch tokens |
| 211 | + auto & inp = chunks[k].tokens; |
| 212 | + |
| 213 | + const uint64_t n_toks = inp.size(); |
| 214 | + |
| 215 | + // encode if at capacity |
| 216 | + if (batch.n_tokens + n_toks > n_batch) { |
| 217 | + float * out = emb + p * n_embd; |
| 218 | + batch_decode(ctx, batch, out, s, n_embd); |
| 219 | + llama_batch_clear(batch); |
| 220 | + p += s; |
| 221 | + s = 0; |
| 222 | + } |
| 223 | + |
| 224 | + // add to batch |
| 225 | + batch_add_seq(batch, inp, s); |
| 226 | + s += 1; |
| 227 | + } |
| 228 | + |
| 229 | + // final batch |
| 230 | + float * out = emb + p * n_embd; |
| 231 | + batch_decode(ctx, batch, out, s, n_embd); |
| 232 | + |
| 233 | + // save embeddings to chunks |
| 234 | + for (int i = 0; i < n_chunks; i++) { |
| 235 | + chunks[i].embedding = std::vector<float>(emb + i * n_embd, emb + (i + 1) * n_embd); |
| 236 | + } |
| 237 | + |
| 238 | + // start loop, receive query and return top k similar chunks based on cosine similarity |
| 239 | + std::string query; |
| 240 | + while (true) { |
| 241 | + printf("Enter query: "); |
| 242 | + std::getline(std::cin, query); |
| 243 | + if (query == "exit" || query == "quit" || query == "q") { |
| 244 | + break; |
| 245 | + } |
| 246 | + std::vector<int32_t> query_tokens = llama_tokenize(ctx, query, true); |
| 247 | + |
| 248 | + struct llama_batch query_batch = llama_batch_init(n_batch, 0, 1); |
| 249 | + batch_add_seq(query_batch, query_tokens, 0); |
| 250 | + float * query_emb = new float[n_embd]; |
| 251 | + batch_decode(ctx, query_batch, query_emb, 1, n_embd); |
| 252 | + std::vector<float> query_embedding(query_emb, query_emb + n_embd); |
| 253 | + delete[] query_emb; |
| 254 | + llama_batch_clear(query_batch); |
| 255 | + |
| 256 | + for (int i = 0; i < n_chunks; i++) { |
| 257 | + float similarity = llama_embd_similarity_cos(chunks[i].embedding.data(), query_embedding.data(), n_embd); |
| 258 | + chunks[i].similarity = similarity; |
| 259 | + } |
| 260 | + std::sort(chunks.begin(), chunks.end(), [](chunk & a, chunk & b) { |
| 261 | + return a.similarity > b.similarity; |
| 262 | + }); |
| 263 | + printf("Top %d similar chunks:\n", params.sparams.top_k); |
| 264 | + for (int i = 0; i < std::min(params.sparams.top_k, (int) chunks.size()); i++) { |
| 265 | + printf("filename: %s\n", chunks[i].filename.c_str()); |
| 266 | + printf("filepos: %lld\n", chunks[i].filepos); |
| 267 | + printf("similarity: %f\n", chunks[i].similarity); |
| 268 | + printf("textdata:\n%s\n", chunks[i].textdata.c_str()); |
| 269 | + printf("--------------------\n"); |
| 270 | + } |
| 271 | + } |
| 272 | + |
| 273 | + // clean up |
| 274 | + llama_print_timings(ctx); |
| 275 | + llama_free(ctx); |
| 276 | + llama_free_model(model); |
| 277 | + llama_backend_free(); |
| 278 | +} |
0 commit comments