Skip to content

SOLR-17780: Add support for scalar quantized dense vectors #3385

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

Open
wants to merge 4 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
23 changes: 11 additions & 12 deletions solr/core/src/java/org/apache/solr/core/SchemaCodecFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,24 @@
import org.apache.lucene.codecs.PostingsFormat;
import org.apache.lucene.codecs.lucene912.Lucene912Codec;
import org.apache.lucene.codecs.lucene912.Lucene912Codec.Mode;
import org.apache.lucene.codecs.lucene99.Lucene99HnswScalarQuantizedVectorsFormat;
import org.apache.lucene.codecs.lucene99.Lucene99HnswVectorsFormat;
import org.apache.lucene.codecs.lucene99.Lucene99ScalarQuantizedVectorsFormat;
import org.apache.lucene.index.SegmentReadState;
import org.apache.lucene.index.SegmentWriteState;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrException.ErrorCode;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.schema.DenseVectorField;
import org.apache.solr.schema.FieldType;
import org.apache.solr.schema.ScalarQuantizedDenseVectorField;
import org.apache.solr.schema.SchemaField;
import org.apache.solr.util.plugin.SolrCoreAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.apache.lucene.codecs.lucene99.Lucene99HnswVectorsFormat.DEFAULT_NUM_MERGE_WORKER;

/**
* Per-field CodecFactory implementation, extends Lucene's and returns postings format
* implementations according to the schema configuration. <br>
Expand Down Expand Up @@ -126,19 +131,13 @@ public DocValuesFormat getDocValuesFormatForField(String field) {
public KnnVectorsFormat getKnnVectorsFormatForField(String field) {
final SchemaField schemaField = core.getLatestSchema().getFieldOrNull(field);
FieldType fieldType = (schemaField == null ? null : schemaField.getType());
if (fieldType instanceof DenseVectorField vectorType) {
String knnAlgorithm = vectorType.getKnnAlgorithm();
if (DenseVectorField.HNSW_ALGORITHM.equals(knnAlgorithm)) {
int maxConn = vectorType.getHnswMaxConn();
int beamWidth = vectorType.getHnswBeamWidth();
var delegate = new Lucene99HnswVectorsFormat(maxConn, beamWidth);
return new SolrDelegatingKnnVectorsFormat(delegate, vectorType.getDimension());
} else {
throw new SolrException(
ErrorCode.SERVER_ERROR, knnAlgorithm + " KNN algorithm is not supported");
}
if (fieldType instanceof DenseVectorField) {
final DenseVectorField vectorField = (DenseVectorField) fieldType;
return new SolrDelegatingKnnVectorsFormat(vectorField.buildKnnVectorsFormat(),
vectorField.getDimension());
} else {
throw new SolrException(ErrorCode.SERVER_ERROR, "field is not a support KNN vector type");
}
return super.getKnnVectorsFormatForField(field);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Locale;
import java.util.Map;
import org.apache.lucene.codecs.KnnVectorsFormat;
import org.apache.lucene.codecs.lucene99.Lucene99HnswVectorsFormat;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.document.KnnByteVectorField;
import org.apache.lucene.document.KnnFloatVectorField;
Expand Down Expand Up @@ -64,6 +65,7 @@
public class DenseVectorField extends FloatPointField {
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
public static final String HNSW_ALGORITHM = "hnsw";
public static final String FLAT_ALGORITHM = "flat";
Copy link
Contributor

Choose a reason for hiding this comment

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

'VECTOR_STORAGE_ALGORITHM' maybe?
Is it used somewhere?

Copy link
Contributor

Choose a reason for hiding this comment

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

reading it more we are mixing up the 'knn' algorithm (only HNSW supported right now), with the 'vector storage' (flat, scalarQuantised and binaryQuantised

public static final String DEFAULT_KNN_ALGORITHM = HNSW_ALGORITHM;
static final String KNN_VECTOR_DIMENSION = "vectorDimension";
static final String KNN_ALGORITHM = "knnAlgorithm";
Expand Down Expand Up @@ -346,6 +348,10 @@ public DenseVectorParser getVectorBuilder(
}
}

public KnnVectorsFormat buildKnnVectorsFormat() {
return new Lucene99HnswVectorsFormat(hnswMaxConn, hnswBeamWidth);
}

@Override
public UninvertingReader.Type getUninversionType(SchemaField sf) {
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* 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.solr.schema;

import org.apache.lucene.codecs.KnnVectorsFormat;
import org.apache.lucene.codecs.lucene99.Lucene99HnswScalarQuantizedVectorsFormat;
import org.apache.lucene.codecs.lucene99.Lucene99ScalarQuantizedVectorsFormat;
import org.apache.lucene.index.VectorEncoding;
import org.apache.lucene.index.VectorSimilarityFunction;
import org.apache.solr.common.SolrException;

import java.util.Map;

import static java.util.Optional.ofNullable;
import static org.apache.lucene.codecs.lucene99.Lucene99HnswVectorsFormat.DEFAULT_NUM_MERGE_WORKER;

public class ScalarQuantizedDenseVectorField extends DenseVectorField {
public static final String BITS = "bits"; //
public static final String CONFIDENCE_INTERVAL = "confidenceInterval";
public static final String DYNAMIC_CONFIDENCE_INTERVAL = "dynamicConfidenceInterval";
public static final String COMPRESS = "compress"; // can only be enabled when bits = 4 per Lucene codec spec
Copy link
Contributor

Choose a reason for hiding this comment

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

these first four lines are param names if I'm not mistaken.

Maybe we can call them '_PARAM' e.g. 'BITS_PARAM'

Or add a comment line at the beginning that clearly group them as param names,
it's a minor though, but can increase code readability

Copy link
Contributor

Choose a reason for hiding this comment

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

to be fair, checking the original DenseVectorField, the same renaming could help there as well I suspect


private static final int DEFAULT_BITS = 7; // use signed byte as default when unspecified
private static final Float DEFAULT_CONFIDENCE_INTERVAL = null; // use dimension scaled confidence interval

/**
* Number of bits to use for storage
* Must be 4 (half-byte) or 7 (signed-byte) per Lucene codec spec
*/
private int bits;

