Skip to content

Commit 838bb6c

Browse files
committed
Enable MPS AOT for macOS by default
1 parent f8a3fd8 commit 838bb6c

File tree

10 files changed

+65
-35
lines changed

10 files changed

+65
-35
lines changed

.ci/scripts/test_model.sh

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ test_model_with_coreml() {
219219
DTYPE=float16
220220

221221
"${PYTHON_EXECUTABLE}" -m examples.apple.coreml.scripts.export --model_name="${MODEL_NAME}" --compute_precision "${DTYPE}" --use_partitioner
222-
EXPORTED_MODEL=$(find "." -type f -name "${MODEL_NAME}*.pte" -print -quit)
222+
EXPORTED_MODEL=$(find "." -type f -name "${MODEL_NAME}*coreml*.pte" -print -quit)
223223

224224
if [ -n "$EXPORTED_MODEL" ]; then
225225
EXPORTED_MODEL_WITH_DTYPE="${EXPORTED_MODEL%.pte}_${DTYPE}.pte"
@@ -244,8 +244,18 @@ test_model_with_coreml() {
244244
}
245245

246246
test_model_with_mps() {
247-
"${PYTHON_EXECUTABLE}" -m examples.apple.mps.scripts.mps_example --model_name="${MODEL_NAME}" --use_fp16
248-
EXPORTED_MODEL=$(find "." -type f -name "${MODEL_NAME}*.pte" -print -quit)
247+
"${PYTHON_EXECUTABLE}" -m examples.apple.mps.scripts.mps_example --model_name="${MODEL_NAME}" --bundled --use_fp16
248+
EXPORTED_MODEL=$(find "." -type f -name "${MODEL_NAME}*mps*.pte" -print -quit)
249+
250+
if [ -z "${EXPORTED_MODEL}" ]; then
251+
echo "[error] failed to export model: no .pte file found"
252+
exit 1
253+
fi
254+
255+
echo "Testing exported model with mps_executor_runner..."
256+
local out_dir=$(mktemp -d)
257+
examples/apple/mps/scripts/build_mps_executor_runner.sh --output="${out_dir}"
258+
"${out_dir}/examples/apple/mps/mps_executor_runner" --bundled_program --model_path "${EXPORTED_MODEL}"
249259
}
250260

251261
if [[ "${BACKEND}" == "portable" ]]; then

.ci/scripts/wheel/test_macos.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@
1111
if __name__ == "__main__":
1212
test_base.run_tests(
1313
model_tests=[
14+
# test_base.ModelTest(
15+
# model=Model.Mv3,
16+
# backend=Backend.XnnpackQuantizationDelegation,
17+
# ),
18+
# test_base.ModelTest(
19+
# model=Model.Mv3,
20+
# backend=Backend.CoreMlExportAndTest,
21+
# ),
1422
test_base.ModelTest(
1523
model=Model.Mv3,
16-
backend=Backend.XnnpackQuantizationDelegation,
17-
),
18-
test_base.ModelTest(
19-
model=Model.Mv3,
20-
backend=Backend.CoreMlExportAndTest,
24+
backend=Backend.AppleMPS,
2125
),
2226
]
2327
)

.github/workflows/trunk.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ jobs:
486486
strategy:
487487
fail-fast: false
488488
with:
489-
runner: macos-m1-stable
489+
runner: macos-latest-xlarge
490490
python-version: '3.11'
491491
submodules: 'recursive'
492492
ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pip-out/
2525
*.bin
2626
*.model
2727
*.pte
28+
*.etdp
2829
!test_bpe_tokenizer.bin
2930
!test_tiktoken_tokenizer.model
3031

CMakePresets.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"CMAKE_TOOLCHAIN_FILE": "${sourceDir}/third-party/ios-cmake/ios.toolchain.cmake",
1616
"EXECUTORCH_BUILD_PRESET_FILE": "${sourceDir}/tools/cmake/preset/macos.cmake",
1717
"PLATFORM": "MAC_ARM64",
18-
"DEPLOYMENT_TARGET": "10.15"
18+
"DEPLOYMENT_TARGET": "12.0"
1919
},
2020
"condition": {
2121
"lhs": "${hostSystemName}",
@@ -77,7 +77,7 @@
7777
"inherits": ["common"],
7878
"cacheVariables": {
7979
"EXECUTORCH_BUILD_PRESET_FILE": "${sourceDir}/tools/cmake/preset/pybind.cmake",
80-
"CMAKE_OSX_DEPLOYMENT_TARGET": "10.15"
80+
"CMAKE_OSX_DEPLOYMENT_TARGET": "12.0"
8181
},
8282
"condition": {
8383
"type": "inList",
@@ -93,7 +93,7 @@
9393
],
9494
"cacheVariables": {
9595
"EXECUTORCH_BUILD_PRESET_FILE": "${sourceDir}/tools/cmake/preset/llm.cmake",
96-
"CMAKE_OSX_DEPLOYMENT_TARGET": "10.15"
96+
"CMAKE_OSX_DEPLOYMENT_TARGET": "12.0"
9797
},
9898
"condition": {
9999
"type": "inList",

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ let package = Package(
120120
name: "executorch",
121121
platforms: [
122122
.iOS(.v17),
123-
.macOS(.v10_15),
123+
.macOS(.v12),
124124
],
125125
products: packageProducts,
126126
targets: packageTargets + [

examples/apple/mps/executor_runner/mps_executor_runner.mm

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#include <chrono>
3737
using namespace std::chrono;
3838

39-
static uint8_t method_allocator_pool[4 * 1024U * 1024U]; // 4 MB
39+
static uint8_t method_allocator_pool[16 * 1024U * 1024U]; // 16 MB
4040

4141
DEFINE_string(model_path, "model.ff", "Model serialized in flatbuffer format.");
4242
DEFINE_string(
@@ -306,12 +306,16 @@ HierarchicalAllocator planned_memory(
306306
ET_LOG(Info, "Inputs prepared.");
307307

308308
int num_iterations = FLAGS_num_runs + (FLAGS_skip_warmup ? 0 : 1);
309+
310+
ET_LOG(Info, "[jathu] num_iterations: %i", num_iterations);
309311
std::vector<float> exec_times;
310-
exec_times.reserve(FLAGS_num_runs);
312+
exec_times.reserve(num_iterations);
311313
for (int i = 0; i < num_iterations; i++) {
312314
auto start_exec_time = high_resolution_clock::now();
313315
// Run the model.
316+
ET_LOG(Info, "[jathu] i: %i executing method...", i);
314317
Error status = method->execute();
318+
ET_LOG(Info, "[jathu] i: %i done executing method", i);
315319
auto end_exec_time = high_resolution_clock::now();
316320
auto duration = duration_cast<microseconds>(end_exec_time - start_exec_time);
317321
exec_times.push_back(duration.count());

examples/apple/mps/scripts/build_mps_executor_runner.sh

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
#!/usr/bin/env bash
22
# Copyright (c) 2024 Apple Inc. All rights reserved.
33
# Provided subject to the LICENSE file in the top level directory.
4+
#
5+
# Copyright (c) Meta Platforms, Inc. and affiliates.
6+
# All rights reserved.
7+
#
8+
# This source code is licensed under the BSD-style license found in the
9+
# LICENSE file in the root directory of this source tree.
410

5-
set -e
11+
12+
set -eux
613

714
MODE="Release"
815
OUTPUT="cmake-out"
@@ -37,26 +44,28 @@ for arg in "$@"; do
3744
done
3845

3946
rm -rf "$OUTPUT"
47+
cmake -DCMAKE_INSTALL_PREFIX=${OUTPUT} \
48+
-DCMAKE_BUILD_TYPE=${MODE} \
49+
-DEXECUTORCH_ENABLE_LOGGING=ON \
50+
-DEXECUTORCH_BUILD_DEVTOOLS=ON \
51+
-DEXECUTORCH_ENABLE_EVENT_TRACER=ON \
52+
-Dprotobuf_BUILD_TESTS=OFF \
53+
-B${OUTPUT} \
54+
--preset macos
55+
56+
cmake --build ${OUTPUT} \
57+
-j$(sysctl -n hw.ncpu) \
58+
--config ${MODE} \
59+
--target install
4060

41-
cmake -DBUCK2="$BUCK" \
42-
-DCMAKE_INSTALL_PREFIX=cmake-out \
43-
-DCMAKE_BUILD_TYPE="$MODE" \
44-
-DEXECUTORCH_BUILD_DEVTOOLS=ON \
45-
-DEXECUTORCH_ENABLE_EVENT_TRACER=ON \
46-
-DEXECUTORCH_BUILD_MPS=ON \
47-
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \
48-
-Bcmake-out .
49-
cmake --build cmake-out -j9 --target install --config "$MODE"
50-
CMAKE_PREFIX_PATH="${PWD}/cmake-out/lib/cmake/ExecuTorch;${PWD}/cmake-out/third-party/gflags"
5161
# build mps_executor_runner
52-
rm -rf cmake-out/examples/apple/mps
53-
cmake \
54-
-DCMAKE_PREFIX_PATH="$CMAKE_PREFIX_PATH" \
55-
-DCMAKE_BUILD_TYPE="$MODE" \
56-
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \
57-
-Bcmake-out/examples/apple/mps \
58-
examples/apple/mps
59-
60-
cmake --build cmake-out/examples/apple/mps -j9 --config "$MODE"
62+
rm -rf "${OUTPUT}/examples/apple/mps"
63+
cmake -DCMAKE_PREFIX_PATH="${OUTPUT}/lib/cmake/ExecuTorch;${OUTPUT}/third-party/gflags" \
64+
-DCMAKE_BUILD_TYPE="$MODE" \
65+
-DCMAKE_OSX_DEPLOYMENT_TARGET="12.0" \
66+
-B"${OUTPUT}/examples/apple/mps" \
67+
-S examples/apple/mps
68+
69+
cmake --build "${OUTPUT}/examples/apple/mps" -j$(sysctl -n hw.ncpu) --config "$MODE"
6170

6271
echo "Build succeeded!"

examples/models/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class Backend(str, Enum):
4646
XnnpackQuantizationDelegation = "xnnpack-quantization-delegation"
4747
CoreMlExportOnly = "coreml"
4848
CoreMlExportAndTest = "coreml-test" # AOT export + test with runner
49+
AppleMPS = "mps"
4950

5051
def __str__(self) -> str:
5152
return self.value

tools/cmake/preset/pybind.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ set_overridable_option(EXECUTORCH_BUILD_EXTENSION_FLAT_TENSOR ON)
2222

2323
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
2424
set_overridable_option(EXECUTORCH_BUILD_COREML ON)
25+
set_overridable_option(EXECUTORCH_BUILD_MPS ON)
2526
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
2627
set_overridable_option(EXECUTORCH_BUILD_COREML ON)
2728
elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows" OR CMAKE_SYSTEM_NAME STREQUAL "WIN32")

0 commit comments

Comments
 (0)