I am leaving the answer here just for completeness.
Assuming that head
signifies the start position of 1’s in a row & tail
denotes the end position of 1’s in the row, the following code might achieve what you expect to do.
>>> import torch
>>> torch.__version__
'1.11.0'
>>> head = torch.tensor([
... [0, 1, 0, 0],
... [1, 0, 0, 0],
... [1, 0, 0, 0],
... [0, 0, 0, 0],
... ])
>>> tail = torch.tensor([
... [0, 0, 1, 0],
... [0, 0, 0, 1],
... [0, 0, 1, 0],
... [0, 0, 0, 0],
... ])
>>> head.cumsum(dim=1) * tail.fliplr().cumsum(dim=1).fliplr()
tensor([[0, 1, 1, 0],
[1, 1, 1, 1],
[1, 1, 1, 0],
[0, 0, 0, 0]])