-
Notifications
You must be signed in to change notification settings - Fork 227
Optimize cuvs-lucene CAGRA_HNSW index build; add example #2481
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
jamxia155
wants to merge
37
commits into
NVIDIA:main
Choose a base branch
from
jamxia155:cuvs-lucene-cagra-hnsw-build-optimization-with-example
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
37 commits
Select commit
Hold shift + click to select a range
c579e87
cuvs-lucene__139: This code allows us to construct the HNSW graph on …
8598e4f
Consolidate duplicated HNSW graph and field-writing methods
nvzm123 f66bdce
Add example: chunked sequential ingestion of large .fbin files
jamxia155 d0b5eb9
Parallelize level-0 HNSW graph serialization in writeGraph
jamxia155 cd23490
Add native flat buffering for single-segment CAGRA_HNSW builds
jamxia155 3ad0d47
Parallelize CAGRA-to-HNSW conversion in GPUBuiltHnswGraph
jamxia155 948b41d
Add prefetching + reused-array to the fbin ingest example
jamxia155 33c85a2
Honor cagraGraphBuildAlgo override in HEURISTIC strategy
jamxia155 1b9b9d8
Expand the fbin ingest example into a full optimized CAGRA-HNSW build
jamxia155 56bc260
Expand OptimizedCagraHnswBuildExample and add early index-sort check
jamxia155 8ffba09
Fix merge errors
jamxia155 cfd3440
Add a Lucene-version tripwire and round-trip test for NativeFlatVecto…
jamxia155 4b4a0d7
Add equivalence test for writerThreads-parallelized graph constructio…
jamxia155 64ee08d
Add functional coverage for native flat buffering (numInputVectors)
jamxia155 22e10fe
Fix merge-time vector-count bug causing intermittent EOF during search
jamxia155 725fc9d
Restore unintended M-derivation change in createMultiLayerHnswGraph
jamxia155 9faae91
Restore unnecessary removals
jamxia155 77cbc65
Fix inconsistent handling of explicit CuVSIvfPqParams under HEURISTIC…
jamxia155 3ab2330
Guard against native flat buffering with quantized fields
jamxia155 68245a1
Merge remote-tracking branch 'origin/main' into cuvs-lucene-cagra-hns…
jamxia155 563428c
Apply Spotless formatting
jamxia155 b99dae2
Merge remote-tracking branch 'origin/main' into cuvs-lucene-cagra-hns…
jamxia155 286f03f
Interrupt in-flight tasks on writeLevel0Parallel failure
jamxia155 36b80d2
Pin NativeFlatVectorsWriter's Lucene99 format, not lucene-core version
jamxia155 7d8812b
Remove TestNativeFlatVectorsWriterFormatConstants
jamxia155 211c341
Add DENSE/SPARSE docsWithField and multi-field round-trip coverage
jamxia155 c420141
Hoist graph.maxConn() out of writeGraph's per-level/per-task loops
jamxia155 1086d44
Document the actual native flat buffering invariant in the example
jamxia155 7d4544f
Merge remote-tracking branch 'origin/main' into cuvs-lucene-cagra-hns…
jamxia155 e446e88
Document and diagnose the numInputVectors auto-flush footgun
jamxia155 6baf35b
Merge remote-tracking branch 'origin/main' into cuvs-lucene-cagra-hns…
jamxia155 e04bfba
Document that numInputVectors applies uniformly to every vector field
jamxia155 e9025da
Remove cagraGraphBuildAlgo override under HEURISTIC strategy
jamxia155 1047334
Merge remote-tracking branch 'origin/main' into cuvs-lucene-cagra-hns…
jamxia155 dc9bcf3
Add CagraHnswBulkIndexWriter for bulk CAGRA_HNSW index building
jamxia155 47077ac
Merge remote-tracking branch 'origin/main' into cuvs-lucene-cagra-hns…
jamxia155 b17ef4f
Move cuvs-lucene examples to examples/java, update spotless triggers
jamxia155 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # Examples | ||
|
|
||
| This maven project contains basic examples that showcase how `cuvs-lucene` can be used. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - The [`cuvs-lucene` prerequisites](../../java/cuvs-lucene/README.md#prerequisites) | ||
|
|
||
| ## Steps | ||
|
|
||
| First build `cuvs-lucene` and install it into your local Maven repository, as described in | ||
| [Building from source](../../java/cuvs-lucene/README.md#building-from-source). From the cuVS repository root: | ||
|
|
||
| ```sh | ||
| ./build.sh libcuvs java lucene | ||
| ``` | ||
|
|
||
| Then return to this directory: | ||
|
|
||
| ```sh | ||
| cd examples/java | ||
| ``` | ||
|
|
||
| To run Accelerated HNSW example do: | ||
|
|
||
| ```sh | ||
| mvn clean install && java -Djava.util.logging.config.file=src/main/resources/logging.properties -cp target/examples-26.10.0-jar-with-merged-services.jar com.nvidia.cuvs.lucene.examples.AcceleratedHnswExample | ||
| ``` | ||
|
|
||
| To run the Index and Search on GPU example do: | ||
|
|
||
| ```sh | ||
| mvn clean install && java -Djava.util.logging.config.file=src/main/resources/logging.properties -cp target/examples-26.10.0-jar-with-merged-services.jar com.nvidia.cuvs.lucene.examples.IndexAndSearchonGPUExample | ||
| ``` | ||
|
|
||
| To run the optimized CAGRA-HNSW build example (reference pattern for efficiently building an | ||
| accelerated HNSW index from a large `.fbin` with every ingest-side knob on — open the file once and | ||
| stream sequential prefetched chunks that overlap the disk read with indexing, hold at most two chunks | ||
| in memory, reuse a single vector array, size a native flat buffer per segment, auto-select the CAGRA | ||
| graph-build algorithm, and optionally partition into K segments built sequentially or overlapped) do: | ||
|
|
||
| ```sh | ||
| mvn clean install && java -Djava.util.logging.config.file=src/main/resources/logging.properties -cp target/examples-26.10.0-jar-with-merged-services.jar com.nvidia.cuvs.lucene.examples.OptimizedCagraHnswBuildExample | ||
| ``` | ||
|
|
||
| With no arguments it generates and indexes a small demo `.fbin` as a single segment; pass a real file, | ||
| chunk size, segment count, and overlap flag as | ||
| `... OptimizedCagraHnswBuildExample <path-to.fbin> <chunkSizeMB> <numSegments> <overlap:true|false>`. |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
263 changes: 263 additions & 0 deletions
263
...es/java/src/main/java/com/nvidia/cuvs/lucene/examples/OptimizedCagraHnswBuildExample.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,263 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package com.nvidia.cuvs.lucene.examples; | ||
|
|
||
| import com.nvidia.cuvs.CagraIndexParams.CuvsDistanceType; | ||
| import com.nvidia.cuvs.lucene.AcceleratedHNSWParams; | ||
| import com.nvidia.cuvs.lucene.CagraHnswBulkIndexWriter; | ||
| import com.nvidia.cuvs.lucene.FbinVectorSource; | ||
| import java.io.IOException; | ||
| import java.nio.ByteBuffer; | ||
| import java.nio.ByteOrder; | ||
| import java.nio.channels.FileChannel; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
| import java.nio.file.StandardOpenOption; | ||
| import java.util.Random; | ||
| import java.util.UUID; | ||
| import java.util.logging.Logger; | ||
| import org.apache.commons.io.FileUtils; | ||
| import org.apache.lucene.document.Document; | ||
| import org.apache.lucene.document.Field; | ||
| import org.apache.lucene.document.KnnFloatVectorField; | ||
| import org.apache.lucene.document.StringField; | ||
| import org.apache.lucene.index.DirectoryReader; | ||
| import org.apache.lucene.index.IndexWriterConfig; | ||
| import org.apache.lucene.index.VectorSimilarityFunction; | ||
| import org.apache.lucene.search.IndexSearcher; | ||
| import org.apache.lucene.search.KnnFloatVectorQuery; | ||
| import org.apache.lucene.search.ScoreDoc; | ||
| import org.apache.lucene.search.TopDocs; | ||
| import org.apache.lucene.store.Directory; | ||
| import org.apache.lucene.store.FSDirectory; | ||
|
|
||
| /** | ||
| * Reference usage of {@link CagraHnswBulkIndexWriter}: builds an accelerated HNSW index (whose | ||
| * graph is built on the GPU with CAGRA) from a {@code .fbin} vector file on local disk. | ||
| * | ||
| * <p>Demonstrates both ways to use {@link CagraHnswBulkIndexWriter}: | ||
| * | ||
| * <ul> | ||
| * <li>{@link #main} — the one-shot convenience path. All of the bulk-build mechanics — | ||
| * prefetched streaming reads, native flat buffering, the {@code IndexWriterConfig} tuning | ||
| * that guarantees a single unmerged segment per slice, K-segment partitioning, and combining | ||
| * the result by hardlink — are owned by {@link CagraHnswBulkIndexWriter} itself; see its | ||
| * Javadoc for how each of those works and the tradeoffs of {@code numSegments} and {@code | ||
| * overlap}. This example only wires up what's genuinely application-specific: where the | ||
| * vectors come from ({@link FbinVectorSource}, or your own {@link | ||
| * com.nvidia.cuvs.lucene.VectorSource} for a different data source), the graph-build quality | ||
| * knobs ({@link AcceleratedHNSWParams}), and — via a {@link | ||
| * CagraHnswBulkIndexWriter.FieldCallback} — any per-vector metadata to attach. | ||
| * <li>{@link #runManualExample} — the manual, direct-instance path: construct a {@link | ||
| * CagraHnswBulkIndexWriter} yourself and drive {@code addDocument}/{@code close} exactly | ||
| * like a plain Lucene {@code IndexWriter}, building each {@link Document} (metadata included) | ||
| * yourself instead of going through a callback. | ||
| * </ul> | ||
| * | ||
| * <p>Usage: {@code OptimizedCagraHnswBuildExample [<path-to.fbin>] [<chunkSizeMB>] [<numSegments>] | ||
| * [<overlap:true|false>]}. With no arguments a small demo {@code .fbin} is generated and indexed as a | ||
| * single segment. | ||
| */ | ||
| public class OptimizedCagraHnswBuildExample { | ||
|
|
||
| private static final Logger log = | ||
| Logger.getLogger(OptimizedCagraHnswBuildExample.class.getName()); | ||
| private static final String ID_FIELD = "id"; | ||
| private static final String CATEGORY_FIELD = "category"; | ||
| private static final String VECTOR_FIELD = "vector_field"; | ||
|
|
||
| public static void main(String[] args) throws Exception { | ||
| int chunkSizeMB = args.length >= 2 ? Integer.parseInt(args[1]) : 32; | ||
| int numSegments = args.length >= 3 ? Math.max(1, Integer.parseInt(args[2])) : 1; | ||
| boolean overlap = args.length >= 4 && Boolean.parseBoolean(args[3]); | ||
| Path indexDirPath = Paths.get(UUID.randomUUID().toString()); | ||
|
|
||
| Path fbinPath; | ||
| boolean generated = false; | ||
| if (args.length >= 1) { | ||
| fbinPath = Paths.get(args[0]); | ||
| } else { | ||
| fbinPath = Paths.get("demo-" + UUID.randomUUID() + ".fbin"); | ||
| writeDemoFbin(fbinPath, 5000, 32, new Random(222)); | ||
| generated = true; | ||
| log.info("No .fbin provided; generated a demo file at " + fbinPath); | ||
| } | ||
|
|
||
| try { | ||
| int dim; | ||
| try (FbinVectorSource probe = new FbinVectorSource(fbinPath, 1)) { | ||
| dim = probe.dimensions(); | ||
| } | ||
|
|
||
| CagraHnswBulkIndexWriter.Config config = | ||
| CagraHnswBulkIndexWriter.Config.builder() | ||
| .field(VECTOR_FIELD, dim, VectorSimilarityFunction.EUCLIDEAN) | ||
| .idField(ID_FIELD) | ||
| .graphBuild( | ||
| new AcceleratedHNSWParams.Builder() | ||
| // HEURISTIC lets cuVS pick the build algorithm and auto-tune its parameters | ||
| // based on maxConn and beamWidth below. | ||
| .withStrategy(AcceleratedHNSWParams.Strategy.HEURISTIC) | ||
| // Primary recall/graph-size knobs. Higher values improve recall at the cost | ||
| // of a larger graph and longer build. Match to your dataset and recall | ||
| // target. | ||
| .withMaxConn(32) | ||
| .withBeamWidth(32) | ||
| // Must match the distance metric used when querying the index. | ||
| .withCuvsDistanceType(CuvsDistanceType.L2Expanded) | ||
| // Starting point: one thread per logical CPU. Profile and tune for your | ||
| // hardware. | ||
| .withWriterThreads(Runtime.getRuntime().availableProcessors()) | ||
| .build()) | ||
| .segments(numSegments, overlap) | ||
| .targetDirectory(indexDirPath) | ||
| .build(); | ||
|
|
||
| log.info( | ||
| "Indexing " | ||
| + fbinPath | ||
| + " (" | ||
| + dim | ||
| + "-dim) into " | ||
| + numSegments | ||
| + " segment(s), " | ||
| + (overlap && numSegments > 1 ? "overlapped" : "sequential") | ||
| + " build, " | ||
| + chunkSizeMB | ||
| + " MB prefetched chunks"); | ||
|
|
||
| // FieldCallback lets the one-shot path attach metadata per vector: indexFbin/build build the | ||
| // id+vector fields internally (they own the loop), so this is how a caller reaches the | ||
| // Document to add anything else -- here, an illustrative "even"/"odd" category by id. | ||
| CagraHnswBulkIndexWriter.indexFbin( | ||
| fbinPath, | ||
| config, | ||
| (doc, id) -> | ||
| doc.add( | ||
| new StringField(CATEGORY_FIELD, id % 2 == 0 ? "even" : "odd", Field.Store.YES)), | ||
| chunkSizeMB); | ||
| log.info("Index build complete: " + indexDirPath); | ||
|
|
||
| runSampleSearch(indexDirPath, fbinPath, 5); | ||
| } finally { | ||
| FileUtils.deleteDirectory(indexDirPath.toFile()); | ||
| if (generated) { | ||
| Files.deleteIfExists(fbinPath); | ||
| } | ||
| } | ||
|
|
||
| runManualExample(); | ||
| } | ||
|
|
||
| /** Runs one k-NN query using the first vector in the file to show the index is searchable. */ | ||
| private static void runSampleSearch(Path indexDirPath, Path fbinPath, int topK) throws Exception { | ||
| float[] queryVector; | ||
| try (FbinVectorSource reader = new FbinVectorSource(fbinPath, 1)) { | ||
| queryVector = reader.get(0); | ||
| } | ||
| try (Directory dir = FSDirectory.open(indexDirPath); | ||
| DirectoryReader reader = DirectoryReader.open(dir)) { | ||
| IndexSearcher searcher = new IndexSearcher(reader); | ||
| TopDocs results = | ||
| searcher.search(new KnnFloatVectorQuery(VECTOR_FIELD, queryVector, topK), topK); | ||
| log.info("Sample search returned " + results.scoreDocs.length + " hits:"); | ||
| for (int i = 0; i < results.scoreDocs.length; i++) { | ||
| ScoreDoc sd = results.scoreDocs[i]; | ||
| Document hit = searcher.storedFields().document(sd.doc); | ||
| log.info( | ||
| " rank " | ||
| + (i + 1) | ||
| + ": id=" | ||
| + hit.get(ID_FIELD) | ||
| + " category=" | ||
| + hit.get(CATEGORY_FIELD) | ||
| + " score=" | ||
| + sd.score); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Short demonstration of the manual, direct-instance API: {@link CagraHnswBulkIndexWriter} is | ||
| * constructed directly and driven with {@code addDocument}/{@code close}, the same shape as a | ||
| * plain Lucene {@code IndexWriter} — the caller builds each {@link Document} itself, including | ||
| * whatever metadata it wants, with no callback needed since it already owns the loop. Unlike the | ||
| * one-shot path above, this only ever builds a single segment; K-segment partitioning and | ||
| * overlap are only available via {@link CagraHnswBulkIndexWriter#indexFbin}/{@link | ||
| * CagraHnswBulkIndexWriter#build}. | ||
| */ | ||
| private static void runManualExample() throws Exception { | ||
| int numDocs = 200; | ||
| int dim = 16; | ||
| Random random = new Random(7); | ||
| Path manualIndexDirPath = Paths.get("manual-" + UUID.randomUUID()); | ||
|
|
||
| try { | ||
| CagraHnswBulkIndexWriter.Config config = | ||
| CagraHnswBulkIndexWriter.Config.builder() | ||
| .field(VECTOR_FIELD, dim, VectorSimilarityFunction.EUCLIDEAN) | ||
| .graphBuild(new AcceleratedHNSWParams.Builder().build()) | ||
| .build(); | ||
|
|
||
| float[][] vectors = new float[numDocs][dim]; | ||
| try (Directory dir = FSDirectory.open(manualIndexDirPath); | ||
| CagraHnswBulkIndexWriter writer = | ||
| new CagraHnswBulkIndexWriter(dir, new IndexWriterConfig(), config, numDocs)) { | ||
| for (int i = 0; i < numDocs; i++) { | ||
| for (int j = 0; j < dim; j++) { | ||
| vectors[i][j] = random.nextFloat() * 100; | ||
| } | ||
| Document doc = new Document(); | ||
| doc.add(new StringField(ID_FIELD, Integer.toString(i), Field.Store.YES)); | ||
| doc.add(new StringField(CATEGORY_FIELD, i % 2 == 0 ? "even" : "odd", Field.Store.YES)); | ||
| doc.add( | ||
| new KnnFloatVectorField( | ||
| VECTOR_FIELD, vectors[i], VectorSimilarityFunction.EUCLIDEAN)); | ||
| writer.addDocument(doc); // same call shape as a plain IndexWriter | ||
| } | ||
| } // close() runs the single native-buffered flush (the GPU CAGRA build happens here) | ||
|
|
||
| try (Directory dir = FSDirectory.open(manualIndexDirPath); | ||
| DirectoryReader reader = DirectoryReader.open(dir)) { | ||
| IndexSearcher searcher = new IndexSearcher(reader); | ||
| TopDocs results = searcher.search(new KnnFloatVectorQuery(VECTOR_FIELD, vectors[0], 1), 1); | ||
| Document hit = searcher.storedFields().document(results.scoreDocs[0].doc); | ||
| log.info( | ||
| "Manual example: nearest neighbor of vector 0 is id=" | ||
| + hit.get(ID_FIELD) | ||
| + " category=" | ||
| + hit.get(CATEGORY_FIELD)); | ||
| } | ||
| } finally { | ||
| FileUtils.deleteDirectory(manualIndexDirPath.toFile()); | ||
| } | ||
| } | ||
|
|
||
| /** Writes a small random {@code .fbin} so the example is runnable without external data. */ | ||
| private static void writeDemoFbin(Path path, int numVectors, int dim, Random random) | ||
| throws IOException { | ||
| ByteBuffer buf = | ||
| ByteBuffer.allocate(8 + numVectors * dim * Float.BYTES).order(ByteOrder.LITTLE_ENDIAN); | ||
| buf.putInt(numVectors); // .fbin header: [num_vectors int32][dimension int32] | ||
| buf.putInt(dim); | ||
| for (int i = 0; i < numVectors; i++) { | ||
| for (int j = 0; j < dim; j++) { | ||
| buf.putFloat(random.nextFloat() * 100); | ||
| } | ||
| } | ||
| buf.flip(); | ||
| try (FileChannel ch = | ||
| FileChannel.open( | ||
| path, | ||
| StandardOpenOption.CREATE, | ||
| StandardOpenOption.WRITE, | ||
| StandardOpenOption.TRUNCATE_EXISTING)) { | ||
| while (buf.hasRemaining()) { | ||
| ch.write(buf); | ||
| } | ||
| } | ||
| } | ||
| } | ||
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
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.
Let's please move this
examples/java. cuvs lucene is a library.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.
Moved.