-
Notifications
You must be signed in to change notification settings - Fork 684
Add Metal backend build system and runtime integration #15024
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
manuelcandales
wants to merge
3
commits into
gh/manuelcandales/143/head
Choose a base branch
from
gh/manuelcandales/144/head
base: gh/manuelcandales/143/head
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+679
−3
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
# 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. | ||
# | ||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) | ||
|
||
# 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's remove |
||
|
||
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 | ||
) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cmake_minimum_required(VERSION 3.29)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if(NOT APPLE)
message(FATAL_ERROR "Metal backend requires macOS")
endif()