-
Notifications
You must be signed in to change notification settings - Fork 113
/
KnnGraphTester.java
947 lines (890 loc) · 32.7 KB
/
KnnGraphTester.java
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import static org.apache.lucene.search.DocIdSetIterator.NO_MORE_DOCS;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
import java.util.Arrays;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashSet;
import java.util.Locale;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.Executors;
import org.apache.lucene.codecs.Codec;
import org.apache.lucene.codecs.KnnVectorsFormat;
import org.apache.lucene.codecs.KnnVectorsReader;
import org.apache.lucene.codecs.lucene99.Lucene99Codec;
import org.apache.lucene.codecs.lucene99.Lucene99HnswVectorsFormat;
import org.apache.lucene.codecs.lucene99.Lucene99HnswScalarQuantizedVectorsFormat;
import org.apache.lucene.codecs.lucene99.Lucene99HnswVectorsReader;
import org.apache.lucene.codecs.perfield.PerFieldKnnVectorsFormat;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.document.KnnByteVectorField;
import org.apache.lucene.document.KnnFloatVectorField;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.index.CodecReader;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.LeafReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.StoredFields;
import org.apache.lucene.index.VectorEncoding;
import org.apache.lucene.index.VectorSimilarityFunction;
import org.apache.lucene.search.ConstantScoreScorer;
import org.apache.lucene.search.ConstantScoreWeight;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.KnnFloatVectorQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryVisitor;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.ScoreMode;
import org.apache.lucene.search.Scorer;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.TotalHits;
import org.apache.lucene.search.Weight;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.BitSetIterator;
import org.apache.lucene.util.FixedBitSet;
import org.apache.lucene.util.NamedThreadFactory;
import org.apache.lucene.util.PrintStreamInfoStream;
import org.apache.lucene.util.SuppressForbidden;
import org.apache.lucene.util.hnsw.HnswGraph;
import org.apache.lucene.util.hnsw.NeighborQueue;
import org.apache.lucene.util.hppc.IntIntHashMap;
/**
* For testing indexing and search performance of a knn-graph
*
* <p>java -cp .../lib/*.jar org.apache.lucene.util.hnsw.KnnGraphTester -ndoc 1000000 -search
* .../vectors.bin
*/
public class KnnGraphTester {
private static final String KNN_FIELD = "knn";
private static final String ID_FIELD = "id";
private static final double WRITER_BUFFER_MB = 1994d;
private int numDocs;
private int dim;
private int topK;
private int numIters;
private int fanout;
private Path indexPath;
private boolean quiet;
private boolean reindex;
private boolean forceMerge;
private int reindexTimeMsec;
private int beamWidth;
private int maxConn;
private boolean quantize;
private int numMergeThread;
private int numMergeWorker;
private ExecutorService exec;
private VectorSimilarityFunction similarityFunction;
private VectorEncoding vectorEncoding;
private FixedBitSet matchDocs;
private float selectivity;
private boolean prefilter;
private boolean randomCommits;
private KnnGraphTester() {
// set defaults
numDocs = 1000;
numIters = 1000;
dim = 256;
topK = 100;
numMergeThread = 1;
numMergeWorker = 1;
fanout = topK;
similarityFunction = VectorSimilarityFunction.DOT_PRODUCT;
vectorEncoding = VectorEncoding.FLOAT32;
selectivity = 1f;
prefilter = false;
randomCommits = false;
}
public static void main(String... args) throws Exception {
new KnnGraphTester().runWithCleanUp(args);
}
private void runWithCleanUp(String... args) throws Exception {
try {
run(args);
} finally {
cleanUp();
}
}
private void cleanUp() {
if (exec != null) {
exec.shutdownNow();
}
}
private void run(String... args) throws Exception {
String operation = null;
Path docVectorsPath = null, queryPath = null, outputPath = null;
for (int iarg = 0; iarg < args.length; iarg++) {
String arg = args[iarg];
switch (arg) {
case "-search":
case "-check":
case "-stats":
case "-dump":
if (operation != null) {
throw new IllegalArgumentException(
"Specify only one operation, not both " + arg + " and " + operation);
}
operation = arg;
if (operation.equals("-search")) {
if (iarg == args.length - 1) {
throw new IllegalArgumentException(
"Operation " + arg + " requires a following pathname");
}
queryPath = Paths.get(args[++iarg]);
}
break;
case "-fanout":
if (iarg == args.length - 1) {
throw new IllegalArgumentException("-fanout requires a following number");
}
fanout = Integer.parseInt(args[++iarg]);
break;
case "-beamWidthIndex":
if (iarg == args.length - 1) {
throw new IllegalArgumentException("-beamWidthIndex requires a following number");
}
beamWidth = Integer.parseInt(args[++iarg]);
break;
case "-maxConn":
if (iarg == args.length - 1) {
throw new IllegalArgumentException("-maxConn requires a following number");
}
maxConn = Integer.parseInt(args[++iarg]);
break;
case "-dim":
if (iarg == args.length - 1) {
throw new IllegalArgumentException("-dim requires a following number");
}
dim = Integer.parseInt(args[++iarg]);
break;
case "-ndoc":
if (iarg == args.length - 1) {
throw new IllegalArgumentException("-ndoc requires a following number");
}
numDocs = Integer.parseInt(args[++iarg]);
break;
case "-niter":
if (iarg == args.length - 1) {
throw new IllegalArgumentException("-niter requires a following number");
}
numIters = Integer.parseInt(args[++iarg]);
break;
case "-reindex":
reindex = true;
break;
case "-quantize":
quantize = true;
break;
case "-topK":
if (iarg == args.length - 1) {
throw new IllegalArgumentException("-topK requires a following number");
}
topK = Integer.parseInt(args[++iarg]);
break;
case "-out":
outputPath = Paths.get(args[++iarg]);
break;
case "-docs":
docVectorsPath = Paths.get(args[++iarg]);
break;
case "-encoding":
String encoding = args[++iarg];
switch (encoding) {
case "byte":
vectorEncoding = VectorEncoding.BYTE;
break;
case "float32":
vectorEncoding = VectorEncoding.FLOAT32;
break;
default:
throw new IllegalArgumentException("-encoding can be 'byte' or 'float32' only");
}
break;
case "-metric":
String metric = args[++iarg];
switch (metric) {
case "mip":
similarityFunction = VectorSimilarityFunction.MAXIMUM_INNER_PRODUCT;
break;
case "cosine":
similarityFunction = VectorSimilarityFunction.COSINE;
break;
case "euclidean":
similarityFunction = VectorSimilarityFunction.EUCLIDEAN;
break;
case "angular":
similarityFunction = VectorSimilarityFunction.DOT_PRODUCT;
break;
default:
throw new IllegalArgumentException("-metric can be 'angular', 'euclidean', 'cosine', or 'mip' only");
}
break;
case "-forceMerge":
forceMerge = true;
break;
case "-prefilter":
prefilter = true;
break;
case "-randomCommits":
randomCommits = true;
break;
case "-filterSelectivity":
if (iarg == args.length - 1) {
throw new IllegalArgumentException("-filterSelectivity requires a following float");
}
selectivity = Float.parseFloat(args[++iarg]);
if (selectivity <= 0 || selectivity >= 1) {
throw new IllegalArgumentException("-filterSelectivity must be between 0 and 1");
}
break;
case "-quiet":
quiet = true;
break;
case "-numMergeWorker":
numMergeWorker = Integer.parseInt(args[++iarg]);
if (numMergeWorker <= 0) {
throw new IllegalArgumentException("-numMergeWorker should be >= 1");
}
break;
case "-numMergeThread":
numMergeThread = Integer.parseInt(args[++iarg]);
exec = Executors.newFixedThreadPool(numMergeThread, new NamedThreadFactory("hnsw-merge"));
if (numMergeThread <= 0) {
throw new IllegalArgumentException("-numMergeThread should be >= 1");
}
break;
default:
throw new IllegalArgumentException("unknown argument " + arg);
// usage();
}
}
if (operation == null && reindex == false) {
usage();
}
if (prefilter && selectivity == 1f) {
throw new IllegalArgumentException("-prefilter requires filterSelectivity between 0 and 1");
}
indexPath = Paths.get(formatIndexPath(docVectorsPath));
if (reindex) {
if (docVectorsPath == null) {
throw new IllegalArgumentException("-docs argument is required when indexing");
}
reindexTimeMsec = createIndex(docVectorsPath, indexPath);
}
if (forceMerge) {
forceMerge();
}
if (operation != null) {
switch (operation) {
case "-search":
if (docVectorsPath == null) {
throw new IllegalArgumentException("missing -docs arg");
}
if (selectivity < 1) {
matchDocs = generateRandomBitSet(numDocs, selectivity);
}
if (outputPath != null) {
testSearch(indexPath, queryPath, outputPath, null);
} else {
testSearch(indexPath, queryPath, null, getNN(docVectorsPath, queryPath));
}
break;
case "-stats":
printFanoutHist(indexPath);
break;
}
}
}
private String formatIndexPath(Path docsPath) {
return docsPath.getFileName() + "-" + maxConn + "-" + beamWidth + ".index";
}
@SuppressForbidden(reason = "Prints stuff")
private void printFanoutHist(Path indexPath) throws IOException {
try (Directory dir = FSDirectory.open(indexPath);
DirectoryReader reader = DirectoryReader.open(dir)) {
for (LeafReaderContext context : reader.leaves()) {
LeafReader leafReader = context.reader();
KnnVectorsReader vectorsReader =
((PerFieldKnnVectorsFormat.FieldsReader) ((CodecReader) leafReader).getVectorReader())
.getFieldReader(KNN_FIELD);
HnswGraph knnValues = ((Lucene99HnswVectorsReader) vectorsReader).getGraph(KNN_FIELD);
System.out.printf("Leaf %d has %d layers\n", context.ord, knnValues.numLevels());
System.out.printf("Leaf %d has %d documents\n", context.ord, leafReader.maxDoc());
printGraphFanout(knnValues, leafReader.maxDoc());
printGraphConnectedNess(knnValues);
}
}
}
@SuppressForbidden(reason = "Prints stuff")
private void forceMerge() throws IOException {
IndexWriterConfig iwc = new IndexWriterConfig().setOpenMode(IndexWriterConfig.OpenMode.APPEND);
iwc.setCodec(getCodec(maxConn, beamWidth, exec, numMergeWorker, quantize));
if (quiet == false) {
// not a quiet place!
iwc.setInfoStream(new PrintStreamInfoStream(System.out));
}
System.out.println("Force merge index in " + indexPath);
long start = System.currentTimeMillis();
try (IndexWriter iw = new IndexWriter(FSDirectory.open(indexPath), iwc)) {
iw.forceMerge(1);
}
System.out.println("Force merge done in: " + (System.currentTimeMillis() - start) + " ms");
}
@SuppressForbidden(reason = "Prints stuff")
private void printGraphConnectedNess(HnswGraph knnValues) throws IOException {
int numLevels = knnValues.numLevels();
for (int level = numLevels - 1; level >= 0; level--) {
HnswGraph.NodesIterator nodesIterator = knnValues.getNodesOnLevel(level);
int numNodesOnLayer = nodesIterator.size();
IntIntHashMap connectedNodes = new IntIntHashMap(numNodesOnLayer);
// Start at entry point and search all nodes on this level
int entryPoint = knnValues.entryNode();
Deque<Integer> stack = new ArrayDeque<>();
stack.push(entryPoint);
while (!stack.isEmpty()) {
int node = stack.pop();
if (connectedNodes.containsKey(node)) {
continue;
}
connectedNodes.put(node, 1);
knnValues.seek(level, node);
int friendOrd;
while ((friendOrd = knnValues.nextNeighbor()) != NO_MORE_DOCS) {
stack.push(friendOrd);
}
}
System.out.printf(
"Graph level=%d size=%d, connectedness=%.2f\n",
level, numNodesOnLayer, connectedNodes.size() / (float) numNodesOnLayer);
}
}
@SuppressForbidden(reason = "Prints stuff")
private void printGraphFanout(HnswGraph knnValues, int numDocs) throws IOException {
int numLevels = knnValues.numLevels();
for (int level = numLevels - 1; level >= 0; level--) {
int count = 0;
int min = Integer.MAX_VALUE, max = 0, total = 0;
HnswGraph.NodesIterator nodesIterator = knnValues.getNodesOnLevel(level);
int[] leafHist = new int[nodesIterator.size()];
while (nodesIterator.hasNext()) {
int node = nodesIterator.nextInt();
int n = 0;
knnValues.seek(level, node);
while (knnValues.nextNeighbor() != NO_MORE_DOCS) {
++n;
}
++leafHist[n];
max = Math.max(max, n);
min = Math.min(min, n);
if (n > 0) {
++count;
total += n;
}
}
System.out.printf(
"Graph level=%d size=%d, Fanout min=%d, mean=%.2f, max=%d\n",
level, count, min, total / (float) count, max);
printHist(leafHist, max, count, 10);
}
}
@SuppressForbidden(reason = "Prints stuff")
private void printHist(int[] hist, int max, int count, int nbuckets) {
System.out.print("%");
for (int i = 0; i <= nbuckets; i++) {
System.out.printf("%4d", i * 100 / nbuckets);
}
System.out.printf("\n %4d", hist[0]);
int total = 0, ibucket = 1;
for (int i = 1; i <= max && ibucket <= nbuckets; i++) {
total += hist[i];
while (total >= count * ibucket / nbuckets) {
System.out.printf("%4d", i);
++ibucket;
}
}
System.out.println();
}
@SuppressForbidden(reason = "Prints stuff")
private void testSearch(Path indexPath, Path queryPath, Path outputPath, int[][] nn)
throws IOException {
TopDocs[] results = new TopDocs[numIters];
long elapsed, totalCpuTime, totalVisited = 0;
ExecutorService executorService = Executors.newFixedThreadPool(8);
try (FileChannel input = FileChannel.open(queryPath)) {
VectorReader targetReader = VectorReader.create(input, dim, vectorEncoding);
if (quiet == false) {
System.out.println("running " + numIters + " targets; topK=" + topK + ", fanout=" + fanout);
}
long start;
ThreadMXBean bean = ManagementFactory.getThreadMXBean();
long cpuTimeStartNs;
try (Directory dir = FSDirectory.open(indexPath);
DirectoryReader reader = DirectoryReader.open(dir)) {
IndexSearcher searcher = new IndexSearcher(reader);
numDocs = reader.maxDoc();
Query bitSetQuery = prefilter ? new BitSetQuery(matchDocs) : null;
for (int i = 0; i < numIters; i++) {
// warm up
float[] target = targetReader.next();
if (prefilter) {
doKnnVectorQuery(searcher, KNN_FIELD, target, topK, fanout, bitSetQuery);
} else {
doKnnVectorQuery(searcher, KNN_FIELD, target, (int) (topK / selectivity), fanout, null);
}
}
targetReader.reset();
start = System.nanoTime();
cpuTimeStartNs = bean.getCurrentThreadCpuTime();
for (int i = 0; i < numIters; i++) {
float[] target = targetReader.next();
if (prefilter) {
results[i] = doKnnVectorQuery(searcher, KNN_FIELD, target, topK, fanout, bitSetQuery);
} else {
results[i] =
doKnnVectorQuery(
searcher, KNN_FIELD, target, (int) (topK / selectivity), fanout, null);
if (matchDocs != null) {
results[i].scoreDocs =
Arrays.stream(results[i].scoreDocs)
.filter(scoreDoc -> matchDocs.get(scoreDoc.doc))
.toArray(ScoreDoc[]::new);
}
}
}
totalCpuTime =
TimeUnit.NANOSECONDS.toMillis(bean.getCurrentThreadCpuTime() - cpuTimeStartNs);
elapsed = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start); // ns -> ms
StoredFields storedFields = reader.storedFields();
for (int i = 0; i < numIters; i++) {
totalVisited += results[i].totalHits.value;
for (ScoreDoc doc : results[i].scoreDocs) {
if (doc.doc != NO_MORE_DOCS) {
// there is a bug somewhere that can result in doc=NO_MORE_DOCS! I think it happens
// in some degenerate case (like input query has NaN in it?) that causes no results to
// be returned from HNSW search?
doc.doc = Integer.parseInt(storedFields.document(doc.doc).get("id"));
} else {
System.out.println("NO_MORE_DOCS!");
}
}
}
}
if (quiet == false) {
System.out.println(
"completed "
+ numIters
+ " searches in "
+ elapsed
+ " ms: "
+ ((1000 * numIters) / elapsed)
+ " QPS "
+ "CPU time="
+ totalCpuTime
+ "ms");
}
} finally {
executorService.shutdown();
}
if (outputPath != null) {
ByteBuffer buf = ByteBuffer.allocate(4);
IntBuffer ibuf = buf.order(ByteOrder.LITTLE_ENDIAN).asIntBuffer();
try (OutputStream out = Files.newOutputStream(outputPath)) {
for (int i = 0; i < numIters; i++) {
for (ScoreDoc doc : results[i].scoreDocs) {
ibuf.position(0);
ibuf.put(doc.doc);
out.write(buf.array());
}
}
}
} else {
if (quiet == false) {
System.out.println("checking results");
}
float recall = checkResults(results, nn);
totalVisited /= numIters;
System.out.printf(
Locale.ROOT,
"%5.3f\t%5.2f\t%d\t%d\t%d\t%d\t%d\t%d\t%.2f\t%s\n",
recall,
totalCpuTime / (float) numIters,
numDocs,
fanout,
maxConn,
beamWidth,
totalVisited,
reindexTimeMsec,
selectivity,
prefilter ? "pre-filter" : "post-filter");
}
}
private abstract static class VectorReader {
final float[] target;
final ByteBuffer bytes;
final FileChannel input;
static VectorReader create(FileChannel input, int dim, VectorEncoding vectorEncoding) {
int bufferSize = dim * vectorEncoding.byteSize;
return switch (vectorEncoding) {
case BYTE -> new VectorReaderByte(input, dim, bufferSize);
case FLOAT32 -> new VectorReaderFloat32(input, dim, bufferSize);
};
}
VectorReader(FileChannel input, int dim, int bufferSize) {
this.bytes = ByteBuffer.wrap(new byte[bufferSize]).order(ByteOrder.LITTLE_ENDIAN);
this.input = input;
target = new float[dim];
}
void reset() throws IOException {
input.position(0);
}
protected final void readNext() throws IOException {
this.input.read(bytes);
bytes.position(0);
}
abstract float[] next() throws IOException;
}
private static class VectorReaderFloat32 extends VectorReader {
VectorReaderFloat32(FileChannel input, int dim, int bufferSize) {
super(input, dim, bufferSize);
}
@Override
float[] next() throws IOException {
readNext();
bytes.asFloatBuffer().get(target);
return target;
}
}
private static class VectorReaderByte extends VectorReader {
private final byte[] scratch;
VectorReaderByte(FileChannel input, int dim, int bufferSize) {
super(input, dim, bufferSize);
scratch = new byte[dim];
}
@Override
float[] next() throws IOException {
readNext();
bytes.get(scratch);
for (int i = 0; i < scratch.length; i++) {
target[i] = scratch[i];
}
return target;
}
byte[] nextBytes() throws IOException {
readNext();
bytes.get(scratch);
return scratch;
}
}
private static TopDocs doKnnVectorQuery(
IndexSearcher searcher, String field, float[] vector, int k, int fanout, Query filter)
throws IOException {
ProfiledKnnFloatVectorQuery profiledQuery = new ProfiledKnnFloatVectorQuery(field, vector, k, fanout, filter);
TopDocs docs = searcher.search(profiledQuery, k);
return new TopDocs(new TotalHits(profiledQuery.totalVectorCount(), docs.totalHits.relation), docs.scoreDocs);
}
private float checkResults(TopDocs[] results, int[][] nn) {
int totalMatches = 0;
int totalResults = results.length * topK;
for (int i = 0; i < results.length; i++) {
// System.out.println(Arrays.toString(nn[i]));
// System.out.println(Arrays.toString(results[i].scoreDocs));
totalMatches += compareNN(nn[i], results[i]);
}
return totalMatches / (float) totalResults;
}
private int compareNN(int[] expected, TopDocs results) {
int matched = 0;
/*
System.out.print("expected=");
for (int j = 0; j < expected.length; j++) {
System.out.print(expected[j]);
System.out.print(", ");
}
System.out.print('\n');
System.out.println("results=");
for (int j = 0; j < results.scoreDocs.length; j++) {
System.out.print("" + results.scoreDocs[j].doc + ":" + results.scoreDocs[j].score + ", ");
}
System.out.print('\n');
*/
Set<Integer> expectedSet = new HashSet<>();
for (int i = 0; i < topK; i++) {
expectedSet.add(expected[i]);
}
for (ScoreDoc scoreDoc : results.scoreDocs) {
if (expectedSet.contains(scoreDoc.doc)) {
++matched;
}
}
return matched;
}
private int[][] getNN(Path docPath, Path queryPath) throws IOException {
// look in working directory for cached nn file
String hash = Integer.toString(Objects.hash(docPath, queryPath, numDocs, numIters, topK, similarityFunction.ordinal()), 36);
String nnFileName = "nn-" + hash + ".bin";
Path nnPath = Paths.get(nnFileName);
if (Files.exists(nnPath) && isNewer(nnPath, docPath, queryPath) && selectivity == 1f) {
return readNN(nnPath);
} else {
// TODO: enable computing NN from high precision vectors when
// checking low-precision recall
int[][] nn = computeNN(docPath, queryPath, vectorEncoding);
if (selectivity == 1f) {
writeNN(nn, nnPath);
}
return nn;
}
}
private boolean isNewer(Path path, Path... others) throws IOException {
FileTime modified = Files.getLastModifiedTime(path);
for (Path other : others) {
if (Files.getLastModifiedTime(other).compareTo(modified) >= 0) {
return false;
}
}
return true;
}
private int[][] readNN(Path nnPath) throws IOException {
int[][] result = new int[numIters][];
try (FileChannel in = FileChannel.open(nnPath)) {
IntBuffer intBuffer =
in.map(FileChannel.MapMode.READ_ONLY, 0, numIters * topK * Integer.BYTES)
.order(ByteOrder.LITTLE_ENDIAN)
.asIntBuffer();
for (int i = 0; i < numIters; i++) {
result[i] = new int[topK];
intBuffer.get(result[i]);
}
}
return result;
}
private void writeNN(int[][] nn, Path nnPath) throws IOException {
if (quiet == false) {
System.out.println("writing true nearest neighbors to " + nnPath);
}
ByteBuffer tmp =
ByteBuffer.allocate(nn[0].length * Integer.BYTES).order(ByteOrder.LITTLE_ENDIAN);
try (OutputStream out = Files.newOutputStream(nnPath)) {
for (int i = 0; i < numIters; i++) {
tmp.asIntBuffer().put(nn[i]);
out.write(tmp.array());
}
}
}
@SuppressForbidden(reason = "Uses random()")
private static FixedBitSet generateRandomBitSet(int size, float selectivity) {
FixedBitSet bitSet = new FixedBitSet(size);
for (int i = 0; i < size; i++) {
if (Math.random() < selectivity) {
bitSet.set(i);
} else {
bitSet.clear(i);
}
}
return bitSet;
}
private int[][] computeNN(Path docPath, Path queryPath, VectorEncoding encoding)
throws IOException {
int[][] result = new int[numIters][];
if (quiet == false) {
System.out.println("computing true nearest neighbors of " + numIters + " target vectors");
}
try (FileChannel in = FileChannel.open(docPath);
FileChannel qIn = FileChannel.open(queryPath)) {
VectorReader docReader = VectorReader.create(in, dim, encoding);
VectorReader queryReader = VectorReader.create(qIn, dim, encoding);
for (int i = 0; i < numIters; i++) {
float[] query = queryReader.next();
NeighborQueue queue = new NeighborQueue(topK, false);
for (int j = 0; j < numDocs; j++) {
float[] doc = docReader.next();
float d = similarityFunction.compare(query, doc);
if (matchDocs == null || matchDocs.get(j)) {
queue.insertWithOverflow(j, d);
}
}
docReader.reset();
result[i] = new int[topK];
for (int k = topK - 1; k >= 0; k--) {
result[i][k] = queue.topNode();
queue.pop();
// System.out.print(" " + n);
}
if (quiet == false && (i + 1) % 10 == 0) {
System.out.print(" " + (i + 1));
System.out.flush();
}
}
}
return result;
}
private int createIndex(Path docsPath, Path indexPath) throws IOException {
IndexWriterConfig iwc = new IndexWriterConfig().setOpenMode(IndexWriterConfig.OpenMode.CREATE);
iwc.setCodec(getCodec(maxConn, beamWidth, exec, numMergeWorker, quantize));
// iwc.setMergePolicy(NoMergePolicy.INSTANCE);
iwc.setRAMBufferSizeMB(WRITER_BUFFER_MB);
iwc.setUseCompoundFile(false);
FieldType fieldType =
switch (vectorEncoding) {
case BYTE -> KnnByteVectorField.createFieldType(dim, similarityFunction);
case FLOAT32 -> KnnFloatVectorField.createFieldType(dim, similarityFunction);
};
if (quiet == false) {
iwc.setInfoStream(new PrintStreamInfoStream(System.out));
System.out.println("creating index in " + indexPath);
}
long start = System.nanoTime();
try (FSDirectory dir = FSDirectory.open(indexPath);
IndexWriter iw = new IndexWriter(dir, iwc)) {
try (FileChannel in = FileChannel.open(docsPath)) {
VectorReader vectorReader = VectorReader.create(in, dim, vectorEncoding);
for (int i = 0; i < numDocs; i++) {
Document doc = new Document();
switch (vectorEncoding) {
case BYTE -> doc.add(
new KnnByteVectorField(
KNN_FIELD, ((VectorReaderByte) vectorReader).nextBytes(), fieldType));
case FLOAT32 -> doc.add(
new KnnFloatVectorField(KNN_FIELD, vectorReader.next(), fieldType));
}
doc.add(new StoredField(ID_FIELD, i));
iw.addDocument(doc);
if (randomCommits && i % 500 == 0 && Math.random() < 0.5) {
iw.commit();
}
}
if (quiet == false) {
System.out.println("Done indexing " + numDocs + " documents; now flush");
}
}
}
long elapsed = System.nanoTime() - start;
if (quiet == false) {
System.out.println(
"Indexed " + numDocs + " documents in " + TimeUnit.NANOSECONDS.toSeconds(elapsed) + "s");
}
return (int) TimeUnit.NANOSECONDS.toMillis(elapsed);
}
private static Codec getCodec(int maxConn, int beamWidth, ExecutorService exec, int numMergeWorker, boolean quantize) {
if (exec == null) {
return new Lucene99Codec() {
@Override
public KnnVectorsFormat getKnnVectorsFormatForField(String field) {
return quantize ?
new Lucene99HnswScalarQuantizedVectorsFormat(maxConn, beamWidth) :
new Lucene99HnswVectorsFormat(maxConn, beamWidth);
}
};
} else {
return new Lucene99Codec() {
@Override
public KnnVectorsFormat getKnnVectorsFormatForField(String field) {
return quantize ?
new Lucene99HnswScalarQuantizedVectorsFormat(maxConn, beamWidth, numMergeWorker, null, exec) :
new Lucene99HnswVectorsFormat(maxConn, beamWidth, numMergeWorker, exec);
}
};
}
}
private static void usage() {
String error =
"Usage: TestKnnGraph [-reindex] [-search {queryfile}|-stats|-check] [-docs {datafile}] [-niter N] [-fanout N] [-maxConn N] [-beamWidth N] [-filterSelectivity N] [-prefilter]";
System.err.println(error);
System.exit(1);
}
private static class ProfiledKnnFloatVectorQuery extends KnnFloatVectorQuery {
private final Query filter;
private final int k;
private final int fanout;
private final String field;
private final float[] target;
private long totalVectorCount;
ProfiledKnnFloatVectorQuery(String field, float[] target, int k, int fanout, Query filter) {
super(field, target, k + fanout, filter);
this.field = field;
this.target = target;
this.k = k;
this.fanout = fanout;
this.filter = filter;
}
@Override
protected TopDocs mergeLeafResults(TopDocs[] perLeafResults) {
TopDocs td = TopDocs.merge(k, perLeafResults);
totalVectorCount = td.totalHits.value;
return td;
}
long totalVectorCount() {
return totalVectorCount;
}
}
private static class BitSetQuery extends Query {
private final FixedBitSet docs;
private final int cardinality;
BitSetQuery(FixedBitSet docs) {
this.docs = docs;
this.cardinality = docs.cardinality();
}
@Override
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost)
throws IOException {
return new ConstantScoreWeight(this, boost) {
@Override
public Scorer scorer(LeafReaderContext context) throws IOException {
return new ConstantScoreScorer(
this, score(), scoreMode, new BitSetIterator(docs, cardinality));
}
@Override
public boolean isCacheable(LeafReaderContext ctx) {
return false;
}
};
}
@Override
public void visit(QueryVisitor visitor) {}
@Override
public String toString(String field) {
return "BitSetQuery";
}
@Override
public boolean equals(Object other) {
return sameClassAs(other) && docs.equals(((BitSetQuery) other).docs);
}
@Override
public int hashCode() {
return 31 * classHash() + docs.hashCode();
}
}
}