不见春山
骑马倚斜桥,满楼红袖招。
Home
Categories
Archives
Tags
About
Home
tensorflow-gpu 安装笔记
tensorflow-gpu 安装笔记
取消
tensorflow-gpu 安装笔记
由
ctaoist
发布于 2021-05-21
·
最后更新:2022-07-13
1
公司另一个项目组整了台 GPU 服务器,可以蹭一蹭,tensorflow-gpu 环境的搭建还是有点麻烦,使用 conda 会省时省力一些。 ## 安装 cuda 和 cudnn ### 手动安装 [官方下载地址](https://developer.nvidia.com/cuda-toolkit-archive) ### conada 安装 换源请参考 [conda 换源](#conda-换源) 初次使用 conda 需要初始化 shell: ```sh conda init powershell # win10 初始化 powershell, linux 则 bash/zsh 等 ``` >ps. powershell 需要开启执行脚本的权限, 打开PowerShell 然后输入 `get-executionpolicy`,得到: `Restricted`, 表明相应的权限没有开启。以管理员身份打开 PowerShell 输入 `set-executionpolicy remotesigned`。 查询 tf, cuda, cudnn 三者的对应关系 https://www.tensorflow.org/install/source_windows#gpu ```sh conda create -n tf #创建虚环境,也可以使用默认环境 conda activate tf # 激活虚环境 conda install pip # 在虚环境中安装pip,否则仍为全局的,无法起到隔离的作用 conda install cudatoolkit=10.1 cudnn=7.6.0 # 更高版本的 tf 可能需要完整安装 cuda conda install -c nvidia cuda=11.7 # tf2.3 暂不需要 conda deactivate # 退出当前的虚环境 ``` >ps. 我也不太清楚是 Linux 还是 Miniconda 的问题,conda 激活虚环境后没有更新 `LD_LIBRARY_PATH` 变量,导致 tensorflow 提示找不到 cuda 的一些动态库,需要手动 `export LD_LIBRARY_PATH="$CONDA_PREFIX/lib"`, 也可以参考[启动Conda环境时自动更改环境变量](https://lichenhao.netlify.app/post/2019-08-17-conda-activate/) 参考: - https://www.tensorflow.org/install/source?hl=zh-cn - [WIN10安装TENSORFLOW(GPU版本)详解(超详细,从零开始)](https://zhuanlan.zhihu.com/p/37086409) ### 查询已经安装的 cuda 和 cudnn 版本 ```sh cat /usr/local/cuda/version.txt # cuda 版本 cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2 # or cat /usr/local/cuda/include/cudnn_version.h | grep CUDNN_MAJOR -A 2 ## 如果使用 conda 安装 conda list ``` ## 安装 tensorflow-gpu ### 从 pip 安装 ```sh pip3 install tensorflow-gpu==2.3.0 ``` ### 从源码 build ```sh pip3 install -U numpy wheel pip3 install -U keras_preprocessing --no-deps ``` 安装确定版本的 bazel:[bazel 版本参考](https://www.tensorflow.org/install/source?hl=zh-cn#gpu_support_2), [bazel 下载地址](https://github.com/bazelbuild/bazel/releases) 拉取 tensorflow 的源码并配置 build: ```sh git clone -b r2.3 --depth=1 https://github.com/tensorflow/tensorflow.git cd tensorflow ./configure ``` 需要注意的是要在配置中明确 gpu 的算力: `算力1,算力2`, 逗号分隔。[gpu 算力查询](https://developer.nvidia.com/cuda-gpus); **cuda 版本也要和算力匹配**: 10.x 版本的 cuda 应该最高只支持 7.5 算力, 11.x 版本的 cuda 最高支持的算力 8.6, [cuda 算力说明](https://docs.nvidia.com/cuda/ampere-compatibility-guide/index.html#building-ampere-compatible-apps-using-cuda-10-0)。 开始编译: ```sh bazel build tensorflow/tools/pip_package:build_pip_package --local_ram_resources=8192 --local_cpu_resources=6 # 最多8G内存,6个逻辑cpu ``` >ps. 另一个项目要求使用 cudatoolkit=10.3, 我这边就只能使用 tensorflow=2.3 版本, 卡是 GTX 3090, 但这样搭配有点问题, 所以尝试从源码编译,但很明显, 编译失败了。最后使用 conda 创建虚环境安装更高版本的 tf 和 cuda。 ## gpu 测试 ```py gpus = tf.config.experimental.list_physical_devices('GPU') if gpus: try: # Currently, memory growth needs to be the same across GPUs for gpu in gpus: tf.config.experimental.set_memory_growth(gpu, True) logical_gpus = tf.config.experimental.list_logical_devices('GPU') print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs") except RuntimeError as e: # Memory growth must be set before GPUs have been initialized print(e) ``` ## Troubleshoot - Installation issue with Tensorflow-cpu, no module named '\_pwyrap\_tensorflow\_internal' 安装后 windows 上可能出现这个问题,很可能是由于没装 VC++ Run time (2015、2017、2019) 造成的,可以前往[官网](https://support.microsoft.com/zh-cn/topic/%E6%9C%80%E6%96%B0%E6%94%AF%E6%8C%81%E7%9A%84-visual-c-%E4%B8%8B%E8%BD%BD-2647da03-1eea-4433-9aff-95f26a218cc0)下载。 - InternalError: Blas GEMM launch failed : a.shape=(100, 784), b.shape=(784, 10), m=100, n=10 这种一般是显存不够或者别的程序占用GPU导致的。 参考: - https://stackoverflow.com/questions/41117740/tensorflow-crashes-with-cublas-status-alloc-failed ## conda 换源 ```sh conda config --add channels https://mirrors.aliyun.com/anaconda/pkgs/free conda config --add channels https://mirrors.aliyun.com/anaconda/pkgs/main conda config --add channels https://mirrors.aliyun.com/anaconda/pkgs/msys2 conda config --add channels https://mirrors.aliyun.com/anaconda/pkgs/r conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/Paddle conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/auto conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/biobakery conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/bioconda conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/c4aarch64 conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/caffe2 conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/conda-forge conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/deepmodeling conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/dglteam conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/fastai conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/fermi conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/idaholab conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/intel conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/matsci conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/menpo conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/mordred-descriptor conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/msys2 conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/numba conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/ohmeta conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/omnia conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/plotly conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/psi4 conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/pytorch conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/pytorch-test conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/pytorch3d conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/pyviz conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/qiime2 conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/rapidsai conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/rdkit conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/simpleitk conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/stackless conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/ursky conda config --set show_channel_urls yes ```
机器学习
Tensorflow
WIn10
该博客文章由作者通过
CC BY 4.0
进行授权。
分享
最近更新
ROS修改 MTU 和 MSS 解决上网慢和页面显示不全问题
WireGuard 搭建组网教程
EPUB 格式详解
Quartus ii 调用 Modelsim
vscode + verilog 环境配置
热门标签
Tensorflow
VPN
Linux
Router
虚拟组网
ROS
zerotier
C++
WIn10
Math
文章目录
RNN与LSTM
理解 typedef 定义的函数指针