"expand" on non-singleton dimensions without actually repeating the memory?

I am wondering if it is possible to have something like ‘torch.repeat’ but returns a view instead of actually repeating the memory? ‘torch.expand’ unfortunately seems to work only on singleton dimensions. Thanks in advance!

1 Like

Hi,

Unfortunately, this is not possible.
The low level reason is that torch.expand is implemented by setting the stride on one dimension to 0 (and thus reading always the same value). But you can use this trick only to read the first element, and it does not work if there are more than one.

1 Like

I am not familiar with the low level implementation, but I understand the reason you explained. It is a pity that it can not be achieved easily. I have no idea how complicated it can be, but maybe we could have some kind of circular indexing implemented for this?

Anyway, thank you for your fast reply!

You could try flattening your tensor and then expanding it. The final shape of the tensor may not be what you’re looking for but depending on what you’re using it for you could try to work around that.

Thank you!

However if I am not mistaken this can only expand the the first (maybe also the last?) dimension but can not expand the dimensions in the middle. I guess a similar work-around might be to use “torch.unsqueeze” to add a singleton dimension before the desired dimension then expand it instead. But then it is not contiguous anymore, and using “view” to get the desired shape directly will raise an error.