Skip to content

Use thread local storage for frontend compile cache#3280

Merged
zcbenz merged 1 commit intoml-explore:mainfrom
zcbenz:frontend-compile-cache
Mar 19, 2026
Merged

Use thread local storage for frontend compile cache#3280
zcbenz merged 1 commit intoml-explore:mainfrom
zcbenz:frontend-compile-cache

Conversation

@zcbenz
Copy link
Collaborator

@zcbenz zcbenz commented Mar 19, 2026

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).

Copy link
Member

@angeloskath angeloskath left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@zcbenz
Copy link
Collaborator Author

zcbenz commented Mar 19, 2026

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.

@zcbenz zcbenz merged commit 70a0da6 into ml-explore:main Mar 19, 2026
16 checks passed
@zcbenz zcbenz deleted the frontend-compile-cache branch March 19, 2026 22:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants