kaivalnp commented on code in PR #17013:
URL: https://github.com/apache/lucene/pull/17013#discussion_r4604624977


##########
lucene/core/src/java25/org/apache/lucene/internal/vectorization/MemorySegmentQuantizedVectorOps.java:
##########
@@ -0,0 +1,397 @@
+/*
+ * 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 static java.lang.foreign.ValueLayout.JAVA_BYTE;
+import static java.lang.foreign.ValueLayout.JAVA_LONG_UNALIGNED;
+import static java.nio.ByteOrder.LITTLE_ENDIAN;
+import static jdk.incubator.vector.VectorOperators.ADD;
+import static jdk.incubator.vector.VectorOperators.BIT_COUNT;
+import static jdk.incubator.vector.VectorOperators.LSHL;
+import static jdk.incubator.vector.VectorOperators.LSHR;
+import static jdk.incubator.vector.VectorOperators.ZERO_EXTEND_B2I;
+import static jdk.incubator.vector.VectorOperators.ZERO_EXTEND_B2S;
+import static jdk.incubator.vector.VectorOperators.ZERO_EXTEND_S2I;
+import static 
org.apache.lucene.internal.vectorization.PanamaVectorConstants.PRERERRED_INT_SPECIES;
+import static 
org.apache.lucene.internal.vectorization.PanamaVectorUtilSupport.BYTE_SPECIES;
+import static 
org.apache.lucene.internal.vectorization.PanamaVectorUtilSupport.BYTE_SPECIES_FULL;
+import static 
org.apache.lucene.internal.vectorization.PanamaVectorUtilSupport.SHORT_SPECIES;
+import static 
org.apache.lucene.internal.vectorization.PanamaVectorUtilSupport.VECTOR_BITSIZE;
+
+import java.lang.foreign.MemorySegment;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.VarHandle;
+import java.nio.ByteOrder;
+import jdk.incubator.vector.ByteVector;
+import jdk.incubator.vector.IntVector;
+import jdk.incubator.vector.LongVector;
+import jdk.incubator.vector.ShortVector;
+import jdk.incubator.vector.Vector;
+import jdk.incubator.vector.VectorShape;
+import jdk.incubator.vector.VectorSpecies;
+
+/**
+ * Dot products of a quantized query on heap with quantized vectors read in 
place from a {@link
+ * MemorySegment}, several vectors per pass so that the CPU overlaps their 
cache misses instead of
+ * waiting for the misses of one vector after the other. The 8 bit kernels 
read four vectors per
+ * pass; the 4, 2 and 1 bit kernels read two, because a pass over four would 
exceed the budget C2
+ * has for inlining the vector operations of a method, and vector operations 
that are not inlined
+ * are hundreds of times slower.
+ *
+ * <p>The query and vector layouts are those of {@link
+ * org.apache.lucene.codecs.lucene104.Lucene104ScalarQuantizedVectorsFormat}; 
see the matching
+ * single vector methods of {@link org.apache.lucene.util.VectorUtil} for each 
layout. The results
+ * are identical to those methods. The callers pass the vector length, which 
they know from the
+ * index, and a query of exactly the length the layout needs. Every kernel 
zeroes {@code
+ * results[0..3]} first and then adds to them, pass by pass.
+ */
+final class MemorySegmentQuantizedVectorOps {
+
+  // Reads query longs in the order of the segment reads, which is the native 
one. (The handles in
+  // BitUtil use a randomized order in tests.)
+  private static final VarHandle NATIVE_LONG =
+      MethodHandles.byteArrayViewVarHandle(long[].class, 
ByteOrder.nativeOrder());
+
+  // Bit counts are summed in 64 bit lanes like the single vector kernel of 
VectorUtil does for
+  // 256 bit vectors and up.
+  private static final VectorSpecies<Long> LONG_SPECIES =
+      LongVector.SPECIES_MAX.withShape(VectorShape.forBitSize(VECTOR_BITSIZE));
+
+  private MemorySegmentQuantizedVectorOps() {}
+
+  // -- unsigned bytes (8 bit and 7 bit values)
+
+  /**
+   * Dot products of the first {@code length} bytes of {@code q} with the four 
vectors of that
+   * length at the given offsets, all treated as unsigned bytes, into {@code 
results[0..3]}.
+   */
+  static void uint8DotProductBulk(

Review Comment:
   `TestLucene104MemorySegmentScalarQuantizedVectorScorer` checks for equal 
scores b/w the default and memory-segment scorer, and is exercised with some 
values of the (randomized) `tests.vectorsize` argument.
   
   Example:
   
   ```
   ./gradlew :lucene:core:test --tests 
TestLucene104MemorySegmentScalarQuantizedVectorScorer -Ptests.vectorsize=256
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to