Closed abdulfatir closed 1 year ago
There is overhead associated with functorch.grad, so yes, it's expected that functorch.grad (by itself) is slower than autograd. The overhead is less noticeable the larger the program is. functorch.grad is a wrapper around torch.autograd.grad that does some additional bookkeeping; the overhead is necessary because one can arbitrarily nest functorch.grad with other function transforms (like vmap), but one is not able to do the same with torch.autograd.grad.
Is the overhead a problem for you? There is likely some low-hanging fruit we can do to cut some of the overhead and it's on our roadmap to ensure that the upcoming torch.compile feature helps cut overhead of functorch transforms.
Thanks, I did look into the source code after posting this issue and realized that the behavior could be due to the additional bookkeeping. I am curious to know which bookkeeping operations slow things down though.
My use case was mainly the cleanliness of code provided by functorch API but I guess something similar could be achieved by writing a wrapping function over torch.autograd.grad
.
If you're interested, https://docs.google.com/document/d/14qyaa3xIjmVxYiMLlIlQErunYgR_uR1WupsKMZlnGY4/edit# goes over the design (but it might assume knowledge about how PyTorch internals work). That and other information is linked over at the PyTorch Wiki for the curious (https://github.com/pytorch/pytorch/wiki/Core-Frontend-Onboarding)
Thank you!
functorch.grad
appears to be almost 2.5 times slower thantorch.autograd.grad
in my simple test below. Is this expected?Versions