alessandrobenedetti commented on code in PR #3385: URL: https://github.com/apache/solr/pull/3385#discussion_r2179755734
########## solr/core/src/java/org/apache/solr/schema/ScalarQuantizedDenseVectorField.java: ########## @@ -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 Review Comment: these first four lines are param names if I'm not mistaken. Maybe we can call them '<name>_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 ########## solr/core/src/java/org/apache/solr/schema/ScalarQuantizedDenseVectorField.java: ########## @@ -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 + + 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)) { Review Comment: From an initial review the ScalarQuantizedDenseVectorField should always return the Lucene99HnswScalarQuantizedVectorsFormat. The traditional DenseVectorField, should always return Lucene99HnswVectorsFormat ########## solr/core/src/java/org/apache/solr/schema/DenseVectorField.java: ########## @@ -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"; Review Comment: 'VECTOR_STORAGE_ALGORITHM' maybe? Is it used somewhere? ########## solr/core/src/java/org/apache/solr/schema/DenseVectorField.java: ########## @@ -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"; Review Comment: reading it more we are mixing up the 'knn' algorithm (only HNSW supported right now), with the 'vector storage' (flat, scalarQuantised and binaryQuantised ########## solr/core/src/java/org/apache/solr/schema/ScalarQuantizedDenseVectorField.java: ########## @@ -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 + + 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)) { Review Comment: 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) ########## solr/core/src/java/org/apache/solr/schema/ScalarQuantizedDenseVectorField.java: ########## @@ -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 Review Comment: to be fair, checking the original DenseVectorField, the same renaming could help there as well I suspect -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org For additional commands, e-mail: issues-h...@solr.apache.org