Use thread local storage for frontend compile cache#3280
Merged
zcbenz merged 1 commit intoml-explore:mainfrom Mar 19, 2026
Merged
Use thread local storage for frontend compile cache#3280zcbenz merged 1 commit intoml-explore:mainfrom
zcbenz merged 1 commit intoml-explore:mainfrom
Conversation
angeloskath
approved these changes
Mar 19, 2026
Member
angeloskath
left a comment
There was a problem hiding this comment.
Looks great!
I wonder if people rely on compile being ran once. They shouldn't but this could be a fun bug to debug in the future.
@mx.compile
def fun(x):
call_my_run_once_and_only_once_side_effect_op()
return x**2 + 2 * x + 1
# thread 1
fun(mx.ones((10,))
# thread 2
fun(mx.ones((10,))) # oops
Collaborator
Author
|
That would already be a problem since changing the default stream would invalidate the compile cache. In a multi-thread environment we expect users to use different streams as default streams so hopefully it would not be a problem. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #2086, #3078, #3216.
The frontend compile cache uses function ID as key so it is very unlikely functions in different threads would shared cache, and making it thread local should be enough to ensure thread safety.
For backend compile cache, i.e.
Compiled::eval_cpu/eval_gpu, it is reasonable to share cache between threads and they are already guarded with locks (except for the CUDA backend which I will fix in followup PRs).