Failing to compile static library on M1 Mac with USE_MPS=ON

christo: <p>If I compile a static library with <code>USE_MPS=OFF</code> and <code>BUILD_SHARED_LIBS=OFF</code>, everything compiles successfully. When I turn on MPS with <code>USE_MPS=ON</code>, I get linking errors at the end of the compilation (see below). There are a couple of those errors, but they all look the same. It happens when linking the following files:</p>
<ul>
<li><code>bin/test_lazy</code></li>
<li><code>bin/Dict_test</code></li>
<li><code>bin/tutorial_tensorexpr</code></li>
<li><code>bin/test_api</code></li>
<li><code>bin/test_tensorexpr</code></li>
<li><code>bin/parallel_benchmark</code></li>
<li><code>bin/test_jit</code></li>
<li><code>bin/Dimname_test</code></li>
<li><code>bin/MaybeOwned_test</code></li>
<li><code>bin/torch_shm_manager</code></li>
</ul>
<p>I’m not overly proficient in compiling C++, but I wonder if I need to install some dependencies for MPS? I thought it came with macOS.</p>
<p>Thanks a lot in advance. Any help in solving this issue would be very appreciated.</p>
<pre><code class="lang-auto">[5385/5446] Linking CXX executable bin/torch_shm_manager
FAILED: bin/torch_shm_manager 
: &amp;&amp; /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -fvisibility-inlines-hidden -DUSE_PTHREADPOOL -DNDEBUG -DUSE_KINETO -DLIBKINETO_NOCUPTI -DLIBKINETO_NOROCTRACER -DUSE_PYTORCH_QNNPACK -DUSE_XNNPACK -DSYMBOLICATE_MOBILE_DEBUG_HANDLE -O2 -fPIC -Wall -Wextra -Werror=return-type -Werror=non-virtual-dtor -Werror=braced-scalar-init -Werror=range-loop-construct -Werror=bool-operation -Wnarrowing -Wno-missing-field-initializers -Wno-type-limits -Wno-array-bounds -Wno-unknown-pragmas -Wno-unused-parameter -Wno-unused-function -Wno-unused-result -Wno-strict-overflow -Wno-strict-aliasing -Wvla-extension -Wsuggest-override -Wnewline-eof -Winconsistent-missing-override -Winconsistent-missing-destructor-override -Wno-pass-failed -Wno-error=pedantic -Wno-error=old-style-cast -Wno-error=inconsistent-missing-override -Wno-error=inconsistent-missing-destructor-override -Wconstant-conversion -Wno-invalid-partial-specialization -Wno-missing-braces -Qunused-arguments -fcolor-diagnostics -faligned-new -Wno-unused-but-set-variable -fno-math-errno -fno-trapping-math -Werror=format -DUSE_MPS -Wno-unused-private-field -Wno-missing-braces -O3 -DNDEBUG -DNDEBUG -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names -rdynamic caffe2/torch/lib/libshm/CMakeFiles/torch_shm_manager.dir/manager.cpp.o -o bin/torch_shm_manager  -Wl,-rpath,/Users/christo/Code/6_External/pytorch-static-2.2/build/lib  lib/libshm.dylib  lib/libtorch.a  -Wl,-force_load,"/Users/christo/Code/6_External/pytorch-static-2.2/build/lib/libtorch_cpu.a"  -Wl,-force_load,"/Users/christo/Code/6_External/pytorch-static-2.2/build/lib/libcaffe2_protos.a"  lib/libc10.a  lib/libpytorch_qnnpack.a  lib/libnnpack.a  lib/libXNNPACK.a  lib/libcpuinfo.a  lib/libclog.a  lib/libpthreadpool.a  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk/usr/lib/libm.tbd  /opt/homebrew/lib/libomp.dylib  lib/libfoxi_loader.a  lib/libkineto.a  -Xlinker -framework -Xlinker Accelerate  -lm  sleef/lib/libsleef.a  -Wl,-force_load,"/Users/christo/Code/6_External/pytorch-static-2.2/build/lib/libonnx.a"  lib/libonnx_proto.a  lib/libprotobuf.a &amp;&amp; :
ld: Undefined symbols:
  _MTLCopyAllDevices, referenced from:
      at::mps::MPSDevice::MPSDevice() in libtorch_cpu.a[958](MPSDevice.mm.o)
  _NSClassFromString, referenced from:
      at::mps::MPSDevice::isMacOS13Plus(at::mps::MacOSVersion) const in libtorch_cpu.a[958](MPSDevice.mm.o)
      at::mps::MPSDevice::MPSDevice() in libtorch_cpu.a[958](MPSDevice.mm.o)
  _OBJC_CLASS_$_MPSCommandBuffer, referenced from:
       in libtorch_cpu.a[965](MPSStream.mm.o)
  _OBJC_CLASS_$_MPSGraph, referenced from:
       in libtorch_cpu.a[968](OperationUtils.mm.o)
  _OBJC_CLASS_$_MPSGraphCompilationDescriptor, referenced from:
       in libtorch_cpu.a[965](MPSStream.mm.o)
  _OBJC_CLASS_$_MPSGraphConvolution2DOpDescriptor, referenced from:
       in libtorch_cpu.a[977](Convolution.mm.o)
  _OBJC_CLASS_$_MPSGraphDepthwiseConvolution3DOpDescriptor, referenced from:
       in libtorch_cpu.a[977](Convolution.mm.o)
  _OBJC_CLASS_$_MPSGraphExecutionDescriptor, referenced from:
       in libtorch_cpu.a[965](MPSStream.mm.o)
  _OBJC_CLASS_$_MPSGraphLSTMDescriptor, referenced from:
       in libtorch_cpu.a[1000](RnnOps.mm.o)
  _OBJC_CLASS_$_MPSGraphPooling2DOpDescriptor, referenced from:
       in libtorch_cpu.a[995](Pooling.mm.o)
  _OBJC_CLASS_$_MPSGraphRandomOpDescriptor, referenced from:
       in libtorch_cpu.a[980](Distributions.mm.o)
  _OBJC_CLASS_$_MPSGraphTensorData, referenced from:
       in libtorch_cpu.a[968](OperationUtils.mm.o)
  _OBJC_CLASS_$_MPSMatrix, referenced from:
       in libtorch_cpu.a[989](LinearAlgebra.mm.o)
  _OBJC_CLASS_$_MPSMatrixDescriptor, referenced from:
       in libtorch_cpu.a[989](LinearAlgebra.mm.o)
  _OBJC_CLASS_$_MPSMatrixSolveTriangular, referenced from:
       in libtorch_cpu.a[989](LinearAlgebra.mm.o)
  _OBJC_CLASS_$_MPSNDArray, referenced from:
       in libtorch_cpu.a[968](OperationUtils.mm.o)
  _OBJC_CLASS_$_MPSNDArrayDescriptor, referenced from:
       in libtorch_cpu.a[968](OperationUtils.mm.o)
  _OBJC_CLASS_$_MTLCompileOptions, referenced from:
       in libtorch_cpu.a[958](MPSDevice.mm.o)
  _OBJC_CLASS_$_MTLHeapDescriptor, referenced from:
       in libtorch_cpu.a[957](MPSAllocator.mm.o)
  _OBJC_CLASS_$_MTLSharedEventListener, referenced from:
       in libtorch_cpu.a[959](MPSEvent.mm.o)
  _OBJC_CLASS_$_NSArray, referenced from:
       in libtorch_cpu.a[968](OperationUtils.mm.o)
  _OBJC_CLASS_$_NSConstantArray, referenced from:
       in libtorch_cpu.a[968](OperationUtils.mm.o)
       in libtorch_cpu.a[968](OperationUtils.mm.o)
       in libtorch_cpu.a[968](OperationUtils.mm.o)
       in libtorch_cpu.a[968](OperationUtils.mm.o)
       in libtorch_cpu.a[969](Activation.mm.o)
       in libtorch_cpu.a[969](Activation.mm.o)
       in libtorch_cpu.a[969](Activation.mm.o)
      ...
  _OBJC_CLASS_$_NSConstantIntegerNumber, referenced from:
       in libtorch_cpu.a[968](OperationUtils.mm.o)
       in libtorch_cpu.a[977](Convolution.mm.o)
       in libtorch_cpu.a[980](Distributions.mm.o)
       in libtorch_cpu.a[985](Indexing.mm.o)
       in libtorch_cpu.a[992](Pad.mm.o)
       in libtorch_cpu.a[1012](UpSample.mm.o)
  _OBJC_CLASS_$_NSData, referenced from:
       in libtorch_cpu.a[980](Distributions.mm.o)
  _OBJC_CLASS_$_NSDictionary, referenced from:
       in libtorch_cpu.a[969](Activation.mm.o)
  _OBJC_CLASS_$_NSMutableArray, referenced from:
       in libtorch_cpu.a[968](OperationUtils.mm.o)
  _OBJC_CLASS_$_NSMutableDictionary, referenced from:
       in libtorch_cpu.a[972](BinaryOps.mm.o)
  _OBJC_CLASS_$_NSMutableSet, referenced from:
       in libtorch_cpu.a[1013](View.mm.o)
  _OBJC_CLASS_$_NSNumber, referenced from:
       in libtorch_cpu.a[968](OperationUtils.mm.o)
  _OBJC_CLASS_$_NSString, referenced from:
       in libtorch_cpu.a[958](MPSDevice.mm.o)
  ___CFConstantStringClassReference, referenced from:
       in libtorch_cpu.a[958](MPSDevice.mm.o)
       in libtorch_cpu.a[968](OperationUtils.mm.o)
       in libtorch_cpu.a[968](OperationUtils.mm.o)
       in libtorch_cpu.a[968](OperationUtils.mm.o)
       in libtorch_cpu.a[968](OperationUtils.mm.o)
       in libtorch_cpu.a[972](BinaryOps.mm.o)
       in libtorch_cpu.a[972](BinaryOps.mm.o)
      ...
  _objc_alloc, referenced from:
      at::native::mps::printTensorNDArray(at::Tensor const&amp;) in libtorch_cpu.a[968](OperationUtils.mm.o)
      at::native::mps::ndArrayFromTensor(at::Tensor const&amp;, NSArray&lt;NSNumber*&gt;*, MPSDataType) in libtorch_cpu.a[968](OperationUtils.mm.o)
      at::native::mps::Placeholder::Placeholder(MPSGraphTensor*, at::Tensor const&amp;, NSArray&lt;NSNumber*&gt;*, bool, MPSDataType) in libtorch_cpu.a[968](OperationUtils.mm.o)
      at::native::mps::getMPSGraphTensorData(MPSGraph*, at::mps::MPSStream*, at::Tensor const&amp;) in libtorch_cpu.a[968](OperationUtils.mm.o)
      at::native::mps::getMPSGraphTensorData(MPSGraph*, at::mps::MPSStream*, at::Tensor const&amp;) in libtorch_cpu.a[968](OperationUtils.mm.o)
      at::native::mps::getMPSGraphTensorData(MPSGraph*, at::mps::MPSStream*, at::Tensor const&amp;) in libtorch_cpu.a[968](OperationUtils.mm.o)
      at::native::mps::getMPSGraphTensorFromScalar(at::mps::MPSStream*, at::native::mps::MPSScalar&amp;) in libtorch_cpu.a[968](OperationUtils.mm.o)
      at::native::mps::getMPSGraphTensorFromScalar(at::mps::MPSStream*, at::native::mps::MPSScalar&amp;) in libtorch_cpu.a[968](OperationUtils.mm.o)
      at::native::mps::getMPSGraphTensorFromScalar(at::mps::MPSStream*, at::native::mps::MPSScalar&amp;) in libtorch_cpu.a[968](OperationUtils.mm.o)
      ...
  _objc_alloc_init, referenced from:
      at::mps::MPSDevice::isMacOS13Plus(at::mps::MacOSVersion) const in libtorch_cpu.a[958](MPSDevice.mm.o)
      at::mps::MPSEvent::recordLocked(bool) in libtorch_cpu.a[959](MPSEvent.mm.o)
      at::mps::MPSEvent::notifyLocked(void (id&lt;MTLSharedEvent&gt;, unsigned long long) block_pointer) in libtorch_cpu.a[959](MPSEvent.mm.o)
      at::mps::MPSEvent::notify(bool, void (id&lt;MTLSharedEvent&gt;, unsigned long long) block_pointer) in libtorch_cpu.a[959](MPSEvent.mm.o)
      ____ZN2at3mps8MPSEvent6notifyEbU13block_pointerFvPU25objcproto14MTLSharedEvent11objc_objectyE_block_invoke in libtorch_cpu.a[959](MPSEvent.mm.o)
      at::mps::MPSEvent::synchronize() in libtorch_cpu.a[959](MPSEvent.mm.o)
      at::native::_lstm_mps(at::Tensor const&amp;, c10::ArrayRef&lt;at::Tensor&gt;, c10::ArrayRef&lt;at::Tensor&gt;, bool, long long, double, bool, bool, bool) in libtorch_cpu.a[1000](RnnOps.mm.o)
      ...
  _objc_autorelease, referenced from:
      at::mps::MPSDevice::isMacOS13Plus(at::mps::MacOSVersion) const in libtorch_cpu.a[958](MPSDevice.mm.o)
      at::mps::MPSDevice::metalIndexingPSO(std::__1::basic_string&lt;char, std::__1::char_traits&lt;char&gt;, std::__1::allocator&lt;char&gt;&gt; const&amp;) in libtorch_cpu.a[958](MPSDevice.mm.o)
      at::mps::MPSDevice::MPSDevice() in libtorch_cpu.a[958](MPSDevice.mm.o)
      at::native::mps::printTensorNDArray(at::Tensor const&amp;) in libtorch_cpu.a[968](OperationUtils.mm.o)
      at::native::mps::ndArrayFromTensor(at::Tensor const&amp;, NSArray&lt;NSNumber*&gt;*, MPSDataType) in libtorch_cpu.a[968](OperationUtils.mm.o)
      at::native::mps::Placeholder::Placeholder(MPSGraphTensor*, at::Tensor const&amp;, NSArray&lt;NSNumber*&gt;*, bool, MPSDataType) in libtorch_cpu.a[968](OperationUtils.mm.o)
      at::native::mps::getMPSGraphTensorData(MPSGraph*, at::mps::MPSStream*, at::Tensor const&amp;) in libtorch_cpu.a[968](OperationUtils.mm.o)
      at::native::mps::getMPSGraphTensorData(MPSGraph*, at::mps::MPSStream*, at::Tensor const&amp;) in libtorch_cpu.a[968](OperationUtils.mm.o)
      at::native::mps::getMPSGraphTensorData(MPSGraph*, at::mps::MPSStream*, at::Tensor const&amp;) in libtorch_cpu.a[968](OperationUtils.mm.o)
      ...
  _objc_autoreleasePoolPop, referenced from:
      ____ZN2at3mps8MPSEvent6recordEbb_block_invoke in libtorch_cpu.a[959](MPSEvent.mm.o)
      ____ZN2at3mps8MPSEvent4waitEbb_block_invoke in libtorch_cpu.a[959](MPSEvent.mm.o)
      ____ZN2at3mps8MPSEvent6notifyEbU13block_pointerFvPU25objcproto14MTLSharedEvent11objc_objectyE_block_invoke in libtorch_cpu.a[959](MPSEvent.mm.o)
      ____ZN2at3mps9MPSStream19addCompletedHandlerEU13block_pointerFvPU27objcproto16MTLCommandBuffer11objc_objectE_block_invoke in libtorch_cpu.a[965](MPSStream.mm.o)
      ____ZN2at3mps9MPSStream4fillEPU19objcproto9MTLBuffer11objc_objecthmmNS0_8SyncTypeE_block_invoke in libtorch_cpu.a[965](MPSStream.mm.o)
      ____ZN2at3mps9MPSStream4copyEPU19objcproto9MTLBuffer11objc_objectS3_mmmyNS0_8SyncTypeE_block_invoke in libtorch_cpu.a[965](MPSStream.mm.o)
      ____ZN2at3mps9MPSStream4copyEPU19objcproto9MTLBuffer11objc_objectS3_mmmyNS0_8SyncTypeE_block_invoke in libtorch_cpu.a[965](MPSStream.mm.o)
      ...
  _objc_autoreleasePoolPush, referenced from:
      ____ZN2at3mps8MPSEvent6recordEbb_block_invoke in libtorch_cpu.a[959](MPSEvent.mm.o)
      ____ZN2at3mps8MPSEvent4waitEbb_block_invoke in libtorch_cpu.a[959](MPSEvent.mm.o)
      ____ZN2at3mps8MPSEvent6notifyEbU13block_pointerFvPU25objcproto14MTLSharedEvent11objc_objectyE_block_invoke in libtorch_cpu.a[959](MPSEvent.mm.o)
      ____ZN2at3mps9MPSStream19addCompletedHandlerEU13block_pointerFvPU27objcproto16MTLCommandBuffer11objc_objectE_block_invoke in libtorch_cpu.a[965](MPSStream.mm.o)
      ____ZN2at3mps9MPSStream4fillEPU19objcproto9MTLBuffer11objc_objecthmmNS0_8SyncTypeE_block_invoke in libtorch_cpu.a[965](MPSStream.mm.o)
      ____ZN2at3mps9MPSStream4copyEPU19objcproto9MTLBuffer11objc_objectS3_mmmyNS0_8SyncTypeE_block_invoke in libtorch_cpu.a[965](MPSStream.mm.o)
      at::native::relu_mps(at::Tensor const&amp;) in libtorch_cpu.a[969](Activation.mm.o)
      ...
  _objc_enumerationMutation, referenced from:
      at::native::mps::getMPSShapeString(NSArray&lt;NSNumber*&gt;*) in libtorch_cpu.a[968](OperationUtils.mm.o)
  _objc_msgSend, referenced from:
       in objc-stubs-file
  _objc_opt_new, referenced from:
      at::mps::HeapAllocator::HeapBlock::createHeapBlock(at::mps::HeapAllocator::AllocParams&amp;, id&lt;MTLDevice&gt;, unsigned int) in libtorch_cpu.a[957](MPSAllocator.mm.o)
      at::mps::MPSDevice::getMetalIndexingLibrary() in libtorch_cpu.a[958](MPSDevice.mm.o)
      at::mps::MPSStream::MPSStream(c10::Stream) in libtorch_cpu.a[965](MPSStream.mm.o)
      at::mps::MPSStream::MPSStream(c10::Stream) in libtorch_cpu.a[965](MPSStream.mm.o)
      at::native::mps::make_mps_graph() in libtorch_cpu.a[968](OperationUtils.mm.o)
      ____ZN2at6native3mpsL15binary_mps_implERNS_18TensorIteratorBaseENSt3__112basic_stringIcNS4_11char_traitsIcEENS4_9allocatorIcEEEE_block_invoke in libtorch_cpu.a[971](BinaryKernel.mm.o)
      at::native::mps::binaryOpTensor(at::Tensor const&amp;, at::Tensor const&amp;, c10::Scalar const&amp;, at::Tensor const&amp;, std::__1::basic_string&lt;char, std::__1::char_traits&lt;char&gt;, std::__1::allocator&lt;char&gt;&gt;, MPSGraphTensor* (at::native::mps::BinaryOpCachedGraph*, MPSGraphTensor*, MPSGraphTensor*) block_pointer) in libtorch_cpu.a[972](BinaryOps.mm.o)
      ...
  _objc_opt_respondsToSelector, referenced from:
      at::mps::MPSDevice::isMacOS13Plus(at::mps::MacOSVersion) const in libtorch_cpu.a[958](MPSDevice.mm.o)
  _objc_release, referenced from:
      at::mps::HeapAllocator::HeapBlock::createHeapBlock(at::mps::HeapAllocator::AllocParams&amp;, id&lt;MTLDevice&gt;, unsigned int) in libtorch_cpu.a[957](MPSAllocator.mm.o)
      at::mps::HeapAllocator::MPSHeapAllocatorImpl::release_buffer(at::mps::HeapAllocator::BufferBlock*, bool) in libtorch_cpu.a[957](MPSAllocator.mm.o)
      at::mps::HeapAllocator::MPSHeapAllocatorImpl::release_buffer(at::mps::HeapAllocator::BufferBlock*, bool) in libtorch_cpu.a[957](MPSAllocator.mm.o)
      std::__1::unique_ptr&lt;at::mps::MPSDevice, std::__1::default_delete&lt;at::mps::MPSDevice&gt;&gt;::~unique_ptr[abi:v160006]() in libtorch_cpu.a[958](MPSDevice.mm.o)
      std::__1::unique_ptr&lt;at::mps::MPSDevice, std::__1::default_delete&lt;at::mps::MPSDevice&gt;&gt;::~unique_ptr[abi:v160006]() in libtorch_cpu.a[958](MPSDevice.mm.o)
      at::mps::MPSDevice::~MPSDevice() in libtorch_cpu.a[958](MPSDevice.mm.o)
      at::mps::MPSDevice::~MPSDevice() in libtorch_cpu.a[958](MPSDevice.mm.o)
      ...
  _objc_retain, referenced from:
      at::mps::MPSDevice::MPSDevice() in libtorch_cpu.a[958](MPSDevice.mm.o)
      at::mps::MPSStream::commandBuffer() in libtorch_cpu.a[965](MPSStream.mm.o)
      at::mps::MPSStream::commandEncoder() in libtorch_cpu.a[965](MPSStream.mm.o)
      at::mps::MPSStream::commandEncoder() in libtorch_cpu.a[965](MPSStream.mm.o)
      at::mps::MPSStream::commandEncoder() in libtorch_cpu.a[965](MPSStream.mm.o)
      at::mps::MPSStream::synchronize(at::mps::SyncType) in libtorch_cpu.a[965](MPSStream.mm.o)
      at::mps::MPSStream::commit() in libtorch_cpu.a[965](MPSStream.mm.o)
      ...
clang: error: linker command failed with exit code 1 (use -v to see invocation)
</code></pre>

He_Jiaxing: <p>same error on m3 pro <img src="https://discuss.pytorch.org/images/emoji/apple/joy.png?v=12" title=":joy:" class="emoji" alt=":joy:" loading="lazy" width="20" height="20">and no solution found <img src="https://discuss.pytorch.org/images/emoji/apple/cry.png?v=12" title=":cry:" class="emoji" alt=":cry:" loading="lazy" width="20" height="20"></p>