-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add a de-duplicating flat vector format #15979
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+3,469
−8
Merged
Changes from 4 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
23ed23a
Add a de-duplicating vector format
kaivalnp 514f898
iter
kaivalnp 91936b9
iter
kaivalnp cbce9f0
address comments
kaivalnp fe8b33d
Merge branch 'main' into dedup-raw-vectors
kaivalnp caa1d32
add fp16 support
kaivalnp 4052a01
address comments
kaivalnp 4970f93
fix failing tests
kaivalnp d1294e3
move format to sandbox
kaivalnp c20d501
fixes from AI review
kaivalnp ebc308b
Merge branch 'main' into dedup-raw-vectors
kaivalnp 6de567e
address comments, harden tests
kaivalnp 784da4f
iter
kaivalnp a67cf12
address comments
kaivalnp c629a13
Merge branch 'main' into dedup-raw-vectors
kaivalnp 7d8a669
Merge branch 'main' into dedup-raw-vectors
kaivalnp 0322b86
nit Javadoc
kaivalnp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
...e/core/src/java/org/apache/lucene/codecs/lucene106/dedup/DedupFlatFieldVectorsWriter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| /* | ||
| * 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 java.util.ArrayList; | ||
| import java.util.List; | ||
| import org.apache.lucene.codecs.hnsw.FlatFieldVectorsWriter; | ||
| import org.apache.lucene.index.DocsWithFieldSet; | ||
| import org.apache.lucene.internal.hppc.IntArrayList; | ||
| import org.apache.lucene.internal.hppc.ObjectCursor; | ||
| import org.apache.lucene.util.RamUsageEstimator; | ||
|
|
||
| /** | ||
| * Buffers one field's vectors during flush, de-duplicating them through a shared {@link | ||
| * DedupGroup}. | ||
| * | ||
| * @lucene.experimental | ||
| */ | ||
| final class DedupFlatFieldVectorsWriter<T> extends FlatFieldVectorsWriter<T> { | ||
| private static final long SHALLOW_SIZE = | ||
| RamUsageEstimator.shallowSizeOfInstance(DedupFlatFieldVectorsWriter.class); | ||
|
|
||
| private final DedupGroup<T> group; | ||
| private final DocsWithFieldSet docsWithFieldSet; | ||
| private final List<T> vectors; | ||
| private final IntArrayList ordToVecOrd; | ||
| private int lastDocID; | ||
| private boolean finished; | ||
|
|
||
| DedupFlatFieldVectorsWriter(DedupGroup<T> group) { | ||
| this.group = group; | ||
| this.docsWithFieldSet = new DocsWithFieldSet(); | ||
| this.vectors = new ArrayList<>(); | ||
| this.ordToVecOrd = new IntArrayList(); | ||
| this.lastDocID = -1; | ||
| this.finished = false; | ||
| } | ||
|
|
||
| @Override | ||
| public List<T> getVectors() { | ||
| return vectors; | ||
| } | ||
|
|
||
| @Override | ||
| public DocsWithFieldSet getDocsWithFieldSet() { | ||
| return docsWithFieldSet; | ||
| } | ||
|
|
||
| IntArrayList getOrdToVecOrd() { | ||
| return ordToVecOrd; | ||
| } | ||
|
|
||
| @Override | ||
| public void finish() { | ||
| if (finished) { | ||
| throw new IllegalStateException("already finished"); | ||
| } | ||
| finished = true; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isFinished() { | ||
| return finished; | ||
| } | ||
|
|
||
| @Override | ||
| public T copyValue(T vectorValue) { | ||
| throw new UnsupportedOperationException(); // handled inside group | ||
| } | ||
|
|
||
| @Override | ||
| public void addValue(int docID, T vectorValue) throws IOException { | ||
| if (finished) { | ||
| throw new IllegalStateException("already finished"); | ||
| } else if (docID <= lastDocID) { | ||
| throw new IllegalArgumentException( | ||
| "docID=" + docID + " not going forwards, indexed lastDocID=" + lastDocID); | ||
| } | ||
|
|
||
| lastDocID = docID; | ||
| docsWithFieldSet.add(docID); | ||
|
|
||
| ObjectCursor<T> cursor = group.addUnique(vectorValue); | ||
| vectors.add(cursor.value); // owned vector value | ||
| ordToVecOrd.add(cursor.index); // index in group | ||
| } | ||
|
|
||
| @Override | ||
| public long ramBytesUsed() { | ||
| return SHALLOW_SIZE | ||
| + docsWithFieldSet.ramBytesUsed() | ||
| + (long) vectors.size() * RamUsageEstimator.NUM_BYTES_OBJECT_REF | ||
| + ordToVecOrd.ramBytesUsed(); | ||
| } | ||
| } |
132 changes: 132 additions & 0 deletions
132
lucene/core/src/java/org/apache/lucene/codecs/lucene106/dedup/DedupFlatVectorsFormat.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| /* | ||
| * 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.hnsw.FlatVectorsFormat; | ||
| import org.apache.lucene.codecs.hnsw.FlatVectorsReader; | ||
| import org.apache.lucene.codecs.hnsw.FlatVectorsWriter; | ||
| import org.apache.lucene.index.SegmentReadState; | ||
| import org.apache.lucene.index.SegmentWriteState; | ||
|
|
||
| /** | ||
| * Flat vector format that stores each distinct vector once. | ||
| * | ||
| * <p>Vectors that share the same dimension and encoding form a <i>group</i>. Within a group, an | ||
| * identical vector is stored a single time regardless of how many documents (across all fields that | ||
| * map to that group) reference it; each field then keeps a per-document {@code ordToVecOrd} map | ||
|
kaivalnp marked this conversation as resolved.
Outdated
|
||
| * from its document ordinal to the group ordinal of the shared vector. This is well suited to | ||
| * indexes with repeated vectors, e.g. several fields derived from the same embedding, or heavily | ||
| * duplicated content. | ||
| * | ||
| * <h2>.vdd (vector de-dup data) file</h2> | ||
| * | ||
| * <ul> | ||
| * <li>For each group, its distinct vectors, aligned to 4 bytes (BYTE) or 64 bytes (FLOAT32). | ||
| * <li>For each field: | ||
| * <ul> | ||
| * <li>The sparse-encoding data (only when some documents lack the field): DocIds encoded by | ||
| * {@link | ||
| * org.apache.lucene.codecs.lucene90.IndexedDISI#writeBitSet(org.apache.lucene.search.DocIdSetIterator, | ||
| * org.apache.lucene.store.IndexOutput, byte)}, followed by the ordinal-to-doc mapping | ||
| * encoded by {@link org.apache.lucene.util.packed.DirectMonotonicWriter}. | ||
| * <li>The {@code ordToVecOrd} map (aligned to 4 bytes): one entry per document ordinal | ||
| * giving the group ordinal of the shared vector, packed by {@link | ||
| * org.apache.lucene.util.packed.DirectWriter}. | ||
| * </ul> | ||
| * </ul> | ||
| * | ||
| * <h2>.vdm (vector de-dup metadata) file</h2> | ||
| * | ||
| * <p>A list of groups, each: | ||
| * | ||
| * <ul> | ||
| * <li><b>[int32]</b> group ordinal | ||
| * <li><b>[int32]</b> vector dimension | ||
| * <li><b>[int32]</b> vector encoding ordinal | ||
| * <li><b>[int32]</b> group size (number of distinct vectors) | ||
| * <li><b>[int64]</b> offset to this group's vectors in the .vdd file | ||
| * <li><b>[int64]</b> length of this group's vectors, in bytes | ||
| * </ul> | ||
| * | ||
| * <p>terminated by <b>[int32]</b> {@code -1}, then a list of fields, each: | ||
| * | ||
| * <ul> | ||
| * <li><b>[int32]</b> field number | ||
| * <li><b>[int32]</b> vector similarity function ordinal | ||
| * <li><b>[int32]</b> vector dimension | ||
| * <li><b>[int32]</b> vector encoding ordinal | ||
| * <li><b>[int32]</b> ordinal of the group holding this field's vectors | ||
| * <li><b>[int32]</b> the number of documents having values for this field | ||
| * <li>the sparse-encoding metadata (docs-with-field offset/length and ordToDoc configuration), as | ||
| * written by {@link | ||
| * org.apache.lucene.codecs.lucene95.OrdToDocDISIReaderConfiguration#writeStoredMeta} | ||
| * <li><b>[int64]</b> offset to this field's {@code ordToVecOrd} map in the .vdd file | ||
| * <li><b>[int64]</b> length of this field's {@code ordToVecOrd} map, in bytes | ||
| * </ul> | ||
| * | ||
| * <p>also terminated by <b>[int32]</b> {@code -1}. | ||
| * | ||
| * <h2>Complexity</h2> | ||
| * | ||
| * <p>Let {@code N} be the number of indexed vectors (one per document per field), {@code U} the | ||
| * number of distinct vectors, and {@code d} the dimension. De-duplication interns each vector via a | ||
| * hash lookup with linear probing, resolving hash collisions with a full equality check. | ||
| * | ||
| * <ul> | ||
| * <li><b>Indexing (flush):</b> expected {@code O(N * d)} time (a hash plus occasional equality | ||
| * check per vector). Heap is {@code O(U * d)} for the distinct vectors held in the group, | ||
| * plus {@code O(N)} for the per-document references and {@code ordToVecOrd} entries. | ||
| * <li><b>Merge:</b> expected {@code O(N * d)} time; distinct vectors are written to disk as soon | ||
| * as they are first seen rather than buffered, so heap stays {@code O(N)} (the per-field | ||
| * {@code ordToVecOrd} maps and light per-vector handles) with no {@code O(U * d)} term. When | ||
| * a source segment is itself in this format, equality is decided by comparing group ordinals | ||
| * in {@code O(1)} without reading the vectors back. | ||
| * <li><b>Reading:</b> both the vectors and the {@code ordToVecOrd} map stay off-heap; a read | ||
| * resolves a document ordinal to its vector via one extra {@code ordToVecOrd} lookup. | ||
| * </ul> | ||
| * | ||
| * @lucene.experimental | ||
| */ | ||
| final class DedupFlatVectorsFormat extends FlatVectorsFormat { | ||
| static final String NAME = "DedupFlatVectorsFormat"; | ||
|
|
||
| static final String META_CODEC_NAME = "DedupFlatVectorsFormatMeta"; | ||
| static final String META_EXTENSION = "vdm"; | ||
|
|
||
| static final String VECTOR_DATA_CODEC_NAME = "DedupFlatVectorsFormatVectorData"; | ||
| static final String VECTOR_DATA_EXTENSION = "vdd"; | ||
|
|
||
| static final int VERSION_START = 0; | ||
| static final int VERSION_CURRENT = VERSION_START; | ||
|
|
||
| private static final DedupFlatVectorsScorer FLAT_VECTORS_SCORER = new DedupFlatVectorsScorer(); | ||
|
|
||
| DedupFlatVectorsFormat() { | ||
| super(NAME); | ||
| } | ||
|
|
||
| @Override | ||
| public FlatVectorsWriter fieldsWriter(SegmentWriteState state) throws IOException { | ||
| return new DedupFlatVectorsWriter(state, FLAT_VECTORS_SCORER); | ||
| } | ||
|
|
||
| @Override | ||
| public FlatVectorsReader fieldsReader(SegmentReadState state) throws IOException { | ||
| return new DedupFlatVectorsReader(state, FLAT_VECTORS_SCORER); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.