Skip to content

Commit f73507b

Browse files
qukhanxnnpack-bot
authored andcommitted
Add a new fingerprinting method for XNNPack weight cache.
Warning: this new fingerprinting method is currently experimental. Every operation in XNNPack that uses the weight cache should implement a corresponding fingerprinting function. The goal of that fingerprint is to check whether the pre-computation (packing) that takes place for this op has changed and if a previous weight cache can be reused directly or not. The expected way for this to work is that a fingerprint function would assume a predetermined set of constant inputs passed to the create function and would then hash the resulting cache buffer. The `xnn_weights_cache_look_up_key` has a new field that holds the fingerprint identifier. It is set by kernel setup functions when they try to do cache look-ups. PiperOrigin-RevId: 809079455
1 parent 3399dd2 commit f73507b

File tree

14 files changed

+763
-406
lines changed

14 files changed

+763
-406
lines changed

BUILD.bazel

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ xnnpack_cc_library(
140140
"include/xnnpack.h",
141141
],
142142
deps = [
143+
":fingerprint_id",
143144
"@pthreadpool",
144145
],
145146
)
@@ -773,6 +774,50 @@ xnnpack_cc_library(
773774
],
774775
)
775776

777+
xnnpack_cc_library(
778+
name = "fingerprint_id",
779+
hdrs = ["src/operators/fingerprint_id.h"],
780+
)
781+
782+
xnnpack_cc_library(
783+
name = "fingerprint_cache",
784+
srcs = ["src/operators/fingerprint_cache.c"],
785+
hdrs = ["src/operators/fingerprint_cache.h"],
786+
deps = [
787+
":allocator",
788+
":cache",
789+
":fingerprint_id",
790+
":init_once",
791+
":mutex",
792+
":operator_delete",
793+
":xnnpack_h",
794+
],
795+
)
796+
797+
xnnpack_cc_library(
798+
name = "fingerprint_check",
799+
srcs = ["src/xnnpack/fingerprint_check.c"],
800+
deps = [
801+
":fingerprint_cache",
802+
":fingerprint_id",
803+
":operators",
804+
":xnnpack_h",
805+
],
806+
)
807+
808+
xnnpack_cc_library(
809+
name = "operator_delete",
810+
srcs = ["src/operator-delete.c"],
811+
deps = [
812+
":allocator",
813+
":logging",
814+
":operator_h",
815+
":operator_utils",
816+
":params",
817+
":xnnpack_h",
818+
],
819+
)
820+
776821
xnnpack_cc_library(
777822
name = "operators",
778823
srcs = OPERATOR_SRCS,
@@ -804,6 +849,7 @@ xnnpack_cc_library(
804849
":microparams_init",
805850
":node_type",
806851
":normalization",
852+
":operator_delete",
807853
":operator_type",
808854
":operator_utils",
809855
":pack_lh",
@@ -932,6 +978,7 @@ xnnpack_cc_library(
932978
"//:allocator",
933979
"//:build_identifier", # build_cleaner: keep
934980
"//:common", # build_cleaner: keep
981+
"//:fingerprint_id",
935982
"//:init_once",
936983
"//:logging",
937984
"//:math", # build_cleaner: keep

CMakeLists.txt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,9 @@ IF(XNNPACK_BUILD_LIBRARY)
939939
ADD_LIBRARY(xnnpack-allocator OBJECT src/allocator.c)
940940
ADD_LIBRARY(xnnpack-cache OBJECT src/cache.c)
941941
ADD_LIBRARY(xnnpack-datatype OBJECT src/datatype.c)
942+
ADD_LIBRARY(xnnpack-operator-delete OBJECT src/operator-delete.c)
943+
ADD_LIBRARY(xnnpack-fingerprint-cache OBJECT src/operators/fingerprint_cache.c)
944+
ADD_LIBRARY(xnnpack-fingerprint-check OBJECT src/xnnpack/fingerprint_check.c)
942945
ADD_LIBRARY(xnnpack-memory OBJECT src/memory.c)
943946
ADD_LIBRARY(xnnpack-microkernel-utils OBJECT src/microkernel-utils.c)
944947
ADD_LIBRARY(xnnpack-mutex OBJECT src/mutex.c)
@@ -962,21 +965,25 @@ IF(XNNPACK_BUILD_LIBRARY)
962965
TARGET_LINK_LIBRARIES(xnnpack-allocator PRIVATE xnnpack-base xnnpack-logging)
963966
TARGET_LINK_LIBRARIES(xnnpack-cache PRIVATE xnnpack-base xnnpack-logging)
964967
TARGET_LINK_LIBRARIES(xnnpack-datatype PRIVATE xnnpack-base)
968+
TARGET_LINK_LIBRARIES(xnnpack-operator-delete PRIVATE xnnpack-base xnnpack-logging)
965969
TARGET_LINK_LIBRARIES(xnnpack-memory PRIVATE xnnpack-base xnnpack-logging)
966970
TARGET_LINK_LIBRARIES(xnnpack-microkernel-utils PRIVATE xnnpack-base xnnpack-hardware-config xnnpack-logging)
967971
TARGET_LINK_LIBRARIES(xnnpack-mutex PRIVATE xnnpack-base xnnpack-logging)
968972
TARGET_LINK_LIBRARIES(xnnpack-operators PRIVATE xnnpack-base xnnpack-allocator xnnpack-indirection
969973
xnnpack-logging xnnpack-microkernel-utils xnnpack-normalization xnnpack-operator-utils xnnpack-pack-lh xnnpack-packing
970974
xnnpack-reference-ukernels xnnpack-datatype)
975+
TARGET_LINK_LIBRARIES(xnnpack-fingerprint-cache PRIVATE xnnpack-base xnnpack-allocator xnnpack-cache xnnpack-mutex xnnpack-operator-delete)
976+
TARGET_LINK_LIBRARIES(xnnpack-fingerprint-check PRIVATE xnnpack-base xnnpack-fingerprint-cache xnnpack-operators)
971977
TARGET_LINK_LIBRARIES(xnnpack-operator-run PRIVATE xnnpack-base xnnpack-logging)
972978
TARGET_LINK_LIBRARIES(xnnpack-operator-utils PRIVATE xnnpack-base xnnpack-logging)
973979
TARGET_LINK_LIBRARIES(xnnpack-reference-ukernels PRIVATE xnnpack-base xnnpack-datatype)
974980
TARGET_LINK_LIBRARIES(xnnpack-subgraph PRIVATE xnnpack-base xnnpack-allocator xnnpack-logging xnnpack-memory xnnpack-mutex xnnpack-operators xnnpack-operator-run xnnpack-datatype)
975981
TARGET_LINK_LIBRARIES(XNNPACK PRIVATE xnnpack-base xnnpack-allocator xnnpack-cache
976982
xnnpack-hardware-config xnnpack-indirection xnnpack-memory xnnpack-microkernel-utils xnnpack-microparams-init
977-
xnnpack-mutex xnnpack-normalization xnnpack-operators xnnpack-operator-run xnnpack-operator-utils xnnpack-pack-lh xnnpack-packing
978-
xnnpack-microkernels-prod xnnpack-subgraph xnnpack-datatype xnnpack-reference-ukernels)
979-
TARGET_LINK_LIBRARIES(XNNPACK PUBLIC pthreadpool xnnpack-logging)
983+
xnnpack-mutex xnnpack-normalization xnnpack-operators xnnpack-operator-run
984+
xnnpack-operator-utils xnnpack-pack-lh xnnpack-packing xnnpack-fingerprint-cache xnnpack-microkernels-prod
985+
xnnpack-subgraph xnnpack-datatype xnnpack-reference-ukernels)
986+
TARGET_LINK_LIBRARIES(XNNPACK PUBLIC pthreadpool xnnpack-logging xnnpack-fingerprint-check)
980987
SET_TARGET_PROPERTIES(XNNPACK PROPERTIES C_EXTENSIONS YES)
981988
ENDIF()
982989
IF(NOT MSVC)

build_srcs.bzl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ Lists of target-specific sources used to build XNNPACK.
1212
"""
1313

1414
OPERATOR_SRCS = [
15-
"src/operator-delete.c",
1615
"src/operator-run.c",
1716
"src/operators/argmax-pooling-nhwc.c",
1817
"src/operators/average-pooling-nhwc.c",

include/experimental.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <stdint.h>
1717

1818
#include "include/xnnpack.h"
19+
#include "src/operators/fingerprint_id.h"
1920

2021
#ifdef __cplusplus
2122
extern "C" {
@@ -98,6 +99,29 @@ enum xnn_status xnn_update_runtime_with_threadpool(
9899
xnn_runtime_t runtime,
99100
xnn_threadpool_t threadpool);
100101

102+
103+
typedef struct xnn_fingerprint* xnn_fingerprint_t;
104+
105+
struct xnn_fingerprint {
106+
enum xnn_fingerprint_id id;
107+
uint32_t value;
108+
};
109+
110+
/// Check whether the given configuration matches one that is currently in use.
111+
///
112+
/// @returns True if the configuration matches.
113+
bool xnn_check_fingerprint(struct xnn_fingerprint fingerprint);
114+
115+
/// Return the fingerprint corresponding to the given id or NULL if it wasn't
116+
/// set.
117+
const struct xnn_fingerprint* xnn_get_fingerprint(enum xnn_fingerprint_id id);
118+
119+
/// Set the given fingerprint.
120+
void xnn_set_fingerprint(struct xnn_fingerprint fingerprint);
121+
122+
/// Clear all fingerprints that were computed until now.
123+
void xnn_clear_fingerprints();
124+
101125
#ifdef __cplusplus
102126
} // extern "C"
103127
#endif

include/xnnpack.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Facebook, Inc. and its affiliates.
22
// All rights reserved.
33
//
4-
// Copyright 2019 Google LLC
4+
// Copyright 2019-2025 Google LLC
55
//
66
// This source code is licensed under the BSD-style license found in the
77
// LICENSE file in the root directory of this source tree.
@@ -15,6 +15,7 @@
1515
#include <stddef.h>
1616
#include <stdint.h>
1717

18+
#include "src/operators/fingerprint_id.h"
1819
#include <pthreadpool.h>
1920

2021
#ifdef __cplusplus
@@ -2297,6 +2298,8 @@ struct xnn_weights_cache_look_up_key {
22972298
const void* kernel;
22982299
/// Pointer to the original bias, could be NULL.
22992300
const void* bias;
2301+
2302+
enum xnn_fingerprint_id fingerprint_id;
23002303
};
23012304

23022305
/// A group of function pointers to manage weights cache. All functions may be

0 commit comments

Comments
 (0)