Most efficient way to use Column major when reshape a 1D array in PyTorch

import torch

    p = torch.arange(0, 12, requires_grad=False, dtype=torch.int32)
    pr = torch.reshape(p, (4, 3))

what I want is

pr = [0 4 8 
      1 5 9
      2 6 10
      3 7 11]

but it actually becomes

pr = [0 1 2 
      3 4 5
      6 7 8
      9 10 11]

I search online it said permute can do it, but it will make a copy of your array, what is the most efficient way to reshape it in PyTorch?

I am not sure permute will necessarily make a copy of the array:

>>> import torch
>>> p = torch.arange(0, 12, requires_grad=False, dtype=torch.int32)
>>> p.data_ptr()
94744753781760
>>> p.is_contiguous()
True
>>> prt = torch.reshape(p, (3, 4)).t()
>>> prt
tensor([[ 0,  4,  8],
        [ 1,  5,  9],
        [ 2,  6, 10],
        [ 3,  7, 11]], dtype=torch.int32)
>>> prt.data_ptr()
94744753781760
>>> prt.is_contiguous()
False
>>> 
1 Like