Skip to content

Commit 56ac34b

Browse files
qukhanxnnpack-bot
authored andcommitted
Add a graph rewrite to fallback to fp32 when fp16 isn't supported.
PiperOrigin-RevId: 917373652
1 parent f873466 commit 56ac34b

12 files changed

Lines changed: 1170 additions & 3 deletions

File tree

BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,7 @@ xnnpack_cc_library(
904904
":xnnpack_h",
905905
"//src/configs:config_hdrs",
906906
"//src/configs:hardware_config",
907+
"//src/subgraph/rewrites:fp16_to_fp32",
907908
"@pthreadpool",
908909
],
909910
)

BUILD.gn

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,10 @@ xnnpack_source_set("subgraph") {
381381

382382
# From build_srcs.bzl
383383
sources = SUBGRAPH_SRCS
384+
sources += [
385+
"src/subgraph/rewrites/fp16_to_fp32.cc",
386+
"src/subgraph/rewrites/fp16_to_fp32.h",
387+
]
384388
}
385389

386390
xnnpack_source_set("table") {

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,7 @@ SET(SUBGRAPH_SRCS
565565
src/subgraph/max-pooling-2d.c
566566
src/subgraph/pack-lh.c
567567
src/subgraph/reshape-helpers.c
568+
src/subgraph/rewrites/fp16_to_fp32.cc
568569
src/subgraph/rope.c
569570
src/subgraph/softmax.c
570571
src/subgraph/space-to-depth-2d.c
@@ -996,7 +997,7 @@ IF(XNNPACK_BUILD_LIBRARY)
996997
TARGET_LINK_LIBRARIES(xnnpack-operator-run PRIVATE xnnpack-base xnnpack-logging)
997998
TARGET_LINK_LIBRARIES(xnnpack-operator-utils PRIVATE xnnpack-base xnnpack-logging)
998999
TARGET_LINK_LIBRARIES(xnnpack-reference-ukernels PRIVATE xnnpack-base xnnpack-datatype)
999-
TARGET_LINK_LIBRARIES(xnnpack-subgraph PRIVATE xnnpack-base xnnpack-allocator xnnpack-logging xnnpack-memory xnnpack-mutex xnnpack-operators xnnpack-operator-run xnnpack-datatype)
1000+
TARGET_LINK_LIBRARIES(xnnpack-subgraph PRIVATE xnnpack-base xnnpack-allocator xnnpack-cache xnnpack-logging xnnpack-memory xnnpack-mutex xnnpack-operators xnnpack-operator-run xnnpack-datatype)
10001001
TARGET_LINK_LIBRARIES(XNNPACK PRIVATE xnnpack-base xnnpack-allocator xnnpack-cache
10011002
xnnpack-hardware-config xnnpack-indirection xnnpack-memory xnnpack-microkernel-utils xnnpack-microparams-init
10021003
xnnpack-mutex xnnpack-normalization xnnpack-operators xnnpack-operator-run

src/runtime.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include "include/experimental.h"
2424
#include "include/xnnpack.h"
25+
#include "src/subgraph/rewrites/fp16_to_fp32.h"
2526
#include "src/xnnpack/allocation-type.h"
2627
#include "src/xnnpack/allocator.h"
2728
#include "src/xnnpack/cache.h"
@@ -598,6 +599,9 @@ enum xnn_status xnn_create_runtime_v4(
598599
goto error;
599600
}
600601

602+
XNN_IF_ERROR_GOTO(error, xnn_subgraph_alias_fp16_fp32_fallback_data(
603+
subgraph, weights_cache));
604+
601605
status = xnn_status_out_of_memory;
602606

603607
runtime = xnn_allocate_zero_memory(sizeof(struct xnn_runtime));
@@ -1194,7 +1198,7 @@ enum xnn_status xnn_delete_runtime(
11941198
xnn_release_memory(runtime->opdata);
11951199

11961200
if (runtime->values != NULL) {
1197-
// Release the buffers created during FP16 rewrite.
1201+
// Release buffers created during rewrites.
11981202
for (size_t i = 0; i < runtime->num_values; i++) {
11991203
struct xnn_runtime_value* value = &runtime->values[i];
12001204
if (value->allocation_type == xnn_allocation_type_dynamic ||

src/subgraph.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "include/experimental.h"
1717
#include "include/xnnpack.h"
18+
#include "src/subgraph/rewrites/fp16_to_fp32.h"
1819
#include "src/xnnpack/allocation-type.h"
1920
#include "src/xnnpack/allocator.h"
2021
#include "src/xnnpack/common.h"
@@ -4590,6 +4591,10 @@ enum xnn_status xnn_subgraph_optimize(xnn_subgraph_t subgraph,
45904591
XNN_RETURN_IF_ERROR(
45914592
xnn_subgraph_optimize_packed_lhs(subgraph, optimization_flags));
45924593

4594+
if (!xnn_is_f16_supported_natively(hardware_config) && !force_fp16) {
4595+
XNN_RETURN_IF_ERROR(xnn_subgraph_fallback_from_fp16_to_fp32(subgraph, optimization_flags));
4596+
}
4597+
45934598
return xnn_status_success;
45944599
}
45954600

src/subgraph/rewrites/BUILD

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
load(
2+
"//:build_defs.bzl",
3+
"xnnpack_cxx_library",
4+
)
5+
6+
package(default_visibility = ["//:__subpackages__"])
7+
8+
xnnpack_cxx_library(
9+
name = "fp16_to_fp32",
10+
srcs = ["fp16_to_fp32.cc"],
11+
hdrs = ["fp16_to_fp32.h"],
12+
deps = [
13+
"//:allocation_type",
14+
"//:allocator",
15+
"//:cache",
16+
"//:fp16",
17+
"//:internal",
18+
"//:logging",
19+
"//:node_type",
20+
"//:subgraph_h",
21+
"//:xnnpack_h",
22+
],
23+
)

0 commit comments

Comments
 (0)