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

Implement binding generator in Rust #318

Closed

Conversation

jerry73204
Copy link
Contributor

The PR totally rewites the OCaml binding generator in Rust. Now the re-generated code passes every test on tch-rs and on large projects including rust-bert and my yolo-dl.

My motivation was to add libtorchvision bindings, but stuck in the OCaml bindgen code. I felt the pain in the hassle of codegen in strings. The rewriting will gain us the Rust's safety feature and make the project self-hosted. (I did not blame OCaml. It's a good language and I learned it recently.)

  • Declaration file deserialization is done with serde in declarative way.
  • Generated Rust code is assembled with quote token by token rather in strings.
  • It becomes easy to refactor and maintain. Every stage is explicitly and statically typed. The compiler type checking will be a good aid.

The new generated bindings should maintain the compatibility with previous code. It is ensured by tests some manual audit on rustdoc. To check if the new bindings works for your project, run make gen in tch-rs directory and add this to your project Cargo.toml.

[patch.crates-io]
tch = { path = "/path/to/tch-rs" }
torch-sys = { path = "/path/to/tch-rs/torch-sys" }

BTW, I'm working on libtorchvision bindings. It will be easier if this PR can be accepted.

@doofin
Copy link

doofin commented Feb 5, 2021

Looks great! hasktorch also generate bindings from Declarations.yaml.Does this use rust-bindgen?
BTW,does this imply that we will get the same api as the pytorch cpp api?

@doofin
Copy link

doofin commented Feb 5, 2021

Just played around with this PR,build without problem and MNIST example works(on newest arch linux)

@jerry73204
Copy link
Contributor Author

Looks great! hasktorch also generate bindings from Declarations.yaml.Does this use rust-bindgen?

@doofin It generates every function signature according to Declarations.yaml without bindgen.

BTW,does this imply that we will get the same api as the pytorch cpp api?

Basically yes, Declarations.yaml is generated by compiling libtorch.

@LaurentMazare
Copy link
Owner

It is already the case that the Rust api in this create is automatically generated from Declarations.yaml and it has been so since the first version of this crate. You can find the generated api in tensor_generated.rs for example.
The design goal was indeed to keep the api as close as possible to the C++ one (which itself is pretty close to the Python version), so that it's easier to port examples and applies existing tutorials. And it should be possible to write more Rust idiomatic crates on top of this one.
The part generating the code from Declarations.yaml is actually in OCaml as it's shared with the ocaml-torch sister project. OCaml is not required to compile this crate though as the generated files are checked in in the repo. This PR rewrites the code generation in Rust, however we would like to keep it in OCaml at the moment so as to continue sharing code and functionalities with ocaml-torch.

@doofin
Copy link

doofin commented Feb 6, 2021

Great,maybe it's better to start a new separate project for codegen.

@jerry73204
Copy link
Contributor Author

Seems it is decided to couple the OCaml project, though I think the opposite. I will move the code to another repo for those interested.

@jerry73204 jerry73204 closed this Feb 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants