-
Notifications
You must be signed in to change notification settings - Fork 222
Fix silent brute force fallback at aligned vector dimensions #2483
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
Open
imotov
wants to merge
2
commits into
NVIDIA:main
Choose a base branch
from
imotov:issue-2482-fix-cagra-padded-dataset-flush
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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
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
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
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
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
115 changes: 115 additions & 0 deletions
115
java/cuvs-lucene/src/test/java/com/nvidia/cuvs/lucene/TestCagraIndexAtAlignedDimensions.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,115 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package com.nvidia.cuvs.lucene; | ||
|
|
||
| import static com.nvidia.cuvs.lucene.ThreadLocalCuVSResourcesProvider.isSupported; | ||
|
|
||
| import com.nvidia.cuvs.lucene.CuVS2510GPUVectorsWriter.IndexType; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import org.apache.lucene.document.Document; | ||
| import org.apache.lucene.document.KnnFloatVectorField; | ||
| import org.apache.lucene.index.IndexWriter; | ||
| import org.apache.lucene.index.IndexWriterConfig; | ||
| import org.apache.lucene.index.VectorSimilarityFunction; | ||
| import org.apache.lucene.store.Directory; | ||
| import org.apache.lucene.tests.util.LuceneTestCase; | ||
| import org.apache.lucene.tests.util.LuceneTestCase.SuppressSysoutChecks; | ||
| import org.apache.lucene.tests.util.TestUtil; | ||
| import org.apache.lucene.util.InfoStream; | ||
| import org.junit.Assume; | ||
| import org.junit.Test; | ||
|
|
||
| /** | ||
| * A CAGRA row is padded to a 16 byte boundary, so a device matrix whose dimension is already a | ||
| * multiple of four sits at the required stride. cuVS refuses to build an owning padded copy of such | ||
| * a matrix and asks for a view instead, and the writer swallows a failed CAGRA build by falling | ||
| * back to a brute force index. The two together are silent: search keeps returning correct results | ||
| * while nothing on the GPU is a CAGRA index any more. | ||
| * | ||
| * <p>These tests pin the dimensions on both sides of that boundary. | ||
| */ | ||
| @SuppressSysoutChecks(bugUrl = "") | ||
| public class TestCagraIndexAtAlignedDimensions extends LuceneTestCase { | ||
|
|
||
| @Test | ||
| public void testCagraIsBuiltAtAnAlignedDimension() throws IOException { | ||
| // 128 floats is 512 bytes, an exact multiple of the 16 byte CAGRA row alignment. | ||
| assertCagraIsBuilt(128); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCagraIsBuiltAtAnUnalignedDimension() throws IOException { | ||
| // 127 floats is not, so the writer has to fall back to an owning padded copy. | ||
| assertCagraIsBuilt(127); | ||
| } | ||
|
|
||
| /** Indexes a segment of the given dimension and fails if the CAGRA build did not survive it. */ | ||
| private void assertCagraIsBuilt(int dimension) throws IOException { | ||
| Assume.assumeTrue("Requires a GPU", isSupported()); | ||
|
|
||
| RecordingInfoStream infoStream = new RecordingInfoStream(); | ||
| try (Directory directory = newDirectory()) { | ||
| IndexWriterConfig config = | ||
| new IndexWriterConfig() | ||
| .setCodec( | ||
| TestUtil.alwaysKnnVectorsFormat( | ||
| new CuVS2510GPUVectorsFormat( | ||
| new GPUSearchParams.Builder().withIndexType(IndexType.CAGRA).build()))) | ||
| .setInfoStream(infoStream); | ||
|
|
||
| try (IndexWriter writer = new IndexWriter(directory, config)) { | ||
| for (int i = 0; i < 64; i++) { | ||
| float[] vector = new float[dimension]; | ||
| for (int d = 0; d < dimension; d++) { | ||
| vector[d] = random().nextFloat(); | ||
| } | ||
| Document doc = new Document(); | ||
| doc.add(new KnnFloatVectorField("vector", vector, VectorSimilarityFunction.EUCLIDEAN)); | ||
| writer.addDocument(doc); | ||
| } | ||
| writer.commit(); | ||
| } | ||
| } | ||
|
|
||
| assertTrue( | ||
| "The CAGRA build fell back to brute force at dimension " | ||
| + dimension | ||
| + ", messages: " | ||
| + infoStream.messages(), | ||
| infoStream.cagraBuildFailures().isEmpty()); | ||
| } | ||
|
|
||
| /** An InfoStream that keeps the messages, so that a test can tell which index type was built. */ | ||
| private static class RecordingInfoStream extends InfoStream { | ||
|
|
||
| private final List<String> messages = Collections.synchronizedList(new ArrayList<>()); | ||
|
|
||
| @Override | ||
| public void message(String component, String message) { | ||
| messages.add(component + ": " + message); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isEnabled(String component) { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public void close() {} | ||
|
|
||
| List<String> messages() { | ||
| synchronized (messages) { | ||
| return List.copyOf(messages); | ||
| } | ||
| } | ||
|
|
||
| List<String> cagraBuildFailures() { | ||
| return messages().stream().filter(message -> message.contains("CAGRA build failed")).toList(); | ||
| } | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