MPS: Fix the memory growing issue and BERT_pytorch network crash fix. (#78006)
Fixes #77753
Pull Request resolved: https://github.com/pytorch/pytorch/pull/78006
Approved by: https://github.com/albanD
(cherry picked from commit cbdb694f158b8471d71822873c3ac130203cc218)