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 2 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
36 changes: 33 additions & 3 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,16 +131,41 @@ 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) {
KnnVectorsFormat delegate;
Copy link
Contributor

Choose a reason for hiding this comment

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

The instanceof checks can be a code smell. I look at all this and wonder: wouldn't it make sense for a method DenseVectorField.buildKnnVectorsFormat() to exist? Or just a getter if it's built in the field type's init()... which would also mean no need to have each of those fields (e.g. no bits, ...) with their getters either.

Copy link
Author

Choose a reason for hiding this comment

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

reworked it. Let me know if this is along the lines of what you were thinking

Copy link
Contributor

Choose a reason for hiding this comment

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

Much better. Did you consider init() also creating the codec to thus avoid the need corresponding fields, getters, setters?

Copy link
Author

Choose a reason for hiding this comment

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

I'm not quite sure I follow. You mean the ScalarQuantizedDenseVectorField init() or the SchemaCodecFactory init()?

Copy link
Contributor

Choose a reason for hiding this comment

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

ScalarQuantizedDenseVectorField.init() (and its subclass, DenseVectorField) creating a KnnVectorsFormat that later can simply be returned.

Copy link
Author

Choose a reason for hiding this comment

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

That's probably true. However it does look like the getters are used in the DenseVectorField tests as KnnVectorsFormat doesn't expose the internal setting values. Personally I think it's ok to leave patterned as-is and having getters affords some level of flexibility and transparency.

But I can make this change if you feel strongly about it

if (fieldType instanceof ScalarQuantizedDenseVectorField scalarQuantizedVectorType) {
final String knnAlgorithm = scalarQuantizedVectorType.getKnnAlgorithm();
switch (knnAlgorithm) {
case DenseVectorField.FLAT_ALGORITHM:
delegate = new Lucene99ScalarQuantizedVectorsFormat(
scalarQuantizedVectorType.getConfidenceInterval(),
scalarQuantizedVectorType.getBits(),
scalarQuantizedVectorType.useCompression());
break;
case DenseVectorField.HNSW_ALGORITHM:
int maxConn = scalarQuantizedVectorType.getHnswMaxConn();
int beamWidth = scalarQuantizedVectorType.getHnswBeamWidth();
delegate = new Lucene99HnswScalarQuantizedVectorsFormat(maxConn,
beamWidth,
DEFAULT_NUM_MERGE_WORKER,
scalarQuantizedVectorType.getBits(),
scalarQuantizedVectorType.useCompression(),
scalarQuantizedVectorType.getConfidenceInterval(),
null);
break;
default:
throw new SolrException(ErrorCode.SERVER_ERROR, knnAlgorithm + " KNN algorithm is not supported");
}
return new SolrDelegatingKnnVectorsFormat(delegate, scalarQuantizedVectorType.getDimension());
} else 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);
delegate = new Lucene99HnswVectorsFormat(maxConn, beamWidth);
return new SolrDelegatingKnnVectorsFormat(delegate, vectorType.getDimension());
} else {
throw new SolrException(
ErrorCode.SERVER_ERROR, knnAlgorithm + " KNN algorithm is not supported");
ErrorCode.SERVER_ERROR, knnAlgorithm + " KNN algorithm is not supported");
}
}
return super.getKnnVectorsFormatForField(field);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,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
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* 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.lucene99.Lucene99ScalarQuantizedVectorsFormat;
import org.apache.lucene.index.VectorEncoding;
import org.apache.lucene.index.VectorSimilarityFunction;

import java.util.Map;

import static java.util.Optional.ofNullable;

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.get(BITS))
.map(Integer::parseInt)
.orElse(DEFAULT_BITS);
args.remove(BITS);

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

boolean useDynamicConfidenceInterval = ofNullable(args.get(DYNAMIC_CONFIDENCE_INTERVAL))
.map(Boolean::parseBoolean)
.orElse(false);
args.remove(DYNAMIC_CONFIDENCE_INTERVAL);

if (useDynamicConfidenceInterval) {
this.confidenceInterval = Lucene99ScalarQuantizedVectorsFormat.DYNAMIC_CONFIDENCE_INTERVAL;
}

this.confidenceInterval = ofNullable(args.get(CONFIDENCE_INTERVAL))
Copy link
Contributor

Choose a reason for hiding this comment

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

this overwrites the confidenceInterval set when useDynamicConfidenceInterval==true above

Copy link
Author

Choose a reason for hiding this comment

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

good catch. I was hoping to make any explicit confidence interval override the dynamic behavior, however with both absence of arg and the default being null I think I will flip the behavior (use dynamic if specified otherwise confidence interval if specified otherwise default confidence interval if none provided)

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay but then let's check if both are set and throw an exception.

Copy link
Author

Choose a reason for hiding this comment

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

If neither are set, it should default to the default behavior (scaled confidence interval) - which according to lucene spec is passing a null confidence interval

.map(Float::parseFloat)
.orElse(DEFAULT_CONFIDENCE_INTERVAL);
args.remove(CONFIDENCE_INTERVAL);
}

public int getBits() {
return bits;
}

public boolean useCompression() {
return compress;
}

public Float getConfidenceInterval() {
return confidenceInterval;
}

}