Skip to content

Commit c101c93

Browse files
kulinsethdbort
authored andcommitted
Add MPS delegate (#754)
Summary: This PR adds MPS Delegate support for ExecuTorch: To use the MPS delegate, follow the following steps: * (AOT) Generate an MPSGraph lowered module ``` python3 -m examples.apple.mps.scripts.mps_example --model_name="mv2" --bundled ``` * (RUNTIME) Once the model binary file is generated, use the mps_executor_runner to run the model: ```bash # Build the mps_executor_runner rm -rf cmake-out && mkdir cmake-out && cd cmake-out && cmake -DEXECUTORCH_BUILD_MPS=1 -DBUCK2=/tmp/buck2 —trace .. && cmake --build . && cd .. # Run the bundled program ./cmake-out/examples/apple/mps/mps_executor_runner --model_path mv2_mps_bundled.pte --bundled_program ``` To see more verbose logging on the runtime, you can enable set -DCMAKE_BUILD_TYPE=Debug while building the runner. An example tutorial can be found under examples/apple/mps/README.md. iOS instructions to run using the MPSDelegate have been updated in examples/demo-apps/apple_ios/README.md Note that the mps_executor_runner can be used with any testcase from test_mps.py and is not limited to examples.apple.mps.scripts.mps_example. For example ``` python3 -m unittest backends.apple.mps.test.test_mps --verbose -k test_mps_backend_cumsum ``` Fix MPS delegate through simulator. Pull Request resolved: #754 Reviewed By: iseeyuan, guangy10 Differential Revision: D50110623 Pulled By: kimishpatel fbshipit-source-id: 800a1d90776206dfac82fba8d9d2a0b83daf8c31
1 parent 8f01ae9 commit c101c93

Some content is hidden

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

60 files changed

+8599
-0
lines changed

CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ option(EXECUTORCH_BUILD_EXAMPLES "Build the ExecuTorch examples.")
137137
option(EXECUTORCH_BUILD_XTENSA_EXAMPLE
138138
"Build the example targeted for the Xtensa Hifi4 DSP" OFF)
139139

140+
# Build mps_executor_runner which depends on MPSGraph framework
141+
option(EXECUTORCH_BUILD_MPS
142+
"Build mps_executor_runner which depends on MPS" OFF)
143+
140144
if(NOT BUCK2)
141145
set(BUCK2 buck2)
142146
endif()
@@ -336,6 +340,12 @@ if(EXECUTORCH_BUILD_ARM_BAREMETAL)
336340
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/arm)
337341
endif()
338342

343+
option(EXECUTORCH_BUILD_MPS "Build the backends/apple/mps directory" OFF)
344+
if(EXECUTORCH_BUILD_MPS)
345+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/apple/mps)
346+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/examples/apple/mps)
347+
endif()
348+
339349
# Add selective build subdirectory
340350
if(BUILD_SELECTIVE_BUILD_TEST)
341351
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/examples/selective_build)

backends/apple/mps/.clang-format

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
Language: ObjC
3+
ColumnLimit: 120
4+
AlignAfterOpenBracket: Align
5+
ObjCBinPackProtocolList: Auto
6+
ObjCBlockIndentWidth: 2
7+
ObjCBreakBeforeNestedBlockParam: true
8+
ObjCSpaceAfterProperty: false
9+
ObjCSpaceBeforeProtocolList: false

backends/apple/mps/CMakeLists.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#
2+
# Copyright (c) 2023 Apple Inc. All rights reserved.
3+
# Provided subject to the LICENSE file in the top level directory.
4+
#
5+
6+
cmake_minimum_required(VERSION 3.19)
7+
8+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
9+
10+
if(NOT CMAKE_CXX_STANDARD)
11+
set(CMAKE_CXX_STANDARD 17)
12+
endif()
13+
14+
if(NOT PYTHON_EXECUTABLE)
15+
set(PYTHON_EXECUTABLE python3)
16+
endif()
17+
18+
# Source root directory for executorch.
19+
if(NOT EXECUTORCH_ROOT)
20+
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
21+
endif()
22+
23+
set(_common_compile_options -Wno-deprecated-declarations)
24+
set(_common_include_directories ${EXECUTORCH_ROOT}/..)
25+
26+
list(TRANSFORM _mps_backend__srcs PREPEND "${EXECUTORCH_ROOT}/")
27+
add_library(mpsdelegate ${_mps_backend__srcs})
28+
target_link_libraries(mpsdelegate PRIVATE ${_executor_runner_libs})
29+
target_include_directories(mpsdelegate
30+
PUBLIC ${_common_include_directories})

backends/apple/mps/TARGETS

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Any targets that should be shared between fbcode and xplat must be defined in
2+
# targets.bzl. This file can contain fbcode-only targets.
3+
4+
load(":targets.bzl", "define_common_targets")
5+
6+
oncall("executorch")
7+
8+
define_common_targets()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
#
3+
# Copyright (c) 2023 Apple Inc. All rights reserved.
4+
# Provided subject to the LICENSE file in the top level directory.
5+
#
6+
7+
# Install required python dependencies for using the MPS Backend
8+
pip install --force-reinstall ninja

0 commit comments

Comments
 (0)