Omp.h not found running some dynamo tests

I am on an M1 mac

Error when trying to run test python test/dynamo/

#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(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:

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 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/

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 Of course you need to run it something like . ../tools/ 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/

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
                conda_lib_path = os.path.join(os.getenv("CONDA_PREFIX"), "lib")
                ipaths.append(os.path.join(os.getenv("CONDA_PREFIX"), "include"))
                # 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"]
            libs = ["omp"] if config.is_fbcode() else ["gomp"]

So a circumvention for me is to change line
libs = ["omp"]
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)

the following now succeeds:
python test/dynamo/