Skip to content

Commit c6ad28a

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: 32be451 ghstack-comment-id: 3392300655 Pull-Request: #15024
1 parent 186d1f8 commit c6ad28a

File tree

5 files changed

+613
-3
lines changed

5 files changed

+613
-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: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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
42+
$<BUILD_INTERFACE:${EXECUTORCH_ROOT}>
43+
$<INSTALL_INTERFACE:include>
44+
# PyTorch AOTI headers from ExecutorTorch's torch detection
45+
${TORCH_INCLUDE_DIRS}
46+
)
47+
48+
# Link Metal framework
49+
find_library(METAL_LIBRARY Metal REQUIRED)
50+
find_library(FOUNDATION_LIBRARY Foundation REQUIRED)
51+
find_library(METALPERFORMANCESHADERS_LIBRARY MetalPerformanceShaders REQUIRED)
52+
find_library(METALPERFORMANCESHADERSGRAPH_LIBRARY MetalPerformanceShadersGraph REQUIRED)
53+
target_link_libraries(metal_backend PUBLIC ${METAL_LIBRARY} ${FOUNDATION_LIBRARY} ${METALPERFORMANCESHADERS_LIBRARY} ${METALPERFORMANCESHADERSGRAPH_LIBRARY})
54+
55+
target_compile_options(metal_backend PUBLIC -fexceptions -frtti -fPIC)
56+
57+
target_link_options(metal_backend PUBLIC -Wl,-export_dynamic)
58+
59+
# Find PyTorch's OpenMP library specifically for libtorch-less AOTI
60+
get_torch_base_path(TORCH_BASE_PATH)
61+
find_library(TORCH_OMP_LIBRARY
62+
NAMES omp libomp
63+
PATHS "${TORCH_BASE_PATH}/lib"
64+
NO_DEFAULT_PATH
65+
)
66+
67+
if(TORCH_OMP_LIBRARY)
68+
message(STATUS "Found PyTorch OpenMP library: ${TORCH_OMP_LIBRARY}")
69+
# Get the directory containing the OpenMP library for rpath
70+
get_filename_component(TORCH_OMP_LIB_DIR ${TORCH_OMP_LIBRARY} DIRECTORY)
71+
message(STATUS "OpenMP library directory: ${TORCH_OMP_LIB_DIR}")
72+
else()
73+
message(WARNING "PyTorch OpenMP library not found, may cause runtime linking issues")
74+
endif()
75+
76+
# Link against appropriate backends and standard libraries
77+
target_link_libraries(
78+
metal_backend
79+
PUBLIC
80+
aoti_common
81+
extension_tensor
82+
${CMAKE_DL_LIBS}
83+
${TORCH_OMP_LIBRARY}
84+
)
85+
86+
# Set rpath for OpenMP library to avoid runtime linking issues
87+
if(TORCH_OMP_LIBRARY AND TORCH_OMP_LIB_DIR)
88+
# Add the OpenMP library directory to the rpath
89+
set_target_properties(metal_backend PROPERTIES
90+
BUILD_RPATH "${TORCH_OMP_LIB_DIR}"
91+
INSTALL_RPATH "${TORCH_OMP_LIB_DIR}"
92+
)
93+
# Also try common OpenMP library locations
94+
target_link_options(metal_backend PUBLIC
95+
-Wl,-rpath,${TORCH_OMP_LIB_DIR}
96+
-Wl,-rpath,/usr/local/opt/libomp/lib
97+
-Wl,-rpath,/opt/homebrew/opt/libomp/lib
98+
)
99+
message(STATUS "Added rpath for OpenMP library: ${TORCH_OMP_LIB_DIR}")
100+
endif()
101+
102+
executorch_target_link_options_shared_lib(metal_backend)
103+
install(
104+
TARGETS metal_backend
105+
EXPORT ExecuTorchTargets
106+
DESTINATION lib
107+
)

0 commit comments

Comments
 (0)