@@ -18,7 +18,7 @@ use toml::{self, Value};
1818///
1919/// finalfusion is a format for word embeddings that supports words,
2020/// subwords, memory-mapped matrices, and quantized matrices.
21- #[ pymodinit ]
21+ #[ pymodule ]
2222fn finalfusion ( _py : Python , m : & PyModule ) -> PyResult < ( ) > {
2323 m. add_class :: < PyEmbeddings > ( ) ?;
2424 m. add_class :: < PyWordSimilarity > ( ) ?;
@@ -31,13 +31,11 @@ fn finalfusion(_py: Python, m: &PyModule) -> PyResult<()> {
3131/// vectors) and 1 (identical vectors).
3232#[ pyclass( name=WordSimilarity ) ]
3333struct PyWordSimilarity {
34- #[ prop ( get) ]
34+ #[ pyo3 ( get) ]
3535 word : String ,
3636
37- #[ prop ( get) ]
37+ #[ pyo3 ( get) ]
3838 similarity : f32 ,
39-
40- token : PyToken ,
4139}
4240
4341#[ pyproto]
@@ -69,7 +67,6 @@ struct PyEmbeddings {
6967 // to its method scope.
7068 // 3. None of the methods returns borrowed embeddings.
7169 embeddings : Rc < RefCell < EmbeddingsWrap > > ,
72- token : PyToken ,
7370}
7471
7572#[ pymethods]
@@ -93,7 +90,9 @@ impl PyEmbeddings {
9390 . map_err ( |err| exceptions:: IOError :: py_err ( err. to_string ( ) ) ) ?,
9491 } ;
9592
96- obj. init ( |token| PyEmbeddings { embeddings, token } )
93+ obj. init ( PyEmbeddings { embeddings } ) ;
94+
95+ Ok ( ( ) )
9796 }
9897
9998 /// Perform an anology query.
@@ -128,11 +127,13 @@ impl PyEmbeddings {
128127 let mut r = Vec :: with_capacity ( results. len ( ) ) ;
129128 for ws in results {
130129 r. push (
131- Py :: new ( py, |token| PyWordSimilarity {
132- word : ws. word . to_owned ( ) ,
133- similarity : ws. similarity . into_inner ( ) ,
134- token,
135- } ) ?
130+ Py :: new (
131+ py,
132+ PyWordSimilarity {
133+ word : ws. word . to_owned ( ) ,
134+ similarity : ws. similarity . into_inner ( ) ,
135+ } ,
136+ ) ?
136137 . into_object ( py) ,
137138 )
138139 }
@@ -226,11 +227,13 @@ impl PyEmbeddings {
226227 let mut r = Vec :: with_capacity ( results. len ( ) ) ;
227228 for ws in results {
228229 r. push (
229- Py :: new ( py, |token| PyWordSimilarity {
230- word : ws. word . to_owned ( ) ,
231- similarity : ws. similarity . into_inner ( ) ,
232- token,
233- } ) ?
230+ Py :: new (
231+ py,
232+ PyWordSimilarity {
233+ word : ws. word . to_owned ( ) ,
234+ similarity : ws. similarity . into_inner ( ) ,
235+ } ,
236+ ) ?
234237 . into_object ( py) ,
235238 )
236239 }
@@ -259,14 +262,16 @@ impl PyEmbeddings {
259262
260263#[ pyproto]
261264impl PyIterProtocol for PyEmbeddings {
262- fn __iter__ ( & mut self ) -> PyResult < PyObject > {
265+ fn __iter__ ( slf : PyRefMut < Self > ) -> PyResult < PyObject > {
263266 let gil = Python :: acquire_gil ( ) ;
264267 let py = gil. python ( ) ;
265- let iter = Py :: new ( py, |token| PyEmbeddingIterator {
266- embeddings : self . embeddings . clone ( ) ,
267- idx : 0 ,
268- token,
269- } ) ?
268+ let iter = Py :: new (
269+ py,
270+ PyEmbeddingIterator {
271+ embeddings : slf. embeddings . clone ( ) ,
272+ idx : 0 ,
273+ } ,
274+ ) ?
270275 . into_object ( py) ;
271276
272277 Ok ( iter)
@@ -293,33 +298,34 @@ where
293298struct PyEmbeddingIterator {
294299 embeddings : Rc < RefCell < EmbeddingsWrap > > ,
295300 idx : usize ,
296- token : PyToken ,
297301}
298302
299303#[ pyproto]
300304impl PyIterProtocol for PyEmbeddingIterator {
301- fn __iter__ ( & mut self ) -> PyResult < PyObject > {
302- Ok ( self . into ( ) )
305+ fn __iter__ ( slf : PyRefMut < Self > ) -> PyResult < Py < PyEmbeddingIterator > > {
306+ Ok ( slf . into ( ) )
303307 }
304308
305- fn __next__ ( & mut self ) -> PyResult < Option < ( String , Vec < f32 > ) > > {
306- let embeddings = self . embeddings . borrow ( ) ;
309+ fn __next__ ( mut slf : PyRefMut < Self > ) -> PyResult < Option < ( String , Vec < f32 > ) > > {
310+ let slf = & mut * slf;
311+
312+ let embeddings = slf. embeddings . borrow ( ) ;
307313
308314 use EmbeddingsWrap :: * ;
309315 let vocab = match & * embeddings {
310316 View ( e) => e. vocab ( ) ,
311317 NonView ( e) => e. vocab ( ) ,
312318 } ;
313319
314- if self . idx < vocab. len ( ) {
315- let word = vocab. words ( ) [ self . idx ] . to_string ( ) ;
320+ if slf . idx < vocab. len ( ) {
321+ let word = vocab. words ( ) [ slf . idx ] . to_string ( ) ;
316322
317323 let embed = match & * embeddings {
318- View ( e) => e. storage ( ) . embedding ( self . idx ) ,
319- NonView ( e) => e. storage ( ) . embedding ( self . idx ) ,
324+ View ( e) => e. storage ( ) . embedding ( slf . idx ) ,
325+ NonView ( e) => e. storage ( ) . embedding ( slf . idx ) ,
320326 } ;
321327
322- self . idx += 1 ;
328+ slf . idx += 1 ;
323329
324330 Ok ( Some ( ( word, embed. as_view ( ) . to_vec ( ) ) ) )
325331 } else {
0 commit comments