Skip to content

[CuVS-Java] Automate panama bindings generation, Include IVF_PQ parameters in CAGRA index parameters and other changes #831

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
wants to merge 28 commits into
base: branch-25.06
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
1cd8d67
initial setup
Apr 10, 2025
48f878f
refactor script and fix comment
Apr 10, 2025
9a8c606
update script - add logic to detect and update panama java files
Apr 10, 2025
83d1ae2
update comment/messages in the script
Apr 10, 2025
4dcda3c
plug in the bindings script into the java build script
Apr 11, 2025
5a388c7
update comment
Apr 11, 2025
07e16d0
update script to remove last blank line from generated bindings
Apr 11, 2025
fcef00b
update script - renaming and conditional delete
Apr 11, 2025
4327f63
update script - handle exceptions
Apr 11, 2025
b23aa36
update script - trim last blank line for all generated files
Apr 11, 2025
271dee5
remove manually generated bindings with the new ones
Apr 11, 2025
cc745b7
plug in new binding classes
Apr 11, 2025
20063eb
Merge branch 'branch-25.06' into vivek/automate-panama-bindings
Apr 19, 2025
d347610
bump up javadoc plugin version and fix javadoc
Apr 19, 2025
6021c09
update readme and simplify logging in examples
Apr 19, 2025
df1f6f2
Enable ivf-pq index and search parameter configuration via the cuvs-j…
Apr 20, 2025
064760d
remove panama bindings
Apr 21, 2025
37cc0d4
update gitignore
Apr 21, 2025
02cc69f
update gitignore, add source gpuinfo struct, and simplify script
Apr 21, 2025
53fddbb
add gpuinfo header and simplify script
Apr 21, 2025
806af86
remove license header and a gitignore file
Apr 21, 2025
050defc
Merge branch 'branch-25.06' into vivek/automate-panama-bindings
narangvivek10 Apr 24, 2025
b516348
adding jextract to prerequisites in readme
Apr 24, 2025
570fa2a
Download jextract-22 if not already present
Apr 24, 2025
6dfef85
Debug printing of the CUDA_HOME path, to aid troubleshooting in CI env
Apr 24, 2025
306229d
Fallback to /usr/local/cuda in case we're unable to find a include di…
Apr 24, 2025
73db62a
Merge branch 'branch-25.06' into vivek/automate-panama-bindings
narangvivek10 Apr 28, 2025
5bf8961
build c wrapper before generating panama bindings
Apr 28, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions java/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# cuvs-java
/cuvs-java/target/
/cuvs-java/bin/
/cuvs-java/src/main/java22/com/nvidia/cuvs/internal/panama/
/cuvs-java/*.cag
# internal
/internal/.ninja_deps
/internal/.ninja_log
/internal/CMakeCache.txt
/internal/CMakeFiles/
/internal/CPM_modules/
/internal/_deps/
/internal/build.ninja
/internal/cmake/
/internal/cmake_install.cmake
/internal/cpm-package-lock.cmake
/internal/Makefile
# examples
/examples/target/
30 changes: 21 additions & 9 deletions java/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
Prerequisites
-------------
# CuVS Java API

* JDK 22
* Maven 3.9.6 or later

To build this API, please do `./build.sh java` in the top level directory. Since this API is dependent on `libcuvs` it must be noted that `libcuvs` gets built automatically before building this API.
CuVS Java API provides a Java based simple, efficient, and a robust vector search API.

Alternatively, please build libcuvs (`./build.sh libcuvs` from top level directory) before building the Java API with `./build.sh` from this directory.
> [!CAUTION]
> CuVS 25.06 contains an experimental version and updates to this API are expected in the coming release.

Building
--------
## Prerequisites

`./build.sh` will generate the `libcuvs_java.so` file in the `internal/` directory, and then build the final jar file for the cuVS Java API in the `cuvs-java/` directory.
- [CuVS libraries](https://docs.rapids.ai/api/cuvs/stable/build/#build-from-source)
- [maven 3.9.6 or above](https://maven.apache.org/download.cgi)
- [JDK 22](https://openjdk.org/projects/jdk/22/)


## Building

The libcuvs C and C++ libraries are needed for this API. If libcuvs libraries have not been built and installed, use `./build.sh libcuvs java` in the top level directory to build this API.

Alternatively, if libcuvs libraries are already built and you just want to build this API, please
do `./build.sh java` in the top level directory or just do `./build.sh` in this directory.


## Examples

A few starter examples of CAGRA, HNSW, and Bruteforce index are provided in the `examples` directory.
11 changes: 11 additions & 0 deletions java/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ if [ -z "$CMAKE_PREFIX_PATH" ]; then
export CMAKE_PREFIX_PATH=`pwd`/../cpp/build
fi

# Generate Panama FFM API bindings and update (if any of them changed)
/bin/bash panama-bindings/generate-bindings.sh

BINDINGS_GENERATION_RETURN_VALUE=$?
if [ $BINDINGS_GENERATION_RETURN_VALUE != 0 ]
then
echo "Bindings generation did not complete normally (returned value ${BINDINGS_GENERATION_RETURN_VALUE})"
echo "Forcing this build process to abort"
exit 1
fi

cd internal && cmake . && cmake --build . \
&& cd .. \
&& mvn install:install-file -DgroupId=$GROUP_ID -DartifactId=cuvs-java-internal -Dversion=$VERSION -Dpackaging=so -Dfile=$SO_FILE_PATH/libcuvs_java.so \
Expand Down
1 change: 0 additions & 1 deletion java/cuvs-java/.gitignore

This file was deleted.

5 changes: 1 addition & 4 deletions java/cuvs-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<version>3.11.2</version>
<executions>
<execution>
<id>attach-javadocs</id>
Expand Down Expand Up @@ -298,7 +298,4 @@
</plugin>
</plugins>
</build>



</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@

package com.nvidia.cuvs;

import com.nvidia.cuvs.spi.CuVSProvider;

import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Path;
import java.util.Objects;
import java.util.UUID;

import com.nvidia.cuvs.spi.CuVSProvider;

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class BruteForceQuery {
* @param queryVectors 2D float query vector array
* @param mapping an instance of ID mapping
* @param topK the top k results to return
* @param prefilter the prefilter data to use while searching the BRUTEFORCE
* @param prefilters the prefilters data to use while searching the BRUTEFORCE
* index
* @param numDocs Maximum of bits in each prefilter, representing number of documents in this index.
* Used only when prefilter(s) is/are passed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.io.OutputStream;
import java.nio.file.Path;
import java.util.Objects;
import java.util.UUID;

import com.nvidia.cuvs.spi.CuVSProvider;

Expand Down
127 changes: 120 additions & 7 deletions java/cuvs-java/src/main/java/com/nvidia/cuvs/CagraIndexParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class CagraIndexParams {
private final int graphDegree;
private final int nnDescentNiter;
private final int numWriterThreads;
private final CuVSIvfPqParams cuVSIvfPqParams;

/**
* Enum that denotes which ANN algorithm is used to build CAGRA graph.
Expand Down Expand Up @@ -158,15 +159,106 @@ private CuvsDistanceType(int value) {

}

private CagraIndexParams(int intermediateGraphDegree, int graphDegree,
CagraGraphBuildAlgo CuvsCagraGraphBuildAlgo, int nnDescentNiter, int writerThreads,
CuvsDistanceType cuvsDistanceType) {
/**
* Enum that denotes codebook gen options.
*/
public enum CodebookGen {

PER_SUBSPACE(0),

PER_CLUSTER(1);

/**
* The value for the enum choice.
*/
public final int value;

private CodebookGen(int value) {
this.value = value;
}
}

