|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.knn.index.codec.KNN990Codec; |
| 7 | + |
| 8 | +import com.google.common.annotations.VisibleForTesting; |
| 9 | +import lombok.extern.log4j.Log4j2; |
| 10 | +import org.apache.lucene.codecs.CodecUtil; |
| 11 | +import org.apache.lucene.index.IndexFileNames; |
| 12 | +import org.apache.lucene.index.SegmentReadState; |
| 13 | +import org.apache.lucene.store.IOContext; |
| 14 | +import org.apache.lucene.store.IndexInput; |
| 15 | +import org.opensearch.knn.common.KNNConstants; |
| 16 | +import org.opensearch.knn.quantization.enums.ScalarQuantizationType; |
| 17 | +import org.opensearch.knn.quantization.models.quantizationParams.ScalarQuantizationParams; |
| 18 | +import org.opensearch.knn.quantization.models.quantizationState.MultiBitScalarQuantizationState; |
| 19 | +import org.opensearch.knn.quantization.models.quantizationState.OneBitScalarQuantizationState; |
| 20 | +import org.opensearch.knn.quantization.models.quantizationState.QuantizationState; |
| 21 | +import org.opensearch.knn.quantization.models.quantizationState.QuantizationStateReadConfig; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.Map; |
| 27 | + |
| 28 | +/** |
| 29 | + * Reads quantization states |
| 30 | + */ |
| 31 | +@Log4j2 |
| 32 | +public final class KNN990QuantizationStateReader { |
| 33 | + |
| 34 | + /** |
| 35 | + * Read quantization states and return list of fieldNames and bytes |
| 36 | + * File format: |
| 37 | + * Header |
| 38 | + * QS1 state bytes |
| 39 | + * QS2 state bytes |
| 40 | + * Number of quantization states |
| 41 | + * QS1 field number |
| 42 | + * QS1 state bytes length |
| 43 | + * QS1 position of state bytes |
| 44 | + * QS2 field number |
| 45 | + * QS2 state bytes length |
| 46 | + * QS2 position of state bytes |
| 47 | + * Position of index section (where QS1 field name is located) |
| 48 | + * -1 (marker) |
| 49 | + * Footer |
| 50 | + * |
| 51 | + * @param state the read state to read from |
| 52 | + */ |
| 53 | + public static Map<String, byte[]> read(SegmentReadState state) throws IOException { |
| 54 | + String quantizationStateFileName = getQuantizationStateFileName(state); |
| 55 | + Map<String, byte[]> readQuantizationStateInfos = null; |
| 56 | + |
| 57 | + try (IndexInput input = state.directory.openInput(quantizationStateFileName, IOContext.READ)) { |
| 58 | + CodecUtil.retrieveChecksum(input); |
| 59 | + |
| 60 | + int numFields = getNumFields(input); |
| 61 | + |
| 62 | + readQuantizationStateInfos = new HashMap<>(); |
| 63 | + |
| 64 | + // Read each field's metadata from the index section and then read bytes |
| 65 | + for (int i = 0; i < numFields; i++) { |
| 66 | + int fieldNumber = input.readInt(); |
| 67 | + int length = input.readInt(); |
| 68 | + long position = input.readVLong(); |
| 69 | + byte[] stateBytes = readStateBytes(input, position, length); |
| 70 | + String fieldName = state.fieldInfos.fieldInfo(fieldNumber).getName(); |
| 71 | + readQuantizationStateInfos.put(fieldName, stateBytes); |
| 72 | + } |
| 73 | + } catch (Exception e) { |
| 74 | + log.warn(String.format("Unable to read the quantization state file for segment %s", state.segmentInfo.name), e); |
| 75 | + return Collections.emptyMap(); |
| 76 | + } |
| 77 | + return readQuantizationStateInfos; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Reads an individual quantization state for a given field |
| 82 | + * @param readConfig a config class that contains necessary information for reading the state |
| 83 | + * @return quantization state |
| 84 | + */ |
| 85 | + public static QuantizationState read(QuantizationStateReadConfig readConfig) throws IOException { |
| 86 | + SegmentReadState segmentReadState = readConfig.getSegmentReadState(); |
| 87 | + String field = readConfig.getField(); |
| 88 | + String quantizationStateFileName = getQuantizationStateFileName(segmentReadState); |
| 89 | + int fieldNumber = segmentReadState.fieldInfos.fieldInfo(field).getFieldNumber(); |
| 90 | + |
| 91 | + try (IndexInput input = segmentReadState.directory.openInput(quantizationStateFileName, IOContext.READ)) { |
| 92 | + CodecUtil.retrieveChecksum(input); |
| 93 | + int numFields = getNumFields(input); |
| 94 | + |
| 95 | + long position = -1; |
| 96 | + int length = 0; |
| 97 | + |
| 98 | + // Read each field's metadata from the index section, break when correct field is found |
| 99 | + for (int i = 0; i < numFields; i++) { |
| 100 | + int tempFieldNumber = input.readInt(); |
| 101 | + int tempLength = input.readInt(); |
| 102 | + long tempPosition = input.readVLong(); |
| 103 | + if (tempFieldNumber == fieldNumber) { |
| 104 | + position = tempPosition; |
| 105 | + length = tempLength; |
| 106 | + break; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + if (position == -1 || length == 0) { |
| 111 | + throw new IllegalArgumentException(String.format("Field %s not found", field)); |
| 112 | + } |
| 113 | + |
| 114 | + byte[] stateBytes = readStateBytes(input, position, length); |
| 115 | + |
| 116 | + // Deserialize the byte array to a quantization state object |
| 117 | + ScalarQuantizationType scalarQuantizationType = ((ScalarQuantizationParams) readConfig.getQuantizationParams()).getSqType(); |
| 118 | + switch (scalarQuantizationType) { |
| 119 | + case ONE_BIT: |
| 120 | + return OneBitScalarQuantizationState.fromByteArray(stateBytes); |
| 121 | + case TWO_BIT: |
| 122 | + case FOUR_BIT: |
| 123 | + return MultiBitScalarQuantizationState.fromByteArray(stateBytes); |
| 124 | + default: |
| 125 | + throw new IllegalArgumentException(String.format("Unexpected scalar quantization type: %s", scalarQuantizationType)); |
| 126 | + } |
| 127 | + } catch (Exception e) { |
| 128 | + log.warn(String.format("Unable to read the quantization state file for segment %s", segmentReadState.segmentInfo.name), e); |
| 129 | + return null; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + @VisibleForTesting |
| 134 | + static int getNumFields(IndexInput input) throws IOException { |
| 135 | + long footerStart = input.length() - CodecUtil.footerLength(); |
| 136 | + long markerAndIndexPosition = footerStart - Integer.BYTES - Long.BYTES; |
| 137 | + input.seek(markerAndIndexPosition); |
| 138 | + long indexStartPosition = input.readLong(); |
| 139 | + input.seek(indexStartPosition); |
| 140 | + return input.readInt(); |
| 141 | + } |
| 142 | + |
| 143 | + @VisibleForTesting |
| 144 | + static byte[] readStateBytes(IndexInput input, long position, int length) throws IOException { |
| 145 | + input.seek(position); |
| 146 | + byte[] stateBytes = new byte[length]; |
| 147 | + input.readBytes(stateBytes, 0, length); |
| 148 | + return stateBytes; |
| 149 | + } |
| 150 | + |
| 151 | + @VisibleForTesting |
| 152 | + static String getQuantizationStateFileName(SegmentReadState state) { |
| 153 | + return IndexFileNames.segmentFileName(state.segmentInfo.name, state.segmentSuffix, KNNConstants.QUANTIZATION_STATE_FILE_SUFFIX); |
| 154 | + } |
| 155 | +} |
0 commit comments