Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 18c4a8a

Browse files
committedJan 25, 2025·
Address Review Comments
Signed-off-by: Naveen Tatikonda <navtat@amazon.com>
1 parent 83e1754 commit 18c4a8a

File tree

4 files changed

+61
-47
lines changed

4 files changed

+61
-47
lines changed
 

‎jni/src/faiss_index_service.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ jlong IndexService::initIndexFromTemplate(
187187
//Makes sure the index is deleted when the destructor is called, this cannot be passed in the constructor
188188
idMap->own_fields = true;
189189

190-
// TODO: allocIndex
190+
// TODO: allocIndex for IVF
191191
allocIndex(dynamic_cast<faiss::Index *>(idMap->index), dim, numVectors);
192192

193193
//Release the ownership so as to make sure not delete the underlying index that is created. The index is needed later
@@ -328,7 +328,7 @@ jlong BinaryIndexService::initIndexFromTemplate(
328328
//Makes sure the index is deleted when the destructor is called, this cannot be passed in the constructor
329329
idMap->own_fields = true;
330330

331-
// TODO: allocIndex
331+
// TODO: allocIndex for IVF
332332
allocIndex(dynamic_cast<faiss::Index *>(idMap->index), dim, numVectors);
333333

334334
//Release the ownership so as to make sure not delete the underlying index that is created. The index is needed later
@@ -486,7 +486,7 @@ jlong ByteIndexService::initIndexFromTemplate(
486486
//Makes sure the index is deleted when the destructor is called, this cannot be passed in the constructor
487487
idMap->own_fields = true;
488488

489-
// TODO: allocIndex
489+
// TODO: allocIndex for IVF
490490
allocIndex(dynamic_cast<faiss::Index *>(idMap->index), dim, numVectors);
491491

492492
//Release the ownership so as to make sure not delete the underlying index that is created. The index is needed later

‎src/main/java/org/opensearch/knn/index/KNNIndexShard.java

+11-6
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,7 @@ List<EngineFileContext> getEngineFileContexts(IndexReader indexReader, KNNEngine
193193
fileExtension,
194194
spaceType,
195195
modelId,
196-
quantizationConfig == QuantizationConfig.EMPTY
197-
? VectorDataType.get(
198-
fieldInfo.attributes().getOrDefault(VECTOR_DATA_TYPE_FIELD, VectorDataType.FLOAT.getValue())
199-
)
200-
: quantizationConfig.getQuantizationType() == ScalarQuantizationType.EIGHT_BIT ? VectorDataType.BYTE
201-
: VectorDataType.BINARY
196+
getVectorDataType(quantizationConfig, fieldInfo)
202197
)
203198
);
204199
}
@@ -228,6 +223,16 @@ List<EngineFileContext> getEngineFileContexts(
228223
.collect(Collectors.toList());
229224
}
230225

226+
private VectorDataType getVectorDataType(QuantizationConfig quantizationConfig, FieldInfo fieldInfo) {
227+
if (quantizationConfig == QuantizationConfig.EMPTY) {
228+
return VectorDataType.get(fieldInfo.attributes().getOrDefault(VECTOR_DATA_TYPE_FIELD, VectorDataType.FLOAT.getValue()));
229+
}
230+
if (quantizationConfig.getQuantizationType() == ScalarQuantizationType.EIGHT_BIT) {
231+
return VectorDataType.BYTE;
232+
}
233+
return VectorDataType.BINARY;
234+
}
235+
231236
@AllArgsConstructor
232237
@Getter
233238
@VisibleForTesting

‎src/main/java/org/opensearch/knn/index/codec/KNN990Codec/NativeEngines990KnnVectorsWriter.java

+16-13
Original file line numberDiff line numberDiff line change
@@ -257,18 +257,8 @@ private QuantizationState train(
257257
if (quantizationParams != null && totalLiveDocs > 0) {
258258
KNNVectorValues<?> knnVectorValues = knnVectorValuesSupplier.get();
259259

260-
// We will not be writing quantization state for 8 bits into a segment file because we are not quantizing the query vectors and
261-
// we are storing the template index after training in the quantization state to use it later in the index build strategy for
262-
// ingesting data
263-
if ((quantizationParams.getTypeIdentifier()).equals(
264-
ScalarQuantizationParams.generateTypeIdentifier(ScalarQuantizationType.EIGHT_BIT)
265-
)) {
266-
quantizationState = quantizationService.train(quantizationParams, knnVectorValues, totalLiveDocs, fieldInfo);
267-
} else {
268-
initQuantizationStateWriterIfNecessary();
269-
quantizationState = quantizationService.train(quantizationParams, knnVectorValues, totalLiveDocs, fieldInfo);
270-
quantizationStateWriter.writeState(fieldInfo.getFieldNumber(), quantizationState);
271-
}
260+
quantizationState = quantizationService.train(quantizationParams, knnVectorValues, totalLiveDocs, fieldInfo);
261+
writeQuantizationState(quantizationParams, quantizationState, fieldInfo.getFieldNumber());
272262
}
273263

274264
return quantizationState;
@@ -289,11 +279,24 @@ private int getLiveDocs(KNNVectorValues<?> vectorValues) throws IOException {
289279
return liveDocs;
290280
}
291281

292-
private void initQuantizationStateWriterIfNecessary() throws IOException {
282+
private void writeQuantizationState(QuantizationParams quantizationParams, QuantizationState quantizationState, int fieldNumber)
283+
throws IOException {
284+
285+
// We will not be writing quantization state for 8 bits into a segment file because we are not quantizing the query vectors and
286+
// we are storing the template index after training in the quantization state to use it later in the index build strategy for
287+
// ingesting data
288+
if ((quantizationParams.getTypeIdentifier()).equals(
289+
ScalarQuantizationParams.generateTypeIdentifier(ScalarQuantizationType.EIGHT_BIT)
290+
)) {
291+
return;
292+
}
293+
294+
// Initialize quantization state writer if required
293295
if (quantizationStateWriter == null) {
294296
quantizationStateWriter = new KNN990QuantizationStateWriter(segmentWriteState);
295297
quantizationStateWriter.writeHeader(segmentWriteState);
296298
}
299+
quantizationStateWriter.writeState(fieldNumber, quantizationState);
297300
}
298301

299302
private boolean shouldSkipBuildingVectorDataStructure(final long docCount) {

‎src/main/java/org/opensearch/knn/index/codec/nativeindex/MemOptimizedNativeIndexBuildStrategy.java

+31-25
Original file line numberDiff line numberDiff line change
@@ -58,31 +58,7 @@ public void buildAndWriteIndex(final BuildIndexParams indexInfo) throws IOExcept
5858
KNNEngine engine = indexInfo.getKnnEngine();
5959
Map<String, Object> indexParameters = indexInfo.getParameters();
6060
IndexBuildSetup indexBuildSetup = QuantizationIndexUtils.prepareIndexBuild(knnVectorValues, indexInfo);
61-
long indexMemoryAddress;
62-
63-
if (isTemplate(indexInfo)) {
64-
// Initialize the index from Template
65-
indexMemoryAddress = AccessController.doPrivileged(
66-
(PrivilegedAction<Long>) () -> JNIService.initIndexFromTemplate(
67-
indexInfo.getTotalLiveDocs(),
68-
indexBuildSetup.getDimensions(),
69-
indexParameters,
70-
engine,
71-
getIndexTemplate(indexInfo)
72-
)
73-
);
74-
75-
} else {
76-
// Initialize the index
77-
indexMemoryAddress = AccessController.doPrivileged(
78-
(PrivilegedAction<Long>) () -> JNIService.initIndex(
79-
indexInfo.getTotalLiveDocs(),
80-
indexBuildSetup.getDimensions(),
81-
indexParameters,
82-
engine
83-
)
84-
);
85-
}
61+
long indexMemoryAddress = initializeIndex(indexInfo, indexBuildSetup, indexParameters, engine);
8662

8763
try (
8864
final OffHeapVectorTransfer vectorTransfer = getVectorTransfer(
@@ -150,6 +126,36 @@ public void buildAndWriteIndex(final BuildIndexParams indexInfo) throws IOExcept
150126
}
151127
}
152128

129+
private long initializeIndex(
130+
BuildIndexParams indexInfo,
131+
IndexBuildSetup indexBuildSetup,
132+
Map<String, Object> indexParameters,
133+
KNNEngine engine
134+
) {
135+
if (isTemplate(indexInfo)) {
136+
// Initialize the index from Template
137+
return AccessController.doPrivileged(
138+
(PrivilegedAction<Long>) () -> JNIService.initIndexFromTemplate(
139+
indexInfo.getTotalLiveDocs(),
140+
indexBuildSetup.getDimensions(),
141+
indexParameters,
142+
engine,
143+
getIndexTemplate(indexInfo)
144+
)
145+
);
146+
147+
}
148+
// Initialize the index
149+
return AccessController.doPrivileged(
150+
(PrivilegedAction<Long>) () -> JNIService.initIndex(
151+
indexInfo.getTotalLiveDocs(),
152+
indexBuildSetup.getDimensions(),
153+
indexParameters,
154+
engine
155+
)
156+
);
157+
}
158+
153159
private static boolean isTemplate(final BuildIndexParams indexInfo) {
154160
return (indexInfo.getQuantizationState() instanceof ByteScalarQuantizationState);
155161
}

0 commit comments

Comments
 (0)
Please sign in to comment.