uschindler commented on code in PR #13339: URL: https://github.com/apache/lucene/pull/13339#discussion_r1589472836
########## lucene/core/src/java21/org/apache/lucene/internal/vectorization/DotProductByteVectorScorerSupplier.java: ########## @@ -0,0 +1,46 @@ +/* + * 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.internal.vectorization; + +import java.io.IOException; +import org.apache.lucene.store.MemorySegmentAccessInput; +import org.apache.lucene.util.hnsw.RandomAccessVectorValues; + +final class DotProductByteVectorScorerSupplier extends MemorySegmentByteVectorScorerSupplier { + + DotProductByteVectorScorerSupplier( + int dims, + int maxOrd, + int vectorByteSize, + MemorySegmentAccessInput input, + RandomAccessVectorValues values) { + super(dims, maxOrd, vectorByteSize, input, values); + } + + @Override + public float score(int node) throws IOException { + // divide by 2 * 2^14 (maximum absolute value of product of 2 signed bytes) * len + float raw = PanamaVectorUtilSupport.dotProduct(first, getSegment(node, scratch2)); + return 0.5f + raw / (float) (dims * (1 << 15)); + } + + @Override + public DotProductByteVectorScorerSupplier copy() throws IOException { + return new DotProductByteVectorScorerSupplier( + dims, maxOrd, vectorByteSize, input.clone(), values); Review Comment: this was the reason why we needed the original input! ########## lucene/benchmark-jmh/src/java/org/apache/lucene/benchmark/jmh/VectorScorerBenchmark.java: ########## @@ -0,0 +1,114 @@ +/* + * 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.benchmark.jmh; + +import static org.apache.lucene.index.VectorSimilarityFunction.DOT_PRODUCT; + +import java.io.IOException; +import java.nio.file.Files; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.TimeUnit; +import org.apache.lucene.codecs.lucene95.OffHeapByteVectorValues; +import org.apache.lucene.internal.vectorization.VectorizationProvider; +import org.apache.lucene.store.Directory; +import org.apache.lucene.store.IOContext; +import org.apache.lucene.store.IndexInput; +import org.apache.lucene.store.IndexOutput; +import org.apache.lucene.store.MMapDirectory; +import org.apache.lucene.util.IOUtils; +import org.apache.lucene.util.hnsw.RandomAccessVectorValues; +import org.apache.lucene.util.hnsw.RandomVectorScorerSupplier; +import org.openjdk.jmh.annotations.*; + +@BenchmarkMode(Mode.Throughput) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@State(Scope.Benchmark) +// first iteration is complete garbage, so make sure we really warmup +@Warmup(iterations = 4, time = 1) +// real iterations. not useful to spend tons of time here, better to fork more +@Measurement(iterations = 5, time = 1) +// engage some noise reduction +@Fork( + value = 3, + jvmArgsAppend = {"-Xmx2g", "-Xms2g", "-XX:+AlwaysPreTouch"}) +public class VectorScorerBenchmark { + + @Param({"1", "128", "207", "256", "300", "512", "702", "1024"}) + int size; + + Directory dir; + IndexInput in; + RandomAccessVectorValues vectorValues; + byte[] vec1, vec2; + RandomVectorScorerSupplier scorer; + + @Setup(Level.Iteration) + public void init() throws IOException { + vec1 = new byte[size]; + vec2 = new byte[size]; + ThreadLocalRandom.current().nextBytes(vec1); + ThreadLocalRandom.current().nextBytes(vec2); + + dir = new MMapDirectory(Files.createTempDirectory("VectorScorerBenchmark")); + try (IndexOutput out = dir.createOutput("vector.data", IOContext.DEFAULT)) { + out.writeBytes(vec1, 0, vec1.length); + out.writeBytes(vec2, 0, vec2.length); + } + in = dir.openInput("vector.data", IOContext.DEFAULT); + vectorValues = vectorValues(size, 2, in); + scorer = + VectorizationProvider.getInstance() + .newFlatVectorScorer() + .getRandomVectorScorerSupplier(DOT_PRODUCT, vectorValues); + + // Ensure we're using the right vector scorer Review Comment: do we need that assertion here at all? At other benchmarks we just assume that all is sane when we pass the `--add-modules=jdk.incubator.vector` cmd line option. We would then also not need to open/export the package in module info and add the extra class in the provider safety check. IMHO let's remove all of that here. -- 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