Optimize `setDebugName` time complexity (#52346)
Summary:
`setDebugName` maintains an invariant that all debug names of values in same graph must be distinct. This is achieved by appending numeric suffixes to requested debug names. However, the implementation was slow (O(N^2)) when there are a lot of name conflicts. This PR fixes the problem by adding more book-keeping logic so that time complexity is brought down to O(1) on average.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/52346
Reviewed By: SplitInfinity
Differential Revision: D26564462
Pulled By: gmagogsfm
fbshipit-source-id: 3260fc3b436f1b0bcb45fdd2d1ec759b5828263f