xtensor icon indicating copy to clipboard operation
xtensor copied to clipboard

Buffer size of strided adapters?

Open jdh8 opened this issue 2 years ago • 2 comments

I tried to adapt a splat tensor. However, xtensor threw a runtime error with message xbuffer_storage not resizable. It seems that xtensor expects that buffer size equals to tensor size. I don't think that would be the case with user-provided strides.

#include <xtensor/xadapt.hpp>
#include <xtensor/xio.hpp>
#include <array>

int main()
{
  const float data = 1;
  const std::array<int, 2> shape = { 2, 2 };
  const std::array<int, 2> strides = { 0, 0 };

  // Expected
  std::cout << xt::adapt(&data, 1, xt::no_ownership(), shape, strides) << '\n';

  // Actual working
  std::cout << xt::adapt(&data, 4, xt::no_ownership(), shape, strides) << '\n';
}

jdh8 avatar Jun 13 '22 12:06 jdh8

The error makes sense. You are using the following overload

https://github.com/xtensor-stack/xtensor/blob/308458574baa95b0dabb981a4bae97fa29ff673f/include/xtensor/xadapt.hpp#L161

In this case the size has to match up prod(shape) = 4. The error is telling you exactly that: your shape is not corresponding to your size.

But you had realised this. Instead, you want to pretend to have more entries than you have (let's say broadcast) by setting (one of) the stride(s) to zero. Well, yes, I do agree that that should be legal. Personally I would say that the size argument is useless and pedantically checking the user, thereby limiting potential legal use cases (regardless wether we like them or not ;)). Given that it is there, indeed, I think that we should consider that strides makes this case legal.

tdegeus avatar Jul 26 '22 12:07 tdegeus

P.S. The fastest way to getting this implemented is to open a PR ;)

tdegeus avatar Jul 26 '22 12:07 tdegeus