jatinchowdhury18 / RTNeural

Real-time neural network inferencing
BSD 3-Clause "New" or "Revised" License
551 stars 57 forks source link

Bindings for other languages? #30

Open mishushakov opened 3 years ago

mishushakov commented 3 years ago

hey, yesterday, i had an idea of using vst-rs, which is a Rust implementation of VST

in order to generate Rust bindings, i tried using bindgen, but it failed, because the code in RTNeural has some template-related code

i'm wondering whether you'd be interested in making RTNeural more compatible with other languages in the future?

jatinchowdhury18 commented 3 years ago

Yes, I would definitely like to support Rust, though from doing a bit of research, it seems like it may be a somewhat daunting task!

For the dynamically implemented layers in RTNeural, the only template parameter is the data type (i.e. float or double), so we could probably work out a way to feed code into bindgen that would work. Unfortunately, for the more optimized statically implemented layers, the template parameters also cover things like the layer sizes, so until bindgen can handle templates, I don't think it would be possible to automatically generate bindings for those classes. Further, the library backends for RTNeural (Eigen and xsimd) don't have Rust bindings yet either (at least to my knowledge), likely due to the same limitations of templates.

Would it be possible to share the output of bindgen that was failing for you? It may be possible use macros to remove some of the problematic code when generating the Rust bindings.

In general though, I think this issue may require some minds with more Rust experience than I have under my belt :).

Thanks, Jatin

mishushakov commented 3 years ago

thanks for the detailed response

also i found the cxx module, which allows to interprop Rust and C++

would be cool to try out someday

rcelha commented 1 year ago

I've put something together rather quickly today to test with some vst in rust I have https://github.com/rcelha/rtneural-rs

I haven't tested it for real-time stuff yet tho

jatinchowdhury18 commented 1 year ago

I've put something together rather quickly today to test with some vst in rust I have https://github.com/rcelha/rtneural-rs

I haven't tested it for real-time stuff yet tho

This looks very promising, thanks for sharing!

I had been concerned with how inter-operation might work given the highly-templated nature of RTNeural, but I see how that's working in your code with the "wrapper.h" file, basically constructing a non-templated wrapper of the template class. Very cool! I imagine it would be a little bit more complicated to support RTNeural::ModelT, since every model architecture would probably need its own wrapper class? But the wrapper classes could be generated programmatically, so it probably wouldn't be too bad.

I'd also be curious how the Rust vs. C++ performance would compare, since the inferencing calls are going through Rust's FFI. Probably won't make much difference for RTNeural::Model, but I imagine it could be more significant for RTNeural::ModelT.

Anyway, this looks really cool! I'll have to spend some time getting more familiar with it this weekend :)

rcelha commented 1 year ago

I am glad you like it!

I am not too concerned over performance, I developed a plugin in C++ (JUCE) in which most of the logic was in Rust, then Rust was talking to another Library in C (OnnxRuntime). It was all mostly fine, as there is not much copying and allocating around.

I am gonna try to put an example together with nih-plug to get a few for how that is gonna behave in real world tho.

rcelha commented 1 year ago

@jatinchowdhury18 you can check out the sample project I've put together over a few hours https://github.com/rcelha/rtneural-rs/pull/1

Latency was pretty good across the board, although I didn't profile it. I was able to run a bunch models from AIDA-X with many different sample size configuration (the lowest my sound card goes is 32 samples).

*On a side note, I was trying to find some info on possible overhead between Rust/C interop: https://blog.rust-lang.org/2015/04/24/Rust-Once-Run-Everywhere.html