Skip to content

Commit 3131a95

Browse files
[CuVS-Java] Automate panama bindings generation, Include IVF_PQ parameters in CAGRA index parameters and other changes (rapidsai#831)
The PR includes code changes for the following: - Automation of Panama bindings generation using jextract. - Adding the ability to configure IVF_PQ index and search parameters via the cuvs-java API (to adapt with the following [underlying changes](rapidsai@18a3d90)). - Updating HNSW example to show the above. - Updating the readme files. - Simplifying logging in examples. - Bumping up the maven-javadoc-plugin version. - Updating and consolidating gitignore file. - Removing unused imports etc. Please note that the existing Panama classes are being deleted because they were manually created and managed. With the new cleaner approach, this will not be needed anymore. Now these binding classes will be generated at build time and so no need to be in the codebase. Authors: - Vivek Narang (https://github.com/narangvivek10) - Ishan Chattopadhyaya (https://github.com/chatman) Approvers: - rhdong (https://github.com/rhdong) - James Lamb (https://github.com/jameslamb) URL: rapidsai#831
1 parent f2d70ae commit 3131a95

55 files changed

Lines changed: 1313 additions & 20005 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

java/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# cuvs-java
2+
/cuvs-java/target/
3+
/cuvs-java/bin/
4+
/cuvs-java/src/main/java22/com/nvidia/cuvs/internal/panama/
5+
/cuvs-java/*.cag
6+
# examples
7+
/examples/target/

java/README.md

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
1-
Prerequisites
2-
-------------
1+
# CuVS Java API
32

4-
* JDK 22
5-
* Maven 3.9.6 or later
63

7-
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.
4+
CuVS Java API provides a Java based simple, efficient, and a robust vector search API.
85

9-
Alternatively, please build libcuvs (`./build.sh libcuvs` from top level directory) before building the Java API with `./build.sh` from this directory.
6+
> [!CAUTION]
7+
> CuVS 25.06 contains an experimental version and updates to this API are expected in the coming release.
108
11-
Building
12-
--------
9+
## Prerequisites
1310

14-
`./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.
11+
- [CuVS libraries](https://docs.rapids.ai/api/cuvs/stable/build/#build-from-source)
12+
- [maven 3.9.6 or above](https://maven.apache.org/download.cgi)
13+
- [JDK 22](https://openjdk.org/projects/jdk/22/)
14+
- [jextract for JDK 22](https://jdk.java.net/jextract/) (If not already installed, the build script downloads it)
15+
16+
17+
## Building
18+
19+
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.
20+
21+
Alternatively, if libcuvs libraries are already built and you just want to build this API, please
22+
do `./build.sh java` in the top level directory or just do `./build.sh` in this directory.
23+
24+
25+
## Examples
26+
27+
A few starter examples of CAGRA, HNSW, and Bruteforce index are provided in the `examples` directory.

java/build.sh

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
1+
#!/bin/bash
2+
3+
set -e -u -o pipefail
4+
15
VERSION="25.06.0" # Note: The version is updated automatically when ci/release/update-version.sh is invoked
26
GROUP_ID="com.nvidia.cuvs"
3-
SO_FILE_PATH="./internal"
7+
SO_FILE_PATH="./internal/build"
48

5-
if [ -z "$CMAKE_PREFIX_PATH" ]; then
6-
export CMAKE_PREFIX_PATH=`pwd`/../cpp/build
9+
if [ -z "${CMAKE_PREFIX_PATH:=}" ]; then
10+
export CMAKE_PREFIX_PATH="$(pwd)/../cpp/build"
711
fi
812

9-
cd internal && cmake . && cmake --build . \
10-
&& cd .. \
11-
&& mvn install:install-file -DgroupId=$GROUP_ID -DartifactId=cuvs-java-internal -Dversion=$VERSION -Dpackaging=so -Dfile=$SO_FILE_PATH/libcuvs_java.so \
13+
cmake -B ./internal/build -S ./internal
14+
cmake --build ./internal/build
15+
16+
# Generate Panama FFM API bindings and update (if any of them changed)
17+
./panama-bindings/generate-bindings.sh
18+
19+
# Build the java layer
20+
mvn install:install-file -DgroupId=$GROUP_ID -DartifactId=cuvs-java-internal -Dversion=$VERSION -Dpackaging=so -Dfile=$SO_FILE_PATH/libcuvs_java.so \
1221
&& cd cuvs-java \
1322
&& mvn verify \
1423
&& mvn install:install-file -Dfile=./target/cuvs-java-$VERSION-jar-with-dependencies.jar -DgroupId=$GROUP_ID -DartifactId=cuvs-java -Dversion=$VERSION -Dpackaging=jar

java/cuvs-java/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

java/cuvs-java/pom.xml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@
258258
<plugin>
259259
<groupId>org.apache.maven.plugins</groupId>
260260
<artifactId>maven-javadoc-plugin</artifactId>
261-
<version>2.9.1</version>
261+
<version>3.11.2</version>
262262
<executions>
263263
<execution>
264264
<id>attach-javadocs</id>
@@ -298,7 +298,4 @@
298298
</plugin>
299299
</plugins>
300300
</build>
301-
302-
303-
304301
</project>

java/cuvs-java/src/main/java/com/nvidia/cuvs/BruteForceIndex.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@
1616

1717
package com.nvidia.cuvs;
1818

19-
import com.nvidia.cuvs.spi.CuVSProvider;
20-
2119
import java.io.InputStream;
2220
import java.io.OutputStream;
2321
import java.nio.file.Path;
2422
import java.util.Objects;
25-
import java.util.UUID;
23+
24+
import com.nvidia.cuvs.spi.CuVSProvider;
2625

2726
/**
2827
*

java/cuvs-java/src/main/java/com/nvidia/cuvs/BruteForceQuery.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class BruteForceQuery {
4040
* @param queryVectors 2D float query vector array
4141
* @param mapping an instance of ID mapping
4242
* @param topK the top k results to return
43-
* @param prefilter the prefilter data to use while searching the BRUTEFORCE
43+
* @param prefilters the prefilters data to use while searching the BRUTEFORCE
4444
* index
4545
* @param numDocs Maximum of bits in each prefilter, representing number of documents in this index.
4646
* Used only when prefilter(s) is/are passed.

java/cuvs-java/src/main/java/com/nvidia/cuvs/CagraIndex.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.io.OutputStream;
2121
import java.nio.file.Path;
2222
import java.util.Objects;
23-
import java.util.UUID;
2423

2524
import com.nvidia.cuvs.spi.CuVSProvider;
2625

java/cuvs-java/src/main/java/com/nvidia/cuvs/CagraIndexParams.java

Lines changed: 120 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class CagraIndexParams {
2929
private final int graphDegree;
3030
private final int nnDescentNiter;
3131
private final int numWriterThreads;
32+
private final CuVSIvfPqParams cuVSIvfPqParams;
3233

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

159160
}
160161

161-
private CagraIndexParams(int intermediateGraphDegree, int graphDegree,
162-
CagraGraphBuildAlgo CuvsCagraGraphBuildAlgo, int nnDescentNiter, int writerThreads,
163-
CuvsDistanceType cuvsDistanceType) {
162+
/**
163+
* Enum that denotes codebook gen options.
164+
*/
165+
public enum CodebookGen {
166+
167+
PER_SUBSPACE(0),
168+
169+
PER_CLUSTER(1);
170+
171+
/**
172+
* The value for the enum choice.
173+
*/
174+
public final int value;
175+
176+
private CodebookGen(int value) {
177+
this.value = value;
178+
}
179+
}
180+
181+
/**
182+
* Enum that denotes cuda datatypes.
183+
*/
184+
public enum CudaDataType {
185+
186+
CUDA_R_16F(2),
187+
188+
CUDA_C_16F(6),
189+
190+
CUDA_R_16BF(14),
191+
192+
CUDA_C_16BF(15),
193+
194+
CUDA_R_32F(0),
195+
196+
CUDA_C_32F(4),
197+
198+
CUDA_R_64F(1),
199+
200+
CUDA_C_64F(5),
201+
202+
CUDA_R_4I(16),
203+
204+
CUDA_C_4I(17),
205+
206+
CUDA_R_4U(18),
207+
208+
CUDA_C_4U(19),
209+
210+
CUDA_R_8I(3),
211+
212+
CUDA_C_8I(7),
213+
214+
CUDA_R_8U(8),
215+
216+
CUDA_C_8U(9),
217+
218+
CUDA_R_16I(20),
219+
220+
CUDA_C_16I(21),
221+
222+
CUDA_R_16U(22),
223+
224+
CUDA_C_16U(23),
225+
226+
CUDA_R_32I(10),
227+
228+
CUDA_C_32I(11),
229+
230+
CUDA_R_32U(12),
231+
232+
CUDA_C_32U(13),
233+
234+
CUDA_R_64I(24),
235+
236+
CUDA_C_64I(25),
237+
238+
CUDA_R_64U(26),
239+
240+
CUDA_C_64U(27),
241+
242+
CUDA_R_8F_E4M3(28),
243+
244+
CUDA_R_8F_E5M2(29);
245+
246+
public final int value;
247+
248+
private CudaDataType(int value) {
249+
this.value = value;
250+
}
251+
}
252+
253+
private CagraIndexParams(int intermediateGraphDegree, int graphDegree, CagraGraphBuildAlgo CuvsCagraGraphBuildAlgo,
254+
int nnDescentNiter, int writerThreads, CuvsDistanceType cuvsDistanceType, CuVSIvfPqParams cuVSIvfPqParams) {
164255
this.intermediateGraphDegree = intermediateGraphDegree;
165256
this.graphDegree = graphDegree;
166257
this.cuvsCagraGraphBuildAlgo = CuvsCagraGraphBuildAlgo;
167258
this.nnDescentNiter = nnDescentNiter;
168259
this.numWriterThreads = writerThreads;
169260
this.cuvsDistanceType = cuvsDistanceType;
261+
this.cuVSIvfPqParams = cuVSIvfPqParams;
170262
}
171263

172264
/**
@@ -216,11 +308,19 @@ public int getNumWriterThreads() {
216308
return numWriterThreads;
217309
}
218310

311+
/**
312+
* Gets the IVF_PQ parameters.
313+
*/
314+
public CuVSIvfPqParams getCuVSIvfPqParams() {
315+
return cuVSIvfPqParams;
316+
}
317+
219318
@Override
220319
public String toString() {
221320
return "CagraIndexParams [cuvsCagraGraphBuildAlgo=" + cuvsCagraGraphBuildAlgo + ", cuvsDistanceType="
222321
+ cuvsDistanceType + ", intermediateGraphDegree=" + intermediateGraphDegree + ", graphDegree=" + graphDegree
223-
+ ", nnDescentNiter=" + nnDescentNiter + ", numWriterThreads=" + numWriterThreads + "]";
322+
+ ", nnDescentNiter=" + nnDescentNiter + ", numWriterThreads=" + numWriterThreads + ", cuVSIvfPqParams="
323+
+ cuVSIvfPqParams + "]";
224324
}
225325

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

238-
public Builder() { }
339+
public Builder() {
340+
}
239341

240342
/**
241343
* Sets the degree of input graph for pruning.
@@ -305,14 +407,25 @@ public Builder withNumWriterThreads(int numWriterThreads) {
305407
return this;
306408
}
307409

410+
/**
411+
* Sets the IVF_PQ index parameters.
412+
*
413+
* @param cuVSIvfPqParams the IVF_PQ index parameters
414+
* @return an instance of Builder
415+
*/
416+
public Builder withCuVSIvfPqParams(CuVSIvfPqParams cuVSIvfPqParams) {
417+
this.cuVSIvfPqParams = cuVSIvfPqParams;
418+
return this;
419+
}
420+
308421
/**
309422
* Builds an instance of {@link CagraIndexParams}.
310423
*
311424
* @return an instance of {@link CagraIndexParams}
312425
*/
313426
public CagraIndexParams build() {
314-
return new CagraIndexParams(intermediateGraphDegree, graphDegree, cuvsCagraGraphBuildAlgo,
315-
nnDescentNumIterations, numWriterThreads, cuvsDistanceType);
427+
return new CagraIndexParams(intermediateGraphDegree, graphDegree, cuvsCagraGraphBuildAlgo, nnDescentNumIterations,
428+
numWriterThreads, cuvsDistanceType, cuVSIvfPqParams);
316429
}
317430
}
318431
}

0 commit comments

Comments
 (0)