Omp.h not found running some dynamo tests

I am on an M1 mac

Error when trying to run test python test/dynamo/test_backends.py

#include <omp.h>
         ^~~~~~~
1 error generated.

#include <omp.h>
^~~~~~~
1 error generated.
You can suppress this exception and fall back to eager by setting:
import torch._dynamo
torch._dynamo.config.suppress_errors = True

I also get this error running

import torch
import torch._dynamo
def f(x):
    return (x + x).to(torch.int16)

x = torch.tensor(128, dtype=torch.uint8)
print(f(x))
print(torch.compile(f)(x), backend="aot_eager")

The error message says that this should fall be to eager, but it fails if I set eager.

I notice this seems topic has been previously raised:
https://discuss.pytorch.org/t/pytorch-2-0-compile-problem-in-mac/177330

Any thoughts?

I have LDFLAGS=“-L/opt/homebrew/Cellar/llvm/16.0.6/lib” CPPFLAGS=”-I/opt/homebrew/Cellar/llvm/16.0.6/lib/clang/16/include” as variables in the setup.py But these do not seem to make it into the dynamo compile => so the header is not resolved, because it does not see /opt/homebrew/Cellar/llvm/16.0.6/lib/clang/16/include . I am not sure how to get these llvm folders to be seen by the dynamo compile. It says that the compile it is trying to so is as follows - so I can see if does not have the llvm include folder.

g++ /var/folders/wv/pmg4pqgs0kxb2471qr3bplzh0000gn/T/torchinductor_davidradley/xi/cxi34hceq2g75s7ffa3h3of4thg7jhh2tki6lctdfii4pijze4av.cpp -shared -fPIC -Wall -std=c++17 -Wno-unused-variable -I/Users/davidradley/pytorch/torch/include -I/Users/davidradley/pytorch/torch/include/torch/csrc/api/include -I/Users/davidradley/pytorch/torch/include/TH -I/Users/davidradley/pytorch/torch/include/THC -I/Users/davidradley/miniconda3/include/python3.10 -I/Users/davidradley/miniconda3/include -L/Users/davidradley/miniconda3/lib -lomp -O3 -ffast-math -fno-finite-math-only -Xclang -fopenmp -D C10_USING_CUSTOM_GENERATED_MACROS -o /var/folders/wv/pmg4pqgs0kxb2471qr3bplzh0000gn/T/torchinductor_davidradley/xi/cxi34hceq2g75s7ffa3h3of4thg7jhh2tki6lctdfii4pijze4av.so

The issue appears to be that I had installed xcode. The trick seems to be be to try to force the real Clang to be picked up over the apple supplied one, that does not play well with omp.

I ended up creating a batch file that sets the compiler environment variables to point to the brew llvm. So I run this prior to running the test involving the compiler. This forces the brew installed llvm to be used. The bash script I use is llvmEnv.sh. Of course you need to run it something like . ../tools/llvmEnv.sh so the current environment is updated.

export LLVM=$(brew --prefix llvm)
export PATH="${LLVM}/bin:$PATH";
export COMPILER=${LLVM}/bin/clang++
export CFLAGS="-I /usr/local/include -I${LLVM}/include"
export CXXFLAGS="-I/usr/local/include -I${LLVM}/include"
export LDFLAGS="-L/usr/local/lib -L${LLVM}/lib"
export CXX=${COMPILER}

I am now seeing library not found for -lomp which seems better but not there yet
/opt/homebrew/opt/llvm/bin/clang++ /var/folders/wv/pmg4pqgs0kxb2471qr3bplzh0000gn/T/torchinductor_davidradley/ww/cwwa7rwekb5fdmzhngrduq3mfsuinbo5qqldlgof2pzwu7kb2l3c.cpp -shared -fPIC -Wall -std=c++17 -Wno-unused-variable -I/Users/davidradley/pytorch/torch/include -I/Users/davidradley/pytorch/torch/include/torch/csrc/api/include -I/Users/davidradley/pytorch/torch/include/TH -I/Users/davidradley/pytorch/torch/include/THC -I/Users/davidradley/miniconda3/include/python3.10 -I/Users/davidradley/miniconda3/include -L/Users/davidradley/miniconda3/lib -lomp -O3 -ffast-math -fno-finite-math-only -Xclang -fopenmp -D C10_USING_CUSTOM_GENERATED_MACROS -o /var/folders/wv/pmg4pqgs0kxb2471qr3bplzh0000gn/T/torchinductor_davidradley/ww/cwwa7rwekb5fdmzhngrduq3mfsuinbo5qqldlgof2pzwu7kb2l3c.so

if I remove the -lomp it compiles. I just need to find out who is adding the -lomp

the option omp is added in the codecache code - but is not recognised on my machine.

    if sys.platform == "darwin":
            # GNU OpenMP generally is not available on MacOS
            # There is either Intel OpenMP(for x86) or LLVM OpenMP (for both x86 and arm64)
            libs = ["omp"]
            if os.getenv("CONDA_PREFIX") is not None:
                # On MacOS OpenMP is not available via the system install
                # But on conda can be provided using https://anaconda.org/anaconda/llvm-openmp
                conda_lib_path = os.path.join(os.getenv("CONDA_PREFIX"), "lib")
                ipaths.append(os.path.join(os.getenv("CONDA_PREFIX"), "include"))
                lpaths.append(conda_lib_path)
                # Prefer Intel OpenMP on x86 machine
                if os.uname().machine == "x86_64" and os.path.exists(
                    os.path.join(conda_lib_path, "libiomp5.dylib")
                ):
                    libs = ["iomp5"]
        else:
            libs = ["omp"] if config.is_fbcode() else ["gomp"]

So a circumvention for me is to change line
libs = ["omp"]
to
libs = []

it then works for me.

After pr [inductor] Suport OMP on MacOS by nkflash · Pull Request #105136 · pytorch/pytorch · GitHub there is a new flag that can be set. On the terminal, I now run:

brew install libomp
export OMP_PREFIX=$(brew --prefix libomp)
USE_DISTRIBUTED=0 CMAKE_OSX_ARCHITECTURES=arm64
MACOSX_DEPLOYMENT_TARGET=11.0 USE_MKLDNN=OFF USE_QNNPACK=OFF WERROR=1 BUILD_TEST=OFF USE_PYTORCH_METAL=1 python setup.py develop

the following now succeeds:
python test/dynamo/test_backends.py