Skip to content

Commit a41d36d

Browse files
committed
Update on "[executorch] Migrate extension/test_utils to new namespace"
Migrate these headers to the new `::executorch::extension` namespace. Add temporary aliases from the old `::torch::executor` namespace so we can migrate users incrementally. Differential Revision: [D60938936](https://our.internmc.facebook.com/intern/diff/D60938936/) [ghstack-poisoned]
2 parents 768052d + a2f1bbb commit a41d36d

File tree

236 files changed

+5142
-2485
lines changed

Some content is hidden

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

236 files changed

+5142
-2485
lines changed

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,43 @@
55
# This source code is licensed under the BSD-style license found in the
66
# LICENSE file in the root directory of this source tree.
77

8-
set -ex
8+
set -eux
99

1010
build_qnn_backend() {
1111
echo "Start building qnn backend."
1212
export ANDROID_NDK_ROOT=/opt/ndk
1313
export QNN_SDK_ROOT=/tmp/qnn/2.23.0.240531
14-
export EXECUTORCH_ROOT="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"
14+
export EXECUTORCH_ROOT="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/../.." && pwd)"
1515

1616
bash backends/qualcomm/scripts/build.sh --skip_aarch64 --job_number 2 --release
1717
}
1818

19+
set_up_aot() {
20+
cd $EXECUTORCH_ROOT
21+
if [ ! -d "cmake-out" ]; then
22+
mkdir cmake-out
23+
fi
24+
pushd cmake-out
25+
cmake .. \
26+
-DCMAKE_INSTALL_PREFIX=$PWD \
27+
-DEXECUTORCH_BUILD_QNN=ON \
28+
-DQNN_SDK_ROOT=${QNN_SDK_ROOT} \
29+
-DEXECUTORCH_BUILD_SDK=ON \
30+
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
31+
-DEXECUTORCH_ENABLE_EVENT_TRACER=ON \
32+
-DPYTHON_EXECUTABLE=python3 \
33+
-DEXECUTORCH_SEPARATE_FLATCC_HOST_PROJECT=OFF
34+
cmake --build $PWD --target "PyQnnManagerAdaptor" "PyQnnWrapperAdaptor" -j$(nproc)
35+
# install Python APIs to correct import path
36+
# The filename might vary depending on your Python and host version.
37+
cp -f backends/qualcomm/PyQnnManagerAdaptor.cpython-310-x86_64-linux-gnu.so $EXECUTORCH_ROOT/backends/qualcomm/python
38+
cp -f backends/qualcomm/PyQnnWrapperAdaptor.cpython-310-x86_64-linux-gnu.so $EXECUTORCH_ROOT/backends/qualcomm/python
39+
popd
40+
41+
# Workaround for fbs files in exir/_serialize
42+
cp schema/program.fbs exir/_serialize/program.fbs
43+
cp schema/scalar_type.fbs exir/_serialize/scalar_type.fbs
44+
}
45+
1946
build_qnn_backend
47+
set_up_aot

.ci/scripts/test.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,25 @@ if [[ -z "${BACKEND:-}" ]]; then
2828
exit 1
2929
fi
3030

31+
UPLOAD_DIR=${4:-}
32+
33+
if [[ -z "${PYTHON_EXECUTABLE:-}" ]]; then
34+
PYTHON_EXECUTABLE=python3
35+
fi
3136
which "${PYTHON_EXECUTABLE}"
37+
3238
# Just set this variable here, it's cheap even if we use buck2
3339
CMAKE_OUTPUT_DIR=cmake-out
40+
EXPORTED_MODEL=${MODEL_NAME}
41+
42+
prepare_artifacts_upload() {
43+
if [ -n "$UPLOAD_DIR" ]; then
44+
echo "Preparing for uploading generated artifacs"
45+
zip -j model.zip "${EXPORTED_MODEL}"
46+
mkdir -p "${UPLOAD_DIR}"
47+
mv model.zip "${UPLOAD_DIR}"
48+
fi
49+
}
3450

