I’m still looking for a better solution but if someone is having a similar problem, I sort of figured out how to do it with the help of ideas in the following link:

  
    
    
    Efficient way of calculating sum of unequal sized chunks of tensor 
  
  
    Thanks for your reply. I am actually doing this on GPU. I think the only way is to pad it and do sum along axis. I would assume if I am doing for loop anyway, only do padding with in the for loop would be faster? In any case I will try it out. Thanks a lot!
  


This isn’t the most efficient method because a lot of the terms in cumsum (and cumprod) are unnecessary, if someone finds a more efficient way to do this then please post here but the following seems to work at least for the summation: (NOTE: this has problems with cumprod, for example if  the array is long then the cumulative product can blow up to INF or if there are any zeros in the array, then all subsequent products are zero)
PYTORCH SOLUTION
FOR THE SUM
a=torch.tensor([[1,2,3,4,5,6,7,8]])
b=a.cumsum(1) #cumulative sum over row
c=b.gather(1, torch.tensor([[1,3,7]])) #select relevant terms
d=torch.cat( (torch.tensor([[0]]), b.gather(1, torch.tensor([[1,3]]))),1) #select relevant terms
print(c,d,c-d)
returns
tensor([[ 3, 10, 36]]) tensor([[ 0,  3, 10]]) tensor([[ 3,  7, 26]])
FOR THE PRODUCT
a=torch.tensor([[1,2,3,4,5,6,7,8]])
b=a.cumprod(1) #cumulative sum over row
c=b.gather(1, torch.tensor([[1,3,7]])) #select relevant terms
d=torch.cat( (torch.tensor([[1]]), b.gather(1, torch.tensor([[1,3]]))),1) #select relevant terms
print(c,d,c/d)
returns
tensor([[    2,    24, 40320]]) tensor([[ 1,  2, 24]]) tensor([[   2,   12, 1680]])
NUMPY SOLUTION
a = np.array([[1,2,3,4,5,6,7,8]])
b=np.add.reduceat(a, [0,2,4], axis=1)
print(b,type(b),b.shape)
returns
[[ 3  7 26]] <class ‘numpy.ndarray’> (1, 3)