Solved by Naruto-Sasuke in post #6 
              
              
                import torch

N = 6
C = 4

data = torch.randn(N, C)
head_mask = torch.tensor([1, 1, 0, 0, 1, 0])
head_index = (head_mask == 1).nonzero().view(-1) # 0, 1, 4
head_index_expand = torch.cat([head_index, torch.tensor([head_mask.numel()]) ] ) # 0,1, 4, 6
head_index_diff = (head_index_expand - torch.roll(h…