If you can convert your multiplication and addition to numpy/torch then you dont need these pythonic loops. Easiest way is to represent your adder multiplier using numpy/torch builtin functions. And use above code snippet. You might have to play with the shape of the tensors a little bit, but it definitely possible and fast.