PyTorch Tensor Operation for adding the maximum of the previous row to the next
Follow-Up question to PyTorch: Dynamic Programming as Tensor Operation.
Could the following be written as a tensor operation instead of a loop?
a = torch.Tensor([[1, 2, 3, 4],
                  [5, 6, 7, 8],
                  [9, 10, 11, 12]])

print(a.shape) 
# (3, 4)

for i in range(1, a.shape[0]):
    a[i] = a[i-1].max(dim=0)[0] + a[i]

print(a)
# tensor([[ 1,  2,  3,  4],
#         [ 9, 10, 11, 12],
#         [21, 22, 23, 24]])

Basically adding the maximum of the previous row to all elements of the next.
The interesting part is that you can't compute the maximum for each row beforehand and then add that to the respective row, because adding the first maximum influences what the maximum of the next row is.

Not entirely sure why you're trying to do this, but yes, this is possible. It's basically the same as your last question:
max_vals, _ = a.max(axis=1, keepdim=True)
additions = max_vals.cumsum(0)[:-1]
a[1:, :] += additions

This is because the marginal addition from one row to the next is equivalent to the maximum, so you can take the maximums first, then cumulatively sum them and add them to the original tensor.
