generated from amazon-archives/__template_Custom
-
Notifications
You must be signed in to change notification settings - Fork 186
Add quantization state reader and writer #1997
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
Merged
jmazanec15
merged 47 commits into
opensearch-project:main
from
ryanbogan:quantization_state_writer_reader
Sep 5, 2024
Merged
Changes from 9 commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
e9fedcb
Add quantization state reader and writer
ryanbogan 1f5c030
Make inner class private
ryanbogan 627ac7e
Address PR Feedback
ryanbogan a81e99d
Fix tests
ryanbogan daa39ed
Address PR feedback
ryanbogan 8abf3cd
Add writer tests
ryanbogan e7d5ac8
Add reader tests
ryanbogan c804652
Add changelog entry
ryanbogan f711e39
Remove extra line
ryanbogan 425b920
Address PR Feedback
ryanbogan 2ea5371
Fix javadocs
ryanbogan 89e45de
Make reader methods static
ryanbogan 8cd2ee3
Integrate with merge
ryanbogan d644c9b
Change field name writing to internal field number and change file su…
ryanbogan 5fe570d
Merge branch 'main' into quantization_state_writer_reader
ryanbogan 92bb539
Change integration with native engine writer
ryanbogan 5b03f30
Fix tests
ryanbogan 9b486d8
Integrate with query flow
ryanbogan 0a7d80e
Remove duplicate writeFooter
ryanbogan de89987
Integrate with cache
ryanbogan 59be504
Change implementation and fix tests
ryanbogan 366072f
Add test for reading from QuantizationStateReadConfig
ryanbogan 077a1b6
Add cache manager tests
ryanbogan 3dbbad9
Port changes from feature branch to fix end to end flow
ryanbogan 45b6fbf
Change integration with query flow
ryanbogan 0e0eaf3
Remove unnecessary changes in KNNWeight
ryanbogan 9f8ce0c
Merge branch 'main' into quantization_state_writer_reader
ryanbogan 595ec6f
Merge branch 'main' into quantization_state_writer_reader
ryanbogan 1f67103
Address PR Feedback and fix compile error from rebase
ryanbogan 80e74e3
Abstract common functionality between read methods
ryanbogan 60cd2fa
Avoid repeat calls to quantization cache manager get instance
ryanbogan c0b9e71
Address PR feedback
ryanbogan 776d531
Merge branch 'main' into quantization_state_writer_reader
ryanbogan 8c21304
Add unit tests for KNNWeight
ryanbogan b304e3c
Address PR Feedback
ryanbogan fe19fc0
Merge branch 'main' into quantization_state_writer_reader
ryanbogan d8959d0
Address feedbackK
ryanbogan ba931da
Fix bwc tests
ryanbogan fdfc301
Revert previous change
ryanbogan 0e37d2d
Condense into one loop while reading
ryanbogan eaff8f0
Address PR Feedback
ryanbogan 414b2e4
Address PR Feedback
ryanbogan 2accfb1
Address feedback
ryanbogan a6c87e3
Revert "Address feedback"
ryanbogan ff07704
Address feedback
ryanbogan 34cd60b
Address feedback
ryanbogan b82fb90
Merge branch 'main' into quantization_state_writer_reader
ryanbogan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
src/main/java/org/opensearch/knn/index/codec/KNN990Codec/KNNQuantizationStateReader.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.knn.index.codec.KNN990Codec; | ||
|
|
||
| import com.google.common.annotations.VisibleForTesting; | ||
| import org.apache.lucene.codecs.CodecUtil; | ||
| import org.apache.lucene.index.FieldInfo; | ||
| import org.apache.lucene.index.IndexFileNames; | ||
| import org.apache.lucene.index.SegmentReadState; | ||
| import org.apache.lucene.store.Directory; | ||
| import org.apache.lucene.store.IOContext; | ||
| import org.apache.lucene.store.IndexInput; | ||
| import org.opensearch.knn.common.KNNConstants; | ||
| import org.opensearch.knn.quantization.models.quantizationState.QuantizationState; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Reads quantization states | ||
| */ | ||
| public class KNNQuantizationStateReader { | ||
ryanbogan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
ryanbogan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * Read quantization states and return list of fieldNames and bytes | ||
| * File format: | ||
| * Header | ||
| * QS1 state bytes | ||
| * QS2 state bytes | ||
| * Number of quantization states | ||
| * QS1 field name | ||
| * QS1 state bytes length | ||
| * QS1 position of state bytes | ||
| * QS2 field name | ||
| * QS2 state bytes length | ||
| * QS2 position of state bytes | ||
| * Position of index section (where QS1 field name is located) | ||
| * -1 (marker) | ||
| * Footer | ||
| * | ||
| * @param state the read state to read from | ||
| */ | ||
| public Map<String, byte[]> read(SegmentReadState state) throws IOException { | ||
ryanbogan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| String quantizationStateFileName = IndexFileNames.segmentFileName( | ||
| state.segmentInfo.name, | ||
| state.segmentSuffix, | ||
| KNNConstants.QUANTIZATION_STATE_FILE_SUFFIX | ||
| ); | ||
| Map<String, byte[]> readQuantizationStateInfos = new HashMap<>(); | ||
|
|
||
| IndexInput input = state.directory.openInput(quantizationStateFileName, IOContext.READ); | ||
| CodecUtil.retrieveChecksum(input); | ||
|
|
||
navneet1v marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| int numFields = getNumFields(input); | ||
|
|
||
| List<String> fieldNames = new ArrayList<>(); | ||
| List<Long> positions = new ArrayList<>(); | ||
| List<Integer> lengths = new ArrayList<>(); | ||
|
|
||
| // Read each field's metadata from the index section | ||
| for (int i = 0; i < numFields; i++) { | ||
| fieldNames.add(input.readString()); | ||
| int length = input.readInt(); | ||
| lengths.add(length); | ||
| long position = input.readVLong(); | ||
| positions.add(position); | ||
| } | ||
| // Read each field's bytes | ||
| for (int i = 0; i < numFields; i++) { | ||
| input.seek(positions.get(i)); | ||
| byte[] stateBytes = new byte[lengths.get(i)]; | ||
| input.readBytes(stateBytes, 0, lengths.get(i)); | ||
| readQuantizationStateInfos.put(fieldNames.get(i), stateBytes); | ||
| } | ||
| input.close(); | ||
ryanbogan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return readQuantizationStateInfos; | ||
| } | ||
|
|
||
| /** | ||
| * Reads an individual quantization state for a given field | ||
| * @param directory directory to open input | ||
| * @param segmentName segment name | ||
| * @param segmentSuffix segment suffix | ||
| * @param fieldInfo field information | ||
| * @return quantization state | ||
| */ | ||
| public QuantizationState read(Directory directory, String segmentName, String segmentSuffix, FieldInfo fieldInfo) throws IOException { | ||
ryanbogan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| String quantizationStateFileName = IndexFileNames.segmentFileName( | ||
| segmentName, | ||
| segmentSuffix, | ||
| KNNConstants.QUANTIZATION_STATE_FILE_SUFFIX | ||
| ); | ||
| String fieldName = fieldInfo.getName(); | ||
|
|
||
| IndexInput input = directory.openInput(quantizationStateFileName, IOContext.READ); | ||
| CodecUtil.retrieveChecksum(input); | ||
| int numFields = getNumFields(input); | ||
|
|
||
| long position = -1; | ||
| int length = 0; | ||
|
|
||
| // Read each field's metadata from the index section | ||
| for (int i = 0; i < numFields; i++) { | ||
| String tempFieldName = input.readString(); | ||
| int tempLength = input.readInt(); | ||
| long tempPosition = input.readVLong(); | ||
| if (tempFieldName.equals(fieldName)) { | ||
| position = tempPosition; | ||
| length = tempLength; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (position == -1 || length == 0) { | ||
| throw new IllegalArgumentException(String.format("Field %s not found", fieldName)); | ||
| } | ||
|
|
||
| input.seek(position); | ||
| byte[] stateBytes = new byte[length]; | ||
| input.readBytes(stateBytes, 0, length); | ||
| input.close(); | ||
| // Deserialize the byte array to a quantization state object | ||
| // TODO: Get params from field info and deserialize | ||
| return null; | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| int getNumFields(IndexInput input) throws IOException { | ||
| long footerStart = input.length() - CodecUtil.footerLength(); | ||
| long markerAndIndexPosition = footerStart - Integer.BYTES - Long.BYTES; | ||
| input.seek(markerAndIndexPosition); | ||
| long indexStartPosition = input.readLong(); | ||
| input.readInt(); | ||
ryanbogan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| input.seek(indexStartPosition); | ||
| return input.readInt(); | ||
| } | ||
| } | ||
90 changes: 90 additions & 0 deletions
90
src/main/java/org/opensearch/knn/index/codec/KNN990Codec/KNNQuantizationStateWriter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.knn.index.codec.KNN990Codec; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import org.apache.lucene.codecs.CodecUtil; | ||
| import org.apache.lucene.index.IndexFileNames; | ||
| import org.apache.lucene.index.SegmentWriteState; | ||
| import org.apache.lucene.store.IndexOutput; | ||
| import org.opensearch.knn.common.KNNConstants; | ||
| import org.opensearch.knn.quantization.models.quantizationState.QuantizationState; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Writes quantization states to off heap memory | ||
| */ | ||
| public class KNNQuantizationStateWriter { | ||
ryanbogan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| private final IndexOutput output; | ||
| private List<FieldQuantizationState> fieldQuantizationStates = new ArrayList<>(); | ||
ryanbogan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Constructor | ||
| * @param segmentWriteState segment write state containing segment information | ||
| * @throws IOException exception could be thrown while creating the output | ||
| */ | ||
| public KNNQuantizationStateWriter(SegmentWriteState segmentWriteState) throws IOException { | ||
| String quantizationStateFileName = IndexFileNames.segmentFileName( | ||
| segmentWriteState.segmentInfo.name, | ||
| segmentWriteState.segmentSuffix, | ||
| KNNConstants.QUANTIZATION_STATE_FILE_SUFFIX | ||
| ); | ||
|
|
||
| output = segmentWriteState.directory.createOutput(quantizationStateFileName, segmentWriteState.context); | ||
| } | ||
|
|
||
| /** | ||
| * Writes an index header | ||
| * @param segmentWriteState state containing segment information | ||
| * @throws IOException exception could be thrown while writing header | ||
| */ | ||
| public void writeHeader(SegmentWriteState segmentWriteState) throws IOException { | ||
| CodecUtil.writeIndexHeader(output, "QuantizationCodec", 0, segmentWriteState.segmentInfo.getId(), segmentWriteState.segmentSuffix); | ||
ryanbogan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Writes a quantization state as bytes | ||
| * @param fieldName field name | ||
| * @param quantizationState quantization state | ||
| * @throws IOException could be thrown while writing | ||
| */ | ||
| public void writeState(String fieldName, QuantizationState quantizationState) throws IOException { | ||
| byte[] stateBytes = quantizationState.toByteArray(); | ||
| long position = output.getFilePointer(); | ||
| output.writeBytes(stateBytes, stateBytes.length); | ||
| fieldQuantizationStates.add(new FieldQuantizationState(fieldName, stateBytes, position)); | ||
| } | ||
|
|
||
| /** | ||
| * Writes index footer and other index information for parsing later | ||
| * @throws IOException could be thrown while writing | ||
| */ | ||
| public void writeFooter() throws IOException { | ||
| long indexStartPosition = output.getFilePointer(); | ||
| output.writeInt(fieldQuantizationStates.size()); | ||
| for (FieldQuantizationState fieldQuantizationState : fieldQuantizationStates) { | ||
| output.writeString(fieldQuantizationState.fieldName); | ||
ryanbogan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| output.writeInt(fieldQuantizationState.stateBytes.length); | ||
| output.writeVLong(fieldQuantizationState.position); | ||
| } | ||
| output.writeLong(indexStartPosition); | ||
| output.writeInt(-1); | ||
| CodecUtil.writeFooter(output); | ||
| output.close(); | ||
ryanbogan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| fieldQuantizationStates = new ArrayList<>(); | ||
ryanbogan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @AllArgsConstructor | ||
| private static class FieldQuantizationState { | ||
| final String fieldName; | ||
| final byte[] stateBytes; | ||
| final Long position; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
...test/java/org/opensearch/knn/index/codec/KNN990Codec/KNNQuantizationStateReaderTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.knn.index.codec.KNN990Codec; | ||
|
|
||
| import lombok.SneakyThrows; | ||
| import org.apache.lucene.codecs.Codec; | ||
| import org.apache.lucene.codecs.CodecUtil; | ||
| import org.apache.lucene.index.FieldInfos; | ||
| import org.apache.lucene.index.SegmentInfo; | ||
| import org.apache.lucene.index.SegmentReadState; | ||
| import org.apache.lucene.search.Sort; | ||
| import org.apache.lucene.store.Directory; | ||
| import org.apache.lucene.store.IOContext; | ||
| import org.apache.lucene.store.IndexInput; | ||
| import org.apache.lucene.util.Version; | ||
| import org.mockito.MockedStatic; | ||
| import org.mockito.Mockito; | ||
| import org.opensearch.knn.KNNTestCase; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.anyInt; | ||
| import static org.mockito.ArgumentMatchers.anyLong; | ||
| import static org.mockito.Mockito.mockStatic; | ||
| import static org.mockito.Mockito.times; | ||
|
|
||
| public class KNNQuantizationStateReaderTests extends KNNTestCase { | ||
|
|
||
| @SneakyThrows | ||
| public void testReadFromSegmentReadState() { | ||
| final String segmentName = "test-segment-name"; | ||
| final String segmentSuffix = "test-segment-suffix"; | ||
|
|
||
| final SegmentInfo segmentInfo = new SegmentInfo( | ||
| Mockito.mock(Directory.class), | ||
| Mockito.mock(Version.class), | ||
| Mockito.mock(Version.class), | ||
| segmentName, | ||
| 0, | ||
| false, | ||
| false, | ||
| Mockito.mock(Codec.class), | ||
| Mockito.mock(Map.class), | ||
| new byte[16], | ||
| Mockito.mock(Map.class), | ||
| Mockito.mock(Sort.class) | ||
| ); | ||
|
|
||
| Directory directory = Mockito.mock(Directory.class); | ||
| IndexInput input = Mockito.mock(IndexInput.class); | ||
| Mockito.when(directory.openInput(any(), any())).thenReturn(input); | ||
|
|
||
| final SegmentReadState segmentReadState = new SegmentReadState( | ||
| directory, | ||
| segmentInfo, | ||
| Mockito.mock(FieldInfos.class), | ||
| Mockito.mock(IOContext.class), | ||
| segmentSuffix | ||
| ); | ||
|
|
||
| KNNQuantizationStateReader quantizationStateReader = Mockito.mock(KNNQuantizationStateReader.class); | ||
| Mockito.when(quantizationStateReader.getNumFields(input)).thenReturn(2); | ||
| Mockito.when(quantizationStateReader.read(segmentReadState)).thenCallRealMethod(); | ||
|
|
||
| try (MockedStatic<CodecUtil> mockedStaticCodecUtil = mockStatic(CodecUtil.class)) { | ||
| quantizationStateReader.read(segmentReadState); | ||
|
|
||
| mockedStaticCodecUtil.verify(() -> CodecUtil.retrieveChecksum(input)); | ||
| Mockito.verify(input, times(2)).readInt(); | ||
| Mockito.verify(input, times(2)).readString(); | ||
| Mockito.verify(input, times(2)).readVLong(); | ||
| Mockito.verify(input, times(2)).readBytes(any(byte[].class), anyInt(), anyInt()); | ||
| Mockito.verify(input, times(2)).seek(anyLong()); | ||
| } | ||
| } | ||
|
|
||
| @SneakyThrows | ||
| public void testGetNumFields() { | ||
| IndexInput input = Mockito.mock(IndexInput.class); | ||
| KNNQuantizationStateReader quantizationStateReader = new KNNQuantizationStateReader(); | ||
| quantizationStateReader.getNumFields(input); | ||
|
|
||
| Mockito.verify(input, times(2)).readInt(); | ||
| Mockito.verify(input, times(1)).readLong(); | ||
| Mockito.verify(input, times(2)).seek(anyLong()); | ||
| Mockito.verify(input, times(1)).length(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.