Skip to content

Commit 118ce2a

Browse files
authored
Enable xnnpack in aten mode
Differential Revision: D70704202 Pull Request resolved: #9049
1 parent 14586f9 commit 118ce2a

File tree

7 files changed

+63
-52
lines changed

7 files changed

+63
-52
lines changed

backends/xnnpack/runtime/XNNCompiler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ namespace backends {
2323
namespace xnnpack {
2424
namespace delegate {
2525

26+
using executorch::ET_RUNTIME_NAMESPACE::NamedDataMap;
2627
using executorch::runtime::Error;
2728
using executorch::runtime::FreeableBuffer;
2829
using executorch::runtime::MemoryAllocator;
29-
using executorch::runtime::NamedDataMap;
3030
using executorch::runtime::Result;
3131

3232
/*

backends/xnnpack/runtime/XNNExecutor.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace delegate {
1616
using executorch::aten::ScalarType;
1717
using executorch::aten::SizesType;
1818
using executorch::aten::Tensor;
19-
using executorch::runtime::BackendExecutionContext;
19+
using executorch::ET_RUNTIME_NAMESPACE::BackendExecutionContext;
2020
using executorch::runtime::Error;
2121
using executorch::runtime::EValue;
2222
using executorch::runtime::is_contiguous_dim_order;
@@ -95,11 +95,19 @@ ET_NODISCARD Error XNNExecutor::prepare_args(EValue** args) {
9595
Tensor* tensor = &args[ext_id]->toTensor();
9696
externals_[i].data = tensor->mutable_data_ptr<float>();
9797

98+
executorch::aten::DimOrderType dim_order[kTensorDimensionLimit];
99+
98100
// Reshape runtime inputs
99101
if (i < input_ids_.size()) {
100102
size_t num_dims = tensor->dim();
103+
Error err =
104+
ET_RUNTIME_NAMESPACE::get_dim_order(*tensor, dim_order, num_dims);
105+
ET_CHECK_OR_RETURN_ERROR(
106+
err == Error::Ok,
107+
Internal,
108+
"Failed to retrieve dim order from tensor!");
101109
ET_CHECK_OR_RETURN_ERROR(
102-
is_contiguous_dim_order(tensor->dim_order().data(), tensor->dim()),
110+
is_contiguous_dim_order(dim_order, tensor->dim()),
103111
Internal,
104112
"Expecting default dim_order but got a non default dim_order tensor for external input %u",
105113
i);
@@ -220,7 +228,7 @@ ET_NODISCARD Error XNNExecutor::resize_outputs(EValue** args) const {
220228
expected_output_size, static_cast<size_t>(num_dim)};
221229

222230
ET_LOG(Debug, "Resizing output tensor to a new shape");
223-
Error err = resize_tensor(*out_tensor, output_size);
231+
Error err = ET_RUNTIME_NAMESPACE::resize_tensor(*out_tensor, output_size);
224232
if (err != Error::Ok) {
225233
ET_LOG(Error, "Failed to resize output tensor for XNNExecutor");
226234
return err;

backends/xnnpack/runtime/XNNExecutor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class XNNExecutor {
7575
* Executes the graph using the args prepared at prepare_args().
7676
*/
7777
ET_NODISCARD executorch::runtime::Error forward(
78-
executorch::runtime::BackendExecutionContext& context);
78+
executorch::ET_RUNTIME_NAMESPACE::BackendExecutionContext& context);
7979

8080
/**
8181
* Prepares the outputs to be returned by the delegate

backends/xnnpack/runtime/XNNPACKBackend.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,20 @@ namespace executorch {
2222
namespace backends {
2323

2424
using executorch::backends::xnnpack::delegate::XNNWeightsCache;
25+
using executorch::ET_RUNTIME_NAMESPACE::Backend;
26+
using executorch::ET_RUNTIME_NAMESPACE::BackendExecutionContext;
27+
using executorch::ET_RUNTIME_NAMESPACE::BackendInitContext;
28+
using executorch::ET_RUNTIME_NAMESPACE::CompileSpec;
29+
using executorch::ET_RUNTIME_NAMESPACE::DelegateHandle;
30+
using executorch::ET_RUNTIME_NAMESPACE::NamedDataMap;
2531
using executorch::runtime::ArrayRef;
26-
using executorch::runtime::Backend;
27-
using executorch::runtime::BackendExecutionContext;
28-
using executorch::runtime::BackendInitContext;
29-
using executorch::runtime::CompileSpec;
30-
using executorch::runtime::DelegateHandle;
3132
using executorch::runtime::Error;
3233
using executorch::runtime::EValue;
3334
using executorch::runtime::FreeableBuffer;
34-
using executorch::runtime::NamedDataMap;
3535
using executorch::runtime::Result;
3636

37-
class XnnpackBackend final : public ::executorch::runtime::BackendInterface {
37+
class XnnpackBackend final
38+
: public ::executorch::ET_RUNTIME_NAMESPACE::BackendInterface {
3839
public:
3940
~XnnpackBackend() = default;
4041

backends/xnnpack/runtime/XNNWeightsCache.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ namespace backends {
1919
namespace xnnpack {
2020
namespace delegate {
2121

22+
using executorch::ET_RUNTIME_NAMESPACE::NamedDataMap;
2223
using executorch::runtime::MemoryAllocator;
23-
using executorch::runtime::NamedDataMap;
2424

2525
XNNWeightsCache::XNNWeightsCache() {
2626
weights_cache_.context = this;

backends/xnnpack/runtime/XNNWeightsCache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ namespace backends {
2323
namespace xnnpack {
2424
namespace delegate {
2525

26+
using executorch::ET_RUNTIME_NAMESPACE::NamedDataMap;
2627
using executorch::runtime::Error;
2728
using executorch::runtime::FreeableBuffer;
2829
using executorch::runtime::MemoryAllocator;
29-
using executorch::runtime::NamedDataMap;
3030
using executorch::runtime::Result;
3131

3232
struct PackedDataMeta {

backends/xnnpack/targets.bzl

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
load("@fbsource//xplat/executorch/backends/xnnpack/third-party:third_party_libs.bzl", "third_party_dep")
2-
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")
2+
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "get_aten_mode_options", "runtime")
33

44
def _get_preprocessor_flags():
55
"""
@@ -33,40 +33,42 @@ def define_common_targets():
3333
],
3434
)
3535

36-
runtime.cxx_library(
37-
name = "xnnpack_backend",
38-
srcs = native.glob([
39-
"runtime/*.cpp",
40-
"runtime/profiling/*.cpp",
41-
]),
42-
headers = native.glob([
43-
"runtime/*.h",
44-
"runtime/profiling/*.h",
45-
]),
46-
visibility = [
47-
"//executorch/exir/backend:backend_lib",
48-
"//executorch/exir/backend/test/...",
49-
"//executorch/backends/xnnpack/test/...",
50-
"//executorch/extension/pybindings/...",
51-
"@EXECUTORCH_CLIENTS",
52-
],
53-
preprocessor_flags = [
54-
# Uncomment to enable per operator timings
55-
# "-DENABLE_XNNPACK_PROFILING",
56-
# Uncomment to enable using KleidiAI Kernels
57-
# "-DENABLE_XNNPACK_KLEIDI"
58-
] + _get_preprocessor_flags(),
59-
exported_deps = [
60-
"//executorch/runtime/backend:interface",
61-
],
62-
deps = [
63-
third_party_dep("XNNPACK"),
64-
"//executorch/backends/xnnpack/serialization:xnnpack_flatbuffer_header",
65-
"//executorch/extension/threadpool:threadpool",
66-
"//executorch/runtime/core/exec_aten/util:tensor_util",
67-
"//executorch/runtime/executor:pte_data_map"
68-
],
69-
# XnnpackBackend.cpp needs to compile with executor as whole
70-
# @lint-ignore BUCKLINT: Avoid `link_whole=True` (https://fburl.com/avoid-link-whole)
71-
link_whole = True,
72-
)
36+
for aten_mode in get_aten_mode_options():
37+
aten_suffix = "_aten" if aten_mode else ""
38+
runtime.cxx_library(
39+
name = "xnnpack_backend" + aten_suffix,
40+
srcs = native.glob([
41+
"runtime/*.cpp",
42+
"runtime/profiling/*.cpp",
43+
]),
44+
headers = native.glob([
45+
"runtime/*.h",
46+
"runtime/profiling/*.h",
47+
]),
48+
visibility = [
49+
"//executorch/exir/backend:backend_lib",
50+
"//executorch/exir/backend/test/...",
51+
"//executorch/backends/xnnpack/test/...",
52+
"//executorch/extension/pybindings/...",
53+
"@EXECUTORCH_CLIENTS",
54+
],
55+
preprocessor_flags = [
56+
# Uncomment to enable per operator timings
57+
# "-DENABLE_XNNPACK_PROFILING",
58+
# Uncomment to enable using KleidiAI Kernels
59+
# "-DENABLE_XNNPACK_KLEIDI"
60+
] + _get_preprocessor_flags(),
61+
exported_deps = [
62+
"//executorch/runtime/backend:interface" + aten_suffix,
63+
],
64+
deps = [
65+
third_party_dep("XNNPACK"),
66+
"//executorch/backends/xnnpack/serialization:xnnpack_flatbuffer_header",
67+
"//executorch/extension/threadpool:threadpool",
68+
"//executorch/runtime/core/exec_aten/util:tensor_util" + aten_suffix,
69+
"//executorch/runtime/executor:pte_data_map" + aten_suffix,
70+
],
71+
# XnnpackBackend.cpp needs to compile with executor as whole
72+
# @lint-ignore BUCKLINT: Avoid `link_whole=True` (https://fburl.com/avoid-link-whole)
73+
link_whole = True,
74+
)

0 commit comments

Comments
 (0)