Skip to content

Commit 001657b

Browse files
committed
Update on "[XNNPACK][Weights Cache] Enable in XNNPACK"
We enable the XNNPACK Weights cache in XNNPACK. the weights cache is initialized for the runtime with the named data map and a memory allocator (for now the memory allocator is not used, but i hope in the future this can be used to managed the memory for packed weights). Before Creating the runtime, we first initialize the weights cache, this sets the finalization state to false. As we add weight/bias tensors to the graph, we load them through the named data map in the weights cache, and keep a map of the pointer to the name. When XNNPACK Creates the runtime and packs the weights, it uses the weights_cache method look_up_or_insert. We use the pointers provided in the cache key to look up their names and append them together like ("weightsbias"). We then insert the packed weights with that key. In future look ups, we just use the pointer cached at the named pack tensor key, saving us from packing in the future. After creating the runtime and packing the weights, we finalize the cache. This sets is_finalized to true. We also free all unpacked buffers loaded from the named data map as they are no longer needed. We also keep reference counts for all the packed weights incrementing the packed weights which were used by this runtime. We return a vector of all the packed weight names to the xnn_executor runner. When the XNNExecutor is destroyed, we decrement the counts of the packed buffers and destroy them if necessary. Note that this feature is gated behind the XNN_ENABLE_WEIGHTS_CACHE flag. Since the weights_cache is a global member of the singleton xnnpack backend class, and it is also read/write, we add a mutex to ensure that access to the weights_cache is thread safe. We added a new mutex, so the mutex hiearchy is: workspace_mutex_ -> weights_cache_mutex_ Differential Revision: [D70885926](https://our.internmc.facebook.com/intern/diff/D70885926/) [ghstack-poisoned]
2 parents a26a962 + 81f1b94 commit 001657b

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

backends/xnnpack/test/runtime/test_xnn_weights_cache.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,10 @@ TEST_F(XNNWeightsCacheTest, ReusePackedWeights) {
278278
weight_cache.delete_packed_data(weight_cache.get_packed_data_names());
279279
std::vector<std::string> packed_data_names =
280280
weight_cache.get_packed_data_names();
281-
// check packed data names have been deleted
281+
// Packed Data Still exists because it has a ref count of 2
282+
ASSERT_EQ(packed_data_names.size(), 1);
283+
weight_cache.delete_packed_data(weight_cache.get_packed_data_names());
284+
packed_data_names =
285+
weight_cache.get_packed_data_names();
282286
ASSERT_EQ(packed_data_names.size(), 0);
283287
}

schema/targets.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ def define_common_targets():
7878
# //executorch/runtime/executor/...
7979
"//executorch/codegen/tools/...",
8080
"//executorch/runtime/executor/...",
81+
# Tests have a set up which uses raw flatbuffer.
82+
# TODO will refactor these setup steps into
83+
# testing utils in runtime/executor/... path
84+
"//executorch/backends/xnnpack/test/...",
8185
],
8286
exported_headers = {
8387
OUTPUT_PROGRAM_HEADER: ":{}[{}]".format(PROGRAM_GEN_RULE_NAME, OUTPUT_PROGRAM_HEADER),

0 commit comments

Comments
 (0)