Don’t use  torch.tensor in between and never use torch.FloatTensor, but instead use stack to make a list of tensors into one large tensor.
Of course, the presentation makes me wonder if you can cut the for loops by vectorizing and cumsum, too.
Best regards
Thomas