@@ -12,14 +12,14 @@ use finalfusion::io as ffio;
1212use finalfusion:: prelude:: * ;
1313use finalfusion:: similarity:: * ;
1414use itertools:: Itertools ;
15- use ndarray:: Array2 ;
16- use numpy:: { IntoPyArray , NpyDataType , PyArray1 , PyArray2 , ToPyArray } ;
15+ use numpy:: { IntoPyArray , NpyDataType , PyArray1 } ;
1716use pyo3:: class:: iter:: PyIterProtocol ;
1817use pyo3:: prelude:: * ;
1918use pyo3:: types:: { PyAny , PyTuple } ;
2019use pyo3:: { exceptions, PyMappingProtocol } ;
2120use toml:: { self , Value } ;
2221
22+ use crate :: storage:: PyStorage ;
2323use crate :: { EmbeddingsWrap , PyEmbeddingIterator , PyVocab , PyWordSimilarity } ;
2424
2525/// finalfusion embeddings.
@@ -34,24 +34,6 @@ pub struct PyEmbeddings {
3434 embeddings : Rc < RefCell < EmbeddingsWrap > > ,
3535}
3636
37- impl PyEmbeddings {
38- /// Copy storage to an array.
39- ///
40- /// This should only be used for storage types that do not provide
41- /// an ndarray view that can be copied trivially, such as quantized
42- /// storage.
43- fn copy_storage_to_array ( storage : & dyn Storage ) -> Array2 < f32 > {
44- let ( rows, dims) = storage. shape ( ) ;
45-
46- let mut array = Array2 :: < f32 > :: zeros ( ( rows, dims) ) ;
47- for idx in 0 ..rows {
48- array. row_mut ( idx) . assign ( & storage. embedding ( idx) . as_view ( ) ) ;
49- }
50-
51- array
52- }
53- }
54-
5537#[ pymethods]
5638impl PyEmbeddings {
5739 /// Load embeddings from the given `path`.
@@ -156,6 +138,11 @@ impl PyEmbeddings {
156138 Ok ( PyVocab :: new ( self . embeddings . clone ( ) ) )
157139 }
158140
141+ /// Get the model's storage.
142+ fn storage ( & self ) -> PyStorage {
143+ PyStorage :: new ( self . embeddings . clone ( ) )
144+ }
145+
159146 /// Perform an anology query.
160147 ///
161148 /// This returns words for the analogy query *w1* is to *w2*
@@ -222,31 +209,6 @@ impl PyEmbeddings {
222209 } )
223210 }
224211
225- /// Copy the entire embeddings matrix.
226- fn matrix_copy ( & self ) -> Py < PyArray2 < f32 > > {
227- let embeddings = self . embeddings . borrow ( ) ;
228-
229- use EmbeddingsWrap :: * ;
230- let gil = pyo3:: Python :: acquire_gil ( ) ;
231- let matrix_view = match & * embeddings {
232- View ( e) => e. storage ( ) . view ( ) ,
233- NonView ( e) => match e. storage ( ) {
234- StorageWrap :: MmapArray ( mmap) => mmap. view ( ) ,
235- StorageWrap :: NdArray ( array) => array. view ( ) ,
236- StorageWrap :: QuantizedArray ( quantized) => {
237- let array = Self :: copy_storage_to_array ( quantized. as_ref ( ) ) ;
238- return array. to_pyarray ( gil. python ( ) ) . to_owned ( ) ;
239- }
240- StorageWrap :: MmapQuantizedArray ( quantized) => {
241- let array = Self :: copy_storage_to_array ( quantized) ;
242- return array. to_pyarray ( gil. python ( ) ) . to_owned ( ) ;
243- }
244- } ,
245- } ;
246-
247- matrix_view. to_pyarray ( gil. python ( ) ) . to_owned ( )
248- }
249-
250212 /// Embeddings metadata.
251213 #[ getter]
252214 fn metadata ( & self ) -> PyResult < Option < String > > {
0 commit comments