# Can we add a default backend when OpenMP is not available?

**URL:** <https://dev-discuss.pytorch.org/t/can-we-add-a-default-backend-when-openmp-is-not-available/1382>\
**Category:** compiler\
**Created:** [July 19, 2023, 1:17pm UTC](https://dev-discuss.pytorch.org/t/can-we-add-a-default-backend-when-openmp-is-not-available/1382 "2023-07-19T13:17:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![youkaichao](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/youkaichao/32/1278_2.png) [@youkaichao](https://dev-discuss.pytorch.org/u/youkaichao)\
**Post date:** [July 19, 2023, 1:17pm UTC](https://dev-discuss.pytorch.org/t/can-we-add-a-default-backend-when-openmp-is-not-available/1382/1 "2023-07-19T13:17:06Z")

</div>

Hi, team, when I try to test the `torch.compile` using the following code on my macbookpro:

```python
import torch
from torch import nn

class BackboneModel(nn.Module):

    def __init__ (self, *args, **kwargs) -> None:
        super(). __init__ (*args, **kwargs)
        self.conv1 = nn.Conv2d(6, 6, 6)
        self.bn1 = nn.BatchNorm2d(6)
        self.conv2 = nn.Conv2d(6, 6, 6)
        self.bn2 = nn.BatchNorm2d(6)
        self.conv3 = nn.Conv2d(6, 6, 6)
        self.bn3 = nn.BatchNorm2d(6)

    def forward(self, x):
        # this conv-bn pair can use efficient_conv_bn_eval feature
        x = self.bn1(self.conv1(x))
        # this conv-bn pair cannot use efficient_conv_bn_eval feature
        # because `self.conv2` is used twice
        x = self.bn2(self.conv2(self.conv2(x)))
        # this conv-bn pair can use efficient_conv_bn_eval feature
        # just for the first forward of the `self.bn3`
        x = self.bn3(self.bn3(self.conv3(x)))
        return x

model = BackboneModel()
model.eval()
input = torch.randn(64, 6, 32, 32)

opt_model = torch.compile(model)
output = opt_model(input)

```

I got tons of error outputs, seemingly to complain that `omp.h` is not found.

Can we add a default fallback backend to just return the `forward` function from `fx.GraphModule` when the chosen backend does not work?

---

<div class="post-metadata">

**Author:** ![youkaichao](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/youkaichao/32/1278_2.png) [@youkaichao](https://dev-discuss.pytorch.org/u/youkaichao)\
**Post date:** [July 19, 2023, 1:38pm UTC](https://dev-discuss.pytorch.org/t/can-we-add-a-default-backend-when-openmp-is-not-available/1382/2 "2023-07-19T13:38:52Z")

</div>

It seems my compiler `clang++` does not support `openmp` natively. I have to `brew install libomp`, and add `g++ -Xclang -fopenmp -lomp -L/opt/homebrew/opt/libomp/lib -I/opt/homebrew/opt/libomp/include a.c -o a.out` to compile a hello-world example from [c - How to install OpenMP on Mac M1? - Stack Overflow](https://stackoverflow.com/questions/71061894/how-to-install-openmp-on-mac-m1) .

How can I pass these information into the `inductor` backend?

cc @ezyang

---

<div class="post-metadata">

**Author:** ![jansel](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/jansel/32/74_2.png) [@jansel](https://dev-discuss.pytorch.org/u/jansel)\
**Post date:** [July 20, 2023, 9:08pm UTC](https://dev-discuss.pytorch.org/t/can-we-add-a-default-backend-when-openmp-is-not-available/1382/3 "2023-07-20T21:08:40Z")

</div>

This might be fixed by [[inductor] Suport OMP on MacOS by nkflash · Pull Request #105136 · pytorch/pytorch · GitHub](https://github.com/pytorch/pytorch/pull/105136)

---

<div class="post-metadata">

**Author:** ![jansel](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/jansel/32/74_2.png) [@jansel](https://dev-discuss.pytorch.org/u/jansel)\
**Post date:** [July 20, 2023, 9:35pm UTC](https://dev-discuss.pytorch.org/t/can-we-add-a-default-backend-when-openmp-is-not-available/1382/4 "2023-07-20T21:35:13Z")

</div>

Also, if the above PR isn’t what you want, if you set threads to 1:

> <https://github.com/pytorch/pytorch/blob/fe7187b903a5452bd64652e1e6b5e0b2d206354b/torch/_inductor/config.py#L235-L236>

Then the generated code shouldn’t use OpenMP.

Though in the single threaded case Inductor still might try to include the headers and link flags, which can be safely removed in that case since the generated code won’t need them. A PR to put `if threads>1` around those bits would be welcome if the single-threaded-without-openmp use case is interesting to you.

---

<div class="post-metadata">

**Author:** ![youkaichao](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/youkaichao/32/1278_2.png) [@youkaichao](https://dev-discuss.pytorch.org/u/youkaichao)\
**Post date:** [July 21, 2023, 7:42am UTC](https://dev-discuss.pytorch.org/t/can-we-add-a-default-backend-when-openmp-is-not-available/1382/5 "2023-07-21T07:42:01Z")

</div>

@jansel Oh, yes, that PR can fix the issue.

I need to cherry-pick that PR code, and `export OMP_PREFIX=/opt/homebrew/opt/libomp` to make it work.

Is `OMP_PREFIX` a standard environment variable? I didn’t get this environment variable after `brew install libomp`. It would be better to document somewhere what MacOS users should do, whether to `conda install llvm-openmp` or specify `OMP_PREFIX` after `brew install libomp`.

---

<div class="post-metadata">

**Author:** ![jansel](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/jansel/32/74_2.png) [@jansel](https://dev-discuss.pytorch.org/u/jansel)\
**Post date:** [July 21, 2023, 3:44pm UTC](https://dev-discuss.pytorch.org/t/can-we-add-a-default-backend-when-openmp-is-not-available/1382/6 "2023-07-21T15:44:18Z")

</div>

Good suggestion, I added it to the issue: [Dynamo + MacOS: fatal error: 'omp.h' file not found · Issue #95708 · pytorch/pytorch · GitHub](https://github.com/pytorch/pytorch/issues/95708#issuecomment-1645783401)
