Open
Conversation
ab8a8e3 to
5288e12
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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.
This PR closes #1116 by adding
clear_mooncake_caches!(; gc::Bool=false)as a public API function that clears all Julia-level cachesMooncakeholds globally.Problem :
When
Mooncakedifferentiates a function for the first time, it compiles a reverse-mode rule and stores it inGLOBAL_INTERPRETERSacross three caches :oc_cache(compiledOpaqueClosures),code_cache(CodeInstanceobjects), andinf_cache(InferenceResultobjects). These caches never shrink on their own.In long-running
Mooncakesessions we end up differentiating many distinct function signatures - these accumulate indefinitely. (The Discourse thread linked in Clear Mooncake internal caches #1116 reported the user hitting ~4.5 GB from Mooncake's caches with no way to reclaim it)Solution :
Our function empties
oc_cache,code_cache.dict, andinf_cachein-place for bothForwardModeandReverseModeinterpreters. After calling it,Mooncakere-derives rules from scratch on the next use, and theGCcan reclaim the now-unreferencedDerivedRule,CodeInstance, andInferenceResultobjects.The
gckeyword (default false) optionally runsGC.gc(true)immediately after clearing for users who want memory reclaimed right away rather than at the nextGCcycle.What is exactly freed :
Clearing the caches makes the Julia-level objects (
DerivedRule,CodeInstance,InferenceResult)GCeligible. The JIT-compiled native machine code in main memory is allocated by LLVM outside the GC heap and cannot be reclaimed from user code and remains for the lifetime of the process (at least as of Julia v1.12), to clear this we must restart the REPL instance itself.CI Summary — GitHub Actions
Documentation Preview
Mooncake.jl documentation for PR #1131 is available at:
https://chalk-lab.github.io/Mooncake.jl/previews/PR1131/
Performance
Performance Ratio:
Ratio of time to compute gradient and time to compute function.
Warning: results are very approximate! See here for more context.