Search code examples
c++macosarmlibtorch

How to build libtorch on mac arm?


i downloaded libtorch and make these files on macbook pro ARM:

example-app/
     build/
     libtorch/
     CMakeLists.txt
     example-app.cpp

then i used these commands for build torch:

cmake -DCMAKE_PREFIX_PATH=/path-to-example-app/example-app/libtorch
make

and i get this error:

building for macOS-x86_64 but attempting to link with file built for unknown-arm64

can you help me?


Solution

  • I was able to build libtorch library from source on my Mac M1 and run the C++ example-app project that you are looking at

    (https://medium.com/@albertsundjaja/installing-pytorch-c-api-d52c722f47ec)

    I did it with the following steps:

    % git clone -b master --recurse-submodule https://github.com/pytorch/pytorch.git
    % mkdir pytorch-build
    % cd pytorch-build 
    % cmake -DBUILD_SHARED_LIBS:BOOL=ON -DCMAKE_BUILD_TYPE:STRING=Release -DPYTHON_EXECUTABLE:PATH=`which python3` -DCMAKE_INSTALL_PREFIX:PATH=../pytorch-install ../pytorch
    
    % cmake --build . --target install 
    

    The above process creates a directory called pytorch-install. Copy files from the following directories in pytorch-install to respective directories in /example-app/libtorch/

    bin
    include
    lib
    share

    Important Note: replace files not directories or some needed files from original libtorch will be lost.

    Once I do this, I can run the following command per the tutorial:

    % cmake -DCMAKE_PREFIX_PATH=/path-to-example-app/example-app/libtorch ..
        make
    

    then running the command

    % ./example-app
    

    I get

     0.1816  0.6954  0.8272
     0.7898  0.0256  0.1385
    [ CPUFloatType{2,3} ]