Fix static initialization issue for static build (#90133)
Fixes #83255
Code comes from #83258 after fixing merge conflicts.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/90133
Approved by: https://github.com/soumith, https://github.com/malfet