Omp.h not found running some dynamo tests

davidradl: <p>I am on an M1 mac</p>
<p>Error when trying to run test python test/dynamo/test_backends.py</p>
<pre><code class="lang-auto">#include &lt;omp.h&gt;
         ^~~~~~~
1 error generated.
</code></pre>
<p><span class="hashtag-raw">#include</span> &lt;omp.h&gt;<br>
^~~~~~~<br>
1 error generated.<br>
You can suppress this exception and fall back to eager by setting:<br>
import torch._dynamo<br>
torch._dynamo.config.suppress_errors = True</p>
<p>I also get this error running</p>
<pre><code class="lang-auto">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")
</code></pre>
<p>The error message says that this should fall be to eager, but it fails if I set eager.</p>
<p>I notice this seems topic has been previously raised:<br>
<a href="https://discuss.pytorch.org/t/pytorch-2-0-compile-problem-in-mac/177330">https://discuss.pytorch.org/t/pytorch-2-0-compile-problem-in-mac/177330</a></p>
<p>Any thoughts?</p>

davidradl: <p>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 =&gt; so the header is not resolved, because it does not see <code>/opt/homebrew/Cellar/llvm/16.0.6/lib/clang/16/include</code> . 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.</p>
<p><code>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</code></p>

davidradl: <p>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.</p>
<p>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 <code>. ../tools/llvmEnv.sh</code> so the current environment is updated.</p>
<pre><code class="lang-auto">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}
</code></pre>
<p>I am now seeing <code>library not found for -lomp</code> which seems better but not there yet<br>
<code>/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</code></p>
<p>if I remove the <code>-lomp</code> it compiles. I just need to find out who is adding the <code>-lomp</code></p>

davidradl: <p>the option omp is added in the codecache code - but is not recognised on my machine.</p>
<pre><code class="lang-auto">    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"]
</code></pre>

davidradl: <p>So a circumvention for me is to change line<br>
<code> libs = ["omp"]</code><br>
to<br>
<code> libs = []</code></p>
<p>it then works for me.</p>

davidradl: <p>After pr <a href="https://github.com/pytorch/pytorch/pull/105136" class="inline-onebox" rel="noopener nofollow ugc">[inductor] Suport OMP on MacOS by nkflash · Pull Request #105136 · pytorch/pytorch · GitHub</a>  there is a new flag that can be set. On the terminal, I now run:</p>
<p>brew install libomp<br>
export OMP_PREFIX=$(brew --prefix libomp)<br>
USE_DISTRIBUTED=0  CMAKE_OSX_ARCHITECTURES=arm64<br>
MACOSX_DEPLOYMENT_TARGET=11.0 USE_MKLDNN=OFF USE_QNNPACK=OFF WERROR=1 BUILD_TEST=OFF USE_PYTORCH_METAL=1 python setup.py develop</p>
<p>the following now succeeds:<br>
python test/dynamo/test_backends.py</p>