Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust codegen support #374

Open
avsaase opened this issue Nov 11, 2023 · 7 comments
Open

Rust codegen support #374

avsaase opened this issue Nov 11, 2023 · 7 comments
Labels
core enhancement New feature or request

Comments

@avsaase
Copy link

avsaase commented Nov 11, 2023

Is your feature request related to a problem? Please describe.
Rust is getting more popular in the embedded space and it would be great if symforce could generate efficient Rust code.

Describe the solution you'd like
Symforce has a Rust codegen backend. Ideally it would also support adding doc comments to the rust code to make it easier to work with the generated code.

Describe alternatives you've considered
Call the generated C++ code from Rust with FFI. I haven't tried this yet but it would certainly be much nicer work with generated Rust code directly.

Additional context
I searched for an existing feature request about this but wasn't able to find one.

@aaron-skydio aaron-skydio added enhancement New feature or request core labels Nov 13, 2023
@aaron-skydio
Copy link
Member

This would be great, and it shouldn't be much work to at least start with a minimal Rust backend. One decision that's going to come up is what matrix library to use for vector and matrix inputs and outputs in the API - SymForce just needs these as containers, we don't need any operators. Ideally it would support representing both dense and sparse matrices, just dense might be ok initially but it would be annoying to use a different library for representing sparse matrices.

Also, just noting that the first thing to do here is just codegen support, and a Rust version of the optimizer (either through FFI to the C++ optimizer, or a Rust-native optimizer) would be an additional project.

@avsaase
Copy link
Author

avsaase commented Nov 14, 2023

Is the linear algebra really only needed for its types? If so, then I would choose one that adds minimal size to the compiled binary. The most use linear algebra library in rust, nalgebra is great but it adds quite a bit of size to the binary which is not great for embedded devices.

Does the chosen library need to support n-dimensional arrays, or only vector and matrices?

@aaron-skydio
Copy link
Member

Is the linear algebra really only needed for its types?

For the generated code yes. For the factors we sometimes use Eigen's dense matrix ops to form the linearization, although we don't usually recommend this. More importantly the optimizer uses Eigen's SparseMatrix representation and needs a linear solver (either one from Eigen, or custom).

I think we could very well use a library that doesn't provide any linear algebra operations as long as it has the right containers, but I'm not sure how to best do this without thinking about it a little more.

We don't need nd arrays, just vectors and matrices, although it might be interesting to expose DataBuffer in Rust as an n-d object if that ends up being easy (we don't currently do this in other languages)

@avsaase
Copy link
Author

avsaase commented Dec 10, 2023

I had look through the available linear algebra crates and I think nalgebra is the only viable option. All the other libraries I found either are not no_std compatible, require an allocator, or don't support arbitrary matrix sizes. nalgebra is not the lightest dependency to take on but it's widely used in the Rust ecosystem so it's not unlikely that projects already have a dependency on it, in which case there's not extra code size cost.

@aaron-skydio
Copy link
Member

Nice, thanks for checking! I think it'd make sense to go ahead with nalgebra then

@matte1
Copy link

matte1 commented Jan 16, 2024

Has there been any movement here on supporting Rust @avsaase @aaron-skydio? I'd give a +1 to using nalgebra. If there is a work in progress that needs some help I might be able to throw some cycles at it.

@aaron-skydio
Copy link
Member

Nothing currently in progress. I think the other matrix library we could use is glam - it might be nice to make this configurable (this shouldn't be hard).

An example of the codegen part at least is the CUDA backend, sympy also already has a Rust printer so it might be even less work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants