Skip to content

Commit 0245e81

Browse files
committed
Add bfloat16 to bbq_hnsw
1 parent 6e27ccb commit 0245e81

14 files changed

+205
-457
lines changed

qa/vector/src/main/java/org/elasticsearch/test/knn/KnnIndexTester.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
import org.elasticsearch.index.codec.vectors.diskbbq.ES920DiskBBQVectorsFormat;
3434
import org.elasticsearch.index.codec.vectors.es818.ES818BinaryQuantizedVectorsFormat;
3535
import org.elasticsearch.index.codec.vectors.es818.ES818HnswBinaryQuantizedVectorsFormat;
36-
import org.elasticsearch.index.codec.vectors.es92.ES92BinaryQuantizedBFloat16VectorsFormat;
37-
import org.elasticsearch.index.codec.vectors.es92.ES92HnswBinaryQuantizedBFloat16VectorsFormat;
3836
import org.elasticsearch.logging.Level;
3937
import org.elasticsearch.logging.LogManager;
4038
import org.elasticsearch.logging.Logger;
@@ -127,17 +125,9 @@ static Codec createCodec(CmdLineArgs args) {
127125
} else {
128126
if (args.quantizeBits() == 1) {
129127
if (args.indexType() == IndexType.FLAT) {
130-
if (args.rawVectorSize() == 16) {
131-
format = new ES92BinaryQuantizedBFloat16VectorsFormat();
132-
} else {
133-
format = new ES818BinaryQuantizedVectorsFormat();
134-
}
128+
format = new ES818BinaryQuantizedVectorsFormat();
135129
} else {
136-
if (args.rawVectorSize() == 16) {
137-
format = new ES92HnswBinaryQuantizedBFloat16VectorsFormat(args.hnswM(), args.hnswEfConstruction(), 1, null);
138-
} else {
139-
format = new ES818HnswBinaryQuantizedVectorsFormat(args.hnswM(), args.hnswEfConstruction(), 1, null);
140-
}
130+
format = new ES818HnswBinaryQuantizedVectorsFormat(args.hnswM(), args.hnswEfConstruction(), 1, null);
141131
}
142132
} else if (args.quantizeBits() < 32) {
143133
if (args.indexType() == IndexType.FLAT) {

server/src/main/java/module-info.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,6 @@
491491
exports org.elasticsearch.index.codec.perfield;
492492
exports org.elasticsearch.index.codec.vectors to org.elasticsearch.test.knn, org.elasticsearch.gpu;
493493
exports org.elasticsearch.index.codec.vectors.es818 to org.elasticsearch.test.knn;
494-
exports org.elasticsearch.index.codec.vectors.es92 to org.elasticsearch.test.knn;
495494
exports org.elasticsearch.inference.telemetry;
496495
exports org.elasticsearch.index.codec.vectors.diskbbq to org.elasticsearch.test.knn;
497496
exports org.elasticsearch.index.codec.vectors.cluster to org.elasticsearch.test.knn;

server/src/main/java/org/elasticsearch/index/codec/vectors/DirectIOCapableFlatVectorsFormat.java

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,81 @@
1111

1212
import org.apache.lucene.codecs.hnsw.FlatVectorsReader;
1313
import org.apache.lucene.index.SegmentReadState;
14+
import org.apache.lucene.store.FlushInfo;
15+
import org.apache.lucene.store.IOContext;
16+
import org.apache.lucene.store.MergeInfo;
17+
import org.elasticsearch.common.util.set.Sets;
18+
import org.elasticsearch.index.codec.vectors.es818.DirectIOHint;
19+
import org.elasticsearch.index.store.FsDirectoryFactory;
1420

1521
import java.io.IOException;
22+
import java.util.Set;
1623

1724
public abstract class DirectIOCapableFlatVectorsFormat extends AbstractFlatVectorsFormat {
1825
protected DirectIOCapableFlatVectorsFormat(String name) {
1926
super(name);
2027
}
2128

29+
protected abstract FlatVectorsReader createReader(SegmentReadState state) throws IOException;
30+
31+
static boolean canUseDirectIO(SegmentReadState state) {
32+
return FsDirectoryFactory.isHybridFs(state.directory);
33+
}
34+
2235
@Override
2336
public FlatVectorsReader fieldsReader(SegmentReadState state) throws IOException {
2437
return fieldsReader(state, false);
2538
}
2639

27-
public abstract FlatVectorsReader fieldsReader(SegmentReadState state, boolean useDirectIO) throws IOException;
40+
public FlatVectorsReader fieldsReader(SegmentReadState state, boolean useDirectIO) throws IOException {
41+
if (state.context.context() == IOContext.Context.DEFAULT && useDirectIO && canUseDirectIO(state)) {
42+
// only override the context for the random-access use case
43+
SegmentReadState directIOState = new SegmentReadState(
44+
state.directory,
45+
state.segmentInfo,
46+
state.fieldInfos,
47+
new DirectIOContext(state.context.hints()),
48+
state.segmentSuffix
49+
);
50+
// Use mmap for merges and direct I/O for searches.
51+
return new MergeReaderWrapper(createReader(directIOState), createReader(state));
52+
} else {
53+
return createReader(state);
54+
}
55+
}
56+
57+
static class DirectIOContext implements IOContext {
58+
59+
final Set<FileOpenHint> hints;
60+
61+
DirectIOContext(Set<FileOpenHint> hints) {
62+
// always add DirectIOHint to the hints given
63+
this.hints = Sets.union(hints, Set.of(DirectIOHint.INSTANCE));
64+
}
65+
66+
@Override
67+
public Context context() {
68+
return Context.DEFAULT;
69+
}
70+
71+
@Override
72+
public MergeInfo mergeInfo() {
73+
return null;
74+
}
75+
76+
@Override
77+
public FlushInfo flushInfo() {
78+
return null;
79+
}
80+
81+
@Override
82+
public Set<FileOpenHint> hints() {
83+
return hints;
84+
}
85+
86+
@Override
87+
public IOContext withHints(FileOpenHint... hints) {
88+
return new DirectIOContext(Set.of(hints));
89+
}
90+
}
2891
}

server/src/main/java/org/elasticsearch/index/codec/vectors/es92/ES92BFloat16FlatVectorsFormat.java

Lines changed: 0 additions & 128 deletions
This file was deleted.

server/src/main/java/org/elasticsearch/index/codec/vectors/es92/ES92BinaryQuantizedBFloat16VectorsFormat.java

Lines changed: 0 additions & 121 deletions
This file was deleted.

0 commit comments

Comments
 (0)