Skip to content

Conversation

@manuelcandales
Copy link
Contributor

@manuelcandales manuelcandales commented Oct 22, 2025

This pull request introduces a new caching infrastructure for compiled MPSGraph objects in the Metal backend, significantly improving performance for repeated matrix multiplication and convolution operations by reusing previously compiled graphs and their associated tensors. The changes also include cache statistics tracking and refactoring of the relevant code paths to leverage the cache. Note that caching for the attention operation is not yet implemented.

MPSGraph Caching Infrastructure

  • Added a new global cache (graph_cache) for compiled MPSGraph objects and their input/output tensors, keyed by operation type and parameters, enabling reuse and reducing graph compilation overhead.
  • Introduced GraphCacheKey and CachedGraph structures, along with a custom hash function, to uniquely identify and store cached graphs for matrix multiplication and convolution operations.

Matrix Multiplication and Convolution Refactoring

  • Refactored the matrix multiplication (aoti_torch_mps_mm_out) and convolution (aoti_torch_mps_convolution) functions to check the cache for an existing compiled graph before creating a new one, and to store newly compiled graphs in the cache for future reuse. [1] [2]
  • Removed manual release of MPSGraph objects after execution, as cached graphs are now retained for reuse, preventing unnecessary recompilation and memory leaks. [1] [2]

Cache Statistics and Monitoring

  • Added CacheStats structure to track cache hits and misses.

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@manuelcandales
Copy link
Contributor Author

manuelcandales commented Oct 22, 2025

Stack from ghstack (oldest at bottom):

@pytorch-bot
Copy link

pytorch-bot bot commented Oct 22, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/15346

Note: Links to docs will display an error until the docs builds have been completed.

⏳ No Failures, 116 Pending

As of commit f66ac9c with merge base 526eb18 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

manuelcandales added a commit that referenced this pull request Oct 22, 2025
ghstack-source-id: 1e8dc6b
ghstack-comment-id: 3430555924
Pull-Request: #15346
@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 22, 2025
[ghstack-poisoned]
[ghstack-poisoned]
manuelcandales added a commit that referenced this pull request Oct 22, 2025
ghstack-source-id: ed5ea0c
ghstack-comment-id: 3430555924
Pull-Request: #15346
Base automatically changed from gh/manuelcandales/147/head to main October 22, 2025 07:51
[ghstack-poisoned]
manuelcandales added a commit that referenced this pull request Oct 22, 2025
ghstack-source-id: ed5ea0c
ghstack-comment-id: 3430555924
Pull-Request: #15346
@manuelcandales manuelcandales added the release notes: none Do not include this in the release notes label Oct 22, 2025
@manuelcandales manuelcandales merged commit 3044dd8 into main Oct 22, 2025
126 of 135 checks passed
@manuelcandales manuelcandales deleted the gh/manuelcandales/148/head branch October 22, 2025 08:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. release notes: none Do not include this in the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants