Reshaping block by block

I have the following question

lets say I have a three dimensional tensor [8192,64,1] which is basically, a tensor of 8192 blocks of size 64 by 1… and 8192 is 16 times 512.
eg : [[A]

[Z]8192 such blocks] and [A],[B] etc… are each 64 by 1 in shape

I want reshape the above framework as follows

[[A] [B] [C] … upto 512 columns

… [Z] upto 16 rows]

So the new reshaped tensor would be having 512 columns and 16 rows of such 64 by 1 blocks

In other words, the ultimate shape of the matrix should be 1024 by 512 (1024 because the rows are 16 and the number of elements in each row is 64.

The other constraint is I don’t want to use for loops to achieve this objective.
I am unsure how to proceed with this. Please Help!!

I think you could just use some view and permute ops.
Based on your description, I’ve created some dummy code using much smaller sizes, so that you can print the intermediate outputs and compare them to your reshaping logic:

a, b, c = 8, 6, 1  # initial size
columns = 2
rows = 4

x = torch.arange(a*b*c).view(a, b, c)

y = x.view(rows, columns, -1)

z = y.permute(0, 2, 1).contiguous().view(-1, columns)

Cool this worked for me. Thanks a lot :slight_smile: