1515 */
1616package com .nvidia .cuvs ;
1717
18- import com .nvidia .cuvs .CuVSMatrix .DataType ;
1918import java .util .Arrays ;
2019import java .util .BitSet ;
2120import java .util .Objects ;
2221import java .util .function .LongToIntFunction ;
2322
2423/**
25- * CagraQuery holds the search parameters plus either raw float[][] vectors
26- * or a quantized {@link CuVSMatrix} for querying a CAGRA index .
24+ * CagraQuery holds the CagraSearchParams and the query vectors to be used while
25+ * invoking search .
2726 *
2827 * <p><strong>Thread Safety:</strong> Each CagraQuery instance should use its own
2928 * CuVSResources object that is not shared with other threads. Sharing CuVSResources
3433public class CagraQuery {
3534
3635 private final CagraSearchParams cagraSearchParameters ;
37- private final float [][] queryVectors ;
38- private final CuVSMatrix quantizedQueries ;
3936 private final LongToIntFunction mapping ;
37+ private final float [][] queryVectors ;
4038 private final int topK ;
4139 private final BitSet prefilter ;
4240 private final int numDocs ;
@@ -49,37 +47,30 @@ public class CagraQuery {
4947 * @param cagraSearchParameters an instance of {@link CagraSearchParams} holding
5048 * the search parameters
5149 * @param queryVectors 2D float query vector array
52- * @param quantizedQueries 2D quantized query vector array
5350 * @param mapping a function mapping ordinals (neighbor IDs) to custom user IDs
5451 * @param topK the top k results to return
5552 * @param prefilter A single BitSet to use as filter while searching the CAGRA index
5653 * @param numDocs Total number of dataset vectors; used to align the prefilter correctly
5754 * @param resources CuVSResources instance to use for this query
5855 */
59- private CagraQuery (
56+ public CagraQuery (
6057 CagraSearchParams cagraSearchParameters ,
6158 float [][] queryVectors ,
62- CuVSMatrix quantizedQueries ,
6359 LongToIntFunction mapping ,
6460 int topK ,
6561 BitSet prefilter ,
6662 int numDocs ,
6763 CuVSResources resources ) {
64+ super ();
6865 this .cagraSearchParameters = cagraSearchParameters ;
6966 this .queryVectors = queryVectors ;
70- this .quantizedQueries = quantizedQueries ;
7167 this .mapping = mapping ;
7268 this .topK = topK ;
7369 this .prefilter = prefilter ;
7470 this .numDocs = numDocs ;
7571 this .resources = resources ;
7672 }
7773
78- /** Start building a new CagraQuery. */
79- public static Builder newBuilder (CuVSResources resources ) {
80- return new Builder (resources );
81- }
82-
8374 /**
8475 * Gets the instance of CagraSearchParams initially set.
8576 *
@@ -90,35 +81,14 @@ public CagraSearchParams getCagraSearchParameters() {
9081 }
9182
9283 /**
93- * If this query was built without a quantizer, returns the original float vectors.
94- * Otherwise returns null.
84+ * Gets the query vector 2D float array.
85+ *
86+ * @return 2D float array
9587 */
9688 public float [][] getQueryVectors () {
9789 return queryVectors ;
9890 }
9991
100- /**
101- * If this query was built with a quantizer, returns the quantized Dataset.
102- * Otherwise returns null.
103- */
104- public CuVSMatrix getQuantizedQueries () {
105- return quantizedQueries ;
106- }
107-
108- /** True if this query carries a quantized Dataset instead of float[][] */
109- public boolean hasQuantizedQueries () {
110- return quantizedQueries != null ;
111- }
112-
113- /**
114- * Returns the data type of the query payload:
115- * - 32 for float32 queries
116- * - 8 for quantized queries
117- */
118- public DataType getQueryDataType () {
119- return quantizedQueries != null ? quantizedQueries .dataType () : DataType .FLOAT ;
120- }
121-
12292 /**
12393 * Gets the function mapping ordinals (neighbor IDs) to custom user IDs
12494 */
@@ -164,13 +134,10 @@ public CuVSResources getResources() {
164134
165135 @ Override
166136 public String toString () {
167- return "CagraQuery["
168- + "params="
137+ return "CuVSQuery [cagraSearchParameters="
169138 + cagraSearchParameters
170- + ", floatVectors="
171- + (queryVectors != null ? Arrays .toString (queryVectors ) : "null" )
172- + ", quantized="
173- + (quantizedQueries != null ? ("Dataset@" + quantizedQueries .dataType () + "-bit" ) : "false" )
139+ + ", queryVectors="
140+ + Arrays .toString (queryVectors )
174141 + ", mapping="
175142 + mapping
176143 + ", topK="
@@ -189,7 +156,6 @@ public static class Builder {
189156 private int topK = 2 ;
190157 private BitSet prefilter ;
191158 private int numDocs ;
192- private CuVSQuantizer quantizer ;
193159 private final CuVSResources resources ;
194160
195161 /**
@@ -208,12 +174,12 @@ public Builder(CuVSResources resources) {
208174 /**
209175 * Sets the instance of configured CagraSearchParams to be passed for search.
210176 *
211- * @param params an instance of the configured CagraSearchParams to
212- * be used for this query
177+ * @param cagraSearchParams an instance of the configured CagraSearchParams to
178+ * be used for this query
213179 * @return an instance of this Builder
214180 */
215- public Builder withSearchParams (CagraSearchParams params ) {
216- this .cagraSearchParams = params ;
181+ public Builder withSearchParams (CagraSearchParams cagraSearchParams ) {
182+ this .cagraSearchParams = cagraSearchParams ;
217183 return this ;
218184 }
219185
@@ -267,47 +233,13 @@ public Builder withPrefilter(BitSet prefilter, int numDocs) {
267233 }
268234
269235 /**
270- * Specify a quantizer to automatically transform the float[][] queryVectors
271- * into a quantized {@link CuVSMatrix} using the same quantizer used for training.
272- */
273- public Builder withQuantizer (CuVSQuantizer quantizer ) {
274- this .quantizer = quantizer ;
275- return this ;
276- }
277-
278- /**
279- * Builds the CagraQuery. If a quantizer was provided, queryVectors is ignored
280- * and a quantized Dataset is produced instead.
236+ * Builds an instance of CuVSQuery.
237+ *
238+ * @return an instance of CuVSQuery
281239 */
282- public CagraQuery build () throws Throwable {
283- if (queryVectors == null ) {
284- throw new IllegalArgumentException ("Query vectors must be provided" );
285- }
286-
287- CuVSMatrix quantized = null ;
288- float [][] floatsForQuery = queryVectors ;
289-
290- if (quantizer != null ) {
291- // wrap float[][] in a CuVSMatrix and quantize
292- try (CuVSMatrix tmp = CuVSMatrix .ofArray (queryVectors )) {
293- if (tmp .dataType () != DataType .FLOAT ) {
294- throw new IllegalArgumentException (
295- "Query quantization requires FLOAT input, got " + tmp .dataType ());
296- }
297- quantized = quantizer .transform (tmp );
298- }
299- floatsForQuery = null ;
300- }
301-
240+ public CagraQuery build () {
302241 return new CagraQuery (
303- cagraSearchParams ,
304- floatsForQuery ,
305- quantized ,
306- mapping ,
307- topK ,
308- prefilter ,
309- numDocs ,
310- resources );
242+ cagraSearchParams , queryVectors , mapping , topK , prefilter , numDocs , resources );
311243 }
312244 }
313245}
0 commit comments