Skip to content

Commit 7153f19

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 aa8e0d1 + f7194af commit 7153f19

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

backends/xnnpack/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ option(EXECUTORCH_XNNPACK_SHARED_WORKSPACE
4141
# Keeping this OFF by default due to regressions in decode and model load with
4242
# kleidi kernels
4343
option(EXECUTORCH_XNNPACK_ENABLE_KLEIDI "Enable Arm Kleidi kernels" OFF)
44+
45+
# Turning this on cache weights between partitions and methods. If weights
46+
# are shared across methods/partitions then this can reduce load time and
47+
# memory usage
48+
49+
# Keeping this off maintains existing behavior. Turning this on serializes
50+
# execution and initialization of delegates, to be revisited
51+
option(EXECUTORCH_XNNPACK_ENABLE_WEIGHT_CACHE
52+
"Enable weights cache to cache and manage all packed weights" OFF)
53+
54+
if (EXECUTORCH_XNNPACK_ENABLE_WEIGHT_CACHE)
55+
add_definitions(-DENABLE_XNNPACK_WEIGHTS_CACHE)
56+
endif()
4457
if(EXECUTORCH_XNNPACK_SHARED_WORKSPACE)
4558
add_definitions(-DENABLE_XNNPACK_SHARED_WORKSPACE)
4659
endif()

backends/xnnpack/runtime/XNNPACKBackend.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,6 @@ class XnnpackBackend final : public ::executorch::runtime::BackendInterface {
8383
}
8484

8585
const NamedDataMap* named_data_map = context.get_named_data_map();
86-
weights_cache_->initialize_for_runtime(
87-
context.get_runtime_allocator(),
88-
named_data_map
89-
);
90-
91-
// This is needed to serialize access to xnn_create_runtime which is not
9286
// thread safe. This can heppen when multiple threads call init() on
9387
// the same backend instance.
9488
#ifdef ENABLE_XNNPACK_SHARED_WORKSPACE
@@ -98,7 +92,9 @@ class XnnpackBackend final : public ::executorch::runtime::BackendInterface {
9892
#ifdef ENABLE_XNNPACK_WEIGHTS_CACHE
9993
const std::lock_guard<std::mutex> lock(weights_cache_mutex_);
10094
#endif
101-
95+
weights_cache_->initialize_for_runtime(
96+
context.get_runtime_allocator(),
97+
named_data_map);
10298

10399
// Executor has been allocated but not constructed, ensure that runtime_ is
104100
// nullptr by constructing it in place here. NOTE: Since we use placement

backends/xnnpack/targets.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ def define_common_targets():
5757
# "-DENABLE_XNNPACK_KLEIDI"
5858
] + _get_preprocessor_flags(),
5959
exported_deps = [
60-
third_party_dep("XNNPACK"),
6160
"//executorch/runtime/backend:interface",
6261
],
6362
deps = [
63+
third_party_dep("XNNPACK"),
6464
"//executorch/backends/xnnpack/serialization:xnnpack_flatbuffer_header",
6565
"//executorch/extension/threadpool:threadpool",
6666
"//executorch/runtime/core/exec_aten/util:tensor_util",

0 commit comments

Comments
 (0)