Repository: pytorch
Issue: xpu: support triton against clang with nightly wheels
Labels: triaged,module: xpu

Using XPU nightly wheel works for me to run pytorch in eager mode on Intel GPU (I used PVC). For that it seem there is no need to install oneAPI following https://www.intel.com/content/www/us/en/developer/articles/tool/pytorch-prerequisites-for-intel-gpu/2-5.html. This makes sense considering that the torch wheel includes libsycl-preview.so library which is a key dependency for XPU backend path.

However, when I try `torch.compile()`, i.e. triton, it does not work for me unless I install oneAPI. Can torch XPU wheel and maybe XPU triton build be modified in a way that oneAPI installation won't be required similar to pytorch eager mode?

There are at least these things to take care of:
- [ ] SYCL header files are missing installing torch xpu wheel
- [ ] SYCL runtime is missing (`libsycl.so`) or might need a fix on triton xpu side to switch to `libsycl-preview.so`
- [ ] Triton XPU might need fixes to support specific clang versions or requirement called out explicitly (that's based on my attempt to feed the build with SYCL headers - it still fails with not supporting `std::is_signed_v` and few other C++ features)

Tried:
```
$ cat test.py
import torch

def foo(x, y):
    a = torch.sin(x)
    b = torch.cos(y)
    return a + b

foocomp = torch.compile(foo)
t = foocomp(torch.randn(10, 10).to('xpu'), torch.randn(10, 10).to('xpu'))
print('torch compile sin+cos:', t)
```
Result:
```
$ python3 test.py
In file included from /tmp/tmp3tpt71ga/main.cpp:17:
/usr/local/lib/python3.10/dist-packages/triton/backends/intel/include/sycl_functions.h:15:10: fatal error: 'sycl/sycl.hpp' file not found
#include <sycl/sycl.hpp>
         ^~~~~~~~~~~~~~~
1 error generated.
<...>
torch._dynamo.exc.BackendCompilerFailed: backend='inductor' raised:
CalledProcessError: Command '['/usr/bin/clang++', '/tmp/tmp3tpt71ga/main.cpp', '-O3', '-shared', '-fPIC', '-Wno-psabi', '-o', '/tmp/tmp3tpt71ga/spirv_utils.cpython-310-x86_64-linux-gnu.so', '-lze_loader', '-lsycl', '-L/usr/local/lib/python3.10/dist-packages/triton/backends/intel/lib', '-I/usr/local/include', '-I/usr/local/lib/python3.10/dist-packages/triton/backends/intel/include', '-I/tmp/tmp3tpt71ga', '-I/usr/include/python3.10', '-I/usr/local/lib/python3.10/dist-packages/numpy/_core/include']' returned non-zero exit status 1.
```

Tried on docker image built with the following Dockerfile:
```
ARG IMAGE=ubuntu:22.04
FROM $IMAGE

ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y --no-install-recommends \
        gpg \
        python3 \
        python3-pip \
        wget && \
    rm -rf /var/lib/apt/lists/*

RUN pip3 install --pre torch --index-url https://download.pytorch.org/whl/nightly/xpu/
RUN pip3 install numpy

ARG DGPU_KEY_FILE=/usr/share/keyrings/intel-graphics.gpg
RUN wget -qO - https://repositories.intel.com/gpu/intel-graphics.key | \
    gpg --yes --dearmor --output $DGPU_KEY_FILE

RUN echo "deb [arch=amd64 signed-by=$DGPU_KEY_FILE] https://repositories.intel.com/gpu/ubuntu jammy/lts/2350 unified" | \
    tee /etc/apt/sources.list.d/intel-gpu.list

# Installing packages to handle the following errors.
#
# Importing torch:
#   "ImportError: libze_loader.so.1: cannot open shared object file: No such file or directory" - needs level-zero
#
# Running eager mode operations on XPU:
# * "RuntimeError: No XPU devices are available." - needs intel-level-zero-gpu
#
# Running convolution on XPU:
# * "RuntimeError: could not create an engine" - needs intel-opencl-icd
RUN apt-get update && apt-get install -y --no-install-recommends \
        intel-level-zero-gpu \
        intel-opencl-icd \
        level-zero && \
    rm -rf /var/lib/apt/lists/*

# Installing packages for torch.compile to work (as far as we can get as of now):
RUN apt-get update && apt-get install -y --no-install-recommends \
        clang && \
        level-zero-dev && \
    rm -rf /var/lib/apt/lists/*
```

Versions in the build I've tried:
```
# pip3 list
Package            Version
------------------ ---------------------
filelock           3.13.1
fsspec             2024.6.1
Jinja2             3.1.4
MarkupSafe         2.1.5
mpmath             1.3.0
networkx           3.3
numpy              2.1.2
packaging          22.0
pip                22.0.2
Pygments           2.11.2
pytorch-triton-xpu 3.1.0+91b14bf559
PyYAML             5.4.1
setuptools         59.6.0
sympy              1.13.1
torch              2.6.0.dev20241007+xpu
typing_extensions  4.12.2
wheel              0.37.1
```

CC: @gujinghui @EikanWang @fengyuan14 @guangyey @jgong5 @vlad-penkin

cc @gujinghui @EikanWang @fengyuan14 @guangyey