/**
* Confidence interval to use for scalar quantization
* Default is calculated as `1-1/(vector_dimensions + 1)`
*/
private Float confidenceInterval;

/**
* When enabled, in conjunction with 4 bit size, will pair values into single bytes for 50% reduction in memory usage
* (comes at the cost of some decode speed penalty)
*/
private boolean compress;

public ScalarQuantizedDenseVectorField(int dimension,
VectorSimilarityFunction similarityFunction,
VectorEncoding vectorEncoding,
int bits,
Float confidenceInterval,
boolean compress) {
super(dimension, similarityFunction, vectorEncoding);
this.bits = bits;
this.confidenceInterval = confidenceInterval;
this.compress = compress;
}

@Override
public void init(IndexSchema schema, Map<String, String> args) {
super.init(schema, args);

this.bits = ofNullable(args.remove(BITS))
.map(Integer::parseInt)
.orElse(DEFAULT_BITS);

this.compress = ofNullable(args.remove(COMPRESS))
.map(Boolean::parseBoolean)
.orElse(false);

this.confidenceInterval = ofNullable(args.remove(CONFIDENCE_INTERVAL))
.map(Float::parseFloat)
.orElse(DEFAULT_CONFIDENCE_INTERVAL);

if (ofNullable(args.remove(DYNAMIC_CONFIDENCE_INTERVAL))
.map(Boolean::parseBoolean)
.orElse(false)) {
this.confidenceInterval = Lucene99ScalarQuantizedVectorsFormat.DYNAMIC_CONFIDENCE_INTERVAL;
}
}

@Override
public KnnVectorsFormat buildKnnVectorsFormat() {
final String knnAlgorithm = getKnnAlgorithm();
if (KNN_ALGORITHM.equals(knnAlgorithm)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

mmmm not sure about this part:

The only knn algorithm supported right now is 'HNSW'.
While for vector storage, there's flat (what currently supported), scalar quantised and binary quantised.
Effectively in Lucene HNSW vector formats define how to build and read the HNSW graph (where each vector ID is a node).
Vector storage formats define how to store the vectors themselves.

The vector storage format is directly (and only) used by a the graph format parent ( I had in my to-do list to re-organise the package to easily differentiate graph from storage formats)

Lucene99HnswVectorsFormat uses Lucene99FlatVectorsFormat.
• Store for each field the vector metadata (.vemf) and vector data (vec).
• It has a dedicated writer and reader, dependent on the vector encoding (BYTE or
FLOAT32).
• It has its scorer (that just uses the supplied similarity).

Lucene99HnswScalarQuantizedVectorsFormat uses Lucene99ScalarQuantizedVectorsFormat (for scalar quantisation).
• Lossy compression from float32 to a number of bits (4 or 7)
• Raw disk increases (raw + quantized vectors).
• Off-heap memory decreases as quantised vectors are loaded.
• Search is faster.
• It has a dedicated writer and reader, dependent on the bits to use for quantisation.

Given that, I suspect with this contribution we want to differentiate between:
Lucene99HnswVectorsFormat and Lucene99HnswScalarQuantizedVectorsFormat (without manipulating directly the vector storage).

-> the if then else should be changed I suspect, I don't understand it as it is right now as we are mixing a graph format (Lucene99HnswScalarQuantizedVectorsFormat already using the scalar quantised storage format) and a vectore storage format (Lucene99ScalarQuantizedVectorsFormat)

Copy link
Contributor

Choose a reason for hiding this comment

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

From an initial review the ScalarQuantizedDenseVectorField should always return the Lucene99HnswScalarQuantizedVectorsFormat.

The traditional DenseVectorField, should always return Lucene99HnswVectorsFormat

return new Lucene99HnswScalarQuantizedVectorsFormat(
getHnswMaxConn(),
getHnswBeamWidth(),
DEFAULT_NUM_MERGE_WORKER,
getBits(),
useCompression(),
getConfidenceInterval(),
null
);
} else if (FLAT_ALGORITHM.equals(knnAlgorithm)) {
return new Lucene99ScalarQuantizedVectorsFormat(
getConfidenceInterval(),
getBits(),
useCompression());
} else {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, String.format("Unrecognized KNN algorithm: %s", knnAlgorithm));
}
}

public int getBits() {
return bits;
}

public boolean useCompression() {
return compress;
}

public Float getConfidenceInterval() {
return confidenceInterval;
}

}
Loading