3551
build_cmake_executor_runner() {
3652
echo "Building executor_runner"
@@ -114,6 +130,7 @@ test_model_with_xnnpack() {
114130
fi
115131

116132
OUTPUT_MODEL_PATH="${MODEL_NAME}_xnnpack_${SUFFIX}.pte"
133+
EXPORTED_MODEL=${OUTPUT_MODEL_PATH}
117134

118135
# Run test model
119136
if [[ "${BUILD_TOOL}" == "buck2" ]]; then
@@ -129,9 +146,36 @@ test_model_with_xnnpack() {
129146
fi
130147
}
131148

149+
test_model_with_qnn() {
150+
source "$(dirname "${BASH_SOURCE[0]}")/build-qnn-sdk.sh"
151+
echo "ANDROID_NDK_ROOT: $ANDROID_NDK_ROOT"
152+
echo "QNN_SDK_ROOT: $QNN_SDK_ROOT"
153+
echo "EXECUTORCH_ROOT: $EXECUTORCH_ROOT"
154+
155+
export LD_LIBRARY_PATH=$QNN_SDK_ROOT/lib/x86_64-linux-clang/
156+
export PYTHONPATH=$EXECUTORCH_ROOT/..
157+
158+
if [[ "${MODEL_NAME}" == "dl3" ]]; then
159+
"${PYTHON_EXECUTABLE}" -m examples.qualcomm.scripts.deeplab_v3 -b ${CMAKE_OUTPUT_DIR} -m SM8550 --compile_only --download
160+
EXPORTED_MODEL=./deeplab_v3/dlv3_qnn.pte
161+
fi
162+
}
163+
132164
if [[ "${BACKEND}" == "portable" ]]; then
133165
echo "Testing ${MODEL_NAME} with portable kernels..."
134166
test_model
167+
elif [[ "${BACKEND}" == "qnn" ]]; then
168+
echo "Testing ${MODEL_NAME} with qnn..."
169+
test_model_with_qnn
170+
if [[ $? -eq 0 ]]; then
171+
prepare_artifacts_upload
172+
fi
173+
elif [[ "${BACKEND}" == "xnnpack" ]]; then
174+
echo "Testing ${MODEL_NAME} with xnnpack..."
175+
test_model_with_xnnpack true true
176+
if [[ $? -eq 0 ]]; then
177+
prepare_artifacts_upload
178+
fi
135179
else
136180
set +e
137181
if [[ "${BACKEND}" == *"quantization"* ]]; then
@@ -153,5 +197,7 @@ else
153197
if [[ -n "${Q_ERROR:-}" ]] || [[ -n "${D_ERROR:-}" ]] || [[ -n "${Q_D_ERROR:-}" ]]; then
154198
echo "Portable q8 ${Q_ERROR:-ok}," "Delegation fp32 ${D_ERROR:-ok}," "Delegation q8 ${Q_D_ERROR:-ok}"
155199
exit 1
200+
else
201+
prepare_artifacts_upload
156202
fi
157203
fi

.ci/scripts/test_llava.sh

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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 -exu
9+
# shellcheck source=/dev/null
10+
11+
if [[ -z "${PYTHON_EXECUTABLE:-}" ]]; then
12+
PYTHON_EXECUTABLE=python3
13+
fi
14+
15+
cmake_install_executorch_libraries() {
16+
cmake \
17+
-DCMAKE_INSTALL_PREFIX=cmake-out \
18+
-DCMAKE_BUILD_TYPE=Debug \
19+
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
20+
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
21+
-DEXECUTORCH_BUILD_KERNELS_CUSTOM=ON \
22+
-DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON \
23+
-DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON \
24+
-DEXECUTORCH_BUILD_XNNPACK=ON \
25+
-DEXECUTORCH_DO_NOT_USE_CXX11_ABI=ON \
26+
-DEXECUTORCH_XNNPACK_SHARED_WORKSPACE=ON \
27+
-Bcmake-out .
28+
29+
30+
cmake --build cmake-out -j9 --target install --config Debug
31+
}
32+
33+
cmake_build_llava_runner() {
34+
dir=examples/models/llava
35+
python_lib=$($PYTHON_EXECUTABLE -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())')
36+
37+
cmake \
38+
-DCMAKE_INSTALL_PREFIX=cmake-out \
39+
-DCMAKE_BUILD_TYPE=Debug \
40+
-DEXECUTORCH_BUILD_KERNELS_CUSTOM=ON \
41+
-DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON \
42+
-DEXECUTORCH_BUILD_XNNPACK=ON \
43+
-DCMAKE_PREFIX_PATH="$python_lib" \
44+
-Bcmake-out/${dir} \
45+
${dir}
46+
47+
48+
cmake --build cmake-out/${dir} -j9 --config Debug
49+
}
50+
51+
# only export the one without custom op for now since it's
52+
export_llava() {
53+
echo "Starting to export Llava. This will take about 6 mins"
54+
$PYTHON_EXECUTABLE -m executorch.examples.models.llava.export_llava --pte-name llava.pte --with-artifacts
55+
}
56+
57+
run_and_verify() {
58+
NOW=$(date +"%H:%M:%S")
59+
echo "Starting to run llava runner at ${NOW}"
60+
if [[ ! -f "llava.pte" ]]; then
61+
echo "Export failed. Abort"
62+
exit 1
63+
fi
64+
if [[ ! -f "image.pt" ]]; then
65+
echo "image.pt is missing."
66+
exit 1
67+
fi
68+
if [[ ! -f "tokenizer.bin" ]]; then
69+
echo "tokenizer.bin is missing."
70+
exit 1
71+
fi
72+
RUNTIME_ARGS="--model_path=llava.pte \
73+
--tokenizer_path=tokenizer.bin \
74+
--image_path=image.pt \
75+
--prompt=ASSISTANT: \
76+
--temperature=0 \
77+
--seq_len=650"
78+
cmake-out/examples/models/llava/llava_main ${RUNTIME_ARGS} > result.txt
79+
# verify result.txt
80+
RESULT=$(cat result.txt)
81+
# set the expected prefix to be the same as prompt because there's a bug in sdpa_with_kv_cache that causes <unk> tokens.
82+
EXPECTED_PREFIX="ASSISTANT:"
83+
if [[ "${RESULT}" == *"${EXPECTED_PREFIX}"* ]]; then
84+
echo "Expected result prefix: ${EXPECTED_PREFIX}"
85+
echo "Actual result: ${RESULT}"
86+
echo "Success"
87+
exit 0
88+
else
89+
echo "Expected result prefix: ${EXPECTED_PREFIX}"
90+
echo "Actual result: ${RESULT}"
91+
echo "Failure; results not the same"
92+
exit 1
93+
fi
94+
}
95+
96+
cmake_install_executorch_libraries
97+
cmake_build_llava_runner
98+
export_llava
99+
run_and_verify

.github/workflows/android-perf.yml

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,24 +135,39 @@ jobs:
135135
fail-fast: false
136136
with:
137137
runner: linux.2xlarge
138-
docker-image: executorch-ubuntu-22.04-clang12
138+
docker-image: executorch-ubuntu-22.04-clang12-android
139139
submodules: 'true'
140140
timeout: 60
141141
upload-artifact: android-models
142142
script: |
143143
# The generic Linux job chooses to use base env, not the one setup by the image
144+
echo "::group::Setting up dev environment"
144145
CONDA_ENV=$(conda env list --json | jq -r ".envs | .[-1]")
145146
conda activate "${CONDA_ENV}"
146-
147+
if [[ ${{ matrix.delegate }} == "qnn" ]]; then
148+
PYTHON_EXECUTABLE=python bash .ci/scripts/setup-qnn-deps.sh
149+
PYTHON_EXECUTABLE=python bash .ci/scripts/build-qnn-sdk.sh
150+
fi
147151
PYTHON_EXECUTABLE=python bash .ci/scripts/setup-linux.sh "cmake"
148-
echo "Exporting model: ${{ matrix.model }}"
149-
export ARTIFACTS_DIR_NAME=artifacts-to-be-uploaded/${{ matrix.model }}_${{ matrix.delegate }}
152+
ARTIFACTS_DIR_NAME=artifacts-to-be-uploaded/${{ matrix.model }}_${{ matrix.delegate }}
153+
echo "::endgroup::"
150154
151-
# TODO(T197546696): Note that the following scripts/steps only work for llama. It's expected to fail for other models+delegates.
152-
# Install requirements for export_llama
153-
PYTHON_EXECUTABLE=python bash examples/models/llama2/install_requirements.sh
154-
# Test llama2
155-
PYTHON_EXECUTABLE=python bash .ci/scripts/test_llama.sh "${{ matrix.model }}.pt" "cmake" "fp32" "xnnpack+custom+qe" "${ARTIFACTS_DIR_NAME}"\
155+
echo "::group::Exporting ${{ matrix.delegate }} model: ${{ matrix.model }}"
156+
BUILD_MODE="cmake"
157+
DTYPE="fp32"
158+
159+
if [[ ${{ matrix.model }} == "llama*" ]]; then
160+
# Install requirements for export_llama
161+
PYTHON_EXECUTABLE=python bash examples/models/llama2/install_requirements.sh
162+
# Test llama2
163+
if [[ ${{ matrix.delegate }} == "xnnpack" ]]; then
164+
DELEGATE_CONFIG="xnnpack+custom+qe"
165+
fi
166+
PYTHON_EXECUTABLE=python bash .ci/scripts/test_llama.sh "${{ matrix.model }}.pt" "${BUILD_MODE}" "${DTYPE}" "${DELEGATE_CONFIG}" "${ARTIFACTS_DIR_NAME}"
167+
else
168+
PYTHON_EXECUTABLE=python bash .ci/scripts/test.sh "${{ matrix.model }}" "${BUILD_MODE}" "${{ matrix.delegate }}" "${ARTIFACTS_DIR_NAME}"
169+
fi
170+
echo "::endgroup::"
156171
157172
# Upload models to S3. The artifacts are needed not only by the device farm but also TorchChat
158173
upload-models:

.github/workflows/android.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ jobs:
147147

148148
# Let's see how expensive this job is, we might want to tone it down by running it periodically
149149
test-llama-app:
150+
# Only PR from ExecuTorch itself has permission to access AWS, forked PRs will fail to
151+
# authenticate with the cloud service
152+
if: ${{ !github.event.pull_request.head.repo.fork }}
150153
needs: upload-artifacts
151154
permissions:
152155
id-token: write

.github/workflows/pull.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ jobs:
187187
# Test selective build
188188
PYTHON_EXECUTABLE=python bash examples/selective_build/test_selective_build.sh "${BUILD_TOOL}"
189189
190-
test-export-llava-linux:
191-
name: test-export-llava-linux
190+
test-llava-runner-linux:
191+
name: test-llava-runner-linux
192192
uses: pytorch/test-infra/.github/workflows/linux_job.yml@main
193193
strategy:
194194
fail-fast: false
@@ -215,6 +215,9 @@ jobs:
215215
# run python unittest
216216
python -m unittest examples.models.llava.test.test_llava
217217
218+
# run e2e (export, tokenizer and runner)
219+
PYTHON_EXECUTABLE=python bash .ci/scripts/test_llava.sh
220+
218221
test-quantized-aot-lib-linux:
219222
name: test-quantized-aot-lib-linux
220223
uses: pytorch/test-infra/.github/workflows/linux_job.yml@main

.github/workflows/trunk.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,26 @@ jobs:
270270
PYTHON_EXECUTABLE=python ${CONDA_RUN} bash examples/models/llama2/install_requirements.sh
271271
# Test llama2
272272
PYTHON_EXECUTABLE=python ${CONDA_RUN} bash .ci/scripts/test_llama.sh stories110M.pt "${BUILD_TOOL}" "${DTYPE}" "${MODE}"
273+
274+
test-qnn-model:
275+
name: test-qnn-model
276+
uses: pytorch/test-infra/.github/workflows/linux_job.yml@main
277+
strategy:
278+
matrix:
279+
dtype: [fp32]
280+
model: [dl3]
281+
fail-fast: false
282+
with:
283+
runner: linux.2xlarge
284+
docker-image: executorch-ubuntu-22.04-clang12-android
285+
submodules: 'true'
286+
ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}
287+
timeout: 900
288+
script: |
289+
# The generic Linux job chooses to use base env, not the one setup by the image
290+
CONDA_ENV=$(conda env list --json | jq -r ".envs | .[-1]")
291+
conda activate "${CONDA_ENV}"
292+
PYTHON_EXECUTABLE=python bash .ci/scripts/setup-linux.sh cmake
293+
PYTHON_EXECUTABLE=python bash .ci/scripts/setup-qnn-deps.sh
294+
PYTHON_EXECUTABLE=python bash .ci/scripts/build-qnn-sdk.sh
295+
PYTHON_EXECUTABLE=python bash .ci/scripts/test.sh ${{ matrix.model }} "cmake" "qnn"

.github/workflows/update-viablestrict.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ jobs:
2020
with:
2121
repository: pytorch/executorch
2222
stable-branch: viable/strict
23-
requires: '[\"pull\", \"lint\", \"trunk\", \"Build documentation\", \"Android\", \"Apple\"]'
23+
requires: '[\"pull\", \"lint\", \"trunk\", \"Build documentation\", "^Android$", "^Apple$"]'
2424
secret-bot-token: ${{ secrets.UPDATEBOT_TOKEN }}
2525
rockset-api-key: ${{ secrets.ROCKSET_API_KEY }}

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ if(EXECUTORCH_ENABLE_EVENT_TRACER)
130130
add_definitions(-DET_EVENT_TRACER_ENABLED)
131131
endif()
132132

133+
option(EXECUTORCH_DO_NOT_USE_CXX11_ABI "Define _GLIBCXX_USE_CXX11_ABI=0 if ON"
134+
OFF
135+
)
136+
if(EXECUTORCH_DO_NOT_USE_CXX11_ABI)
137+
add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)
138+
endif()
133139
# -ffunction-sections -fdata-sections: breaks function and data into sections so
134140
# they can be properly gc'd. -s: strip symbol. -fno-exceptions -fno-rtti:
135141
# disables exceptions and runtime type.

backends/apple/coreml/runtime/test/CoreMLBackendDelegateTests.mm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525
return [NSData dataWithContentsOfURL:url];
2626
}
2727

28-
class DataLoaderImpl: public DataLoader {
28+
class DataLoaderImpl final : public DataLoader {
2929
public:
3030
DataLoaderImpl(std::string filePath)
3131
:data_(read_data(filePath))
3232
{}
3333

3434
Result<FreeableBuffer> load(
35-
size_t offset, size_t size, __ET_UNUSED const DataLoader::SegmentInfo& segment_info) override {
35+
size_t offset, size_t size, ET_UNUSED const DataLoader::SegmentInfo& segment_info) const override {
3636
NSData *subdata = [data_ subdataWithRange:NSMakeRange(offset, size)];
3737
return FreeableBuffer(subdata.bytes, size, nullptr);
3838
}
@@ -42,7 +42,7 @@
4242
}
4343

4444
private:
45-
NSData *data_;
45+
NSData * const data_;
4646
};
4747

4848
using Buffer = std::vector<uint8_t>;

0 commit comments

Comments
 (0)