Solved by albanD in post #2 
              
              
                This creates a rather large computational graph indeed. 
self.M*x though could be computed a single time and then reused in the loop, that will reduce it quire a bit ! 
Also even though your use case is more complex, remember that using .expand() and then doing one single multiplucation (possibly wi…