Creating a tensor that counts difference between a target and a column number

I have a first tensor named “target” that is a 1D tensor that contain n integers from 0 to 3
target = (2,0,3,2,1,1,2,3,0,…,2)

Within the framework of a custom loss function, I would like to create a 2D tensor of sise (n, 4) for which the value of each item would be equal to the absolute difference between the target and the column
adj_target = ((2,1,0,1),(0,1,2,3),(3,2,1,0),…)
Example :
First row of adj_target is a function of the first row of target = 2

  • 1st column = abs(0-2) = 2
  • 2nd column = abs(1-2) = 1
  • 3rd column = abs(2-2) = 0
    -4th column = abs(3-2) = 1
    Second row of adj_target is a function of the first row of target = 0
  • 1st column = abs(0-0) = 0
  • 2nd column = abs(1-0) = 1
  • 3rd column = abs(2-0) = 2
    -4th column = abs(3-0) = 3
    Third row of adj_target is a function of the first row of target = 3
  • 1st column = abs(0-3) = 3
  • 2nd column = abs(1-3) = 2
  • 3rd column = abs(2-3) = 1
    -4th column = abs(3-3) = 0
    etc…

Is there an easy and efficient way to do it ?
Thanks in advance

target = torch.tensor((2,0,3,2,1,1,2,3))
t_size = target.numel()
expand_tar = target.view(1, -1).expand(4,t_size )
adj_target = torch.abs(expand_tar - torch.arange(4).view(-1, 1).expand(4, t_size )).t()