Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
6420712
Update
manuelcandales Oct 10, 2025
d036c07
Update
manuelcandales Oct 10, 2025
1a22c5e
Update
manuelcandales Oct 10, 2025
d6f0bc9
Update
manuelcandales Oct 10, 2025
7e11615
Update
manuelcandales Oct 10, 2025
dfa435a
Update
manuelcandales Oct 10, 2025
648ee07
Update
manuelcandales Oct 10, 2025
3bea537
Update
manuelcandales Oct 10, 2025
b177b63
Update
manuelcandales Oct 10, 2025
9779d54
Update
manuelcandales Oct 10, 2025
ca5f1e5
Update
manuelcandales Oct 11, 2025
7e971b0
Update
manuelcandales Oct 11, 2025
f12117b
Update
manuelcandales Oct 11, 2025
5dfcd4f
Update
manuelcandales Oct 11, 2025
57d9e45
Update
manuelcandales Oct 11, 2025
de83a9f
Update
manuelcandales Oct 11, 2025
c4c16aa
Update
manuelcandales Oct 13, 2025
ce0f085
Update
manuelcandales Oct 13, 2025
e391e17
Update
manuelcandales Oct 13, 2025
3572de8
Update
manuelcandales Oct 13, 2025
bcd7655
Update
manuelcandales Oct 13, 2025
71a079d
Update
manuelcandales Oct 13, 2025
fd707f3
Update
manuelcandales Oct 13, 2025
2f092af
Update
manuelcandales Oct 13, 2025
89d3f14
Update
manuelcandales Oct 13, 2025
7590e37
Update
manuelcandales Oct 13, 2025
bea144f
Update
manuelcandales Oct 13, 2025
ade75f0
Update
manuelcandales Oct 13, 2025
094b8bb
Update
manuelcandales Oct 13, 2025
00a154e
Update
manuelcandales Oct 13, 2025
e9b3372
Update
manuelcandales Oct 13, 2025
f9c8989
Update
manuelcandales Oct 13, 2025
b87d5de
Update
manuelcandales Oct 13, 2025
81c4588
Update
manuelcandales Oct 13, 2025
8b1d309
Update
manuelcandales Oct 13, 2025
aec8796
Update
manuelcandales Oct 13, 2025
404a1b8
Update
manuelcandales Oct 13, 2025
422e4ba
Update
manuelcandales Oct 13, 2025
d075361
Update
manuelcandales Oct 13, 2025
3229b92
Update
manuelcandales Oct 13, 2025
3e8648e
Update
manuelcandales Oct 13, 2025
971a762
Update
manuelcandales Oct 15, 2025
3425f17
Update
manuelcandales Oct 15, 2025
c837491
Update
manuelcandales Oct 15, 2025
aea11e8
Update
manuelcandales Oct 15, 2025
7f178d3
Update
manuelcandales Oct 15, 2025
23ec80f
Update
manuelcandales Oct 15, 2025
f46adc5
Update
manuelcandales Oct 15, 2025
16d863c
Update
manuelcandales Oct 15, 2025
c80142d
Update
manuelcandales Oct 15, 2025
c3e9d0a
Update
manuelcandales Oct 15, 2025
780d883
Update
manuelcandales Oct 15, 2025
79d8fbc
Update
manuelcandales Oct 15, 2025
b782bb5
Update
manuelcandales Oct 15, 2025
cf93ffd
Update
manuelcandales Oct 15, 2025
4eaa345
Update
manuelcandales Oct 15, 2025
61ead64
Update
manuelcandales Oct 15, 2025
74cbeef
Update
manuelcandales Oct 15, 2025
750badf
Update
manuelcandales Oct 15, 2025
509e5ce
Update
manuelcandales Oct 15, 2025
76e5a45
Update
manuelcandales Oct 15, 2025
71f87b6
Update
manuelcandales Oct 15, 2025
930f6b9
Update
manuelcandales Oct 15, 2025
2667a0c
Update
manuelcandales Oct 15, 2025
6a6ba04
Update
manuelcandales Oct 15, 2025
07c61d0
Update
manuelcandales Oct 15, 2025
95a7024
Update
manuelcandales Oct 15, 2025
f214162
Update
manuelcandales Oct 15, 2025
e8b9828
Update
manuelcandales Oct 15, 2025
7c1b9b2
Update
manuelcandales Oct 15, 2025
dcafb2e
Update
manuelcandales Oct 15, 2025
d37e7ef
Update
manuelcandales Oct 15, 2025
1506e5f
Update
manuelcandales Oct 15, 2025
6f6fd58
Update
manuelcandales Oct 15, 2025
4367977
Update
manuelcandales Oct 15, 2025
735eff5
Update
manuelcandales Oct 15, 2025
9d69769
Update
manuelcandales Oct 17, 2025
1509b6d
Update
manuelcandales Oct 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -605,15 +605,23 @@ if(EXECUTORCH_BUILD_CORTEX_M)
list(APPEND _executorch_backends coretex_m_backend)
endif()

if(EXECUTORCH_BUILD_CUDA)
# Build common AOTI functionality (required for CUDA)
# Build common AOTI functionality if needed by CUDA or Metal backends
if(EXECUTORCH_BUILD_CUDA OR EXECUTORCH_BUILD_METAL)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/aoti)
endif()

