microsoft / knossos-ksc

Compiler with automatic differentiation
Other
45 stars 10 forks source link

Simplify pure python entry points #994

Closed dcrc2 closed 3 years ago

dcrc2 commented 3 years ago

This PR provides a simple (but inefficient) way to call ks functions from python without using torch. This is used in the tests adbench-lstm, gmm and mnistcnn.

The approach here is to generate a C++ wrapper for the ks function which uses std::tuple and std::vector to represent tuples and (1-dimensional) tensors. pybind11 knows how to map these types to python tuples and lists.

The generation of the wrapper is done by a bit of C++ template trickery, rather than using code-generation as in compile.py. I think this makes sense here as it's only intended for use in tests, so we don't need to optimize the wrapper and can get away with simple code.

This PR allows us to remove the file knossos-pybind.h, which provided pybind11 mappings for ks::Tuple. It's quite nice to have that mapping available, but there's no efficiency gain over std::tuple in this context. I think it's better to delete the code because it's almost a direct copy of pybind11's code for std::tuple. If we had an open source project the better way to deal with this would be to submit a PR for pybind11 to make their tuple_caster implementation slightly more generic.