kaivalnp commented on code in PR #15979: URL: https://github.com/apache/lucene/pull/15979#discussion_r3659863857
########## lucene/core/src/test/org/apache/lucene/codecs/lucene106/dedup/TestLucene106DedupHnswVectorsFormat.java: ########## @@ -0,0 +1,91 @@ +/* + * 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.lucene106.dedup; + +import java.io.IOException; +import org.apache.lucene.codecs.Codec; +import org.apache.lucene.codecs.KnnVectorsFormat; +import org.apache.lucene.codecs.KnnVectorsReader; +import org.apache.lucene.codecs.simpletext.SimpleTextKnnVectorsReader; +import org.apache.lucene.index.CodecReader; +import org.apache.lucene.index.LeafReader; +import org.apache.lucene.tests.index.BaseKnnVectorsFormatTestCase; +import org.apache.lucene.tests.util.TestUtil; +import org.junit.Ignore; + +/** + * Runs the standard KNN vectors format suite against the de-duplicating HNSW format. De-duplication + * behavior itself is covered by {@link TestDedupFlatVectorsFormat}. + */ +public class TestLucene106DedupHnswVectorsFormat extends BaseKnnVectorsFormatTestCase { + + private final KnnVectorsFormat format = new Lucene106DedupHnswVectorsFormat(); + + @Override + protected Codec getCodec() { + return TestUtil.alwaysKnnVectorsFormat(format); + } + + @Override + protected boolean supportsFloatVectorFallback() { + return false; // stores raw vectors, no quantized fallback + } + + @Override + protected void assertOffHeapByteSize(LeafReader r, String fieldName) throws IOException { + var fieldInfo = r.getFieldInfos().fieldInfo(fieldName); + + if (r instanceof CodecReader codecReader) { + KnnVectorsReader knnVectorsReader = codecReader.getVectorReader(); + knnVectorsReader = knnVectorsReader.unwrapReaderForField(fieldName); + var offHeap = knnVectorsReader.getOffHeapByteSize(fieldInfo); + long totalByteSize = offHeap.values().stream().mapToLong(Long::longValue).sum(); + if (knnVectorsReader instanceof SimpleTextKnnVectorsReader) { + assertEquals(0L, offHeap.size()); // all vectors are in memory + assertEquals(0L, totalByteSize); + } else { + if (getNumVectors(knnVectorsReader, fieldInfo) == 0) { + assertEquals(0L, totalByteSize); + } else { + assertTrue(totalByteSize > 0); + assertTrue(offHeap.get("vdd") > 0L); // NOTE: different from vec + + if (hasHNSW(knnVectorsReader, fieldInfo)) { + assertTrue(offHeap.get("vex") > 0L); + } else { + assertTrue(offHeap.get("vex") == null || offHeap.get("vex") == 0); + } + } + } + } else { + throw new AssertionError("unexpected:" + r.getClass()); + } + } + + /** + * This test indexes random vectors of small dimensions with high duplicates, checking that RAM + * usage is above a threshold. The RAM usage assumption breaks with the de-duplicating format. + */ + @Override + @Ignore + public void testWriterRamEstimate() {} Review Comment: This led to an interesting discovery -- the [`ramBytesUsed` for the HNSW writer](https://github.com/kaivalnp/lucene/blob/91936b9b43d7244b2663de254ef1c0715c59d754/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99HnswVectorsWriter.java#L237-L245) is calculated by summing up the per-field writer values, and implicitly _assumes_ that the top-level writer uses trivial bytes (and is not counted), which is different for the de-duplicating format (we're attributing the raw de-duped vectors to the top-level writer, and only per-field mappings to field-level writers). I made some changes to remove this assumption and make both cases work with tests! -- 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]
