Repository navigation
Added asynchronous compilation and in-memory/file cache to GpuCompiler. - #446
AndreyPavlenko wants to merge 2 commits into
Conversation
dchigarev
left a comment
There was a problem hiding this comment.
The async-compilation part looks good, left some comments.
CI enables the file cache, except for the IR dump runs.
Does it mean file-cache is enabled by default in CI? I would either disable it or always run two jobs: one with cache enabled and the other where it's disabled. So that we can both:
- not depend on "corner" cases when something goes wrong with the caching and we get strange results in the "main" ci
- test the caching mechanism by seeing if the result is different from the "no-cache" run
| auto rmStaleDirs = [&now](StringRef root, std::chrono::hours staleTime, | ||
| StringRef skip = {}) { | ||
| std::error_code ec; |
There was a problem hiding this comment.
it's a third nested lambda here :)
I don't know, it's really hard to understand and verify the code's flow with all these immediately-invoked lambdas and their early returns (which is a legalized way to use goto semantic IMHO). I would prefer readability here (especially on a sensitive logic like this, where we delete directories on user's computer) and split this one mega-lambda into 3 (or even more) simpler functions that only do one thing at a time:
resolveCacheRootcleanupIfDueremoveStaleDirs
What do you think?
| } | ||
|
|
||
| // Clean up before resolving symlinks, so it never leaves the cache tree. | ||
| if (int interval = gcGetEnv("GC_CACHE_CLEAN_INTERVAL", cleanInterval); |
There was a problem hiding this comment.
Should we make the clean-up logic an optional feature that is disabled by default (e.g. make default value for GC_CACHE_CLEAN_INTERVAL to be 0 even when GC_CACHE_DIR is unset)?
I mean, libraries (pytorch, triton, pip, hf, triton) never clean their disk-cache automatically. Some of them have a dedicated method to do it manually (like pip cache purge) but it never happens automatically (especially on that small interval like 24 hours), I guess it should be either user's or OS'es responsibility to do it?
Some not very clever users (me) may store some of their temp files right at the cache directory (because they treat cached kernels in plain-text as kernel dumps and it appears to be a very handy place to store their experimental modified IRs right near the related dump), so we don't want to implicitly remove them
There was a problem hiding this comment.
I believe we need to clean the internal cache. 24 hours is the cleanup interval, but it cleans the entries older than GC_CACHE_MAX_HOURS (defaults to 30*24).
| if (auto ocloc = sys::findProgramByName("ocloc"); | ||
| ocloc && !fs::createTemporaryFile("ocloc", "ver", out)) { |
There was a problem hiding this comment.
Is this the only way we can determine ocloc version? I mean, if LLVM is linked against a different version of ocloc it won't be correct? I usually redefine LD_LIBRARY_PATH to check compilation result between different IGC versions, so in this case oclocVersion would fail. Can we extract the version from the loaded version or it's impossible and we just have to accept the risk of incorrect cache-key in that corner case?
There was a problem hiding this comment.
The best option is to get this info from upstream. I've created the PR llvm/llvm-project#243637
| GpuCompiler &GpuCompiler::get() { | ||
| static GpuCompiler instance(gcGetEnv("GC_THREADS", 0u)); | ||
| return instance; | ||
| } |
There was a problem hiding this comment.
Can we make the pool-size configurable via some parameter as well (e.g. build-opts or something)? So that we can potentially support OV's compilation_num_threads parameter.
There was a problem hiding this comment.
OV can just create its own instance of GC. Currently it uses this one, the shared instance.
| : ctx(std::make_unique<mlir::MLIRContext>( | ||
| mlir::gc::getDialectRegistry())), |
There was a problem hiding this comment.
not related to the PR specifically, but have you considered having a single mlir-context between OV and GC? For example having a single static method in GC (like GraphCompiler::getContext()) so that we can reuse it between OV/GC and wouldn't need to dump&parse the module between contexts? Are there any downsides?
There was a problem hiding this comment.
It was my initial intent, but I encountered context corruption issues. Seems, OV mutates the context, but I'm not sure. This requires additional investigation. Perhaps, it's doable.
Anyway, we need to print the module to calculate the hash.
| gc::cache::Key key({ | ||
| text, | ||
| opts.arch, | ||
| opts.igcOpts, | ||
| opts.wait ? "" : "!wait", | ||
| }); | ||
| return cache.get(key, [&] { |
There was a problem hiding this comment.
I think it's easy to forget to add a new option to the key if we would extend otps. PassPipelineOptions should have a print that prints all the options, so that we'll be able to use it as a key, what do you think?
There was a problem hiding this comment.
Added GPUPipelineOptions::addCacheKeyOpts()
2af0f0c to
0ced049
Compare
5f5cb99 to
f119d97
Compare
GpuCompiler::compile() now schedules the compilation on a thread pool and returns a shared_future. The pool size is set by the constructor concurrency argument (0 means all hardware threads) and can be overridden by the GC_THREADS environment variable. The compiler calculates a BLAKE3 key from the source, the target arch, the IGC options, the wait mode, the Graph Compiler and ocloc versions. If the same source compilation is in progress, the pending shared_future is returned. Compiled programs are held weakly in memory and returned immediately on subsequent compilations of the same source. If enabled (GC_FILE_CACHE_ON), the compiled binaries are also stored in a per-key directory of the file cache (GC_CACHE_DIR or the user cache directory), optionally with a checksum (GC_FILE_CACHE_CHECKSUM). CI enables the file cache, except for the IR dump runs.
f119d97 to
4e68438
Compare
GpuCompiler::compile() now schedules the compilation on a thread pool and returns a shared_future. The pool size is set by the constructor concurrency argument (0 means all hardware threads) and can be overridden by the GC_THREADS environment variable.
The compiler calculates a BLAKE3 key from the source, the target arch, the IGC options, the wait mode, the Graph Compiler and ocloc versions. If the same source compilation is in progress, the pending shared_future is returned. Compiled programs are held weakly in memory and returned immediately on subsequent compilations of the same source.
If enabled (GC_FILE_CACHE_ON), the compiled binaries are also stored in a per-key directory of the file cache (GC_CACHE_DIR or the user cache directory), optionally with a checksum (GC_FILE_CACHE_CHECKSUM). CI enables the file cache, except for the IR dump runs.