Skip to content

Commit 3c00fc1

Browse files
danieldkDaniël de Kok
authored andcommitted
(Mmap)QuantizedArray: rename quantized member to quantized_embeddings
1 parent 7951b2b commit 3c00fc1

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

src/chunks/storage/quantized.rs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::util::padding;
1717
/// Quantized embedding matrix.
1818
pub struct QuantizedArray {
1919
quantizer: PQ<f32>,
20-
quantized: Array2<u8>,
20+
quantized_embeddings: Array2<u8>,
2121
norms: Option<Array1<f32>>,
2222
}
2323

@@ -237,7 +237,9 @@ impl QuantizedArray {
237237

238238
impl Storage for QuantizedArray {
239239
fn embedding(&self, idx: usize) -> CowArray1<f32> {
240-
let mut reconstructed = self.quantizer.reconstruct_vector(self.quantized.row(idx));
240+
let mut reconstructed = self
241+
.quantizer
242+
.reconstruct_vector(self.quantized_embeddings.row(idx));
241243
if let Some(ref norms) = self.norms {
242244
reconstructed *= norms[idx];
243245
}
@@ -246,7 +248,10 @@ impl Storage for QuantizedArray {
246248
}
247249

248250
fn shape(&self) -> (usize, usize) {
249-
(self.quantized.rows(), self.quantizer.reconstructed_len())
251+
(
252+
self.quantized_embeddings.rows(),
253+
self.quantizer.reconstructed_len(),
254+
)
250255
}
251256
}
252257

@@ -280,15 +285,15 @@ impl ReadChunk for QuantizedArray {
280285
let mut quantized_embeddings_vec = vec![0u8; n_embeddings * quantizer.quantized_len()];
281286
read.read_exact(&mut quantized_embeddings_vec)
282287
.map_err(|e| ErrorKind::io_error("Cannot read quantized embeddings", e))?;
283-
let quantized = Array2::from_shape_vec(
288+
let quantized_embeddings = Array2::from_shape_vec(
284289
(n_embeddings, quantizer.quantized_len()),
285290
quantized_embeddings_vec,
286291
)
287292
.map_err(Error::Shape)?;
288293

289294
Ok(QuantizedArray {
290295
quantizer,
291-
quantized,
296+
quantized_embeddings,
292297
norms,
293298
})
294299
}
@@ -306,7 +311,7 @@ impl WriteChunk for QuantizedArray {
306311
Self::write_chunk(
307312
write,
308313
&self.quantizer,
309-
self.quantized.view(),
314+
self.quantized_embeddings.view(),
310315
self.norms.as_ref().map(Array1::view),
311316
)
312317
}
@@ -401,11 +406,11 @@ where
401406
rng,
402407
);
403408

404-
let quantized = quantizer.quantize_batch(embeds.as_view());
409+
let quantized_embeddings = quantizer.quantize_batch(embeds.as_view());
405410

406411
QuantizedArray {
407412
quantizer,
408-
quantized,
413+
quantized_embeddings,
409414
norms,
410415
}
411416
}
@@ -414,7 +419,7 @@ where
414419
/// Memory-mapped quantized embedding matrix.
415420
pub struct MmapQuantizedArray {
416421
quantizer: PQ<f32>,
417-
quantized: Mmap,
422+
quantized_embeddings: Mmap,
418423
norms: Option<Mmap>,
419424
}
420425

@@ -429,12 +434,12 @@ impl MmapQuantizedArray {
429434
ArrayView1::from_shape_ptr((n_embeddings,), norms.as_ptr() as *const f32))
430435
}
431436

432-
unsafe fn quantized(&self) -> ArrayView2<u8> {
437+
unsafe fn quantized_embeddings(&self) -> ArrayView2<u8> {
433438
let n_embeddings = self.shape().0;
434439

435440
ArrayView2::from_shape_ptr(
436441
(n_embeddings, self.quantizer.quantized_len()),
437-
self.quantized.as_ptr(),
442+
self.quantized_embeddings.as_ptr(),
438443
)
439444
}
440445
}
@@ -498,7 +503,7 @@ impl MmapQuantizedArray {
498503

499504
impl Storage for MmapQuantizedArray {
500505
fn embedding(&self, idx: usize) -> CowArray1<f32> {
501-
let quantized = unsafe { self.quantized() };
506+
let quantized = unsafe { self.quantized_embeddings() };
502507

503508
let mut reconstructed = self.quantizer.reconstruct_vector(quantized.row(idx));
504509
if let Some(norms) = unsafe { self.norms() } {
@@ -510,7 +515,7 @@ impl Storage for MmapQuantizedArray {
510515

511516
fn shape(&self) -> (usize, usize) {
512517
(
513-
self.quantized.len() / self.quantizer.quantized_len(),
518+
self.quantized_embeddings.len() / self.quantizer.quantized_len(),
514519
self.quantizer.reconstructed_len(),
515520
)
516521
}
@@ -537,12 +542,12 @@ impl MmapChunk for MmapQuantizedArray {
537542
None
538543
};
539544

540-
let quantized =
545+
let quantized_embeddings =
541546
Self::mmap_quantized_embeddings(read, n_embeddings, quantizer.quantized_len())?;
542547

543548
Ok(MmapQuantizedArray {
544549
quantizer,
545-
quantized,
550+
quantized_embeddings,
546551
norms,
547552
})
548553
}
@@ -560,7 +565,7 @@ impl WriteChunk for MmapQuantizedArray {
560565
QuantizedArray::write_chunk(
561566
write,
562567
&self.quantizer,
563-
unsafe { self.quantized() },
568+
unsafe { self.quantized_embeddings() },
564569
unsafe { self.norms() },
565570
)
566571
}
@@ -649,7 +654,7 @@ mod tests {
649654
cursor.seek(SeekFrom::Start(0)).unwrap();
650655
let arr = QuantizedArray::read_chunk(&mut cursor).unwrap();
651656
assert_eq!(arr.quantizer, check_arr.quantizer);
652-
assert_eq!(arr.quantized, check_arr.quantized);
657+
assert_eq!(arr.quantized_embeddings, check_arr.quantized_embeddings);
653658
}
654659

655660
#[test]

0 commit comments

Comments
 (0)