libTorch M1 build - duplicate symbols and other errors

Aaron_Leese: <p>Hi all!</p>
<p>I’m trying to compile libTorch, but finding errors no matter how I build it.<br>
This is on an <strong>M1 mac</strong>.</p>
<p>I’ve tried all the various recommend methods from the official docs, here:</p><aside class="onebox githubblob" data-onebox-src="https://github.com/pytorch/pytorch/blob/master/docs/libtorch.rst">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/blob/master/docs/libtorch.rst" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/pytorch/blob/master/docs/libtorch.rst" target="_blank" rel="noopener nofollow ugc">pytorch/pytorch/blob/master/docs/libtorch.rst</a></h4>


      <pre><code class="lang-rst">libtorch (C++-only)
===================

The core of pytorch does not depend on Python. A
CMake-based build system compiles the C++ source code into a shared
object, libtorch.so.

Building libtorch using Python
------------------------------

You can use a python script/module located in tools package to build libtorch
::
   cd &lt;pytorch_root&gt;

   # Make a new folder to build in to avoid polluting the source directories
   mkdir build_libtorch &amp;&amp; cd build_libtorch

   # You might need to export some required environment variables here.
   Normally setup.py sets good default env variables, but you'll have to do
   that manually.
</code></pre>



  This file has been truncated. <a href="https://github.com/pytorch/pytorch/blob/master/docs/libtorch.rst" target="_blank" rel="noopener nofollow ugc">show original</a>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::<br>
Trying the first method:<br>
<code>python ../tools/build_libtorch.py</code></p>
<p>I get a bunch of undefined symbols error:</p>
<pre><code class="lang-auto">
Undefined symbols for architecture arm64:
  "_MTLCopyAllDevices", referenced from:
Undefined symbols for architecture arm64:
  "_MTLCopyAllDevices", referenced from:
      at::mps::MPSDevice::MPSDevice() in libtorch_cpu.a(MPSDevice.mm.o)
Undefined symbols for architecture arm64:
  "_MTLCopyAllDevices", referenced from:
      at::mps::MPSDevice::MPSDevice() in libtorch_cpu.a(MPSDevice.mm.o)
      at::mps::MPSDevice::MPSDevice() in libtorch_cpu.a(MPSDevice.mm.o)
</code></pre>
<p>Guessing that maybe some subcomponent is compiling for x86_64 instead of arm? Maybe there is a flag necessary to force <strong>arm/M1</strong> compilation?</p>
<p>:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::</p>
<p>Using either other method (<strong>CMAKE</strong> or <strong>Setup.py</strong>, both outlined in the docs) it seems to get further (92%) but then dies in a linking phase with a bunch of duplicate symbols errors like this:</p>
<pre><code class="lang-auto">**duplicate symbol 'at::native::DispatchStub&lt;void (*)(at::TensorIteratorBase&amp;, c10::Scalar const&amp;), at::native::leaky_relu_backward_stub&gt;::DEFAULT' in:
    CMakeFiles/torch_cpu.dir/__/aten/src/ATen/native/cpu/Activation.cpp.DEFAULT.cpp.DEFAULT.cpp.o
    CMakeFiles/torch_cpu.dir/__/aten/src/ATen/native/cpu/Activation.cpp.DEFAULT.cpp.o
**
</code></pre>
<p>Looking at those files, they both just include the “Activation.cpp”. Why exactly you would have 2 files both including some other file is not something I understand, so maybe someone else knows how to make sense of this?</p>