前言

最近想学习一下 Huggingface 的 Transformers 库的使用,需要重新搭建一个虚拟环境,做简单记录方便之后工作。

正文

这次搭建主要是平时测试和玩的环境,不考虑为某某应用兼容用低版本,看一眼 python 的版本。

 差不多是一年一个小版本号呀,3.7 再有一年停止维护了,打算用 3.8 了。

conda create -n dev38 python=3.8

create 命令创建环境。这里 miniconda 的安装,还有 channel 的设置就不赘述了。

提示 conda 需要更新,顺手更新一下 conda。

conda update -n base -c defaults conda

❯ python --version
Python 3.8.13

看了一下,安装了 3.8.13 版本的 python。

然后先安装 pytorch 再说 Transformers。PyTorch 的安装需要 cudatoolkit 来利用显卡。

本机因为之前安装过 cudatoolkit,先检查一下。

nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2020 NVIDIA Corporation
Built on Mon_Oct_12_20:09:46_PDT_2020
Cuda compilation tools, release 11.1, V11.1.105
Build cuda_11.1.TC455_06.29190527_0

可以看到本机安装的是 cuda 11.1 。

但是 pytorch 官网似乎没有给这个选择。

不过看了一下,应该改一下 cudatoolkit 那一部分就好。 先试试安装 CUDA 11.1 。

conda install pytorch torchvision torchaudio cudatoolkit=11.1

这里把 -c 的部分去掉了,这部分大概是选择下载源,我印象里用 pytorch 官方源特别慢,这里关了试试。

❯ conda install pytorch torchvision torchaudio cudatoolkit=11.1
Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Collecting package metadata (repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.

PackagesNotFoundError: The following packages are not available from current channels:

  - cudatoolkit=11.1

Current channels:

  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/linux-64
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/noarch
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/linux-64
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/noarch
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r/linux-64
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r/noarch
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro/linux-64
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro/noarch
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2/linux-64
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2/noarch
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/linux-64
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/noarch
  - https://mirrors.ustc.edu.cn/anaconda/pkgs/free/linux-64
  - https://mirrors.ustc.edu.cn/anaconda/pkgs/free/noarch
  - https://mirrors.ustc.edu.cn/anaconda/pkgs/main/linux-64
  - https://mirrors.ustc.edu.cn/anaconda/pkgs/main/noarch

To search for alternate channels that may provide the conda package you're
looking for, navigate to

    https://anaconda.org

and use the search bar at the top of the page.

然后光荣报错了。然后试试把 11.1 改成 11.3 。

 就可以安装了……就是要下载不少东西。

好,无法使用 cuda,大概率是因为 docker 外宿主机的 driver 版本也有限制。那就只能调整 cuda 到 11.1 了。

 我又将版本回退(这里不加 -c 会报错。

conda install cudatoolkit=11.1 -c nvidia

可是还是检查不到 gpu。看了一下,现在的 torch 版本是1.10.2,而之前安装的能用的环境中 torch 版本是 1.8.2,莫非是 torch 版本问题?

conda install pytorch=1.8.2 -c pytorch-lts

再次回退 pytorch 的版本,注意这里 -c 如果 pytorch 会出错,需要加上 lts (长期维护)。

 然后就可以使用 cuda 了…… 大概是新版本一点的 torch 和我使用的 GPU 服务器的 GPU 驱动五行不合吧…… 但是由于 GPU 服务器不能随意更新驱动版本,可能会导致所有用户的 docker 崩掉,所以也只能就这样用 LTS 了。

接下来安装 Transformers 库。

conda install -c huggingface transformers

这里安装也很顺利。

总结

安装深度学习的环境很容易出各种各样的问题,如果有权限更新服务器的显卡驱动则应该能安装较新版本的库,但是像我一样在 docker 里,与很多用户共用一台服务器的场景,可能就只能用 pytorch 1.8.2 的 LTS 版本了,用靠前一点的版本就会水土不服。

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