/**
* Enum that denotes cuda datatypes.
*/
public enum CudaDataType {

CUDA_R_16F(2),

CUDA_C_16F(6),

CUDA_R_16BF(14),

CUDA_C_16BF(15),

CUDA_R_32F(0),

CUDA_C_32F(4),

CUDA_R_64F(1),

CUDA_C_64F(5),

CUDA_R_4I(16),

CUDA_C_4I(17),

CUDA_R_4U(18),

CUDA_C_4U(19),

CUDA_R_8I(3),

CUDA_C_8I(7),

CUDA_R_8U(8),

CUDA_C_8U(9),

CUDA_R_16I(20),

CUDA_C_16I(21),

CUDA_R_16U(22),

CUDA_C_16U(23),

CUDA_R_32I(10),

CUDA_C_32I(11),

CUDA_R_32U(12),

CUDA_C_32U(13),

CUDA_R_64I(24),

CUDA_C_64I(25),

CUDA_R_64U(26),

CUDA_C_64U(27),

CUDA_R_8F_E4M3(28),

CUDA_R_8F_E5M2(29);

public final int value;

private CudaDataType(int value) {
this.value = value;
}
}

private CagraIndexParams(int intermediateGraphDegree, int graphDegree, CagraGraphBuildAlgo CuvsCagraGraphBuildAlgo,
int nnDescentNiter, int writerThreads, CuvsDistanceType cuvsDistanceType, CuVSIvfPqParams cuVSIvfPqParams) {
this.intermediateGraphDegree = intermediateGraphDegree;
this.graphDegree = graphDegree;
this.cuvsCagraGraphBuildAlgo = CuvsCagraGraphBuildAlgo;
this.nnDescentNiter = nnDescentNiter;
this.numWriterThreads = writerThreads;
this.cuvsDistanceType = cuvsDistanceType;
this.cuVSIvfPqParams = cuVSIvfPqParams;
}

