# Understanding error msg "view size is not compatible with input tensor's size and stride"

Ideally, I would like to execute something like
t = torch.zeros([4, 3, 64, 64])
t[:, :, ::8, ::8].view(4, -1)
but that produces the error

RuntimeError: view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces). Use .reshape(...) instead.

Unfortunately, I can’t use .reshape() or .contiguous() because of memory consumption. This code is called too often to make a copy of the tensor each time. Instead I would like to create one big tensor and slice it each time.

Is there some way to use .transpose() or something similar in combination with the above .view() to achieve my goal? Is there a way to get a more detailed error message to understand which dimension exactly is the problem?

The problem is that element spacing is irregular when you merge dimensions:

t[:,:,::8,::8].stride()
(12288, 4096, 512, 8)

So it is impossible to collapse three last numbers into one.

Thanks for your reply, could you elaborate a little? Can’t result of
t[:,:,::8,::8].view(4, -1)
be the tensor with the same underlying data in storage as t, size = (4, 3*64*64), and stride = (12288, 8)? Both 4096 and 512 are divisible by 8, so wouldn’t that be the solution view() goes for?

Yes, sorry, output from stride() should be reasoned about together with tensor size. Here, your last dimension says: increase pointer by 8 (last stride) 8 times (last dim size), but the third dimension wants to to take steps of 512 elements (it is skipping 7 segments altogether).

Stride of 8 corresponds to t[:,:,:,::8], where you have stride[i] = stride[i+1] * size[i+1]