Is it possible to combine Rust and Python for AI?

Is it possible to combine Rust and Python for AI, so like I end up mainly using Rust for the AI after setting it up with Python (forgive me for my lack of knowledge in this area)?

What kind of AI are you talking about? If you want to roll your own methods, Rust is probably a good language for writing performance-critical code that feeds into a Python API. If you are looking for batteries-included machine learning implementations (that you assume to be faster in Rust than those implemented in Python), you probably won't find any. Rust's machine learning libraries are still immature. Also, the machine learning libraries written in Python are almost definitely accelerated via Cython or just plain C. I suspect that it will be difficult to beat their performance.

I am talking about if I can some how use Rust and some how use it in such a way that I use the Python's built in AI libraries, is that possible?

The excellent PyO3 package has you covered. It provides Rust -> Python and Python -> Rust functionality. Interfacing between languages is usually challenging, though.

2 Likes

So if I write some AI in Rust (using Pyo3 libraries) vs writing some AI in Python, would it be very similar in terms of using the commands?

What do you mean by this exactly may I know?

Also would I achieve the same speeds with Rust (using Pyo3 libraries) vs writing some AI in Python?

Interfacing between languages is a challenge because of different language designs, for example, python is dynamically, weakly typed, and has no idea what a Vec<T> is. Rust on the other hand, is strongly, statically typed, and doesn't know how to work with python's ???-like values. To illustrate this, I'll make an example for two languages that are closer (I'll remove some random ffi stuff for clarity):

//main.rs
struct Foo {
    data: &'static [u8],
    other_data: usize
}
fn get_foo() -> Foo {
    Foo {
        data: &[],
        other_data: 0
    }
}

and in c#:

public class Foo {
    public (IntPtr, IntPtr) data;
    public IntPtr other_data;
}

Notice how your nice &'static [u8] has now become an ugly tuple, and your usize is now a IntPtr, because that preserves the size. Things can get ugly fast, and you could end up passing pointers around, to separate states, and transmuting things and....
So to summarize: FFI (Foreign function interfacing (Talking between languages)) is challenging because there are alot of minor implementation details you need to be aware of to be able to get a valid result.

1 Like

Can all of these issues be fixed with more updates?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.