@@ -8,7 +8,7 @@ use rand::{RngCore, SeedableRng};
88use rand_xorshift:: XorShiftRng ;
99use reductive:: pq:: { QuantizeVector , ReconstructVector , TrainPQ , PQ } ;
1010
11- use super :: { Storage , StorageView } ;
11+ use super :: { sealed :: CloneFromMapping , Storage , StorageView } ;
1212use crate :: chunks:: io:: { ChunkIdentifier , ReadChunk , TypeId , WriteChunk } ;
1313use crate :: error:: { Error , Result } ;
1414use crate :: storage:: NdArray ;
@@ -280,6 +280,20 @@ impl Storage for QuantizedArray {
280280 }
281281}
282282
283+ impl CloneFromMapping for QuantizedArray {
284+ type Result = QuantizedArray ;
285+
286+ fn clone_from_mapping ( & self , mapping : & [ usize ] ) -> Self :: Result {
287+ let quantized_embeddings = self . quantized_embeddings . select ( Axis ( 0 ) , & mapping) ;
288+ let norms = self . norms . as_ref ( ) . map ( |n| n. select ( Axis ( 0 ) , & mapping) ) ;
289+ QuantizedArray {
290+ quantizer : self . quantizer . clone ( ) ,
291+ quantized_embeddings,
292+ norms,
293+ }
294+ }
295+ }
296+
283297impl ReadChunk for QuantizedArray {
284298 fn read_chunk < R > ( read : & mut R ) -> Result < Self >
285299 where
@@ -472,7 +486,7 @@ mod mmap {
472486 use super :: { PQRead , QuantizedArray , Storage } ;
473487 use crate :: chunks:: io:: MmapChunk ;
474488 use crate :: chunks:: io:: { ChunkIdentifier , WriteChunk } ;
475- use crate :: chunks:: storage:: NdArray ;
489+ use crate :: chunks:: storage:: { sealed :: CloneFromMapping , NdArray } ;
476490 use crate :: error:: { Error , Result } ;
477491 use byteorder:: { LittleEndian , ReadBytesExt } ;
478492
@@ -570,6 +584,21 @@ mod mmap {
570584 }
571585 }
572586
587+ impl CloneFromMapping for MmapQuantizedArray {
588+ type Result = QuantizedArray ;
589+
590+ fn clone_from_mapping ( & self , mapping : & [ usize ] ) -> Self :: Result {
591+ let quantized_embeddings =
592+ unsafe { self . quantized_embeddings ( ) } . select ( Axis ( 0 ) , & mapping) ;
593+ let norms = self . norms . as_ref ( ) . map ( |n| n. select ( Axis ( 0 ) , & mapping) ) ;
594+ QuantizedArray {
595+ quantizer : self . quantizer . clone ( ) ,
596+ quantized_embeddings,
597+ norms,
598+ }
599+ }
600+ }
601+
573602 impl MmapChunk for MmapQuantizedArray {
574603 fn mmap_chunk ( read : & mut BufReader < File > ) -> Result < Self > {
575604 ChunkIdentifier :: ensure_chunk_type ( read, ChunkIdentifier :: QuantizedArray ) ?;
0 commit comments