dungba88 commented on code in PR #13991: URL: https://github.com/apache/lucene/pull/13991#discussion_r1841484667
########## lucene/core/src/java/org/apache/lucene/index/MultiVectorSimilarityFunction.java: ########## @@ -0,0 +1,202 @@ +/* + * 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.index; + +import java.util.ArrayList; +import java.util.List; +import org.apache.lucene.util.ArrayUtil; + +/** + * Computes similarity between two multi-vectors. + * + * <p>A multi-vector is a collection of multiple vectors that represent a single document or query. + * MultiVectorSimilarityFunction is used to determine nearest neighbors during indexing and search + * on multi-vectors. + */ +public class MultiVectorSimilarityFunction { + + /** Aggregation function to combine similarity across multiple vector values */ + public enum Aggregation { + + /** + * Sum_Max Similarity between two multi-vectors. Computes the sum of maximum similarity found + * for each vector in the first multi-vector against all vectors in the second multi-vector. + */ + SUM_MAX { + @Override + public float aggregate( + float[] outer, + float[] inner, + VectorSimilarityFunction vectorSimilarityFunction, + int dimension) { + if (outer.length % dimension != 0 || inner.length % dimension != 0) { + throw new IllegalArgumentException("Multi vectors do not match provided dimension value"); + } + + // TODO: can we avoid making vector copies? + List<float[]> outerList = new ArrayList<>(); + List<float[]> innerList = new ArrayList<>(); + for (int i = 0; i < outer.length; i += dimension) { + outerList.add(ArrayUtil.copyOfSubArray(outer, i, i + dimension)); + } + for (int i = 0; i < inner.length; i += dimension) { + innerList.add(ArrayUtil.copyOfSubArray(inner, i, i + dimension)); + } + + float result = 0f; + for (float[] o : outerList) { + float maxSim = Float.MIN_VALUE; + for (float[] i : innerList) { + maxSim = Float.max(maxSim, vectorSimilarityFunction.compare(o, i)); Review Comment: If we add another `compare` method with start and end indexes for both inner and outer, I guess we won't need to copy the array? ########## lucene/core/src/java/org/apache/lucene/index/MultiVectorSimilarityFunction.java: ########## @@ -0,0 +1,202 @@ +/* + * 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.index; + +import java.util.ArrayList; +import java.util.List; +import org.apache.lucene.util.ArrayUtil; + +/** + * Computes similarity between two multi-vectors. + * + * <p>A multi-vector is a collection of multiple vectors that represent a single document or query. + * MultiVectorSimilarityFunction is used to determine nearest neighbors during indexing and search + * on multi-vectors. + */ +public class MultiVectorSimilarityFunction { + + /** Aggregation function to combine similarity across multiple vector values */ + public enum Aggregation { + + /** + * Sum_Max Similarity between two multi-vectors. Computes the sum of maximum similarity found + * for each vector in the first multi-vector against all vectors in the second multi-vector. + */ + SUM_MAX { + @Override + public float aggregate( + float[] outer, + float[] inner, + VectorSimilarityFunction vectorSimilarityFunction, + int dimension) { + if (outer.length % dimension != 0 || inner.length % dimension != 0) { + throw new IllegalArgumentException("Multi vectors do not match provided dimension value"); + } + + // TODO: can we avoid making vector copies? + List<float[]> outerList = new ArrayList<>(); + List<float[]> innerList = new ArrayList<>(); + for (int i = 0; i < outer.length; i += dimension) { + outerList.add(ArrayUtil.copyOfSubArray(outer, i, i + dimension)); + } + for (int i = 0; i < inner.length; i += dimension) { + innerList.add(ArrayUtil.copyOfSubArray(inner, i, i + dimension)); + } + + float result = 0f; + for (float[] o : outerList) { + float maxSim = Float.MIN_VALUE; + for (float[] i : innerList) { + maxSim = Float.max(maxSim, vectorSimilarityFunction.compare(o, i)); + } + result += maxSim; + } + return result; + } + + @Override + public float aggregate( Review Comment: It's so unfortunate that Java doesn't support generic primitive array and has to have duplicate code. -- 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...@lucene.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org