-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a NoQuantizer for when we dont want to quantize (#153)
- Loading branch information
Showing
6 changed files
with
181 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,5 @@ pub mod rabitq; | |
pub mod rabitq_builder; | ||
pub mod quantization; | ||
pub mod typing; | ||
pub mod no_op; | ||
pub mod reader; |
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,94 @@ | ||
use anyhow::Result; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::quantization::Quantizer; | ||
|
||
pub struct NoQuantizer { | ||
dimension: usize, | ||
} | ||
|
||
impl NoQuantizer { | ||
pub fn new(dimension: usize) -> Self { | ||
Self { dimension } | ||
} | ||
} | ||
|
||
impl Quantizer for NoQuantizer { | ||
fn quantize(&self, _value: &[f32]) -> Vec<u8> { | ||
// Throw an error if called | ||
todo!() | ||
} | ||
|
||
fn quantized_dimension(&self) -> usize { | ||
self.dimension | ||
} | ||
|
||
fn original_vector(&self, _quantized_vector: &[u8]) -> Vec<f32> { | ||
// Throw an error if called | ||
todo!() | ||
} | ||
|
||
fn distance( | ||
&self, | ||
_query: &[u8], | ||
_point: &[u8], | ||
_implem: utils::distance::l2::L2DistanceCalculatorImpl, | ||
) -> f32 { | ||
// Throw an error if called | ||
todo!() | ||
} | ||
|
||
fn read(dir: String) -> Result<Self> | ||
where | ||
Self: Sized, | ||
{ | ||
let reader = NoQuantizerReader::new(dir); | ||
reader.read() | ||
} | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct NoQuantizerConfig { | ||
dimension: usize, | ||
} | ||
|
||
pub struct NoQuantizerReader { | ||
base_directory: String, | ||
} | ||
|
||
impl NoQuantizerReader { | ||
pub fn new(base_directory: String) -> Self { | ||
Self { base_directory } | ||
} | ||
|
||
pub fn read(&self) -> Result<NoQuantizer> { | ||
// Deserialieze the config | ||
let config = serde_yaml::from_str::<NoQuantizerConfig>(&std::fs::read_to_string( | ||
&format!("{}/no_op_quantizer_config.yaml", self.base_directory), | ||
)?)?; | ||
Ok(NoQuantizer::new(config.dimension)) | ||
} | ||
} | ||
|
||
// Writer | ||
|
||
pub struct NoQuantizerWriter { | ||
base_directory: String, | ||
} | ||
|
||
impl NoQuantizerWriter { | ||
pub fn new(base_directory: String) -> Self { | ||
Self { base_directory } | ||
} | ||
|
||
pub fn write(&self, quantizer: &NoQuantizer) -> Result<()> { | ||
let config = NoQuantizerConfig { | ||
dimension: quantizer.dimension, | ||
}; | ||
std::fs::write( | ||
&format!("{}/no_op_quantizer_config.yaml", self.base_directory), | ||
serde_yaml::to_string(&config)?, | ||
)?; | ||
Ok(()) | ||
} | ||
} |
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,42 @@ | ||
use std::path::Path; | ||
|
||
use anyhow::Result; | ||
|
||
use crate::no_op::{NoQuantizer, NoQuantizerReader}; | ||
use crate::pq::{ProductQuantizer, ProductQuantizerReader}; | ||
|
||
pub struct QuantizationReader { | ||
base_directory: String, | ||
} | ||
|
||
pub enum QuantizationType { | ||
ProductQuantizer, | ||
NoQuantization, | ||
} | ||
|
||
impl QuantizationReader { | ||
pub fn new(base_directory: String) -> Self { | ||
Self { base_directory } | ||
} | ||
|
||
pub fn get_quantization_type(&self) -> QuantizationType { | ||
// If exists the file, then it is a product quantizer | ||
if Path::new(&self.base_directory) | ||
.join("product_quantizer_config.yaml") | ||
.exists() | ||
{ | ||
return QuantizationType::ProductQuantizer; | ||
} | ||
QuantizationType::NoQuantization | ||
} | ||
|
||
pub fn read_product_quantizer(&self) -> Result<ProductQuantizer> { | ||
let reader = ProductQuantizerReader::new(self.base_directory.clone()); | ||
reader.read() | ||
} | ||
|
||
pub fn read_no_quantization(&self) -> Result<NoQuantizer> { | ||
let reader = NoQuantizerReader::new(self.base_directory.clone()); | ||
reader.read() | ||
} | ||
} |