Skip to content

Commit 6f4fff3

Browse files
committed
Update on "[executorch] Migrate runtime/kernel to new namespace"
Migrate these headers to the new `::executorch::runtime` namespace. Add temporary aliases from the old `::torch::executor` namespace so we can migrate users incrementally. The tests for these headers are updated in the next diff to help demonstrate that the backwards-compatibility aliases work. Differential Revision: [D60557624](https://our.internmc.facebook.com/intern/diff/D60557624/) [ghstack-poisoned]
2 parents 5cc4cc7 + 08253ae commit 6f4fff3

File tree

199 files changed

+430475
-619
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

199 files changed

+430475
-619
lines changed

.ci/scripts/build-qnn-sdk.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
# Copyright (c) Meta Platforms, Inc. and affiliates.
3+
# All rights reserved.
4+
#
5+
# This source code is licensed under the BSD-style license found in the
6+
# LICENSE file in the root directory of this source tree.
7+
8+
set -ex
9+
10+
build_qnn_backend() {
11+
echo "Start building qnn backend."
12+
export ANDROID_NDK_ROOT=/opt/ndk
13+
export QNN_SDK_ROOT=/tmp/qnn/2.23.0.240531
14+
export EXECUTORCH_ROOT="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"
15+
16+
bash backends/qualcomm/scripts/build.sh --skip_aarch64 --job_number 2 --release
17+
}
18+
19+
build_qnn_backend

.ci/scripts/setup-qnn-deps.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
# Copyright (c) Meta Platforms, Inc. and affiliates.
3+
# All rights reserved.
4+
#
5+
# This source code is licensed under the BSD-style license found in the
6+
# LICENSE file in the root directory of this source tree.
7+
8+
set -ex
9+
10+
install_qnn() {
11+
echo "Start installing qnn."
12+
QNN_INSTALLATION_DIR=/tmp/qnn
13+
mkdir -p "${QNN_INSTALLATION_DIR}"
14+
15+
curl -Lo /tmp/v2.23.0.24.06.24.zip "https://softwarecenter.qualcomm.com/api/download/software/qualcomm_neural_processing_sdk/v2.23.0.24.06.24.zip"
16+
echo "Finishing downloading qnn sdk."
17+
unzip -qo /tmp/v2.23.0.24.06.24.zip -d /tmp
18+
echo "Finishing unzip qnn sdk."
19+
20+
21+
# Print the content for manual verification
22+
ls -lah "/tmp/qairt"
23+
mv "/tmp/qairt"/* "${QNN_INSTALLATION_DIR}"
24+
echo "Finishing installing qnn '${QNN_INSTALLATION_DIR}' ."
25+
26+
ls -lah "${QNN_INSTALLATION_DIR}"
27+
}
28+
29+
install_qnn

.ci/scripts/test_llama.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,25 @@ fi
7272

7373
echo "COREML option ${COREML}"
7474

75+
if [[ "${MODE}" =~ .*qnn.* ]]; then
76+
QNN=ON
77+
export EXECUTORCH_ROOT="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"
78+
export QNN_SDK_ROOT=/tmp/qnn/2.23.0.240531
79+
export LD_LIBRARY_PATH="${QNN_SDK_ROOT}/lib/x86_64-linux-clang"
80+
export PYTHONPATH=".."
81+
cp schema/program.fbs exir/_serialize/program.fbs
82+
cp schema/scalar_type.fbs exir/_serialize/scalar_type.fbs
83+
cp -f build-x86/backends/qualcomm/PyQnnManagerAdaptor.cpython-310-x86_64-linux-gnu.so backends/qualcomm/python
84+
cp -f build-x86/backends/qualcomm/PyQnnWrapperAdaptor.cpython-310-x86_64-linux-gnu.so backends/qualcomm/python
85+
86+
else
87+
QNN=OFF
88+
QNN_SDK_ROOT=""
89+
fi
90+
91+
echo "QNN option ${QNN}"
92+
echo "QNN_SDK_ROOT: ${QNN_SDK_ROOT}"
93+
7594
if [[ -z "${BUCK:-}" ]]; then
7695
BUCK=buck2
7796
fi
@@ -96,6 +115,8 @@ cmake_install_executorch_libraries() {
96115
-DEXECUTORCH_BUILD_XNNPACK="$XNNPACK" \
97116
-DEXECUTORCH_BUILD_MPS="$MPS" \
98117
-DEXECUTORCH_BUILD_COREML="$COREML" \
118+
-DEXECUTORCH_BUILD_QNN="$QNN" \
119+
-DQNN_SDK_ROOT="$QNN_SDK_ROOT" \
99120
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \
100121
-Bcmake-out .
101122
cmake --build cmake-out -j9 --target install --config Debug
@@ -176,6 +197,9 @@ fi
176197
if [[ "${COREML}" == "ON" ]]; then
177198
EXPORT_ARGS="${EXPORT_ARGS} -kv -v --coreml --disable_dynamic_shape"
178199
fi
200+
if [[ "${QNN}" == "ON" ]]; then
201+
EXPORT_ARGS="${EXPORT_ARGS} -kv -v --qnn --disable_dynamic_shape"
202+
fi
179203
# Add dynamically linked library location
180204
$PYTHON_EXECUTABLE -m examples.models.llama2.export_llama ${EXPORT_ARGS}
181205

.github/workflows/android-perf.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ on:
3434
description: The test spec to drive the test on AWS devices
3535
required: false
3636
type: string
37-
default: https://ossci-android.s3.amazonaws.com/executorch/android-llm-device-farm-test-spec.yml
3837
workflow_call:
3938
inputs:
4039
models:
@@ -65,7 +64,6 @@ on:
6564
description: The test spec to drive the test on AWS devices
6665
required: false
6766
type: string
68-
default: https://ossci-android.s3.amazonaws.com/executorch/android-llm-device-farm-test-spec.yml
6967

7068
concurrency:
7169
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref_name }}-${{ github.ref_type == 'branch' && github.sha }}-${{ github.event_name == 'workflow_dispatch' }}-${{ github.event_name == 'schedule' }}
@@ -268,6 +266,7 @@ jobs:
268266
# TODO: Hard code llm_demo_bpe for now in this job.
269267
android-app-archive: https://gha-artifacts.s3.amazonaws.com/${{ github.repository }}/${{ github.run_id }}/artifact/llm_demo_bpe/app-debug.apk
270268
android-test-archive: https://gha-artifacts.s3.amazonaws.com/${{ github.repository }}/${{ github.run_id }}/artifact/llm_demo_bpe/app-debug-androidTest.apk
271-
test-spec: ${{ inputs.test_spec }}
269+
# NB: Need to set the default spec here so that it works for periodic too
270+
test-spec: ${{ inputs.test_spec || 'https://ossci-android.s3.amazonaws.com/executorch/android-llm-device-farm-test-spec.yml' }}
272271
# Uploaded to S3 from the previous job
273272
extra-data: https://gha-artifacts.s3.amazonaws.com/${{ github.repository }}/${{ github.run_id }}/artifact/${{ matrix.model }}_${{ matrix.delegate }}/model.zip

.github/workflows/pull.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,3 +369,38 @@ jobs:
369369
370370
# Run pytest with coverage
371371
pytest -c /dev/null -v -n auto --cov=./ --cov-report=xml backends/arm/test
372+
373+
374+
test-llama-runner-qnn-linux:
375+
name: test-llama-runner-qnn-linux
376+
uses: pytorch/test-infra/.github/workflows/linux_job.yml@main
377+
strategy:
378+
matrix:
379+
dtype: [fp32]
380+
build-tool: [cmake]
381+
mode: [qnn]
382+
fail-fast: false
383+
with:
384+
runner: linux.2xlarge
385+
docker-image: executorch-ubuntu-22.04-clang12-android
386+
submodules: 'true'
387+
ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}
388+
timeout: 900
389+
script: |
390+
# The generic Linux job chooses to use base env, not the one setup by the image
391+
CONDA_ENV=$(conda env list --json | jq -r ".envs | .[-1]")
392+
conda activate "${CONDA_ENV}"
393+
394+
DTYPE=${{ matrix.dtype }}
395+
BUILD_TOOL=${{ matrix.build-tool }}
396+
MODE=${{ matrix.mode }}
397+
398+
PYTHON_EXECUTABLE=python bash .ci/scripts/setup-qnn-deps.sh
399+
PYTHON_EXECUTABLE=python bash .ci/scripts/build-qnn-sdk.sh
400+
401+
# Setup executorch
402+
PYTHON_EXECUTABLE=python bash .ci/scripts/setup-linux.sh buck2
403+
# Install requirements for export_llama
404+
PYTHON_EXECUTABLE=python bash examples/models/llama2/install_requirements.sh
405+
# Test llama2
406+
PYTHON_EXECUTABLE=python bash .ci/scripts/test_llama.sh stories110M.pt "${BUILD_TOOL}" "${DTYPE}" "${MODE}"

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ option(EXECUTORCH_BUILD_GTESTS "Build googletest based test binaries" OFF)
179179

180180
option(EXECUTORCH_BUILD_MPS "Build the MPS backend" OFF)
181181

182+
option(EXECUTORCH_BUILD_NEURON "Build the backends/mediatek directory" OFF)
183+
182184
option(EXECUTORCH_BUILD_PYBIND "Build the Python Bindings" OFF)
183185

184186
option(EXECUTORCH_BUILD_QNN "Build the Qualcomm backend" OFF)
@@ -624,6 +626,10 @@ if(EXECUTORCH_BUILD_EXTENSION_MODULE)
624626
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/module)
625627
endif()
626628

629+
if(EXECUTORCH_BUILD_NEURON)
630+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/mediatek)
631+
endif()
632+
627633
if(EXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL)
628634
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/runner_util)
629635
endif()

CONTRIBUTING.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ We actively welcome your pull requests (PRs).
2323
See the [testing section](#testing) for more information.
2424
1. If you've changed APIs or added a new tool or feature, [update the
2525
documentation](#updating-documentation).
26+
1. If you added an experimental API or deprecated an existing API, follow the
27+
[API Life Cycle and Deprecation Policy](/docs/source/api-life-cycle.md).
2628
1. Make sure your code follows the [style guides](#coding-style) and passes the
2729
[lint checks](#lintrunner).
2830
1. If you haven't already, complete the [Contributor License Agreement ("CLA")](#contributor-license-agreement-cla).

LICENSE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Copyright (c) Meta Platforms, Inc. and affiliates.
66
Copyright 2023 Arm Limited and/or its affiliates.
77
Copyright (c) Qualcomm Innovation Center, Inc.
88
Copyright (c) 2023 Apple Inc.
9+
Copyright (c) 2024 MediaTek Inc.
910

1011
Redistribution and use in source and binary forms, with or without modification,
1112
are permitted provided that the following conditions are met:

backends/apple/coreml/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ if(NOT EXECUTORCH_ROOT)
1313
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
1414
endif()
1515

16+
if(EXECUTORCH_BUILD_SDK)
17+
# protobuf requires frtti
18+
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -frtti" )
19+
endif()
20+
1621
option(COREML_BUILD_EXECUTOR_RUNNER "Build CoreML executor runner." OFF)
1722

1823
# inmemoryfs sources
@@ -59,6 +64,7 @@ set(SDK_SOURCES
5964
runtime/sdk/ETCoreMLModelAnalyzer.mm
6065
runtime/sdk/ETCoreMLModelStructurePath.mm
6166
runtime/sdk/ETCoreMLOperationProfilingInfo.mm
67+
runtime/sdk/ETCoreMLModelDebugInfo.mm
6268
runtime/sdk/ETCoreMLModelDebugger.mm
6369
runtime/sdk/ETCoreMLModelProfiler.mm
6470
runtime/sdk/ETCoreMLPair.mm
@@ -141,6 +147,8 @@ if(EXECUTORCH_BUILD_SDK)
141147
add_subdirectory(
142148
${CMAKE_CURRENT_SOURCE_DIR}/third-party/coremltools/deps/protobuf/cmake
143149
)
150+
151+
target_link_options_shared_lib(libprotobuf-lite)
144152
target_link_libraries(coremldelegate PRIVATE libprotobuf-lite)
145153
endif()
146154

backends/apple/coreml/runtime/delegate/ETCoreMLModelManager.mm

Lines changed: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,37 @@
55
//
66
// Please refer to the license found in the LICENSE file in the root directory of the source tree.
77

8-
#import <ETCoreMLAsset.h>
9-
#import <ETCoreMLAssetManager.h>
10-
#import <ETCoreMLDefaultModelExecutor.h>
11-
#import <ETCoreMLLogging.h>
12-
#import <ETCoreMLModel.h>
13-
#import <ETCoreMLModelCompiler.h>
14-
#import <ETCoreMLModelExecutor.h>
15-
#import <ETCoreMLModelLoader.h>
16-
#import <ETCoreMLModelManager.h>
17-
#import <ETCoreMLStrings.h>
18-
#import <MLModel_Prewarm.h>
19-
#import <MLMultiArray_Copy.h>
8+
#import "ETCoreMLAsset.h"
9+
#import "ETCoreMLAssetManager.h"
10+
#import "ETCoreMLDefaultModelExecutor.h"
11+
#import "ETCoreMLLogging.h"
12+
#import "ETCoreMLModel.h"
13+
#import "ETCoreMLModelCompiler.h"
14+
#import "ETCoreMLModelExecutor.h"
15+
#import "ETCoreMLModelLoader.h"
16+
#import "ETCoreMLModelManager.h"
17+
#import "ETCoreMLStrings.h"
18+
#import "MLModel_Prewarm.h"
19+
#import "MLMultiArray_Copy.h"
2020
#import <filesystem>
21-
#import <inmemory_filesystem_utils.hpp>
21+
#import "inmemory_filesystem_utils.hpp"
2222
#import <iostream>
2323
#import <memory>
24-
#import <model_metadata.h>
25-
#import <multiarray.h>
26-
#import <objc_array_util.h>
24+
#import "model_metadata.h"
25+
#import "multiarray.h"
26+
#import "objc_array_util.h"
2727
#import <optional>
2828
#import <os/lock.h>
29-
#import <serde_json.h>
29+
#import "serde_json.h"
3030
#import <string>
3131
#import <system_error>
3232
#import <vector>
3333

3434
#if ET_EVENT_TRACER_ENABLED
35-
#import <ETCoreMLModelAnalyzer.h>
36-
#import <ETCoreMLModelStructurePath.h>
37-
#import <objc_safe_cast.h>
35+
#import "ETCoreMLModelAnalyzer.h"
36+
#import "ETCoreMLModelDebugInfo.h"
37+
#import "ETCoreMLModelStructurePath.h"
38+
#import "objc_safe_cast.h"
3839
#endif
3940

4041
namespace {
@@ -317,31 +318,14 @@ void add_compute_unit(std::string& identifier, MLComputeUnits compute_units) {
317318
return [[ETCoreMLAsset alloc] initWithBackingAsset:std::move(backingAsset.value())];
318319
}
319320

320-
NSDictionary<ETCoreMLModelStructurePath *, NSString *> * _Nullable get_operation_path_to_symbol_name_map(const inmemoryfs::InMemoryFileSystem *inMemoryFS,
321-
NSError * __autoreleasing *error) {
321+
ETCoreMLModelDebugInfo * _Nullable get_model_debug_info(const inmemoryfs::InMemoryFileSystem *inMemoryFS,
322+
NSError * __autoreleasing *error) {
322323
NSData *file_data = get_file_data(inMemoryFS, ETCoreMLStrings.debugInfoFileRelativePath);
323324
if (!file_data) {
324325
return nil;
325326
}
326-
327-
id object = [NSJSONSerialization JSONObjectWithData:file_data options:(NSJSONReadingOptions)0 error:error];
328-
if (!object) {
329-
return nil;
330-
}
331-
332-
NSDictionary<NSString *, id> *json_dict = SAFE_CAST(object, NSDictionary);
333-
NSMutableDictionary<ETCoreMLModelStructurePath *, NSString *> *result = [NSMutableDictionary dictionaryWithCapacity:json_dict.count];
334-
NSDictionary<NSString *, NSArray<id> *> *debug_symbol_to_operation_path_map = SAFE_CAST(json_dict[ETCoreMLStrings.debugSymbolToOperationPathKeyName], NSDictionary);
335-
for (NSString *symbol_name in debug_symbol_to_operation_path_map) {
336-
NSArray<NSDictionary<NSString *, id> *> *components = SAFE_CAST(debug_symbol_to_operation_path_map[symbol_name], NSArray);
337-
if (components.count == 0) {
338-
continue;
339-
}
340-
ETCoreMLModelStructurePath *path = [[ETCoreMLModelStructurePath alloc] initWithComponents:components];
341-
result[path] = symbol_name;
342-
}
343-
344-
return result;
327+
328+
return [ETCoreMLModelDebugInfo modelDebugInfoFromData:file_data error:error];
345329
}
346330

347331
#endif
@@ -490,16 +474,16 @@ - (nullable NSURL *)compiledModelURLWithIdentifier:(NSString *)identifier
490474
}
491475

492476
NSError *localError = nil;
493-
NSDictionary<ETCoreMLModelStructurePath *, NSString *> *operation_path_to_symbol_name_map = get_operation_path_to_symbol_name_map(inMemoryFS,
494-
&localError);
477+
ETCoreMLModelDebugInfo *debug_info = get_model_debug_info(inMemoryFS, &localError);
495478
if (localError) {
496479
ETCoreMLLogError(localError, "Failed to parse debug info file");
497480
}
498481

482+
499483
return [[ETCoreMLModelAnalyzer alloc] initWithCompiledModelAsset:compiledModelAsset
500484
modelAsset:modelAsset
485+
modelDebugInfo:debug_info
501486
metadata:metadata
502-
operationPathToDebugSymbolMap:operation_path_to_symbol_name_map
503487
configuration:configuration
504488
assetManager:self.assetManager
505489
error:error];

backends/apple/coreml/runtime/delegate/ETCoreMLStrings.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ NS_ASSUME_NONNULL_BEGIN
6666
@property (class, copy, readonly, nonatomic, nullable) NSString* debugInfoFileRelativePath;
6767
/// The debug symbol to operation path key name.
6868
@property (class, copy, readonly, nonatomic, nullable) NSString* debugSymbolToOperationPathKeyName;
69+
/// The debug symbol to handles key name.
70+
@property (class, copy, readonly, nonatomic, nullable) NSString* debugSymbolToHandlesKeyName;
6971

7072
@end
7173

backends/apple/coreml/runtime/delegate/ETCoreMLStrings.mm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ + (NSString *)debugSymbolToOperationPathKeyName {
9595
return ETCoreMLDebugSymbolToOperationPathKeyName;
9696
}
9797

98+
+ (NSString *)debugSymbolToHandlesKeyName {
99+
static NSString * const ETCoreMLDebugSymbolToHandlesKeyName = @"debugSymbolToHandles";
100+
return ETCoreMLDebugSymbolToHandlesKeyName;
101+
}
102+
98103
+ (nullable NSString *)assetsDirectoryPath {
99104
static dispatch_once_t onceToken;
100105
static NSString *result = nil;

backends/apple/coreml/runtime/delegate/coreml_backend_delegate.mm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ ModelLoggingOptions get_logging_options(BackendExecutionContext& context) {
124124
auto event_tracer = context.event_tracer();
125125
if (event_tracer) {
126126
options.log_profiling_info = true;
127-
options.log_intermediate_tensors = event_tracer->intermediate_outputs_logging_status();
127+
auto debug_level = event_tracer->event_tracer_debug_level();
128+
options.log_intermediate_tensors = (debug_level >= EventTracerDebugLogLevel::kIntermediateOutputs);
128129
}
129130

130131
return options;

backends/apple/coreml/runtime/delegate/model_event_logger.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ class ModelEventLogger {
3434
///
3535
/// @param op_path_to_value_map A dictionary with the operation path as the key and the operation's value as the
3636
/// value.
37-
/// @param op_path_to_debug_symbol_name_map A dictionary with the operation path as the key and the symbol name as
38-
/// the value. The symbol name is the delegate handle.
37+
/// @param op_path_to_debug_symbol_name_map A dictionary with the operation path as the key and the debug symbol
38+
/// name as the value.
3939
virtual void log_intermediate_tensors(
4040
NSDictionary<ETCoreMLModelStructurePath*, MLMultiArray*>* op_path_to_value_map,
4141
NSDictionary<ETCoreMLModelStructurePath*, NSString*>* op_path_to_debug_symbol_name_map) const noexcept = 0;

0 commit comments

Comments
 (0)