|
12 | 12 | // |
13 | 13 | // Copyright (c) 2025 TensorChord Inc. |
14 | 14 |
|
15 | | -use crate::datatype::memory_halfvec::HalfvecInput; |
16 | | -use crate::datatype::memory_rabitq4::Rabitq4Output; |
17 | | -use crate::datatype::memory_vector::VectorInput; |
| 15 | +use crate::datatype::memory_halfvec::{HalfvecInput, HalfvecOutput}; |
| 16 | +use crate::datatype::memory_rabitq4::{Rabitq4Input, Rabitq4Output}; |
| 17 | +use crate::datatype::memory_vector::{VectorInput, VectorOutput}; |
18 | 18 | use simd::{Floating, f16}; |
19 | 19 | use vector::VectorBorrowed; |
20 | 20 | use vector::rabitq4::Rabitq4Borrowed; |
| 21 | +use vector::vect::VectBorrowed; |
21 | 22 |
|
22 | 23 | #[pgrx::pg_extern(sql = "")] |
23 | 24 | fn _vchord_vector_quantize_to_rabitq4(vector: VectorInput) -> Rabitq4Output { |
@@ -54,3 +55,30 @@ fn _vchord_halfvec_quantize_to_rabitq4(vector: HalfvecInput) -> Rabitq4Output { |
54 | 55 | &elements, |
55 | 56 | )) |
56 | 57 | } |
| 58 | + |
| 59 | +#[pgrx::pg_extern(sql = "")] |
| 60 | +fn _vchord_rabitq4_dequantize_to_vector(vector: Rabitq4Input) -> VectorOutput { |
| 61 | + let vector = vector.as_borrowed(); |
| 62 | + let scale = vector.sum_of_x2().sqrt() / vector.norm_of_lattice(); |
| 63 | + let mut result = Vec::with_capacity(vector.dim() as _); |
| 64 | + for c in vector.unpacked_code() { |
| 65 | + let base = -0.5 * ((1 << 4) - 1) as f32; |
| 66 | + result.push((base + c as f32) * scale); |
| 67 | + } |
| 68 | + rabitq::rotate::rotate_reversed_inplace(&mut result); |
| 69 | + VectorOutput::new(VectBorrowed::new(&result)) |
| 70 | +} |
| 71 | + |
| 72 | +#[pgrx::pg_extern(sql = "")] |
| 73 | +fn _vchord_rabitq4_dequantize_to_halfvec(vector: Rabitq4Input) -> HalfvecOutput { |
| 74 | + let vector = vector.as_borrowed(); |
| 75 | + let scale = vector.sum_of_x2().sqrt() / vector.norm_of_lattice(); |
| 76 | + let mut result = Vec::with_capacity(vector.dim() as _); |
| 77 | + for c in vector.unpacked_code() { |
| 78 | + let base = -0.5 * ((1 << 4) - 1) as f32; |
| 79 | + result.push((base + c as f32) * scale); |
| 80 | + } |
| 81 | + rabitq::rotate::rotate_reversed_inplace(&mut result); |
| 82 | + let result = f16::vector_from_f32(&result); |
| 83 | + HalfvecOutput::new(VectBorrowed::new(&result)) |
| 84 | +} |
0 commit comments