forked from opensearch-project/k-NN
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
1,165 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
45 changes: 45 additions & 0 deletions
45
src/main/java/org/opensearch/knn/quantization/QuantizationManager.java
This file contains 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,45 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.knn.quantization; | ||
|
||
import org.opensearch.knn.quantization.factory.QuantizerFactory; | ||
import org.opensearch.knn.quantization.models.quantizationParams.QuantizationParams; | ||
import org.opensearch.knn.quantization.models.quantizationState.QuantizationState; | ||
import org.opensearch.knn.quantization.models.requests.SamplingTrainingRequest; | ||
import org.opensearch.knn.quantization.models.requests.TrainingRequest; | ||
import org.opensearch.knn.quantization.quantizer.Quantizer; | ||
import org.opensearch.knn.quantization.sampler.Sampler; | ||
import org.opensearch.knn.quantization.sampler.SamplingFactory; | ||
|
||
public class QuantizationManager { | ||
private static QuantizationManager instance; | ||
|
||
private QuantizationManager() {} | ||
|
||
public static QuantizationManager getInstance() { | ||
if (instance == null) { | ||
instance = new QuantizationManager(); | ||
} | ||
return instance; | ||
} | ||
public <T, R> QuantizationState train(TrainingRequest<T> trainingRequest) { | ||
Quantizer<T, R> quantizer = (Quantizer<T, R>) getQuantizer(trainingRequest.getParams()); | ||
int sampleSize = quantizer.getSamplingSize(); | ||
Sampler sampler = SamplingFactory.getSampler(SamplingFactory.SamplerType.RESERVOIR); | ||
TrainingRequest<T> sampledRequest = new SamplingTrainingRequest<>(trainingRequest, sampler, sampleSize); | ||
return quantizer.train(sampledRequest); | ||
} | ||
public Quantizer<?, ?> getQuantizer(QuantizationParams params) { | ||
return QuantizerFactory.getQuantizer(params); | ||
} | ||
} | ||
|
17 changes: 17 additions & 0 deletions
17
src/main/java/org/opensearch/knn/quantization/enums/QuantizationType.java
This file contains 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,17 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.knn.quantization.enums; | ||
|
||
public enum QuantizationType { | ||
SPACE_QUANTIZATION, | ||
VALUE_QUANTIZATION, | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/org/opensearch/knn/quantization/enums/SQTypes.java
This file contains 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,21 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.knn.quantization.enums; | ||
|
||
public enum SQTypes { | ||
FP16, | ||
INT8, | ||
INT6, | ||
INT4, | ||
ONE_BIT, | ||
TWO_BIT | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/org/opensearch/knn/quantization/enums/ValueQuantizationType.java
This file contains 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,17 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.knn.quantization.enums; | ||
|
||
public enum ValueQuantizationType { | ||
SQ | ||
} | ||
|
34 changes: 34 additions & 0 deletions
34
src/main/java/org/opensearch/knn/quantization/factory/QuantizerFactory.java
This file contains 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,34 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.knn.quantization.factory; | ||
|
||
import org.opensearch.knn.quantization.enums.SQTypes; | ||
import org.opensearch.knn.quantization.models.quantizationParams.QuantizationParams; | ||
import org.opensearch.knn.quantization.models.quantizationParams.SQParams; | ||
import org.opensearch.knn.quantization.quantizer.OneBitScalarQuantizer; | ||
import org.opensearch.knn.quantization.quantizer.Quantizer; | ||
|
||
public class QuantizerFactory { | ||
static { | ||
// Register all quantizers here | ||
QuantizerRegistry.register(SQParams.class, SQTypes.ONE_BIT.name(), OneBitScalarQuantizer::new); | ||
} | ||
|
||
public static Quantizer<?, ?> getQuantizer(QuantizationParams params) { | ||
if (params instanceof SQParams) { | ||
SQParams sqParams = (SQParams) params; | ||
return QuantizerRegistry.getQuantizer(params, sqParams.getSqType().name()); | ||
} | ||
// Add more cases for other quantization parameters here | ||
throw new IllegalArgumentException("Unsupported quantization parameters: " + params.getClass().getName()); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/org/opensearch/knn/quantization/factory/QuantizerRegistry.java
This file contains 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,39 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.knn.quantization.factory; | ||
|
||
import org.opensearch.knn.quantization.models.quantizationParams.QuantizationParams; | ||
import org.opensearch.knn.quantization.quantizer.Quantizer; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Supplier; | ||
|
||
public class QuantizerRegistry { | ||
private static final Map<Class<? extends QuantizationParams>, Map<String, Supplier<? extends Quantizer<?, ?>>>> registry = new HashMap<>(); | ||
|
||
public static <T extends QuantizationParams> void register(Class<T> paramClass, String typeIdentifier, Supplier<? extends Quantizer<?, ?>> quantizerSupplier) { | ||
registry.computeIfAbsent(paramClass, k -> new HashMap<>()).put(typeIdentifier, quantizerSupplier); | ||
} | ||
|
||
public static Quantizer<?, ?> getQuantizer(QuantizationParams params, String typeIdentifier) { | ||
Map<String, Supplier<? extends Quantizer<?, ?>>> typeMap = registry.get(params.getClass()); | ||
if (typeMap == null) { | ||
throw new IllegalArgumentException("No quantizer registered for parameters: " + params.getClass().getName()); | ||
} | ||
Supplier<? extends Quantizer<?, ?>> supplier = typeMap.get(typeIdentifier); | ||
if (supplier == null) { | ||
throw new IllegalArgumentException("No quantizer registered for type identifier: " + typeIdentifier); | ||
} | ||
return supplier.get(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...opensearch/knn/quantization/models/quantizationOutput/OneBitScalarQuantizationOutput.java
This file contains 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,19 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.knn.quantization.models.quantizationOutput; | ||
|
||
public class OneBitScalarQuantizationOutput extends QuantizationOutput<byte[]> { | ||
|
||
public OneBitScalarQuantizationOutput(byte[] quantizedVector) { | ||
super(quantizedVector); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...in/java/org/opensearch/knn/quantization/models/quantizationOutput/QuantizationOutput.java
This file contains 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,24 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.knn.quantization.models.quantizationOutput; | ||
|
||
public abstract class QuantizationOutput<T> { | ||
private final T quantizedVector; | ||
|
||
public QuantizationOutput(T quantizedVector) { | ||
this.quantizedVector = quantizedVector; | ||
} | ||
|
||
public T getQuantizedVector() { | ||
return quantizedVector; | ||
} | ||
} |
Oops, something went wrong.