在装机文里面提到了这款 Intel Arc 770 16G 的显卡,买来主要是为了试试 ML 的一些新鲜玩意。看着隔壁 N 卡不费吹灰之力就能跑起来,实在有点后悔入了这款。不过好在 debian 内核已经支持了较新的驱动,少了一步折腾驱动程序的苦恼。
真正的初
机器刚到的时候试图折腾过一次,那个时候好像文档、参考资料还比较少,折腾了一气之后发现似乎不 work,也没搞清楚为啥,不过感觉 Intel 的 github 上都是一些国内的小伙伴,提过两问题好像答复的都很快。不过还是依稀记得有几个编译时的问题
- 某个 AVX512 相关的实现,因为我是 AMD Ryzen 的 CPU,不支持这个指令集,结果代码编译到那里说是因为 GCC 12 的 regression 导致某个 warning,变成了错误,需要改编译参数
- 后面似乎某个 test 编译不过去,干脆取消了 test
编译是个耗时的事情,pytorch 跟 intel 的 extension 都很慢,等得慌。
重试
隔了这么几个月,觉得是不是该重新试试。这次换了 virtualenvwrapper,方便写 postactivate 脚本。大概安装的流程是这样的
- 安装 intel oneapi,这竟然可以装 XX Gb,疯掉了吧
- 编译 torch 三件套和 intel extension for pytorch
- 安装下游库,比如 whisper 之类的
第一步基本按照 intel 自己的 repo 提供的指令,很快就能搞定。装好之后这些东西会放在 /opt/intel/oneapi/ 里面,一般如果想激活所有的东西的话可以
$ source /opt/intel/oneapi/setvars.sh
:: initializing oneAPI environment ...
bash: BASH_VERSION = 5.2.15(1)-release
args: Using "$@" for setvars.sh arguments: post_activate
:: advisor -- latest
:: ccl -- latest
:: compiler -- latest
:: dal -- latest
:: debugger -- latest
:: dev-utilities -- latest
:: dnnl -- latest
:: dpcpp-ct -- latest
:: dpl -- latest
:: ipp -- latest
:: ippcp -- latest
:: ipp -- latest
:: mkl -- latest
:: mpi -- latest
:: tbb -- latest
:: vtune -- latest
:: oneAPI environment initialized ::
但是某些情况下可以仅仅打开部分 library(每个部分有个自己的子目录,里面也有类似的脚本)。
第二步,我是参考这篇,它的核心部分(我需要 GPU)在这里,但是我做了一些改变
- 使用系统自带的 llvm-13,apt install llvm-13,因此我知道 LLVM_ROOT=/usr/lib/llvm-13
- 提前装好 libpng-dev 与 libjpeg62-turbo-dev,这个大约 torchvision 会用
安装的时候尽量把环境变量放在一行里面,而不要使用 export(因为这样的话根本不晓得什么地方要用什么)。下面是编译过程:pytorch 是最烦人的,看看下面的命令就晓得了
USE_LLVM=/usr/lib/llvm-13 \
LLVM_DIR=/usr/lib/llvm-13/lib/cmake/llvm \
CMAKE_PREFIX_PATH=${VIRTUAL_ENV:-"$(dirname $(command -v python))/../"} \
USE_STATIC_MKL=1 \
_GLIBCXX_USE_CXX11_ABI=1 \
USE_NUMA=0 \
USE_CUDA=0 \
CFLAGS+=" -Wno-error=maybe-uninitialized -Wno-error=uninitialized -Wno-error=restrict" \
CXXFLAGS+=" -Wno-error=maybe-uninitialized -Wno-error=uninitialized -Wno-error=restrict" \
BUILD_TEST=0 \
python setup.py bdist_wheel
vision 最简单,因为只需要跟系统的 jpeg/png 库连接。audio 似乎需要跟 mkl 连接,所以得
source /opt/intel/oneapi/mkl/latest/env/vars.sh
python setup.py bdist_wheel
最后编译 intel extension
source /opt/intel/oneapi/setvars.sh
USE_LLVM=/usr/lib/llvm-13 LLVM_DIR=/usr/lib/llvm-13/lib/cmake/llvm DNNL_GRAPH_BUILD_COMPILER_BACKEND=1 USE_AOT_DEVLIST='ats-m150' python setup.py bdist_wheel
这里 AOT 要跟显卡型号匹配。这样测试了一下(记得要设置 /opt/intel/oneapi/setvars.sh,我直接放在 postactivate 脚本里面了)
python -c "import torch; import torchvision; import torchaudio; import intel_extension_for_pytorch as ipex; print(f'torch_cxx11_abi: {torch.compiled_with_cxx11_abi()}'); print(f'torch_version: {torch.__version__}'); print(f'torchvision_version: {torchvision.__version__}'); print(f'torchaudio_version: {torchaudio.__version__}'); print(f'ipex_version: {ipex.__version__}');"
torch_cxx11_abi: True
torch_version: 2.0.0a0+gite9ebda2
torchvision_version: 0.15.2a0+fa99a53
torchaudio_version: 2.0.2+31de77d
ipex_version: 2.0.110+gitf30a9a6
另外还可以
# in python interpreter
import pytorch
torch.xpu
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'torch' has no attribute 'xpu'
import intel_extension_for_pytorch as ipex
torch.xpu
<module 'intel_extension_for_pytorch.xpu' from '/virtual/env/lib/python3.11/site-packages/intel_extension_for_pytorch/xpu/__init__.py'>
可是我们怎么在下游的库/程序里面使用 GPU 呢?比如试试 whisper 行吗?答案是可以,但是得做点什么。比如这个 PR,可以看见通过测试是否安装了 intel_extension_for_pytorch 来激活这个 xpu 的支持。可惜这个不能自动的识别出来,看起来有点讨厌,这么几个月了也没 merge 进去
后话
也许我们可以复刻 tensorflow / jax,但是的确可能是后来者,大家已经习惯了 N 卡种种便利,Intel 任重而道远。
tensorflow
神奇的是似乎 TF 安装比想象的容易,这次直接上 pip 就搞定了
pip install tensorflow==2.13.0
pip install --upgrade intel-extension-for-tensorflow[xpu]
下面就能看见
import tensorflow as tf
# 2023-08-23 22:27:31.512594: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.
# 2023-08-23 22:27:31.532775: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.
# 2023-08-23 22:27:31.533009: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
# 2023-08-23 22:27:31.969931: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
# 2023-08-23 22:27:32.198645: I itex/core/wrapper/itex_cpu_wrapper.cc:52] Intel Extension for Tensorflow* AVX2 CPU backend is loaded.
# 2023-08-23 22:27:32.464141: I itex/core/wrapper/itex_gpu_wrapper.cc:35] Intel Extension for Tensorflow* GPU backend is loaded.
# 2023-08-23 22:27:32.484690: W itex/core/ops/op_init.cc:58] Op: _QuantizedMaxPool3D is already registered in Tensorflow
# 2023-08-23 22:27:32.506849: I itex/core/devices/gpu/itex_gpu_runtime.cc:129] Selected platform: Intel(R) Level-Zero
# 2023-08-23 22:27:32.507020: I itex/core/devices/gpu/itex_gpu_runtime.cc:154] number of sub-devices is zero, expose root device.
tf.config.list_physical_devices()
# [PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU'), PhysicalDevice(name='/physical_device:XPU:0', device_type='XPU')]
JAX
参考这篇,似乎走到
bazel build //xla/tools/pip_package:build_pip_package
# ...
/usr/bin/ld: cannot find -lstdc++: No such file or directory
icx: error: linker command failed with exit code 1 (use -v to see invocation)
Target //xla/tools/pip_package:build_pip_package failed to build
就挂了,而且为啥没见到 Arc 在支持的列表中 😦 而且我系统里面明明有 llvm 为啥又去下载 build 还挂了,无语。我系统里面可是有 libstdc++ 的呀
ls -l /usr/lib/gcc/x86_64-linux-gnu/12/libstdc++.so
lrwxrwxrwx 1 root root 40 Jul 10 08:11 /usr/lib/gcc/x86_64-linux-gnu/12/libstdc++.so -> ../../../x86_64-linux-gnu/libstdc++.so.6