Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Faiss codec for KNN searches #14178

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion gradle/generation/extract-jdk-apis.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,23 @@ def resources = scriptResources(buildscript)

configure(project(":lucene:core")) {
ext {
apijars = layout.projectDirectory.dir("src/generated/jdk")
mrjarJavaVersions = [ 21 ]
}
}

configure(project(":lucene:sandbox")) {
ext {
mrjarJavaVersions = [ 22 ]
}
}

configure([
project(":lucene:core"),
project(":lucene:sandbox")
]) {
ext {
apijars = layout.projectDirectory.dir("src/generated/jdk")
}

configurations {
apiextractor
Expand Down
3 changes: 2 additions & 1 deletion gradle/generation/extract-jdk-apis/ExtractJdkApis.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public final class ExtractJdkApis {
private static final String PATTERN_VECTOR_VM_INTERNALS = "java.base/jdk/internal/vm/vector/VectorSupport{,$Vector,$VectorMask,$VectorPayload,$VectorShuffle}";

static final Map<Integer,List<String>> CLASSFILE_PATTERNS = Map.of(
21, List.of(PATTERN_PANAMA_FOREIGN, PATTERN_VECTOR_VM_INTERNALS, PATTERN_VECTOR_INCUBATOR)
21, List.of(PATTERN_PANAMA_FOREIGN, PATTERN_VECTOR_VM_INTERNALS, PATTERN_VECTOR_INCUBATOR),
22, List.of(PATTERN_PANAMA_FOREIGN)
);

public static void main(String... args) throws IOException {
Expand Down
1 change: 1 addition & 0 deletions gradle/generation/regenerate.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ configure([
project(":lucene:queryparser"),
project(":lucene:expressions"),
project(":lucene:test-framework"),
project(":lucene:sandbox"),
]) {
task regenerate() {
description "Rerun any code or static data generation tasks."
Expand Down
5 changes: 4 additions & 1 deletion gradle/java/core-mrjar.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

// Produce an MR-JAR with Java 19+ foreign and vector implementations

configure(project(":lucene:core")) {
configure([
project(":lucene:core"),
project(":lucene:sandbox")
]) {
plugins.withType(JavaPlugin) {
mrjarJavaVersions.each { jdkVersion ->
sourceSets.create("main${jdkVersion}") {
Expand Down
Binary file added lucene/sandbox/src/generated/jdk/jdk22.apijar
Binary file not shown.
4 changes: 4 additions & 0 deletions lucene/sandbox/src/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
requires org.apache.lucene.core;
requires org.apache.lucene.queries;
requires org.apache.lucene.facet;
requires java.logging;

exports org.apache.lucene.payloads;
exports org.apache.lucene.sandbox.codecs.faiss;
exports org.apache.lucene.sandbox.codecs.idversion;
exports org.apache.lucene.sandbox.codecs.quantization;
exports org.apache.lucene.sandbox.document;
Expand All @@ -37,4 +39,6 @@

provides org.apache.lucene.codecs.PostingsFormat with
org.apache.lucene.sandbox.codecs.idversion.IDVersionPostingsFormat;
provides org.apache.lucene.codecs.KnnVectorsFormat with
org.apache.lucene.sandbox.codecs.faiss.FaissKnnVectorsFormatProvider;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* 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.
*/
package org.apache.lucene.sandbox.codecs.faiss;

import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.Arrays;
import java.util.logging.Logger;
import org.apache.lucene.codecs.KnnVectorsFormat;
import org.apache.lucene.codecs.KnnVectorsReader;
import org.apache.lucene.codecs.KnnVectorsWriter;
import org.apache.lucene.index.SegmentReadState;
import org.apache.lucene.index.SegmentWriteState;

/**
* Wraps <a href="https://github.com/facebookresearch/faiss">Faiss</a> to create and search vector
* indexes. This class is mainly for backwards compatibility with older versions of Java (&lt;22),
* use underlying format directly after upgrade.
*
* @lucene.experimental
*/
public class FaissKnnVectorsFormatProvider extends KnnVectorsFormat {
private final KnnVectorsFormat delegate;

public FaissKnnVectorsFormatProvider() {
this(new Object[0]);
}

public FaissKnnVectorsFormatProvider(Object... args) {
super(FaissKnnVectorsFormatProvider.class.getSimpleName());

KnnVectorsFormat delegate;
try {
Class<?> cls =
MethodHandles.lookup()
.findClass("org.apache.lucene.sandbox.codecs.faiss.FaissKnnVectorsFormat");

MethodType methodType =
MethodType.methodType(
void.class, Arrays.stream(args).map(Object::getClass).toArray(Class<?>[]::new));

delegate =
(KnnVectorsFormat)
MethodHandles.lookup().findConstructor(cls, methodType).invokeWithArguments(args);

} catch (
@SuppressWarnings("unused")
ClassNotFoundException e) {

delegate = null;
Logger.getLogger(getClass().getName())
.warning("FaissKnnVectorsFormat class missing, this object is unusable!");

} catch (NoSuchMethodException | IllegalAccessException e) {
throw new LinkageError("FaissKnnVectorsFormat is missing correctly typed constructor", e);
} catch (Throwable t) {
throw new RuntimeException(t);
}
this.delegate = delegate;
}

@Override
public KnnVectorsWriter fieldsWriter(SegmentWriteState state) throws IOException {
return delegate.fieldsWriter(state);
}

@Override
public KnnVectorsReader fieldsReader(SegmentReadState state) throws IOException {
return delegate.fieldsReader(state);
}

@Override
public int getMaxDimensions(String fieldName) {
return delegate.getMaxDimensions(fieldName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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.
*/
/**
* Wraps <a href="https://github.com/facebookresearch/faiss">Faiss</a> to create and search vector
* indexes via {@link org.apache.lucene.sandbox.codecs.faiss.FaissKnnVectorsFormatProvider}.
*/
package org.apache.lucene.sandbox.codecs.faiss;
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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.
*/
package org.apache.lucene.sandbox.codecs.faiss;

import java.io.IOException;
import java.util.Locale;
import org.apache.lucene.codecs.KnnVectorsFormat;
import org.apache.lucene.codecs.KnnVectorsReader;
import org.apache.lucene.codecs.KnnVectorsWriter;
import org.apache.lucene.codecs.hnsw.FlatVectorScorerUtil;
import org.apache.lucene.codecs.lucene99.Lucene99FlatVectorsFormat;
import org.apache.lucene.index.SegmentReadState;
import org.apache.lucene.index.SegmentWriteState;

public final class FaissKnnVectorsFormat extends KnnVectorsFormat {
public static final String NAME = FaissKnnVectorsFormat.class.getSimpleName();
static final int VERSION_START = 0;
static final int VERSION_CURRENT = VERSION_START;
static final String META_CODEC_NAME = NAME + "Meta";
static final String DATA_CODEC_NAME = NAME + "Data";
static final String META_EXTENSION = "faissm";
static final String DATA_EXTENSION = "faissd";

private final String description;
private final String indexParams;
private final KnnVectorsFormat rawVectorsFormat;

@SuppressWarnings("unused")
public FaissKnnVectorsFormat() {
this("IDMap,HNSW32", "efConstruction=200");
}

public FaissKnnVectorsFormat(String description, String indexParams) {
super(NAME);
this.description = description;
this.indexParams = indexParams;
this.rawVectorsFormat =
new Lucene99FlatVectorsFormat(FlatVectorScorerUtil.getLucene99FlatVectorsScorer());
Comment on lines +51 to +52
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since Faiss already stores this information in the index what is the reason to have a RawFlatVectorsFormat?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not all Faiss indexes store the original vectors (eg PQ) -- and trying to "reconstruct" vectors may be lossy..

This primarily affects merges, where vectors in smaller segments are read back to create a fresh one -- so we'd keep losing information with each merge

In the ideal scenario we could use Faiss if the index stores full vectors (eg HNSWFlat), and only add raw vectors to Lucene for other indexes. For now I wasn't 100% sure on how to determine this, so I'm storing them in Lucene in all cases

Note that these would only be loaded into memory during indexing, and not search (they aren't accessed by Faiss)

Another point to note is that reading back vectors from Faiss has its own cost (latency if we read them one-by-one, or memory in case of a bulk read and we may need some sort of batching)

}

@Override
public KnnVectorsWriter fieldsWriter(SegmentWriteState state) throws IOException {
return new FaissKnnVectorsWriter(
description, indexParams, state, rawVectorsFormat.fieldsWriter(state));
}

@Override
public KnnVectorsReader fieldsReader(SegmentReadState state) throws IOException {
return new FaissKnnVectorsReader(state, rawVectorsFormat.fieldsReader(state));
}

@Override
public int getMaxDimensions(String fieldName) {
return DEFAULT_MAX_DIMENSIONS;
}

@Override
public String toString() {
return String.format(
Locale.ROOT, "%s(description=%s indexParams=%s)", NAME, description, indexParams);
}
}
Loading