编译 pytorch

作者: , 共 1683 字 , 共阅读 0

目前 pytorch 已经升级到了 1.7.0 ,但在 ubuntu 20.04 下有一个非常诡异的 bug。为此,我们只能自己编译。

下面命令可以下载 pytorch :

git clone https://github.com/pytorch/pytorch
git checkout v1.7.0
git submodule sync
git submodule update --init --recursive

然后直接编译即可:

python setup.py build

编译好的库位于./build/lib.linux-x86_64-3.6/,有 caffe2 和 torch 两个库。为了防止和系统库冲突,可以放在/opt/python目录,然后设置环境变量(若想一直有效,该设置命令需放到/etc/environment或者~/.zshrc等命令行配置文件里):

export PYTHONPATH=/opt/python:$PYTHONPATH

接下来运行:

python -c 'import torch; print(torch.__file__)'

如果显示为/opt/python/torch/__init__.py表示安装成功。

如果把编译好的库同步到其它机器,如果 GPU 不一样,可能出现下面错误:

>>> import torch
>>> x = torch.Tensor(3, 5, 6)
>>> x.to(torch.device("cuda:0"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/python/torch/tensor.py", line 179, in __repr__
    return torch._tensor_str._str(self)
  File "/opt/python/torch/_tensor_str.py", line 372, in _str
    return _str_intern(self)
  File "/opt/python/torch/_tensor_str.py", line 352, in _str_intern
    tensor_str = _tensor_str(self, indent)
  File "/opt/python/torch/_tensor_str.py", line 241, in _tensor_str
    formatter = _Formatter(get_summarized_data(self) if summarize else self)
  File "/opt/python/torch/_tensor_str.py", line 89, in __init__
    nonzero_finite_vals = torch.masked_select(tensor_view, torch.isfinite(tensor_view) & tensor_view.ne(0))
RuntimeError: CUDA error: no kernel image is available for execution on the device

这个是因为编译版本对 CUDA 架构的支持问题,默认只支持 7.5 (我也不知道这是啥),可以在编译前设置架构:

export TORCH_CUDA_ARCH_LIST="6.0;6.1;6.2;7.0;7.5"

然后重新编译安装即可。

Q. E. D.

类似文章:
相似度: 0.146
IT » GIT
GIT 的一个很大的问题是没有权限划分,所有人对项目下所有东西都有(查看)权限(只能设置分支的推送权限),无法对特定文件和文件夹设置单独的权限。这个功能只能借用 SUBMODULE 来实现。
编程 » Python
今天写一段程序时遇到一个问题,查了好一会才搞清楚。代码可以简化为下面这个小代码:
编程 » C++, log
先编译 libfmt.a :
编程 » Python
Python 在搜索模块时,依次搜索sys.path里的位置,直到找到模块为止。下面命令可以查看当前的搜索路径:
相似度: 0.097
编程 » C++, GCC, 编译链接
C++的链接分为两部分,一个是编译时,一个是运行时。但运行时的行为也收到编译参数的影响。
Python 提供很多语法糖,用起来非常方便。@dataclass 就是其中之一。
编程 » Python
在 Python 中,有时候为了获取当前运行的脚本的同目录下的另外一个文件,会这么写:
IT » Python, Ubuntu
主要是update-alternatives命令的使用。
编程 » Python
imp.load_source在动态载入 python 模块时非常有用,但需要注意其特性。
和绿野的队伍一起去走石峡关野长城,结果走到长城上,风实在太大了,准备不周,孩子肚子疼,我们直接从花家窑子关隘沿大路回撤。轨迹已上传到两步路
这是一个很小的环线,会走一段长城,然后下到河边玩冰。