Fix deadlock when forward and backward AD are used at the same time (#67360)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/67360
Test Plan: Imported from OSS
Reviewed By: soulitzer
Differential Revision: D31973040
Pulled By: albanD
fbshipit-source-id: f9c75c6497b622c86e8653027bce45461304eff5