From ba2ff63835d1d04c8b20674522fd8749d6e074f9 Mon Sep 17 00:00:00 2001 From: derekxu Date: Thu, 15 Aug 2024 11:52:55 -0700 Subject: [PATCH] Add QnnBackend dependency to the ET main test binary app in buck for Android OS Differential Revision: D61314481 Pull Request resolved: https://github.com/pytorch/executorch/pull/4732 --- backends/qualcomm/TARGETS | 6 +- backends/qualcomm/aot/ir/TARGETS | 5 ++ backends/qualcomm/aot/ir/targets.bzl | 66 +++++++++++++++++++++ backends/qualcomm/aot/wrappers/TARGETS | 5 ++ backends/qualcomm/aot/wrappers/targets.bzl | 32 +++++++++++ backends/qualcomm/runtime/TARGETS | 5 ++ backends/qualcomm/runtime/targets.bzl | 67 ++++++++++++++++++++++ backends/qualcomm/targets.bzl | 67 ++++++++++++++++++++++ examples/models/llama2/runner/targets.bzl | 1 + 9 files changed, 253 insertions(+), 1 deletion(-) create mode 100644 backends/qualcomm/aot/ir/TARGETS create mode 100644 backends/qualcomm/aot/ir/targets.bzl create mode 100644 backends/qualcomm/aot/wrappers/TARGETS create mode 100644 backends/qualcomm/aot/wrappers/targets.bzl create mode 100644 backends/qualcomm/runtime/TARGETS create mode 100644 backends/qualcomm/runtime/targets.bzl diff --git a/backends/qualcomm/TARGETS b/backends/qualcomm/TARGETS index 5c4f482b5e..0a42614a38 100644 --- a/backends/qualcomm/TARGETS +++ b/backends/qualcomm/TARGETS @@ -1 +1,5 @@ -# This file needs to exist to avoid build system breakage, see https://fburl.com/workplace/jtdlgdmd +load(":targets.bzl", "define_common_targets") + +oncall("executorch") + +define_common_targets() diff --git a/backends/qualcomm/aot/ir/TARGETS b/backends/qualcomm/aot/ir/TARGETS new file mode 100644 index 0000000000..0a42614a38 --- /dev/null +++ b/backends/qualcomm/aot/ir/TARGETS @@ -0,0 +1,5 @@ +load(":targets.bzl", "define_common_targets") + +oncall("executorch") + +define_common_targets() diff --git a/backends/qualcomm/aot/ir/targets.bzl b/backends/qualcomm/aot/ir/targets.bzl new file mode 100644 index 0000000000..a7cc5c03e2 --- /dev/null +++ b/backends/qualcomm/aot/ir/targets.bzl @@ -0,0 +1,66 @@ +load( + "@fbsource//tools/build_defs:default_platform_defs.bzl", + "ANDROID", +) +load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime") +load("@fbsource//xplat/executorch/backends/qualcomm:targets.bzl", "generate_schema_header") + +QCIR_NAME = "qcir" +INPUT_QCIR = QCIR_NAME + ".fbs" +OUTPUT_QCIR_HEADER = QCIR_NAME + "_generated.h" +QCIR_GEN_RULE_NAME = "qcir_generated" + +def define_common_targets(): + """Defines targets that should be shared between fbcode and xplat. + The directory containing this targets.bzl file should also contain both + TARGETS and BUCK files that call this function. + """ + + generate_schema_header( + QCIR_GEN_RULE_NAME, + [INPUT_QCIR], + [OUTPUT_QCIR_HEADER], + OUTPUT_QCIR_HEADER, + ) + + # Header-only library target with the generate executorch program schema header. + runtime.cxx_library( + name = "qcir_schema", + srcs = [], + exported_headers = { + OUTPUT_QCIR_HEADER: ":{}[{}]".format(QCIR_GEN_RULE_NAME, OUTPUT_QCIR_HEADER), + }, + visibility = [ + # Lock this down as tightly as possible to ensure that flatbuffers + # are an implementation detail. Ideally this list would only include + # //executorch/runtime/executor/... + "//executorch/backends/qualcomm/...", + "//executorch/backends/qualcomm/aot/ir/...", + ], + exported_external_deps = ["flatbuffers-api"], + define_static_target = True, + platforms = [ANDROID], + ) + + + runtime.cxx_library( + name = "qcir_utils", + srcs = [ + "qcir_utils.cpp", + ], + exported_headers = [ + "qcir_utils.h", + ], + define_static_target = True, + platforms = [ANDROID], + visibility = ["@EXECUTORCH_CLIENTS"], + deps = [ + "fbsource//third-party/qualcomm/qnn:api", + "//executorch/runtime/backend:interface", + "//executorch/runtime/core:core", + "//executorch/backends/qualcomm/aot/wrappers:wrappers", + ], + exported_deps = [ + ":qcir_schema", + ], + ) diff --git a/backends/qualcomm/aot/wrappers/TARGETS b/backends/qualcomm/aot/wrappers/TARGETS new file mode 100644 index 0000000000..0a42614a38 --- /dev/null +++ b/backends/qualcomm/aot/wrappers/TARGETS @@ -0,0 +1,5 @@ +load(":targets.bzl", "define_common_targets") + +oncall("executorch") + +define_common_targets() diff --git a/backends/qualcomm/aot/wrappers/targets.bzl b/backends/qualcomm/aot/wrappers/targets.bzl new file mode 100644 index 0000000000..08d6920a02 --- /dev/null +++ b/backends/qualcomm/aot/wrappers/targets.bzl @@ -0,0 +1,32 @@ +load( + "@fbsource//tools/build_defs:default_platform_defs.bzl", + "ANDROID", +) +load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime") + +def define_common_targets(): + """Defines targets that should be shared between fbcode and xplat. + + The directory containing this targets.bzl file should also contain both + TARGETS and BUCK files that call this function. + """ + runtime.cxx_library( + name = "wrappers", + srcs = glob([ + "*.cpp", + ]), + exported_headers = glob([ + "*.h", + ]), + define_static_target = True, + platforms = [ANDROID], + visibility = ["@EXECUTORCH_CLIENTS"], + deps = [ + "fbsource//third-party/qualcomm/qnn:api", + "//executorch/runtime/backend:interface", + "//executorch/runtime/core:core", + ], + exported_deps = [ + "//executorch/backends/qualcomm/runtime:logging", + ], + ) diff --git a/backends/qualcomm/runtime/TARGETS b/backends/qualcomm/runtime/TARGETS new file mode 100644 index 0000000000..0a42614a38 --- /dev/null +++ b/backends/qualcomm/runtime/TARGETS @@ -0,0 +1,5 @@ +load(":targets.bzl", "define_common_targets") + +oncall("executorch") + +define_common_targets() diff --git a/backends/qualcomm/runtime/targets.bzl b/backends/qualcomm/runtime/targets.bzl new file mode 100644 index 0000000000..77449e95e2 --- /dev/null +++ b/backends/qualcomm/runtime/targets.bzl @@ -0,0 +1,67 @@ +load( + "@fbsource//tools/build_defs:default_platform_defs.bzl", + "ANDROID", +) +load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime") + +def define_common_targets(): + """Defines targets that should be shared between fbcode and xplat. + + The directory containing this targets.bzl file should also contain both + TARGETS and BUCK files that call this function. + """ + + runtime.cxx_library( + name = "logging", + srcs = [ + "Logging.cpp", + ], + exported_headers = [ + "Logging.h", + ], + define_static_target = True, + platforms = [ANDROID], + visibility = ["@EXECUTORCH_CLIENTS"], + deps = [ + "fbsource//third-party/qualcomm/qnn:api", + "//executorch/runtime/backend:interface", + "//executorch/runtime/core:core", + ], + exported_deps = [ + "//executorch/backends/qualcomm:schema", + ], + ) + + runtime.cxx_library( + name = "runtime", + srcs = glob( + [ + "*.cpp", + "backends/*.cpp", + "backends/htpbackend/*.cpp", + "backends/htpbackend/aarch64/*.cpp", + ], + exclude = ["Logging.cpp"], + ), + exported_headers = glob( + [ + "*.h", + "backends/*.h", + "backends/htpbackend/*.h", + ], + exclude = ["Logging.h"], + ), + define_static_target = True, + link_whole = True, # needed for executorch/examples/models/llama2:main to register QnnBackend + platforms = [ANDROID], + visibility = ["@EXECUTORCH_CLIENTS"], + deps = [ + "fbsource//third-party/qualcomm/qnn:api", + ":logging", + "//executorch/backends/qualcomm:schema", + "//executorch/backends/qualcomm/aot/ir:qcir_utils", + "//executorch/backends/qualcomm/aot/wrappers:wrappers", + "//executorch/runtime/backend:interface", + "//executorch/runtime/core:core", + ], + ) diff --git a/backends/qualcomm/targets.bzl b/backends/qualcomm/targets.bzl index cb89bb24ef..55fe390f6b 100644 --- a/backends/qualcomm/targets.bzl +++ b/backends/qualcomm/targets.bzl @@ -1,6 +1,21 @@ +load( + "@fbsource//tools/build_defs:default_platform_defs.bzl", + "ANDROID", +) load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime") +# Construct the input and output file names. All input and output files rely on scalar_type file. +SCHEMA_NAME = "schema" + +INPUT_SCHEMA = "serialization/" + SCHEMA_NAME + ".fbs" + +OUTPUT_SCHEMA_HEADER = SCHEMA_NAME + "_generated.h" + +SCHEMA_GEN_RULE_NAME = "schema_generated" + +SCHEMA_LIRRARY_NAME = SCHEMA_NAME + def generate_schema_header(rule_name, srcs, headers, default_header): """Generate header file given flatbuffer schema """ @@ -26,3 +41,55 @@ def generate_schema_header(rule_name, srcs, headers, default_header): ]), visibility = [], # Private ) + +def define_common_targets(): + """Defines targets that should be shared between fbcode and xplat. + + The directory containing this targets.bzl file should also contain both + TARGETS and BUCK files that call this function. + """ + + generate_schema_header( + SCHEMA_GEN_RULE_NAME, + [INPUT_SCHEMA], + [OUTPUT_SCHEMA_HEADER], + OUTPUT_SCHEMA_HEADER, + ) + # Header-only library target with the generate executorch program schema header. + runtime.cxx_library( + name = "schema", + srcs = [], + visibility = [ + # Lock this down as tightly as possible to ensure that flatbuffers + # are an implementation detail. Ideally this list would only include + # //executorch/runtime/executor/... + "//executorch/codegen/tools/...", + "//executorch/runtime/executor/...", + "//executorch/backends/qualcomm/...", + "//executorch/backends/qualcomm/runtime/...", + ], + exported_headers = { + OUTPUT_SCHEMA_HEADER: ":{}[{}]".format(SCHEMA_GEN_RULE_NAME, OUTPUT_SCHEMA_HEADER), + }, + exported_external_deps = ["flatbuffers-api"], + define_static_target = True, + platforms = [ANDROID], + ) + + + runtime.cxx_library( + name = "qnn_executorch_backend", + srcs = [], + headers = [], + define_static_target = True, + visibility = ["@EXECUTORCH_CLIENTS"], + deps = [ + "fbsource//third-party/qualcomm/qnn:api", + "//executorch/runtime/backend:interface", + "//executorch/runtime/core:core", + "//executorch/backends/qualcomm/runtime:runtime", + ], + exported_deps = [ + ":schema", + ], + ) diff --git a/examples/models/llama2/runner/targets.bzl b/examples/models/llama2/runner/targets.bzl index 5d8269c744..3ffc10421f 100644 --- a/examples/models/llama2/runner/targets.bzl +++ b/examples/models/llama2/runner/targets.bzl @@ -30,6 +30,7 @@ def define_common_targets(): visibility = [ "@EXECUTORCH_CLIENTS", ], + # qnn_executorch_backend can be added below //executorch/backends/qualcomm:qnn_executorch_backend exported_deps = [ "//executorch/backends/xnnpack:xnnpack_backend", "//executorch/extension/llm/runner:stats",