benwtrent commented on code in PR #14980: URL: https://github.com/apache/lucene/pull/14980#discussion_r2257473164
########## lucene/core/src/java24/org/apache/lucene/internal/vectorization/Lucene99MemorySegmentFloatVectorScorerSupplier.java: ########## @@ -0,0 +1,171 @@ +/* + * 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 org.apache.lucene.util.VectorUtil.normalizeToUnitInterval; + +import java.io.IOException; +import java.lang.foreign.MemorySegment; +import java.util.Optional; +import org.apache.lucene.index.FloatVectorValues; +import org.apache.lucene.index.VectorSimilarityFunction; +import org.apache.lucene.store.FilterIndexInput; +import org.apache.lucene.store.IndexInput; +import org.apache.lucene.store.MemorySegmentAccessInput; +import org.apache.lucene.util.hnsw.RandomVectorScorerSupplier; +import org.apache.lucene.util.hnsw.UpdateableRandomVectorScorer; + +/** A score supplier of vectors whose element size is byte. */ +public abstract sealed class Lucene99MemorySegmentFloatVectorScorerSupplier + implements RandomVectorScorerSupplier { + final int vectorByteSize; + final int maxOrd; + final int dims; + final MemorySegmentAccessInput input; + final FloatVectorValues values; // to support ordToDoc/getAcceptOrds + byte[] queryScratch, scratch1, scratch2, scratch3, scratch4; + + /** + * Return an optional whose value, if present, is the scorer supplier. Otherwise, an empty + * optional is returned. + */ + static Optional<RandomVectorScorerSupplier> create( + VectorSimilarityFunction type, IndexInput input, FloatVectorValues values) { + input = FilterIndexInput.unwrapOnlyTest(input); + if (!(input instanceof MemorySegmentAccessInput msInput)) { + return Optional.empty(); + } + checkInvariants(values.size(), values.getVectorByteLength(), input); + return switch (type) { + case COSINE -> Optional.empty(); // of(new CosineSupplier(msInput, values)); + case DOT_PRODUCT -> Optional.of(new DotProductSupplier(msInput, values)); + case EUCLIDEAN -> Optional.empty(); // of(new EuclideanSupplier(msInput, values)); + case MAXIMUM_INNER_PRODUCT -> + Optional.empty(); // of(new MaxInnerProductSupplier(msInput, values)); + }; + } + + Lucene99MemorySegmentFloatVectorScorerSupplier( + MemorySegmentAccessInput input, FloatVectorValues values) { + this.input = input; + this.values = values; + this.vectorByteSize = values.getVectorByteLength(); + this.maxOrd = values.size(); + this.dims = values.dimension(); + } + + static void checkInvariants(int maxOrd, int vectorByteLength, IndexInput input) { + if (input.length() < (long) vectorByteLength * maxOrd) { + throw new IllegalArgumentException("input length is less than expected vector data"); + } + } + + final void checkOrdinal(int ord) { + if (ord < 0 || ord >= maxOrd) { + throw new IllegalArgumentException("illegal ordinal: " + ord); + } + } + + final MemorySegment getSegment(int ord, byte[] scratch) throws IOException { + long byteOffset = (long) ord * vectorByteSize; + MemorySegment seg = input.segmentSliceOrNull(byteOffset, vectorByteSize); + if (seg == null) { + if (scratch == null) { + scratch = new byte[vectorByteSize]; + } + input.readBytes(byteOffset, scratch, 0, vectorByteSize); + seg = MemorySegment.ofArray(scratch); + } + return seg; + } + + // final void readFloatVector(float[] array, int ord) throws IOException { + // assert array.length == dims; + // long byteOffset = (long) ord * vectorByteSize; + // for (int i = 0; i < array.length; i++) { + // array[i] = Float.intBitsToFloat(input.readInt(byteOffset + i)); + // } + // } Review Comment: delete? -- 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