if(EXECUTORCH_BUILD_CUDA)
# Build CUDA-specific AOTI functionality
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/cuda)
# Add aoti_cuda to backends - it already depends on aoti_common
list(APPEND _executorch_backends aoti_cuda)
endif()

if(EXECUTORCH_BUILD_METAL)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/apple/metal)
list(APPEND _executorch_backends metal_backend)
endif()

if(EXECUTORCH_BUILD_EXTENSION_APPLE)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/apple)
endif()
Expand Down
10 changes: 7 additions & 3 deletions backends/aoti/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@ target_compile_options(
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-fexceptions -frtti -fPIC>
)
# Ensure symbols are exported properly
target_link_options(
aoti_common PUBLIC $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wl,--export-dynamic>
)
if(APPLE)
target_link_options(aoti_common PUBLIC -Wl,-export_dynamic)
else()
target_link_options(
aoti_common PUBLIC $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wl,--export-dynamic>
)
endif()

# Link against ExecuTorch libraries and standard libraries
target_link_libraries(aoti_common PUBLIC extension_tensor ${CMAKE_DL_LIBS})
Expand Down
120 changes: 120 additions & 0 deletions backends/apple/metal/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
#
# Build AOTI Metal backend for runtime.
#
# ### Editing this file ###
#
# This file should be formatted with
# ~~~
# cmake-format -i CMakeLists.txt
# ~~~
# It should also be cmake-lint clean.
#
cmake_minimum_required(VERSION 3.29)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

if(NOT APPLE)
message(FATAL_ERROR "Metal backend requires macOS")
endif()

# Source root directory for executorch.
if(NOT EXECUTORCH_ROOT)
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..)
endif()

include(${EXECUTORCH_ROOT}/tools/cmake/Utils.cmake)
# Use full torch package to get library paths, but only link specific libraries
find_package_torch()

set(_aoti_metal_sources
runtime/metal_backend.cpp
runtime/shims/memory.cpp
runtime/shims/et_metal.mm
runtime/shims/et_metal_ops.mm
runtime/shims/shim_mps.mm
runtime/shims/tensor_attribute.cpp
runtime/shims/utils.cpp
)

add_library(metal_backend STATIC ${_aoti_metal_sources})
target_include_directories(
metal_backend
PUBLIC $<BUILD_INTERFACE:${EXECUTORCH_ROOT}> $<INSTALL_INTERFACE:include>
# PyTorch AOTI headers from ExecutorTorch's torch detection
${TORCH_INCLUDE_DIRS}
)

# Link Metal framework
find_library(METAL_LIBRARY Metal REQUIRED)
find_library(FOUNDATION_LIBRARY Foundation REQUIRED)
find_library(METALPERFORMANCESHADERS_LIBRARY MetalPerformanceShaders REQUIRED)
find_library(
METALPERFORMANCESHADERSGRAPH_LIBRARY MetalPerformanceShadersGraph REQUIRED
)
target_link_libraries(
metal_backend
PUBLIC ${METAL_LIBRARY} ${FOUNDATION_LIBRARY}
${METALPERFORMANCESHADERS_LIBRARY}
${METALPERFORMANCESHADERSGRAPH_LIBRARY}
)

target_compile_options(metal_backend PUBLIC -fexceptions -frtti -fPIC)

target_link_options(metal_backend PUBLIC -Wl,-export_dynamic)

# Find PyTorch's OpenMP library specifically for libtorch-less AOTI
get_torch_base_path(TORCH_BASE_PATH)
find_library(
TORCH_OMP_LIBRARY
NAMES omp libomp
PATHS "${TORCH_BASE_PATH}/lib"
NO_DEFAULT_PATH
)

if(TORCH_OMP_LIBRARY)
message(STATUS "Found PyTorch OpenMP library: ${TORCH_OMP_LIBRARY}")
# Get the directory containing the OpenMP library for rpath
get_filename_component(TORCH_OMP_LIB_DIR ${TORCH_OMP_LIBRARY} DIRECTORY)
message(STATUS "OpenMP library directory: ${TORCH_OMP_LIB_DIR}")
else()
message(
WARNING "PyTorch OpenMP library not found, may cause runtime linking issues"
)
endif()

# Link against appropriate backends and standard libraries
target_link_libraries(
metal_backend PUBLIC aoti_common extension_tensor ${CMAKE_DL_LIBS}
${TORCH_OMP_LIBRARY}
)

# Set rpath for OpenMP library to avoid runtime linking issues
if(TORCH_OMP_LIBRARY AND TORCH_OMP_LIB_DIR)
# Add the OpenMP library directory to the rpath
set_target_properties(
metal_backend PROPERTIES BUILD_RPATH "${TORCH_OMP_LIB_DIR}"
INSTALL_RPATH "${TORCH_OMP_LIB_DIR}"
)
# Also try common OpenMP library locations
target_link_options(
metal_backend PUBLIC -Wl,-rpath,${TORCH_OMP_LIB_DIR}
-Wl,-rpath,/usr/local/opt/libomp/lib
-Wl,-rpath,/opt/homebrew/opt/libomp/lib
)
message(STATUS "Added rpath for OpenMP library: ${TORCH_OMP_LIB_DIR}")
endif()

executorch_target_link_options_shared_lib(metal_backend)
install(
TARGETS metal_backend
EXPORT ExecuTorchTargets
DESTINATION lib
)
Loading
Loading