Pulkitg64 commented on code in PR #16383:
URL: https://github.com/apache/lucene/pull/16383#discussion_r3572166415


##########
lucene/core/src/java/org/apache/lucene/codecs/lucene95/OffHeapFloat16VectorValues.java:
##########
@@ -0,0 +1,334 @@
+/*
+ * 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.codecs.lucene95;
+
+import java.io.IOException;
+import org.apache.lucene.codecs.hnsw.FlatVectorsScorer;
+import org.apache.lucene.codecs.lucene90.IndexedDISI;
+import org.apache.lucene.index.Float16VectorValues;
+import org.apache.lucene.index.VectorEncoding;
+import org.apache.lucene.index.VectorSimilarityFunction;
+import org.apache.lucene.search.DocIdSetIterator;
+import org.apache.lucene.search.VectorScorer;
+import org.apache.lucene.store.IndexInput;
+import org.apache.lucene.store.RandomAccessInput;
+import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.hnsw.RandomVectorScorer;
+import org.apache.lucene.util.packed.DirectMonotonicReader;
+
+/** Read the vector values from the index input. This supports both iterated 
and random access. */
+public abstract class OffHeapFloat16VectorValues extends Float16VectorValues
+    implements HasIndexSlice {
+
+  protected final int dimension;
+  protected final int size;
+  protected final IndexInput slice;
+  protected final int byteSize;
+  protected int lastOrd = -1;
+  protected final short[] value;
+  protected final VectorSimilarityFunction similarityFunction;
+  protected final FlatVectorsScorer flatVectorsScorer;
+
+  OffHeapFloat16VectorValues(
+      int dimension,
+      int size,
+      IndexInput slice,
+      int byteSize,
+      FlatVectorsScorer flatVectorsScorer,
+      VectorSimilarityFunction similarityFunction) {
+    this.dimension = dimension;
+    this.size = size;
+    this.slice = slice;
+    this.byteSize = byteSize;
+    this.similarityFunction = similarityFunction;
+    this.flatVectorsScorer = flatVectorsScorer;
+    value = new short[dimension];
+  }
+
+  @Override
+  public int dimension() {
+    return dimension;
+  }
+
+  @Override
+  public int size() {
+    return size;
+  }
+
+  @Override
+  public IndexInput getSlice() {
+    return slice;
+  }
+
+  @Override
+  public short[] vectorValue(int targetOrd) throws IOException {
+    if (lastOrd == targetOrd) {
+      return value;
+    }
+    slice.seek((long) targetOrd * byteSize);
+    slice.readShorts(value, 0, dimension);
+    lastOrd = targetOrd;
+    return value;
+  }
+
+  @Override
+  public VectorEncoding getEncoding() {
+    return VectorEncoding.FLOAT16;
+  }
+
+  public static OffHeapFloat16VectorValues load(
+      VectorSimilarityFunction vectorSimilarityFunction,
+      FlatVectorsScorer flatVectorsScorer,
+      OrdToDocDISIReaderConfiguration configuration,
+      VectorEncoding vectorEncoding,
+      int dimension,
+      long vectorDataOffset,
+      long vectorDataLength,
+      IndexInput vectorData)
+      throws IOException {
+    if (configuration.docsWithFieldOffset == -2 || vectorEncoding != 
VectorEncoding.FLOAT16) {

Review Comment:
   Yes this is consistent with other encodings as I copied this from 
OffHeapFloatVectorValues only. References:
   * 
[OffHeapFloatVectorValues](https://github.com/apache/lucene/blame/main/lucene/core/src/java/org/apache/lucene/codecs/lucene95/OffHeapFloatVectorValues.java#L117)
   * 
[OffHeapByteVectorValues](https://github.com/apache/lucene/blob/main/lucene/core/src/java/org/apache/lucene/codecs/lucene95/OffHeapByteVectorValues.java#L122)
   



##########
lucene/core/src/java/org/apache/lucene/codecs/lucene95/OffHeapFloat16VectorValues.java:
##########
@@ -0,0 +1,334 @@
+/*
+ * 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.codecs.lucene95;
+
+import java.io.IOException;
+import org.apache.lucene.codecs.hnsw.FlatVectorsScorer;
+import org.apache.lucene.codecs.lucene90.IndexedDISI;
+import org.apache.lucene.index.Float16VectorValues;
+import org.apache.lucene.index.VectorEncoding;
+import org.apache.lucene.index.VectorSimilarityFunction;
+import org.apache.lucene.search.DocIdSetIterator;
+import org.apache.lucene.search.VectorScorer;
+import org.apache.lucene.store.IndexInput;
+import org.apache.lucene.store.RandomAccessInput;
+import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.hnsw.RandomVectorScorer;
+import org.apache.lucene.util.packed.DirectMonotonicReader;
+
+/** Read the vector values from the index input. This supports both iterated 
and random access. */
+public abstract class OffHeapFloat16VectorValues extends Float16VectorValues
+    implements HasIndexSlice {
+
+  protected final int dimension;
+  protected final int size;
+  protected final IndexInput slice;
+  protected final int byteSize;
+  protected int lastOrd = -1;
+  protected final short[] value;
+  protected final VectorSimilarityFunction similarityFunction;
+  protected final FlatVectorsScorer flatVectorsScorer;
+
+  OffHeapFloat16VectorValues(
+      int dimension,
+      int size,
+      IndexInput slice,
+      int byteSize,
+      FlatVectorsScorer flatVectorsScorer,
+      VectorSimilarityFunction similarityFunction) {
+    this.dimension = dimension;
+    this.size = size;
+    this.slice = slice;
+    this.byteSize = byteSize;
+    this.similarityFunction = similarityFunction;
+    this.flatVectorsScorer = flatVectorsScorer;
+    value = new short[dimension];
+  }
+
+  @Override
+  public int dimension() {
+    return dimension;
+  }
+
+  @Override
+  public int size() {
+    return size;
+  }
+
+  @Override
+  public IndexInput getSlice() {
+    return slice;
+  }
+
+  @Override
+  public short[] vectorValue(int targetOrd) throws IOException {
+    if (lastOrd == targetOrd) {
+      return value;
+    }
+    slice.seek((long) targetOrd * byteSize);
+    slice.readShorts(value, 0, dimension);
+    lastOrd = targetOrd;
+    return value;
+  }
+
+  @Override
+  public VectorEncoding getEncoding() {
+    return VectorEncoding.FLOAT16;
+  }
+
+  public static OffHeapFloat16VectorValues load(
+      VectorSimilarityFunction vectorSimilarityFunction,
+      FlatVectorsScorer flatVectorsScorer,
+      OrdToDocDISIReaderConfiguration configuration,
+      VectorEncoding vectorEncoding,
+      int dimension,
+      long vectorDataOffset,
+      long vectorDataLength,
+      IndexInput vectorData)
+      throws IOException {
+    if (configuration.docsWithFieldOffset == -2 || vectorEncoding != 
VectorEncoding.FLOAT16) {
+      return new EmptyOffHeapVectorValues(dimension, flatVectorsScorer, 
vectorSimilarityFunction);
+    }
+    IndexInput bytesSlice = vectorData.slice("vector-data", vectorDataOffset, 
vectorDataLength);
+    int byteSize = dimension * Short.BYTES;
+
+    if (configuration.docsWithFieldOffset == -1) {
+      return new DenseOffHeapVectorValues(
+          dimension,
+          configuration.size,
+          bytesSlice,
+          byteSize,
+          flatVectorsScorer,
+          vectorSimilarityFunction);
+    } else {
+      return new SparseOffHeapVectorValues(
+          configuration,
+          vectorData,
+          bytesSlice,
+          dimension,
+          byteSize,
+          flatVectorsScorer,
+          vectorSimilarityFunction);
+    }
+  }
+
+  /**
+   * Dense vector values that are stored off-heap. This is the most common 
case when every doc has a
+   * vector.
+   */
+  public static class DenseOffHeapVectorValues extends 
OffHeapFloat16VectorValues {
+
+    public DenseOffHeapVectorValues(
+        int dimension,
+        int size,
+        IndexInput slice,
+        int byteSize,
+        FlatVectorsScorer flatVectorsScorer,
+        VectorSimilarityFunction similarityFunction) {
+      super(dimension, size, slice, byteSize, flatVectorsScorer, 
similarityFunction);
+    }
+
+    @Override
+    public DenseOffHeapVectorValues copy() throws IOException {
+      return new DenseOffHeapVectorValues(
+          dimension, size, slice.clone(), byteSize, flatVectorsScorer, 
similarityFunction);
+    }
+
+    @Override
+    public int ordToDoc(int ord) {
+      return ord;
+    }
+
+    @Override
+    public Bits getAcceptOrds(Bits acceptDocs) {
+      return acceptDocs;
+    }
+
+    @Override
+    public DocIndexIterator iterator() {
+      return createDenseIterator();
+    }
+
+    @Override
+    public VectorScorer scorer(short[] query) throws IOException {
+      DenseOffHeapVectorValues copy = copy();
+      DocIndexIterator iterator = copy.iterator();
+      RandomVectorScorer randomVectorScorer =
+          flatVectorsScorer.getRandomVectorScorer(similarityFunction, copy, 
query);
+      return new VectorScorer() {
+        @Override
+        public float score() throws IOException {
+          return randomVectorScorer.score(iterator.docID());
+        }
+
+        @Override
+        public DocIdSetIterator iterator() {
+          return iterator;
+        }
+
+        @Override
+        public VectorScorer.Bulk bulk(DocIdSetIterator matchingDocs) {
+          return Bulk.fromRandomScorerDense(randomVectorScorer, iterator, 
matchingDocs);
+        }
+      };
+    }
+  }
+
+  private static class SparseOffHeapVectorValues extends 
OffHeapFloat16VectorValues {
+    private final DirectMonotonicReader ordToDoc;
+    private final IndexedDISI disi;
+    // dataIn was used to init a new IndexedDIS for #randomAccess()

Review Comment:
   Fixed



##########
lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99FlatVectorsReader.java:
##########
@@ -339,6 +373,7 @@ private record FieldEntry(
           switch (info.getVectorEncoding()) {
             case BYTE -> Byte.BYTES;
             case FLOAT32 -> Float.BYTES;
+            case FLOAT16 -> Short.BYTES;

Review Comment:
   Done



-- 
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