Skip to content

Commit 6f08f7c

Browse files
Add Metal backend build system and runtime integration
Build system changes: - Add EXECUTORCH_BUILD_METAL build option in default.cmake - Fix AOTI linker flags for Apple platforms (use -export_dynamic) - Extend AOTI build support to Metal backend - Register Metal backend and configure Metal framework linkage - Add PyTorch AOTI headers and OpenMP library detection with rpath Runtime implementation: - Add main Metal backend runtime providing entry point for AOTI-compiled model execution on Metal devices This commit ties together all Metal backend components and enables building the complete backend. ghstack-source-id: cde20a4 ghstack-comment-id: 3392300655 Pull-Request: #15024
1 parent e82a25a commit 6f08f7c

File tree

5 files changed

+679
-3
lines changed

5 files changed

+679
-3
lines changed

CMakeLists.txt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,15 +602,23 @@ if(EXECUTORCH_BUILD_CORTEX_M)
602602
list(APPEND _executorch_backends coretex_m_backend)
603603
endif()
604604

605-
if(EXECUTORCH_BUILD_CUDA)
606-
# Build common AOTI functionality (required for CUDA)
605+
# Build common AOTI functionality if needed by CUDA or Metal backends
606+
if(EXECUTORCH_BUILD_CUDA OR EXECUTORCH_BUILD_METAL)
607607
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/aoti)
608+
endif()
609+
610+
if(EXECUTORCH_BUILD_CUDA)
608611
# Build CUDA-specific AOTI functionality
609612
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/cuda)
610613
# Add aoti_cuda to backends - it already depends on aoti_common
611614
list(APPEND _executorch_backends aoti_cuda)
612615
endif()
613616

617+
if(EXECUTORCH_BUILD_METAL)
618+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/apple/metal)
619+
list(APPEND _executorch_backends metal_backend)
620+
endif()
621+
614622
if(EXECUTORCH_BUILD_EXTENSION_APPLE)
615623
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/apple)
616624
endif()

backends/aoti/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ target_include_directories(
3838
)
3939
target_compile_options(aoti_common PUBLIC -fexceptions -frtti -fPIC)
4040
# Ensure symbols are exported properly
41-
target_link_options(aoti_common PUBLIC -Wl,--export-dynamic)
41+
if(APPLE)
42+
target_link_options(aoti_common PUBLIC -Wl,-export_dynamic)
43+
else()
44+
target_link_options(aoti_common PUBLIC -Wl,--export-dynamic)
45+
endif()
4246

4347
# Link against ExecuTorch libraries and standard libraries
4448
target_link_libraries(aoti_common PUBLIC extension_tensor ${CMAKE_DL_LIBS})
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
#
7+
# Build AOTI Metal backend for runtime.
8+
#
9+
# ### Editing this file ###
10+
#
11+
# This file should be formatted with
12+
# ~~~
13+
# cmake-format -i CMakeLists.txt
14+
# ~~~
15+
# It should also be cmake-lint clean.
16+
#
17+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
18+
19+
# Source root directory for executorch.
20+
if(NOT EXECUTORCH_ROOT)
21+
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..)
22+
endif()
23+
24+
include(${EXECUTORCH_ROOT}/tools/cmake/Utils.cmake)
25+
# Use full torch package to get library paths, but only link specific libraries
26+
find_package_torch()
27+
28+
set(_aoti_metal_sources
29+
runtime/metal_backend.cpp
30+
runtime/shims/memory.cpp
31+
runtime/shims/et_metal.mm
32+
runtime/shims/et_metal_ops.mm
33+
runtime/shims/shim_mps.mm
34+
runtime/shims/tensor_attribute.cpp
35+
runtime/shims/utils.cpp
36+
)
37+
38+
add_library(metal_backend STATIC ${_aoti_metal_sources})
39+
target_include_directories(
40+
metal_backend
41+
PUBLIC $<BUILD_INTERFACE:${EXECUTORCH_ROOT}> $<INSTALL_INTERFACE:include>
42+
# PyTorch AOTI headers from ExecutorTorch's torch detection
43+
${TORCH_INCLUDE_DIRS}
44+
)
45+
46+
# Link Metal framework
47+
find_library(METAL_LIBRARY Metal REQUIRED)
48+
find_library(FOUNDATION_LIBRARY Foundation REQUIRED)
49+
find_library(METALPERFORMANCESHADERS_LIBRARY MetalPerformanceShaders REQUIRED)
50+
find_library(
51+
METALPERFORMANCESHADERSGRAPH_LIBRARY MetalPerformanceShadersGraph REQUIRED
52+
)
53+
target_link_libraries(
54+
metal_backend
55+
PUBLIC ${METAL_LIBRARY} ${FOUNDATION_LIBRARY}
56+
${METALPERFORMANCESHADERS_LIBRARY}
57+
${METALPERFORMANCESHADERSGRAPH_LIBRARY}
58+
)
59+
60+
target_compile_options(metal_backend PUBLIC -fexceptions -frtti -fPIC)
61+
62+
target_link_options(metal_backend PUBLIC -Wl,-export_dynamic)
63+
64+
# Find PyTorch's OpenMP library specifically for libtorch-less AOTI
65+
get_torch_base_path(TORCH_BASE_PATH)
66+
find_library(
67+
TORCH_OMP_LIBRARY
68+
NAMES omp libomp
69+
PATHS "${TORCH_BASE_PATH}/lib"
70+
NO_DEFAULT_PATH
71+
)
72+
73+
if(TORCH_OMP_LIBRARY)
74+
message(STATUS "Found PyTorch OpenMP library: ${TORCH_OMP_LIBRARY}")
75+
# Get the directory containing the OpenMP library for rpath
76+
get_filename_component(TORCH_OMP_LIB_DIR ${TORCH_OMP_LIBRARY} DIRECTORY)
77+
message(STATUS "OpenMP library directory: ${TORCH_OMP_LIB_DIR}")
78+
else()
79+
message(
80+
WARNING "PyTorch OpenMP library not found, may cause runtime linking issues"
81+
)
82+
endif()
83+
84+
# Link against appropriate backends and standard libraries
85+
target_link_libraries(
86+
metal_backend PUBLIC aoti_common extension_tensor ${CMAKE_DL_LIBS}
87+
${TORCH_OMP_LIBRARY}
88+
)
89+
90+
# Set rpath for OpenMP library to avoid runtime linking issues
91+
if(TORCH_OMP_LIBRARY AND TORCH_OMP_LIB_DIR)
92+
# Add the OpenMP library directory to the rpath
93+
set_target_properties(
94+
metal_backend PROPERTIES BUILD_RPATH "${TORCH_OMP_LIB_DIR}"
95+
INSTALL_RPATH "${TORCH_OMP_LIB_DIR}"
96+
)
97+
# Also try common OpenMP library locations
98+
target_link_options(
99+
metal_backend PUBLIC -Wl,-rpath,${TORCH_OMP_LIB_DIR}
100+
-Wl,-rpath,/usr/local/opt/libomp/lib
101+
-Wl,-rpath,/opt/homebrew/opt/libomp/lib
102+
)
103+
message(STATUS "Added rpath for OpenMP library: ${TORCH_OMP_LIB_DIR}")
104+
endif()
105+
106+
executorch_target_link_options_shared_lib(metal_backend)
107+
install(
108+
TARGETS metal_backend
109+
EXPORT ExecuTorchTargets
110+
DESTINATION lib
111+
)

0 commit comments

Comments
 (0)