由于相关的基础设施已经完善,so 我们开整。
目前 cpu
版本 torch torchaudio torchvision 的移植工作已经于 2025年04月27日 全部完成
pip install -U pip
pip install pytorch numpy pandas --extra-index-url https://mirrors.loong64.com/pypi/simple
下面是移植过程的相关记录:
torch
需要的补丁:
注:cpuinfo
的补丁是基于 https://github.com/pytorch/cpuinfo/pull/92 修改,由链接的不知名大佬 33749110 提交。
torch
需要处理依赖:
torch
├── numpy # https://github.com/loong64/pypi/blob/master/.github/workflows/numpy.yml
│ └── scipy-openblas # https://github.com/loong64/pypi/blob/master/.github/workflows/scipy-openblas.yml
├── optree # https://github.com/loong64/pypi/blob/master/.github/workflows/optree.yml
└── pyyaml # https://github.com/loong64/pypi/blob/master/.github/workflows/PyYAML.yml
ninja-build
和 cmake
这些包可以直接从仓库安装。
比较难处理的是 scipy-openblas 尽管做了最小化支持,但是在测试流程依然有错误,需要上游处理。
构建完依赖后可以正式开始构建 torch
官方推荐的 源代码构建方式 是使用 conda
,然而我们并不能使用 conda
,而且由于 pytorch
官方自己维护了一套构建的流水线,不使用 cibuildwheel
进行构建,可参考 (https://github.com/pytorch/pytorch/tree/main/.ci/docker)。因此我们也维护了一个 基础镜像 专门用于构建 pytorch
组织下面的其他包(audio
、vison
等)。
准备就绪后正常使用 cibuildwheel
构建即可,可参考
cd /opt
wget https://github.com/pytorch/pytorch/releases/download/v2.7.0/pytorch-v2.7.0.tar.gz
tar -xf pytorch-v2.7.0.tar.gz
cd /opt/pytorch-v2.7.0
mkdir -p .ci/docker/ci_commit_pins
wget -qO .ci/docker/ci_commit_pins/nccl-cu12.txt https://github.com/pytorch/pytorch/raw/refs/tags/v2.7.0/.ci/docker/ci_commit_pins/nccl-cu12.txt
cd /opt/pytorch-v2.7.0/third_party/sleef
wget -qO - https://github.com/loong64/pytorch/raw/refs/heads/main/sleef/sleef_loong64.patch | patch -p1
cd /opt/pytorch-v2.7.0/third_party/cpuinfo
wget -qO - https://github.com/loong64/pytorch/raw/refs/heads/main/cpuinfo/cpuinfo_loong64.patch | patch -p1
cd /opt/pytorch-v2.7.0
CIBW_ENVIRONMENT_LINUX='BUILD_TEST=0 PYTORCH_BUILD_NUMBER=1 PYTORCH_BUILD_VERSION=2.7.0 PIP_EXTRA_INDEX_URL="https://gitlab.com/api/v4/projects/65746188/packages/pypi/simple"' \
CIBW_TEST_SKIP="*"\
CIBW_MANYLINUX_LOONGARCH64_IMAGE=ghcr.io/loong64/manylinuxloongarch64-builder:cpu-loongarch64 \
cibuildwheel --output-dir ../wheelhouse/
torchvision
和 torchaudio
最大的问题就是得先构建 torch
,其次是 ffmpeg
,最后是 auditwheel
修补。
ffmpeg
涉及到许可的相关问题,官方目前给出了构建脚本。
torchvision
的 ffmpeg 构建脚本 build.sh,我们拿过来改改用一用 install_ffmpeg.sh
torchaudio
的 ffmpeg 构建脚本,我们拿过来改改用一用 ffmpeg.yml
然后通过补丁修补 ffmpeg_loong64.patch
torchvision
和 torchaudio
都使用由 pytorch
组织自身维护的修补脚本 repair_manylinux_2_28.sh,因此我们需要照葫芦画瓢制作一份 repair_manylinux_2_38.sh,然后再使用 relocate.py 进行二次修补。
但是 audio
并没有这个脚本,我们抄一份随便改改就能用 relocate.py
再然后由于 https://github.com/pypa/auditwheel/issues/561 版本兼容性问题,我们只能使用 auditwheel <= 6.2.0
。
再再然后因为 auditwheel
从 6.3.0
版本开始才开始支持龙架构,所以还需要移植一下 auditwheel。
最终,他们的构建命令如下:
cd /opt
git clone --recursive --depth=1 -b v0.22.0 https://github.com/pytorch/vision
cd /opt/vision
CIBW_BEFORE_BUILD_LINUX="pip install https://github.com/loong64/auditwheel/releases/download/6.2.0/auditwheel-6.2.0-py3-none-any.whl" \
CIBW_ENVIRONMENT_LINUX='BUILD_VERSION=0.22.0 PIP_EXTRA_INDEX_URL="https://gitlab.com/api/v4/projects/65746188/packages/pypi/simple"' \
CIBW_REPAIR_WHEEL_COMMAND_LINUX='\
mkdir -p dist {dest_dir} && \
cp -f {wheel} dist && \
for pkg in dist/*-linux_*.whl; do \
if [[ ! -e $pkg ]]; then \
continue; \
fi; \
abs_pkg=$(realpath $pkg); \
repair_manylinux_2_38.sh $abs_pkg; \
done && \
python packaging/wheel/relocate.py && \
cp -f dist/*.whl {dest_dir} \
CIBW_MANYLINUX_LOONGARCH64_IMAGE=ghcr.io/loong64/manylinuxloongarch64-builder:cpu-loongarch64 \
cibuildwheel --output-dir ../wheelhouse/
cd /opt
git clone --recursive --depth=1 -b v2.7.0 https://github.com/pytorch/audio
cd /opt/audio
mkdir -p packaging/wheel
wget -qO - https://github.com/loong64/audio/raw/refs/heads/main/ffmpeg_loong64.patch | patch -p1
wget -q -O packaging/wheel/relocate.py https://github.com/loong64/audio/raw/refs/heads/main/relocate.py
CIBW_BEFORE_BUILD_LINUX="pip install torch==2.7.0 wheel https://github.com/loong64/auditwheel/releases/download/6.2.0/auditwheel-6.2.0-py3-none-any.whl" \
CIBW_ENVIRONMENT_LINUX='BUILD_VERSION=2.7.0 PIP_EXTRA_INDEX_URL="https://gitlab.com/api/v4/projects/65746188/packages/pypi/simple"' \
CIBW_REPAIR_WHEEL_COMMAND_LINUX='\
mkdir -p dist {dest_dir} && \
cp -f {wheel} dist && \
for pkg in dist/*-linux_*.whl; do \
if [[ ! -e $pkg ]]; then \
continue; \
fi; \
abs_pkg=$(realpath $pkg); \
repair_manylinux_2_38.sh $abs_pkg; \
done && \
python packaging/wheel/relocate.py && \
cp -f dist/*.whl {dest_dir} \
CIBW_MANYLINUX_LOONGARCH64_IMAGE=ghcr.io/loong64/manylinuxloongarch64-builder:cpu-loongarch64 \
cibuildwheel --output-dir ../wheelhouse/
还有一些其他的依赖,比如 onnx
、pillow
、pandas
等等,基本上没有啥问题,参考 (https://github.com/loong64#pypi-repository) 即可