I found several examples of code where torch.pow(x, 2) is used to square the components of a tensor. Would it be more performant to replace such instances with torch.square(x)?

I have not yet come up with a benchmark to test this; I was hoping someone with more knowledge of torch's internals would be able to expand on why one is faster than the other (or not).

1

1 Answer

In my experiments using timeit for 1M tries to compute a square of a 4096 vector, I found:

  1. * fastest with 8.55s
  2. square with 12.51s
  3. pow with 14.11s
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.