Skip to content

Fix GPU and Java-heap OOM when building large vector indexes - #2476

Open
nvzm123 wants to merge 2 commits into
NVIDIA:mainfrom
nvzm123:zackm_cuvslucene-139
Open

Fix GPU and Java-heap OOM when building large vector indexes#2476
nvzm123 wants to merge 2 commits into
NVIDIA:mainfrom
nvzm123:zackm_cuvslucene-139

Conversation

@nvzm123

@nvzm123 nvzm123 commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

Summary

This ports NVIDIA/cuvs-lucene#173 into java/cuvs-lucene following the move into the cuVS monorepo.

It addresses two memory-pressure problems when building accelerated HNSW indexes over large datasets:

  • Uses host-backed cuVS matrices instead of eagerly placing the complete dataset in device memory.
  • Streams merged vectors into a native host matrix and samples higher-layer vectors from that matrix, avoiding a complete List<float[]> copy on the Java heap.

The change also consolidates the HNSW graph and field-writing paths around CuVSMatrix, removes redundant size/resource parameters, and derives quantized-vector byte width from the matrix so binary and scalar quantization use the correct width.

The original work superseded NVIDIA/cuvs-lucene#141. The related increase of MAX_HNSW_LAYERS to 99 is already present on the current monorepo main and is therefore not part of this diff.

Validation status

Fresh port-only structural checks:

  • cd java/cuvs-lucene && mvn -q -DskipTests compile — passed
  • cd java/cuvs-lucene && mvn -q spotless:check — passed

These commands compile and check formatting; they do not constitute behavioral or GPU validation.

The source PR historically reported local passes for:

  • TestMerge, including testLargeScaleMerge with -DlargeScale=true
  • TestCuVSVectorsFormat
  • TestLucene99AcceleratedHNSWVectorsFormat
  • TestQuantizedVectorsFormats

Those results predate this port and are not validation of the current branch. The original PR's final standalone-repository CI run was not green, so the relevant Java/GPU suites must be rerun from java/cuvs-lucene. We also need combined validation with PR #2475, including its full PyLucene GPU end-to-end suite.

The source PR also recorded an intermittent, seed-dependent TestLucene99AcceleratedHNSWVectorsFormat.testCheckIntegrityReadsAllBytes failure during full-suite randomized graph testing. It passed in isolation and reportedly occurred at a similar rate on the source base branch. This historical observation has not yet been re-evaluated in the monorepo.

Signed-off-by: Zack Meeks <zmeeks@nvidia.com>
@copy-pr-bot

copy-pr-bot Bot commented Aug 17, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@nvzm123
nvzm123 marked this pull request as ready for review August 20, 2026 04:03
@nvzm123
nvzm123 requested a review from a team as a code owner August 20, 2026 04:03
rapids-bot Bot pushed a commit that referenced this pull request Sep 10, 2026
This PR replaces [cuVS-Lucene #195](NVIDIA/cuvs-lucene#195) as cuVS-Lucene has been merged into cuVS.

This PR builds on the out-of-core host-streaming refactor from #2476 by @nvzm123. This PR subsumes #2476 to avoid stacking the PRs.

On top of #2476, this PR adds the following.

**Improvements**

- Native flat buffering: stream vectors directly into a native host matrix during indexing instead of buffering them as a heap List<float[]>, cutting peak host memory from ~2x to ~1x and eliminating the per-vector matrix-assembly copy. Opt-in; requires all input vectors to be indexed in the original order, i.e. not supported for sorted, merged, or filtered index segments; not currently supported for segments with quantized fields.
- Parallelized CAGRA-to-HNSW graph conversion: materializing the CAGRA adjacency into on-heap NeighborArrays was a serial per-node loop; parallelized under the existing `writerThreads` knob.
- Parallelized level-0 HNSW graph serialization: level-0 (all N nodes) is now delta/VInt-encoded in parallel across memory-bounded waves of threads, then concatenated to the `IndexOutput` in node order according to on-disk format in the serial path.

**New example: OptimizedCagraHnswBuildExample**

A reference pattern for building a large accelerated HNSW index with all ingest- and build-side optimizations, showcasing:

- Streaming, prefetched, bounded-memory ingestion: open the source file once, read it front-to-back in large sequential chunks; hold at most two chunks in memory; fill the next chunk on a background thread while the ingest thread drains the current one, hiding disk read behind indexing; unpack into a caller-reused float[] allocation (no per-vector allocation, safe because Lucene copies the value eagerly inside `addDocument`).
- Native flat buffering sized exactly to each segment's vector count via `withNumInputVectors`, avoiding the heap-buffered assembly copy.
- Overlapping multi-segment build:
    - Default: K single-segment passes appended to one directory; peak host memory is one slice (N/K).
    - Overlapped: a bounded pool (`PIPELINE_DEPTH`) builds segments concurrently into their own directories with the GPU commit serialized on a semaphore (ingest overlaps a prior segment's GPU commit), then combines the finished per-segment indexes by hardlinking their files into the final directory via `HardlinkCopyDirectoryWrapper` + `addIndexes` (no bulk copy of vector data). Peak host memory is up to `PIPELINE_DEPTH * N / K`.
- An `enableRMMAsyncMemory()` call (with a note that it must not be used with CPU-only codecs) to opt-in to RMM-managed memory resources, plus exposing the primary tuning knobs (withMaxConn, withBeamWidth, withCuvsDistanceType, withWriterThreads).

Authors:
  - James Xia (https://github.com/jamxia155)
  - https://github.com/nvzm123
  - Igor Motov (https://github.com/imotov)

Approvers:
  - James Lamb (https://github.com/jameslamb)
  - Igor Motov (https://github.com/imotov)

URL: #2481
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant