EmbarkStudios / cervo

Utility wrappers for tract
Apache License 2.0
40 stars 1 forks source link

tract's Nnef cross-thread sharing #22

Closed kali closed 2 years ago

kali commented 2 years ago

Congrats on getting this done!

Was just skimming over bit of the code, trying to find places where I can actually makes things easier to integrate, and I spotted this one... What prevents you to just share the Nnef instance process-wide ?

https://github.com/EmbarkStudios/cervo/blob/d6c2918e41ca07a46ccd3af8329fbe7e9360ff4c/crates/cervo-nnef/src/lib.rs#L37

tgolsson commented 2 years ago

Thank you @kali! The NNEF instance isn't Send, and this was the least effort solution to work around it.

$ cargo build
   Compiling cervo-nnef v0.1.1-alpha.0 (/home/ts/Repositories/inference-engine/crates/cervo-nnef)
error[E0277]: `(dyn for<'r, 's, 't0> Fn(&'r mut ModelBuilder<'s>, &'t0 [std::string::String]) -> Result<ControlFlow<()>, anyhow::Error> + 'static)` cannot be sent between threads safely
  --> crates/cervo-nnef/src/lib.rs:46:12
   |
46 |     sendit(tract_nnef::nnef().with_tract_core())
   |     ------ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `(dyn for<'r, 's, 't0> Fn(&'r mut ModelBuilder<'s>, &'t0 [std::string::String]) -> Result<ControlFlow<()>, anyhow::Error> + 'static)` cannot be sent between threads safely
   |     |
   |     required by a bound introduced by this call
   |
   = help: the trait `Send` is not implemented for `(dyn for<'r, 's, 't0> Fn(&'r mut ModelBuilder<'s>, &'t0 [std::string::String]) -> Result<ControlFlow<()>, anyhow::Error> + 'static)`
   = note: required because of the requirements on the impl of `Send` for `std::ptr::Unique<(dyn for<'r, 's, 't0> Fn(&'r mut ModelBuilder<'s>, &'t0 [std::string::String]) -> Result<ControlFlow<()>, anyhow::Error> + 'static)>`
   = note: required because it appears within the type `Box<(dyn for<'r, 's, 't0> Fn(&'r mut ModelBuilder<'s>, &'t0 [std::string::String]) -> Result<ControlFlow<()>, anyhow::Error> + 'static)>`
   = note: required because of the requirements on the impl of `Send` for `std::ptr::Unique<Box<(dyn for<'r, 's, 't0> Fn(&'r mut ModelBuilder<'s>, &'t0 [std::string::String]) -> Result<ControlFlow<()>, anyhow::Error> + 'static)>>`
   = note: required because it appears within the type `__alloc::raw_vec::RawVec<Box<(dyn for<'r, 's, 't0> Fn(&'r mut ModelBuilder<'s>, &'t0 [std::string::String]) -> Result<ControlFlow<()>, anyhow::Error> + 'static)>>`
   = note: required because it appears within the type `Vec<Box<(dyn for<'r, 's, 't0> Fn(&'r mut ModelBuilder<'s>, &'t0 [std::string::String]) -> Result<ControlFlow<()>, anyhow::Error> + 'static)>>`
   = note: required because it appears within the type `Registry`
   = note: required because of the requirements on the impl of `Send` for `std::ptr::Unique<Registry>`
   = note: required because it appears within the type `__alloc::raw_vec::RawVec<Registry>`
   = note: required because it appears within the type `Vec<Registry>`
   = note: required because it appears within the type `Nnef`
note: required by a bound in `sendit`
  --> crates/cervo-nnef/src/lib.rs:44:18
   |
44 | pub fn sendit<T: Send>(t: T) {}
   |                  ^^^^ required by this bound in `sendit`
kali commented 2 years ago

Interesting. It should be. I'll have a look :)

tgolsson commented 2 years ago

Made this a lazy_static in latest upgrade and thus global! Thanks @kali.