/**
Expand Down Expand Up @@ -216,11 +308,19 @@ public int getNumWriterThreads() {
return numWriterThreads;
}

/**
* Gets the IVF_PQ parameters.
*/
public CuVSIvfPqParams getCuVSIvfPqParams() {
return cuVSIvfPqParams;
}

@Override
public String toString() {
return "CagraIndexParams [cuvsCagraGraphBuildAlgo=" + cuvsCagraGraphBuildAlgo + ", cuvsDistanceType="
+ cuvsDistanceType + ", intermediateGraphDegree=" + intermediateGraphDegree + ", graphDegree=" + graphDegree
+ ", nnDescentNiter=" + nnDescentNiter + ", numWriterThreads=" + numWriterThreads + "]";
+ ", nnDescentNiter=" + nnDescentNiter + ", numWriterThreads=" + numWriterThreads + ", cuVSIvfPqParams="
+ cuVSIvfPqParams + "]";
}

/**
Expand All @@ -234,8 +334,10 @@ public static class Builder {
private int graphDegree = 64;
private int nnDescentNumIterations = 20;
private int numWriterThreads = 2;
private CuVSIvfPqParams cuVSIvfPqParams = new CuVSIvfPqParams.Builder().build();

public Builder() { }
public Builder() {
}

/**
* Sets the degree of input graph for pruning.
Expand Down Expand Up @@ -305,14 +407,25 @@ public Builder withNumWriterThreads(int numWriterThreads) {
return this;
}

/**
* Sets the IVF_PQ index parameters.
*
* @param cuVSIvfPqParams the IVF_PQ index parameters
* @return an instance of Builder
*/
public Builder withCuVSIvfPqParams(CuVSIvfPqParams cuVSIvfPqParams) {
this.cuVSIvfPqParams = cuVSIvfPqParams;
return this;
}

/**
* Builds an instance of {@link CagraIndexParams}.
*
* @return an instance of {@link CagraIndexParams}
*/
public CagraIndexParams build() {
return new CagraIndexParams(intermediateGraphDegree, graphDegree, cuvsCagraGraphBuildAlgo,
nnDescentNumIterations, numWriterThreads, cuvsDistanceType);
return new CagraIndexParams(intermediateGraphDegree, graphDegree, cuvsCagraGraphBuildAlgo, nnDescentNumIterations,
numWriterThreads, cuvsDistanceType, cuVSIvfPqParams);
}
}
}
Loading