Thanks for your response.
1. Factoring out decay factor:
So yes in principle the idea is correct to implement weighted cumulative sum as:
y[i] = (y[i-1] + x[i]/(w^i)) / w^i

But the array is rather long so can have a loss of precision.
2. For loops:
The second way of using for loops is also viable but I have to see if there are much performance drop (I do not need backpropagation anyway).
3. Implement tensor version:
Have never done this before, but would like to give it a try. Can you recommend how to give it a shot?
Thanks!