ankane / torch.rb

Deep learning for Ruby, powered by LibTorch
Other
704 stars 30 forks source link

Chunk function is not working #13

Closed mib32 closed 4 years ago

mib32 commented 4 years ago

On 0.3.4, libtorch 1.6.0 version, running this command:

Torch.chunk(Torch.randn(20, 10), 10)

fails with error:

*** ArgumentError Exception: Unable to convert std::__1::vector<at::Tensor, std::__1::allocator<at::Tensor> >*

P.S. I'd be happy to make a PR, but I would appreciate if I could have some hint where to look, because the C++ part feels a bit cryptic to me .. %)

ankane commented 4 years ago

Hey @mib32, thanks for reporting. That error indicates Rice doesn't know how to convert a std::vector<torch::Tensor> C++ object to Ruby. The steps to fix would be:

  1. Create a new wrap function for std::vector<torch::Tensor>: https://github.com/ankane/torch.rb/blob/master/ext/torch/templates.cpp (and add it to templates.hpp)

  2. Update code that generates the C++ functions to use wrap

https://github.com/ankane/torch.rb/blob/0707437ce3864962781ca7888d2c976e29d0ef90/lib/torch/native/generator.rb#L133-L136

If you look up chunk in native_functions.yaml, the return type is Tensor(a)[] (anything with [] will need wrapped). You can run bundle exec rake generate:functions and search for the _chunk method in ext/torch/tensor_functions.cpp to see if wrap is being added.

  1. Add a test case, compile the code and run the tests
bundle exec rake compile -- --with-torch-dir=/path/to/libtorch
bundle exec rake test

It's a bit of a complicated fix, so happy to take it if that's preferred.

ankane commented 4 years ago

Fixed in #14