seismic 0.2.1

Seismic is designed for effective and efficient KNN retrieval over learned sparse embeddings.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "97bb4212",
   "metadata": {},
   "outputs": [],
   "source": [
    "from seismic import SeismicIndex"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c912a968",
   "metadata": {},
   "source": [
    "## Indexing"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee9a4a91",
   "metadata": {},
   "source": [
    "### Building"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c3696691",
   "metadata": {},
   "source": [
    "We can build the index either from a jsonl file or a compressed archive .tar.gz containing the jsonl file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2eefe4af",
   "metadata": {},
   "outputs": [],
   "source": [
    "json_input_file = \"\"\n",
    "compressed_input_file = \"\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "feb5215e",
   "metadata": {},
   "source": [
    "We can use the default configuration by specifying only the input file or choose each of the parameters."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "08e55de8",
   "metadata": {},
   "outputs": [],
   "source": [
    "index = SeismicIndex.build(json_input_file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5fb2b50c",
   "metadata": {},
   "outputs": [],
   "source": [
    "index = SeismicIndex.build(\n",
    "    compressed_input_file,\n",
    "    n_postings=3500,\n",
    "    centroid_fraction=0.1,\n",
    "    min_cluster_size=2,\n",
    "    summary_energy=0.4, \n",
    "    batched_indexing=10000000)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59d4942e",
   "metadata": {},
   "source": [
    "By setting the nknn parameter we can build the knn graph together with the index."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "80f6f899",
   "metadata": {},
   "outputs": [],
   "source": [
    "index = SeismicIndex.build(\n",
    "    json_input_file,\n",
    "    n_postings=3500,\n",
    "    centroid_fraction=0.1,\n",
    "    min_cluster_size=2,\n",
    "    summary_energy=0.4,\n",
    "    nknn=10,\n",
    "    batched_indexing=10000000)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "45135b50",
   "metadata": {},
   "source": [
    "While, if we set also the knn_path, we can add to the index a precomputed knn graph.\n",
    "In this case, the nknn parameter allow us to add a subset of the knn graph (with less neighbors)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8c5305aa",
   "metadata": {},
   "outputs": [],
   "source": [
    "knn_path = \"\"\n",
    "\n",
    "index = SeismicIndex.build(\n",
    "    json_input_file,\n",
    "    n_postings=3500,\n",
    "    centroid_fraction=0.1,\n",
    "    min_cluster_size=2,\n",
    "    summary_energy=0.4,\n",
    "    knn_path=knn_path,\n",
    "    nknn=5,\n",
    "    batched_indexing=10000000)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34eecf33",
   "metadata": {},
   "source": [
    "Once the index is constructed, we can serialize and store it in a file.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "06de631d",
   "metadata": {},
   "outputs": [],
   "source": [
    "index_path = \"\"\n",
    "\n",
    "index.save(index_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f572d70",
   "metadata": {},
   "source": [
    "### Loading"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a8c20f59",
   "metadata": {},
   "source": [
    "We may load a serialized index to query it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "44532aed",
   "metadata": {},
   "outputs": [],
   "source": [
    "index_path = \"\"\n",
    "\n",
    "index = SeismicIndex.load(index_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "00e1c150",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Number of documents: \", index.len)\n",
    "print(\"Avg number of non-zero components: \", index.nnz / index.len)\n",
    "print(\"Dimensionality of the vectors: \", index.dim)\n",
    "\n",
    "index.print_space_usage_byte()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f618f314",
   "metadata": {},
   "source": [
    "### KNN Graph\n",
    "\n",
    "Given an inverted index, we can build a knn graph and attach to it with the build_knn function.\n",
    "It is also possible to serialise the graph and then link it to another index with the load_knn function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "946322aa",
   "metadata": {},
   "outputs": [],
   "source": [
    "nknn=10\n",
    "index.build_knn(nknn)\n",
    "\n",
    "knn_path = \"\"\n",
    "\n",
    "index.save_knn(knn_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a0034c7",
   "metadata": {},
   "source": [
    "When adding the knn graph we can specify a subset of the neighbours we want for each entry of the index or load the full knn graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8f93fb6c",
   "metadata": {},
   "outputs": [],
   "source": [
    "index_path = \"\"\n",
    "knn_path = \"\"\n",
    "\n",
    "#load full knn graph\n",
    "index.load_knn2(knn_path)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f27085b7",
   "metadata": {},
   "outputs": [],
   "source": [
    "nknn = 5\n",
    "#load partial graph\n",
    "index.load_knn2(knn_path, nknn)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c73a4d8a",
   "metadata": {},
   "source": [
    "### Perform the search"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93ed4c1a",
   "metadata": {},
   "source": [
    "Prepare the data to perform the search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "46e2f0d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import json\n",
    "\n",
    "file_path = \"\"\n",
    "\n",
    "queries = []\n",
    "with open(file_path, 'r') as f:\n",
    "    for line in f:\n",
    "        queries.append(json.loads(line))\n",
    "\n",
    "MAX_TOKEN_LEN = 30\n",
    "string_type  = f'U{MAX_TOKEN_LEN}'\n",
    "\n",
    "queries_ids = np.array([q['id'] for q in queries], dtype=string_type)\n",
    "\n",
    "query_components = []\n",
    "query_values = []\n",
    "\n",
    "for query in queries:\n",
    "    vector = query['vector']\n",
    "    query_components.append(np.array(list(vector.keys()), dtype=string_type))\n",
    "    query_values.append(np.array(list(vector.values()), dtype=np.float32))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8286c10",
   "metadata": {},
   "source": [
    "We can ran a single search or a parallel batch search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8f68dc58",
   "metadata": {},
   "outputs": [],
   "source": [
    "results = index.search(\n",
    "    query_id=str(queries_ids[0]),\n",
    "    query_components=query_components[0],\n",
    "    query_values=query_values[0],\n",
    "    k=10,\n",
    "    query_cut=20,\n",
    "    heap_factor=0.7,\n",
    "    n_knn=0,\n",
    "    sorted=True,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5682de5f",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "results = index.batch_search(\n",
    "    queries_ids=queries_ids,\n",
    "    query_components=query_components,\n",
    "    query_values=query_values,\n",
    "    k=10,\n",
    "    query_cut=20,\n",
    "    heap_factor=0.7,\n",
    "    n_knn=0,\n",
    "    sorted=True,\n",
    "    num_threads=1,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c6b6cf4",
   "metadata": {},
   "source": [
    "## Evaluation of results\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4216faaa",
   "metadata": {},
   "source": [
    "Evaulation of the results with the ir_measure package for the choosen dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "90784514",
   "metadata": {},
   "outputs": [],
   "source": [
    "import ir_measures\n",
    "import ir_datasets\n",
    "\n",
    "ir_results = [ir_measures.ScoredDoc(query_id, doc_id, score) for r in results for (query_id, score, doc_id) in r]\n",
    "qrels = ir_datasets.load('msmarco-passage/dev/small').qrels\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c0aa5cd",
   "metadata": {},
   "outputs": [],
   "source": [
    "from ir_measures import *\n",
    "\n",
    "ir_measures.calc_aggregate([RR@10], qrels, ir_results)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a3a67154",
   "metadata": {},
   "source": [
    "# Raw Seismic Index\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7afb44c",
   "metadata": {},
   "source": [
    "Input file in Seismic inner format (this means that we have to provide a method to produce documents and queries in the seismic inner format)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9d2e63df",
   "metadata": {},
   "outputs": [],
   "source": [
    "from seismic import SeismicIndexRaw"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d2f21e7b",
   "metadata": {},
   "outputs": [],
   "source": [
    "input_path = \"\"\n",
    "\n",
    "index = SeismicIndexRaw.build(input_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5b3d2654",
   "metadata": {},
   "outputs": [],
   "source": [
    "queries_path=\"\"\n",
    "\n",
    "query_path=\"\"\n",
    "\n",
    "\n",
    "results = index.batch_search(\n",
    "    query_path,\n",
    "    k=10,\n",
    "    query_cut=3,\n",
    "    heap_factor=0.9,\n",
    "    n_knn=0,\n",
    "    sorted=True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}