Solved by KFrank in post #2 
              
              
                Hi Nauryzbay! 

You may use scatter_add_() (with pytorch tensors) for this: 
>>> import torch
>>> torch.__version__
'1.9.0'
>>> A = torch.tensor ([
...     [1, 2, 3, 4, 5, 6],
...     [2, 3, 4, 5, 6, 1],
...     [3, 4, 5, 6, 1, 2],
... ])
>>> B = torch.tensor ([
...     [0, 1],
...     [0, 3],
...  …