From 0cab1406a1b89852bc5023b196777a2730be4783 Mon Sep 17 00:00:00 2001 From: maxren Date: Sat, 19 Aug 2023 17:34:57 -0700 Subject: [PATCH 1/3] Fix CPUINFO dependency for xnnpack/threadpool Differential Revision: D48335278 fbshipit-source-id: 38e67b7cf0f274cc4cb7e43b401ffa497832712e --- backends/xnnpack/threadpool/targets.bzl | 2 +- .../backends/xnnpack/third-party/third_party_libs.bzl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/backends/xnnpack/threadpool/targets.bzl b/backends/xnnpack/threadpool/targets.bzl index 0c9e26d020d..a0e41422e01 100644 --- a/backends/xnnpack/threadpool/targets.bzl +++ b/backends/xnnpack/threadpool/targets.bzl @@ -27,8 +27,8 @@ def define_common_targets(): exported_headers = _THREADPOOL_HEADERS, exported_deps = [ third_party_dep("pthreadpool"), + third_party_dep("cpuinfo"), ], - external_deps = ["cpuinfo"], exported_preprocessor_flags = [ "-DET_USE_THREADPOOL", ], diff --git a/shim/xplat/executorch/backends/xnnpack/third-party/third_party_libs.bzl b/shim/xplat/executorch/backends/xnnpack/third-party/third_party_libs.bzl index 401264bdc4d..75501ef5b2b 100644 --- a/shim/xplat/executorch/backends/xnnpack/third-party/third_party_libs.bzl +++ b/shim/xplat/executorch/backends/xnnpack/third-party/third_party_libs.bzl @@ -8,7 +8,7 @@ _THIRD_PARTY_LIBS = { "FXdiv": ["//xplat/third-party/FXdiv:FXdiv", "//backends/xnnpack/third-party:FXdiv"], "XNNPACK": ["//xplat/third-party/XNNPACK:XNNPACK", "//backends/xnnpack/third-party:XNNPACK"], "clog": ["//xplat/third-party/clog:clog", "//backends/xnnpack/third-party:clog"], - "cpuinfo": ["//third-party/cpuinfo:cpuinfo", "//backends/xnnpack/third-party:cpuinfo"], + "cpuinfo": ["fbsource//third-party/cpuinfo:cpuinfo", "//backends/xnnpack/third-party:cpuinfo"], "pthreadpool": ["//xplat/third-party/pthreadpool:pthreadpool", "//backends/xnnpack/third-party:pthreadpool"], "pthreadpool_header": ["//xplat/third-party/pthreadpool:pthreadpool_header", "//backends/xnnpack/third-party:pthreadpool_header"], } From 4c1899ff6e36744c36a8dec34d1e180ddf4aaa1d Mon Sep 17 00:00:00 2001 From: maxren Date: Sat, 19 Aug 2023 17:34:57 -0700 Subject: [PATCH 2/3] move qnnpack utils under xnnpack Differential Revision: D48367520 fbshipit-source-id: 10e06c480412b9235ca36503ccbe80f4b4e36604 --- backends/qnnpack/QNNPackBackend.cpp | 2 +- backends/qnnpack/targets.bzl | 21 +------------------ backends/qnnpack/test/TARGETS | 3 --- backends/xnnpack/runtime/XNNExecutor.cpp | 2 +- .../runtime}/utils/utils.cpp | 2 +- .../runtime}/utils/utils.h | 0 backends/xnnpack/targets.bzl | 18 +++++++++++++++- backends/xnnpack/test/TARGETS | 3 +++ .../test/runtime/test_runtime_utils.cpp} | 2 +- .../{qnnpack => xnnpack}/test/targets.bzl | 13 +++--------- 10 files changed, 28 insertions(+), 38 deletions(-) rename backends/{qnnpack => xnnpack/runtime}/utils/utils.cpp (99%) rename backends/{qnnpack => xnnpack/runtime}/utils/utils.h (100%) rename backends/{qnnpack/test/test_utils.cpp => xnnpack/test/runtime/test_runtime_utils.cpp} (98%) rename backends/{qnnpack => xnnpack}/test/targets.bzl (64%) diff --git a/backends/qnnpack/QNNPackBackend.cpp b/backends/qnnpack/QNNPackBackend.cpp index eb33c7fd94d..6206ab80006 100644 --- a/backends/qnnpack/QNNPackBackend.cpp +++ b/backends/qnnpack/QNNPackBackend.cpp @@ -8,7 +8,7 @@ #include #include -#include +#include #include #include #include diff --git a/backends/qnnpack/targets.bzl b/backends/qnnpack/targets.bzl index f0fc478ae8c..c4e63d106d7 100644 --- a/backends/qnnpack/targets.bzl +++ b/backends/qnnpack/targets.bzl @@ -42,25 +42,6 @@ def define_common_targets(): ], ) - for aten_mode in (True, False): - aten_suffix = "_aten" if aten_mode else "" - runtime.cxx_library( - name = "qnnpack_utils" + aten_suffix, - srcs = [ - "utils/utils.cpp", - ], - exported_headers = ["utils/utils.h"], - deps = [ - "//executorch/runtime/core/exec_aten:lib" + aten_suffix, - "//executorch/runtime/backend:backend_registry", - ], - visibility = [ - "//executorch/backends/qnnpack/test/...", - "//executorch/backends/xnnpack/...", - "@EXECUTORCH_CLIENTS", - ], - ) - runtime.cxx_library( name = "qnnpack_backend", srcs = [ @@ -84,6 +65,7 @@ def define_common_targets(): "//executorch/runtime/core/exec_aten/util:tensor_util", "//executorch/runtime/backend:backend_registry", "//executorch/backends/xnnpack/threadpool:threadpool", + "//executorch/backends/xnnpack:dynamic_quant_utils", "//executorch/util:memory_utils", "//{prefix}caffe2/aten/src/ATen/native/quantized/cpu/qnnpack:pytorch_qnnpack".format( prefix = ( @@ -91,7 +73,6 @@ def define_common_targets(): ), ), ":qnnpack_schema", - ":qnnpack_utils", ], platforms = [ ANDROID, diff --git a/backends/qnnpack/test/TARGETS b/backends/qnnpack/test/TARGETS index ee5d1838a95..d0a25fa9ce4 100644 --- a/backends/qnnpack/test/TARGETS +++ b/backends/qnnpack/test/TARGETS @@ -1,7 +1,4 @@ load("@fbcode_macros//build_defs:python_unittest.bzl", "python_unittest") -load(":targets.bzl", "define_common_targets") - -define_common_targets() python_unittest( name = "test_qnnpack", diff --git a/backends/xnnpack/runtime/XNNExecutor.cpp b/backends/xnnpack/runtime/XNNExecutor.cpp index f862021799b..5e39c86c1ba 100644 --- a/backends/xnnpack/runtime/XNNExecutor.cpp +++ b/backends/xnnpack/runtime/XNNExecutor.cpp @@ -6,8 +6,8 @@ * LICENSE file in the root directory of this source tree. */ -#include #include +#include namespace torch { namespace executor { diff --git a/backends/qnnpack/utils/utils.cpp b/backends/xnnpack/runtime/utils/utils.cpp similarity index 99% rename from backends/qnnpack/utils/utils.cpp rename to backends/xnnpack/runtime/utils/utils.cpp index 0c4b30fa11f..2331f4df52e 100644 --- a/backends/qnnpack/utils/utils.cpp +++ b/backends/xnnpack/runtime/utils/utils.cpp @@ -6,7 +6,7 @@ * LICENSE file in the root directory of this source tree. */ -#include +#include #include namespace torch { diff --git a/backends/qnnpack/utils/utils.h b/backends/xnnpack/runtime/utils/utils.h similarity index 100% rename from backends/qnnpack/utils/utils.h rename to backends/xnnpack/runtime/utils/utils.h diff --git a/backends/xnnpack/targets.bzl b/backends/xnnpack/targets.bzl index 571cf4681f0..c4aa62e7f8e 100644 --- a/backends/xnnpack/targets.bzl +++ b/backends/xnnpack/targets.bzl @@ -2,6 +2,22 @@ load("@fbsource//xplat/executorch/backends/xnnpack/third-party:third_party_libs. load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime") def define_common_targets(): + runtime.cxx_library( + name = "dynamic_quant_utils", + srcs = [ + "runtime/utils/utils.cpp", + ], + exported_headers = ["runtime/utils/utils.h"], + deps = [ + "//executorch/runtime/core/exec_aten:lib", + "//executorch/runtime/backend:backend_registry", + ], + visibility = [ + "//executorch/backends/xnnpack/...", + "@EXECUTORCH_CLIENTS", + ], + ) + runtime.genrule( name = "gen_xnnpack_schema", srcs = [ @@ -52,8 +68,8 @@ def define_common_targets(): deps = [ third_party_dep("XNNPACK"), ":xnnpack_schema", + ":dynamic_quant_utils", # TODO Use (1) portable for choose_qparams(), (2) xnnpack for quantize_per_tensor(), "//executorch/runtime/backend:backend_registry", - "//executorch/backends/qnnpack:qnnpack_utils", # TODO Use (1) portable for choose_qparams(), (2) xnnpack for quantize_per_tensor() "//executorch/backends/xnnpack/threadpool:threadpool", "//executorch/util:memory_utils", "//executorch/runtime/core/exec_aten/util:tensor_util", diff --git a/backends/xnnpack/test/TARGETS b/backends/xnnpack/test/TARGETS index d5e4248e69f..6ccc8d4c345 100644 --- a/backends/xnnpack/test/TARGETS +++ b/backends/xnnpack/test/TARGETS @@ -1,4 +1,7 @@ load("@fbcode_macros//build_defs:python_unittest.bzl", "python_unittest") +load(":targets.bzl", "define_common_targets") + +define_common_targets() python_unittest( name = "test_xnnpack", diff --git a/backends/qnnpack/test/test_utils.cpp b/backends/xnnpack/test/runtime/test_runtime_utils.cpp similarity index 98% rename from backends/qnnpack/test/test_utils.cpp rename to backends/xnnpack/test/runtime/test_runtime_utils.cpp index 24818036713..c057b32ecea 100644 --- a/backends/qnnpack/test/test_utils.cpp +++ b/backends/xnnpack/test/runtime/test_runtime_utils.cpp @@ -13,7 +13,7 @@ #include #include #include -#include "executorch/backends/qnnpack/utils/utils.h" +#include "executorch/backends/xnnpack/runtime/utils/utils.h" using namespace ::testing; diff --git a/backends/qnnpack/test/targets.bzl b/backends/xnnpack/test/targets.bzl similarity index 64% rename from backends/qnnpack/test/targets.bzl rename to backends/xnnpack/test/targets.bzl index 720658e038e..b2d94fef631 100644 --- a/backends/qnnpack/test/targets.bzl +++ b/backends/xnnpack/test/targets.bzl @@ -1,26 +1,19 @@ -load( - "@fbsource//tools/build_defs:default_platform_defs.bzl", - "ANDROID", - "APPLE", - "CXX", -) load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime") def define_common_targets(): runtime.cxx_test( - name = "qnnpack_utils_test", - srcs = ["test_utils.cpp"], + name = "dynamic_quant_utils_test", + srcs = ["runtime/test_runtime_utils.cpp"], fbcode_deps = [ "//caffe2:ATen-cpu", ], xplat_deps = [ "//caffe2:aten_cpu", ], - platforms = [ANDROID, APPLE, CXX], deps = [ "//executorch/runtime/core/exec_aten/testing_util:tensor_util", "//executorch/runtime/core/exec_aten/util:scalar_type_util", "//executorch/extension/aten_util:aten_bridge", - "//executorch/backends/qnnpack:qnnpack_utils", + "//executorch/backends/xnnpack:dynamic_quant_utils", ], ) From cc3e95dc1f59dcf422148c65b91900d1fc7cb000 Mon Sep 17 00:00:00 2001 From: Max Ren Date: Sat, 19 Aug 2023 17:35:18 -0700 Subject: [PATCH 3/3] persist constants file (#63) Summary: Pull Request resolved: https://github.com/pytorch/executorch/pull/63 For OSS, we can not have a buck genrule for the python constants file. Instead, we move the gen rule into a bash script Reviewed By: salilsdesai Differential Revision: D48338629 fbshipit-source-id: 8ef5797bbba214a372d46c0e057a95920afac8ba --- backends/xnnpack/utils/TARGETS | 21 +------------ .../xnnpack/utils/gen_xnnpack_constants.sh | 30 +++++++++++++++++++ backends/xnnpack/utils/xnnpack_constants.py | 29 ++++++++++++++++++ 3 files changed, 60 insertions(+), 20 deletions(-) create mode 100644 backends/xnnpack/utils/gen_xnnpack_constants.sh create mode 100644 backends/xnnpack/utils/xnnpack_constants.py diff --git a/backends/xnnpack/utils/TARGETS b/backends/xnnpack/utils/TARGETS index 524caf19909..0152b074df1 100644 --- a/backends/xnnpack/utils/TARGETS +++ b/backends/xnnpack/utils/TARGETS @@ -1,27 +1,8 @@ -load("@fbcode_macros//build_defs:native_rules.bzl", "buck_genrule") load("@fbcode_macros//build_defs:python_library.bzl", "python_library") -buck_genrule( - name = "xnnpack_constants", - srcs = { - "xnnpack.h": "//xplat/third-party/XNNPACK:xnnpack.h", - }, - outs = { - "xnnpack_constants.py": ["xnnpack_constants.py"], - }, - cmd = " && ".join([ - "echo UINT32_MAX = 4294967295 > ${OUT}/xnnpack_constants.py", - "awk '/^#define\\s+XNN_/ { print $2,\"=\",$3} ' xnnpack.h >> ${OUT}/xnnpack_constants.py", - "if ! grep -qc \"^XNN_\" ${OUT}/xnnpack_constants.py; then false; fi", - ]), - visibility = [ - "//executorch/backends/xnnpack/utils:xnnpack_utils", - ], -) - python_library( name = "xnnpack_utils", - srcs = glob(["*.py"]) + [":xnnpack_constants[xnnpack_constants.py]"], + srcs = glob(["*.py"]), deps = [ "//caffe2:torch", "//executorch/exir:lib", diff --git a/backends/xnnpack/utils/gen_xnnpack_constants.sh b/backends/xnnpack/utils/gen_xnnpack_constants.sh new file mode 100644 index 00000000000..6be9d4519f3 --- /dev/null +++ b/backends/xnnpack/utils/gen_xnnpack_constants.sh @@ -0,0 +1,30 @@ +#!/bin/bash +# Copyright (c) Meta Platforms, Inc. and affiliates. +# All rights reserved. +# +# This source code is licensed under the BSD-style license found in the +# LICENSE file in the root directory of this source tree. + +# Usage: +# This script is used to autogenerate constants found in the xnnpack header file +# as constants in python, which is used in xnnpack delegate. This script requires +# the path to the XNNPACK folder as an argument + +# Example: +# bash gen_xnnpack_constants.sh ~/fbsource/xplat/third-party/XNNPACK/XNNPACK + + +# constant's file header +{ + echo "# Copyright (c) Meta Platforms, Inc. and affiliates." + echo "# All rights reserved." + echo "#" + echo "# This source code is licensed under the BSD-style license found in the" + echo "# LICENSE file in the root directory of this source tree." + echo "" + echo "# Auto-generated by gen_xnnpack_constants.sh script. Do not modify" +} > xnnpack_constants.py + +echo UINT32_MAX = 4294967295 >> xnnpack_constants.py +awk '/^#define\s+XNN_/ { print $2,"=",$3} ' "$1"/include/xnnpack.h >> xnnpack_constants.py +if ! grep -qc "^XNN_" xnnpack_constants.py; then false; fi diff --git a/backends/xnnpack/utils/xnnpack_constants.py b/backends/xnnpack/utils/xnnpack_constants.py new file mode 100644 index 00000000000..63c8d6fdeef --- /dev/null +++ b/backends/xnnpack/utils/xnnpack_constants.py @@ -0,0 +1,29 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# All rights reserved. +# +# This source code is licensed under the BSD-style license found in the +# LICENSE file in the root directory of this source tree. + +# Auto-generated by gen_xnnpack_constants.sh script. Do not modify +UINT32_MAX = 4294967295 +XNN_EXTRA_BYTES = 16 +XNN_MAX_TENSOR_DIMS = 6 +XNN_FLAG_SPARSE_INFERENCE = 0x00000001 +XNN_FLAG_HINT_SPARSE_INFERENCE = XNN_FLAG_SPARSE_INFERENCE +XNN_FLAG_FP16_INFERENCE = 0x00000002 +XNN_FLAG_HINT_FP16_INFERENCE = XNN_FLAG_FP16_INFERENCE +XNN_FLAG_FORCE_FP16_INFERENCE = 0x00000004 +XNN_FLAG_BASIC_PROFILING = 0x00000008 +XNN_FLAG_DEPTHWISE_CONVOLUTION = 0x00000001 +XNN_FLAG_TRANSPOSE_WEIGHTS = 0x00000001 +XNN_FLAG_INPUT_NHWC = 0x00000002 +XNN_FLAG_TENSORFLOW_SAME_PADDING = 0x00000004 +XNN_FLAG_TENSORFLOW_RESHAPE_2D = 0x00000004 +XNN_FLAG_TENSORFLOW_LEGACY_MODE = 0x00000004 +XNN_FLAG_FP32_STATIC_WEIGHTS = 0x00000008 +XNN_FLAG_ALIGN_CORNERS = 0x00000008 +XNN_FLAG_YIELD_WORKERS = 0x00000010 +XNN_VALUE_FLAG_EXTERNAL_INPUT = 0x00000001 +XNN_VALUE_FLAG_EXTERNAL_OUTPUT = 0x00000002 +XNN_VALUE_FLAG_PERSISTENT = 0x00000004 +XNN_INVALID_VALUE_ID = UINT32_MAX