-
Notifications
You must be signed in to change notification settings - Fork 21.3k
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
[GPU] Enable Metal on macosx #47635
[GPU] Enable Metal on macosx #47635
Changes from all commits
3783225
faf6913
de3daef
879b688
a2fc2b4
788ebc9
5a06789
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
#include <ATen/ATen.h> | ||
|
||
|
||
#if defined(C10_IOS) | ||
#if (C10_IOS || TARGET_OS_MAC) | ||
#import <ATen/native/metal/mpscnn/MPSCNNOps.h> | ||
#endif | ||
|
||
|
@@ -94,19 +94,19 @@ c10::intrusive_ptr<Conv2dOpContext> conv2d_prepack( | |
Tensor conv2d_prepack_run( | ||
const Tensor& input, | ||
const c10::intrusive_ptr<Conv2dOpContext>& op_context) { | ||
#if defined(C10_IOS) | ||
#if (C10_IOS || TARGET_OS_MAC) | ||
return mpscnn::conv2d(input, *op_context); | ||
#else | ||
TORCH_CHECK(false, "conv2d_prepack_run can only be invoked on iOS"); | ||
TORCH_CHECK(false, "conv2d_prepack_run can only be invoked on iOS and MacOS"); | ||
return input; | ||
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. nit: I don't think we need 'return input;' here as TORCH_CHECK(false, ...) throws. |
||
#endif | ||
} | ||
|
||
Tensor copy_to_host(const Tensor& input) { | ||
#if defined(C10_IOS) | ||
#if (C10_IOS || TARGET_OS_MAC) | ||
return mpscnn::copy_to_host(input); | ||
#else | ||
TORCH_CHECK(false, "copy_to_host can only be invoked on iOS"); | ||
TORCH_CHECK(false, "copy_to_host can only be invoked on iOS and MacOS"); | ||
return input; | ||
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. ditto |
||
#endif | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1195,6 +1195,18 @@ if(USE_CUDA) | |
|
||
endif() | ||
|
||
# ---[ Metal(OSX) modification | ||
if(APPLE AND USE_PYTORCH_METAL) | ||
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. nit: can we make 'USE_PYTORCH_METAL' only enabled when 'APPLE' is true? 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.
|
||
if(NOT INTERN_BUILD_MOBILE) | ||
include(../cmake/Metal.cmake) | ||
# We need to link the system frameworks explicitly | ||
find_library(metal NAMES Metal) | ||
find_library(mps NAMES MetalPerformanceShaders) | ||
find_library(foundation NAMES Foundation) | ||
find_library(accelerate NAMES Accelerate) | ||
target_link_libraries(torch_cpu PUBLIC ${metal} ${mps} ${foundation} ${accelerate}) | ||
endif() | ||
endif() | ||
|
||
# Note [Global dependencies] | ||
# Some libraries (e.g. OpenMPI) like to dlopen plugins after they're initialized, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
if(NOT APPLE) | ||
return() | ||
endif() | ||
|
||
if(NOT USE_PYTORCH_METAL) | ||
return() | ||
endif() | ||
|
||
if(IOS OR INTERN_BUILD_MOBILE) | ||
return() | ||
endif() | ||
|
||
set(OSX_PLATFORM "MacOSX.platform") | ||
exec_program(/usr/bin/xcode-select ARGS -print-path OUTPUT_VARIABLE CMAKE_XCODE_DEVELOPER_DIR) | ||
set(XCODE_POST_43_ROOT "${CMAKE_XCODE_DEVELOPER_DIR}/Platforms/${OSX_PLATFORM}/Developer") | ||
set(XCODE_PRE_43_ROOT "/Developer/Platforms/${OSX_PLATFORM}/Developer") | ||
if(NOT DEFINED CMAKE_OSX_DEVELOPER_ROOT) | ||
if(EXISTS ${XCODE_POST_43_ROOT}) | ||
set(CMAKE_OSX_DEVELOPER_ROOT ${XCODE_POST_43_ROOT}) | ||
elseif(EXISTS ${XCODE_PRE_43_ROOT}) | ||
set(CMAKE_OSX_DEVELOPER_ROOT ${XCODE_PRE_43_ROOT}) | ||
endif(EXISTS ${XCODE_POST_43_ROOT}) | ||
endif(NOT DEFINED CMAKE_OSX_DEVELOPER_ROOT) | ||
set(CMAKE_OSX_DEVELOPER_ROOT ${CMAKE_OSX_DEVELOPER_ROOT} CACHE PATH "Location of OSX SDKs root directory") | ||
|
||
if(NOT DEFINED CMAKE_OSX_SDK_ROOT) | ||
file(GLOB _CMAKE_OSX_SDKS "${CMAKE_OSX_DEVELOPER_ROOT}/SDKs/*") | ||
if(_CMAKE_OSX_SDKS) | ||
list(SORT _CMAKE_OSX_SDKS) | ||
list(REVERSE _CMAKE_OSX_SDKS) | ||
list(GET _CMAKE_OSX_SDKS 0 CMAKE_OSX_SDK_ROOT) | ||
message(STATUS "_CMAKE_OSX_SDKS: ${_CMAKE_OSX_SDKS}") | ||
else(_CMAKE_OSX_SDKS) | ||
message(FATAL_ERROR "No OSX SDK's found in default search path ${CMAKE_OSX_DEVELOPER_ROOT}.") | ||
endif(_CMAKE_OSX_SDKS) | ||
message(STATUS "Toolchain using default OSX SDK: ${CMAKE_OSX_SDK_ROOT}") | ||
endif(NOT DEFINED CMAKE_OSX_SDK_ROOT) | ||
set(CMAKE_OSX_SDK_ROOT ${CMAKE_OSX_SDK_ROOT} CACHE PATH "Location of the selected OSX SDK") | ||
set(CMAKE_FRAMEWORK_PATH | ||
${CMAKE_OSX_SDK_ROOT}/System/Library/Frameworks | ||
${CMAKE_OSX_SDK_ROOT}/System/Library/PrivateFrameworks | ||
${CMAKE_OSX_SDK_ROOT}/Developer/Library/Frameworks | ||
) | ||
message(STATUS "CMAKE_FRAMEWORK_PATH: ${CMAKE_FRAMEWORK_PATH}") | ||
set(CMAKE_FIND_FRAMEWORK FIRST) |
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.
Nothing wrong about this - just feel that having C10_IOS (defined by us) and TARGET_OS_MAC (defined by apple) on the same condition is a little unbalanced, lol.
If this condition is going to be used at other places, shall we create a dedicated macro? Not sure what's the best convention in PyTorch codebase (cc: @dzhulgakov):
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.
How about (TARGET_OS_PHONE || TARGET_OS_MAC). Would that make you feel better 🤣