Skip to content

Commit 792ef43

Browse files
pytorchbotjorgep31415
andauthored
[ET-VK] Move save_cache from Runtime dtor to model destroy (#7002)
Pull Request resolved: pytorch/executorch#6974 ## Issue `Runtime` is a local static variable. Hence we'd expect the Runtime dtor to be called on program exit. But on Android devices it's not being invoked. This behavior is different than that seen 6 months ago (D57085281). It's unclear what changed. This means the cache is not saved due to the following chain never being invoked. `~Runtime()` > `~Adapter()` > `~ComputePipelineCache()` > `save_cache()`. ## Solution Move cache saving to `VulkanBackend.cpp`'s model destroy. This makes sense since the cache is tied to the model and not the runtime. ## Resources https://medium.com/@martin00001313/mastering-static-objects-in-c-initialization-destruction-and-best-practices-760b17734195 ghstack-source-id: 254701488 @exported-using-ghexport Differential Revision: [D66179917](https://our.internmc.facebook.com/intern/diff/D66179917/) --------- Co-authored-by: jorgep31415 <[email protected]>
1 parent 0dc25e5 commit 792ef43

File tree

3 files changed

+6
-3
lines changed

3 files changed

+6
-3
lines changed

backends/vulkan/runtime/VulkanBackend.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,10 @@ class VulkanBackend final : public ::executorch::runtime::BackendInterface {
621621
void destroy(DelegateHandle* handle) const override {
622622
if (handle != nullptr) {
623623
ComputeGraph* compute_graph = static_cast<ComputeGraph*>(handle);
624+
compute_graph->context()
625+
->adapter_ptr()
626+
->compute_pipeline_cache()
627+
.save_cache();
624628
// ComputeGraph is not trivially destructible. Since
625629
// this was constructed manually in init(), we must destroy it manually
626630
// here.

backends/vulkan/runtime/vk_api/Pipeline.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,6 @@ ComputePipelineCache::~ComputePipelineCache() {
406406
return;
407407
}
408408

409-
save_cache();
410-
411409
vkDestroyPipelineCache(device_, pipeline_cache_, nullptr);
412410
pipeline_cache_ = VK_NULL_HANDLE;
413411
}

backends/vulkan/runtime/vk_api/Pipeline.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,10 @@ class ComputePipelineCache final {
269269
}
270270
};
271271

272+
void save_cache();
273+
272274
private:
273275
std::vector<char> load_cache();
274-
void save_cache();
275276

276277
// Multiple threads could potentially be adding entries into the cache, so use
277278
// a mutex to manage access

0 commit comments

Comments
 (0)