Skip to content

Commit 5a4bc45

Browse files
authored
Drop cache_loaded_ flag; gate init on metadata.empty() (pytorch#20245)
Differential Revision: D108178967 Pull Request resolved: pytorch#20245
1 parent 56ad81b commit 5a4bc45

2 files changed

Lines changed: 11 additions & 16 deletions

File tree

backends/xnnpack/runtime/XNNWeightsCache.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,19 @@ Error XNNWeightsCache::initialize_for_runtime(
141141
return Error::Ok;
142142
}
143143

144-
// Already loaded earlier this session; just reopen the write fd that
145-
// save_packed_index() closed. Subsequent reserve_space can extend the
146-
// file for any entries not in the saved trailer.
147-
if (cache_loaded_) {
144+
// Entries already in memory (from a prior load_packed_cache or a prior
145+
// fresh-write session). Just reopen the write fd that save_packed_index
146+
// closed; subsequent reserve_space can extend the file. Using metadata
147+
// emptiness (not a separate flag) as the gate avoids a latent bug
148+
// where fresh-write→save→re-init re-enters load_packed_cache and
149+
// double-mmaps the same file.
150+
if (!name_to_packed_data_metadata_.empty()) {
148151
packed_file_fd_ = open_locked(packed_cache_path_, O_RDWR);
149152
return Error::Ok;
150153
}
151154

152-
// First init for this path: try to load the saved trailer; on success
153-
// open a write fd for any new entries. If load fails, fall through to
155+
// No in-memory entries: try to load the saved trailer; on success open
156+
// a write fd for any new entries. If load fails, fall through to
154157
// fresh-write below.
155158
if (load_packed_cache()) {
156159
ET_LOG(
@@ -280,7 +283,6 @@ void XNNWeightsCache::full_unload() {
280283
packed_data_ptrs_.clear();
281284
ptr_to_file_offset_.clear();
282285
file_ptr_to_region_index_.clear();
283-
cache_loaded_ = false;
284286
if (packed_file_fd_ >= 0) {
285287
close(packed_file_fd_);
286288
packed_file_fd_ = -1;
@@ -721,7 +723,6 @@ bool XNNWeightsCache::load_packed_cache() {
721723
name_to_packed_data_metadata_[name] = meta;
722724
}
723725

724-
cache_loaded_ = true;
725726
packed_file_used_ = index_start;
726727
// In-memory state matches the on-disk trailer; the next save would be
727728
// a no-op. Initialize watermark so save_packed_index short-circuits.

backends/xnnpack/runtime/XNNWeightsCache.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@ struct PackedDataMeta {
3939
bool in_current_runtime{};
4040
// True if this entry's bytes are persisted in the on-disk cache file
4141
// (either originally loaded via load_packed_cache, or freshly packed
42-
// and then save_packed_index-ed). Used by delete_packed_data to
43-
// detect when all persistent entries are gone, at which point
44-
// cache_loaded_ is auto-invalidated so the next init re-enters
45-
// load_packed_cache and reuses the saved file instead of re-packing.
42+
// and then save_packed_index-ed). delete_packed_data preserves these
43+
// entries so the next init reuses the saved file instead of re-packing.
4644
bool from_load{false};
4745
// Per-ukernel seed from xnn_weights_cache_look_up_key.seed. XNNPACK
4846
// guarantees this is consistent across runs of the same ukernel; when
@@ -195,10 +193,6 @@ class XNNWeightsCache {
195193
std::string packed_cache_path_;
196194
int packed_file_fd_{-1};
197195
size_t packed_file_used_{0};
198-
// True once load_packed_cache() has populated metadata from a saved
199-
// index, OR once a fresh-write session has been persisted to disk via
200-
// save_packed_index() (so subsequent inits can load from it).
201-
bool cache_loaded_{false};
202196
// Tracks file offset of each file-backed allocation. Used by
203197
// save_packed_index() to serialize (name → offset, size) index.
204198
std::unordered_map<void*, size_t> ptr_to_file_offset_;

0 commit comments

Comments
 (0)