[MPS][BE] Introduce `LookUpOrCreateCachedGraph` (#99422)
A template that replaces following common pattern:
```cpp
MPSGraphCache* cache_ = MPSGraphCache::getInstance();
CachedGraph* cachedGraph = cache_->LookUpAs<CachedGraph>(key);
if (!cachedGraph) {
cachedGraph = cache_->CreateCachedGraphAs<CachedGraph>(key, ^MPSCachedGraph*() {
CachedGraph* newCachedGraph = nil;
@autoreleasepool {
MPSGraph* mpsGraph = make_mps_graph();
newCachedGraph = new PoolingCachedGraph(mpsGraph);
...
}
return newCachedGraph:
);
}
```
with
```cpp
auto cachedGraph = LookUpOrCreateCachedGraph<CachedGraph>(key, [&](auto mpsGraph, auto newCachedGraph) {
...
});
```
Fixes memory leak in addmv_out_mps_impl, where new entires were added the cache without doing the lookup first.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/99422
Approved by: https://github.com/albanD, https://github.com/kulinseth