navneet1v commented on code in PR #14131:
URL: https://github.com/apache/lucene/pull/14131#discussion_r1911714546


##########
lucene/sandbox/src/java/org/apache/lucene/sandbox/vectorsearch/CuVSVectorsWriter.java:
##########
@@ -0,0 +1,402 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.sandbox.vectorsearch;
+
+import com.nvidia.cuvs.BruteForceIndex;
+import com.nvidia.cuvs.BruteForceIndexParams;
+import com.nvidia.cuvs.CagraIndex;
+import com.nvidia.cuvs.CagraIndexParams;
+import com.nvidia.cuvs.CagraIndexParams.CagraGraphBuildAlgo;
+import com.nvidia.cuvs.CuVSResources;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import org.apache.commons.lang3.SerializationUtils;
+import org.apache.lucene.codecs.CodecUtil;
+import org.apache.lucene.codecs.KnnFieldVectorsWriter;
+import org.apache.lucene.codecs.KnnVectorsWriter;
+import org.apache.lucene.index.FieldInfo;
+import org.apache.lucene.index.IndexFileNames;
+import org.apache.lucene.index.MergeState;
+import org.apache.lucene.index.SegmentWriteState;
+import org.apache.lucene.index.Sorter.DocMap;
+import org.apache.lucene.store.IndexOutput;
+import org.apache.lucene.util.IOUtils;
+import org.apache.lucene.util.SuppressForbidden;
+
+public class CuVSVectorsWriter extends KnnVectorsWriter {
+
+  // protected Logger log = Logger.getLogger(getClass().getName());
+
+  private List<CagraFieldVectorsWriter> fieldVectorWriters = new ArrayList<>();
+  private IndexOutput cuVSIndex = null;
+  private SegmentWriteState segmentWriteState = null;
+  private String cuVSDataFilename = null;
+
+  private CagraIndex cagraIndex;
+  private CagraIndex cagraIndexForHnsw;
+
+  private int cuvsWriterThreads;
+  private int intGraphDegree;
+  private int graphDegree;
+  private MergeStrategy mergeStrategy;
+  private CuVSResources resources;
+
+  public enum MergeStrategy {
+    TRIVIAL_MERGE,
+    NON_TRIVIAL_MERGE
+  };
+
+  public CuVSVectorsWriter(
+      SegmentWriteState state,
+      int cuvsWriterThreads,
+      int intGraphDegree,
+      int graphDegree,
+      MergeStrategy mergeStrategy,
+      CuVSResources resources)
+      throws IOException {
+    super();
+    this.segmentWriteState = state;
+    this.mergeStrategy = mergeStrategy;
+    this.cuvsWriterThreads = cuvsWriterThreads;
+    this.intGraphDegree = intGraphDegree;
+    this.graphDegree = graphDegree;
+    this.resources = resources;
+
+    cuVSDataFilename =
+        IndexFileNames.segmentFileName(
+            this.segmentWriteState.segmentInfo.name,
+            this.segmentWriteState.segmentSuffix,
+            CuVSVectorsFormat.VECTOR_DATA_EXTENSION);
+  }
+
+  @Override
+  public long ramBytesUsed() {
+    return 0;
+  }
+
+  @Override
+  public void close() throws IOException {
+    IOUtils.close(cuVSIndex);
+    cuVSIndex = null;
+    fieldVectorWriters.clear();
+    fieldVectorWriters = null;
+  }
+
+  @Override
+  public KnnFieldVectorsWriter<?> addField(FieldInfo fieldInfo) throws 
IOException {
+    CagraFieldVectorsWriter cagraFieldVectorWriter = new 
CagraFieldVectorsWriter(fieldInfo);
+    fieldVectorWriters.add(cagraFieldVectorWriter);
+    return cagraFieldVectorWriter;
+  }
+
+  @SuppressForbidden(reason = "A temporary java.util.File is needed for 
Cagra's serialization")
+  private byte[] createCagraIndex(float[][] vectors, List<Integer> mapping) 
throws Throwable {
+    CagraIndexParams indexParams =
+        new CagraIndexParams.Builder(resources)
+            .withNumWriterThreads(cuvsWriterThreads)
+            .withIntermediateGraphDegree(intGraphDegree)
+            .withGraphDegree(graphDegree)
+            .withCagraGraphBuildAlgo(CagraGraphBuildAlgo.NN_DESCENT)

Review Comment:
   I have some experience with building the Cagra index, and I think NN_DESCENT 
is faster in cagra index creation but it has a high GPU memory footprint. 
Should we use IVF_PQ here? Or can we have a hybrid approach where if doc count 
is < a specific number then we use NN_DESCENT else IVF_PQ.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to