LaurentMazare / tch-rs

Rust bindings for the C++ api of PyTorch.
Apache License 2.0
4.33k stars 344 forks source link

How does gen work? #5

Closed ehsanmok closed 5 years ago

ehsanmok commented 5 years ago

Hi

Do you mind explaining how tch-rs/gen work? and when does it kick in?

Why not binding against c-api in $LIBTORCH/include/torch/csrc/api/include/torch/ directly?

Thanks

LaurentMazare commented 5 years ago

tch-rs/gen/gen.ml generates:

The generated files are also stored in git so there is no need to run this code generation in the normal build process. It's only useful to execute it when updating to a new major libtorch version but this is rather infrequent (there was no need to rerun it between 1.0.0 and 1.0.1).

The code generation is in ocaml as it's adapted from the ocaml-torch bindings. To run it you will need a working ocaml install including the dune, base, stdio, and yaml packages, the Declarations.yaml file and you could run dune exec gen/gen.exe.

Binding to the library directly would be a bit tricky: it's in C++ so bindgen does not work very well, writing manual wrappers is a bit of a pain as there are more than a thousand functions to expose.

ehsanmok commented 5 years ago

Very interesting! thanks for the clarifications. Absolutely makes sense to reuse what you've made before.

Would it be able to support more than one major libtorch versions at the same time (in the future)?

LaurentMazare commented 5 years ago

With the current release cycle of PyTorch, I hope for that not to be necessary. However if this becomes a concern, we could certainly consider generating the code in the build process at some point.