symforce icon indicating copy to clipboard operation
symforce copied to clipboard

Rust codegen support

Open avsaase opened this issue 9 months ago • 7 comments

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.

avsaase avatar Nov 11 '23 08:11 avsaase

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.

aaron-skydio avatar Nov 13 '23 21:11 aaron-skydio

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?

avsaase avatar Nov 14 '23 20:11 avsaase

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)

aaron-skydio avatar Nov 14 '23 21:11 aaron-skydio

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.

avsaase avatar Dec 10 '23 16:12 avsaase

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

aaron-skydio avatar Dec 11 '23 21:12 aaron-skydio

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.

matte1 avatar Jan 16 '24 04:01 matte1

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

aaron-skydio avatar Jan 16 '24 20:01 aaron-skydio