Linux/nvidia:修订间差异
来自康健生活
| (未显示同一用户的44个中间版本) | |||
| 第1行: | 第1行: | ||
==引言== | == 引言 == | ||
查看计算机的显卡。 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
lspci | grep -i vga | |||
# or | |||
lspci | egrep 'VGA|3D'</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
00:02.0 VGA compatible controller: Intel Corporation UHD Graphics (rev 05) | |||
01:00.0 VGA compatible controller: NVIDIA Corporation TU106 [GeForce RTX 2060] (rev a1) | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
检查已经安装的 nvidia 驱动。 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
nvidia-detector</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
nvidia-driver-495 | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
== 卸载 == | |||
卸载所有已安装的 nvidia 驱动以及 cuda 相关文件。 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
sudo apt remove --purge nvidia* | |||
sudo apt remove --purge cuda* | |||
sudo rm -rf cuda*</syntaxhighlight>|$|/home/kangkk}} | |||
== 安装 == | |||
输入 <code>ubuntu-drivers</code> 命令进行查看 ubuntu 系统推荐的驱动。 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | {{Shc_out|<syntaxhighlight lang="bash"> | ||
ubuntu-drivers devices</syntaxhighlight>|<syntaxhighlight lang="bash">== /sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0 == | ubuntu-drivers devices</syntaxhighlight>|<syntaxhighlight lang="bash">== /sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0 == | ||
| 第16行: | 第38行: | ||
driver : xserver-xorg-video-nouveau - distro free builtin | driver : xserver-xorg-video-nouveau - distro free builtin | ||
</syntaxhighlight>|$|/home/kangkk}} | </syntaxhighlight>|$|/home/kangkk}} | ||
进行驱动安装。 | |||
{{shc|<syntaxhighlight lang="bash"> | {{shc|<syntaxhighlight lang="bash"> | ||
sudo apt install nvidia-driver-495</syntaxhighlight>|$|/home/kangkk}} | sudo apt install nvidia-driver-495</syntaxhighlight>|$|/home/kangkk}} | ||
== | |||
== nvidia-smi == | |||
输入 nvidia-smi 命令查看支持的 cuda 版本。 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | {{Shc_out|<syntaxhighlight lang="bash"> | ||
nvidia-smi</syntaxhighlight>|<syntaxhighlight lang="bash">Sun Nov 7 17:33:37 2021 | nvidia-smi</syntaxhighlight>|<syntaxhighlight lang="bash">Sun Nov 7 17:33:37 2021 | ||
| 第44行: | 第70行: | ||
+-----------------------------------------------------------------------------+ | +-----------------------------------------------------------------------------+ | ||
</syntaxhighlight>|$|/home/kangkk}} | </syntaxhighlight>|$|/home/kangkk}} | ||
==CUDA== | |||
== CUDA == | |||
[https://developer.nvidia.com/cuda-toolkit-archive 官网]下载,按照步骤安装即可。 | |||
[[文件:安装cuda步骤示意图.png|500px|无框|无|安装cuda步骤示意图]] | |||
=== 检查 === | |||
验证 GPU 是否支持 CUDA。 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
lspci | grep -i nvidia | |||
</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
01:00.0 VGA compatible controller: NVIDIA Corporation TU106 [GeForce RTX 2060] (rev a1) | |||
01:00.1 Audio device: NVIDIA Corporation TU106 High Definition Audio Controller (rev a1) | |||
01:00.2 USB controller: NVIDIA Corporation TU106 USB 3.1 Host Controller (rev a1) | |||
01:00.3 Serial bus controller [0c80]: NVIDIA Corporation TU106 USB Type-C UCSI Controller (rev a1) | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
检查显卡是否在 [https://developer.nvidia.com/cuda-gpus cuda-gpus] 列表中。 | |||
查询 CUDA 的版本。 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
cat /usr/local/cuda/version.json</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
{ | |||
"cuda" : { | |||
"name" : "CUDA SDK", | |||
"version" : "11.5.1" | |||
}, | |||
"cuda_cudart" : { | |||
"name" : "CUDA Runtime (cudart)", | |||
"version" : "11.5.117" | |||
}, | |||
... | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
测试 CUDA。 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
cd /usr/local/cuda/samples/1_Utilities/deviceQuery | |||
sudo make</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
/usr/local/cuda-11.5/bin/nvcc -ccbin g++ -I../../common/inc -m64 --threads 0 --std=c++11 -gencode arch=compute_35,code=sm_35 -gencode arch=compute_37,code=sm_37 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_61,code=sm_61 -gencode arch=compute_70,code=sm_70 -gencode arch=compute_75,code=sm_75 -gencode arch=compute_80,code=sm_80 -gencode arch=compute_86,code=sm_86 -gencode arch=compute_86,code=compute_86 -o deviceQuery.o -c deviceQuery.cpp | |||
nvcc warning : The 'compute_35', 'compute_37', 'compute_50', 'sm_35', 'sm_37' and 'sm_50' architectures are deprecated, and may be removed in a future release (Use -Wno-deprecated-gpu-targets to suppress warning). | |||
/usr/local/cuda-11.5/bin/nvcc -ccbin g++ -m64 -gencode arch=compute_35,code=sm_35 -gencode arch=compute_37,code=sm_37 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_61,code=sm_61 -gencode arch=compute_70,code=sm_70 -gencode arch=compute_75,code=sm_75 -gencode arch=compute_80,code=sm_80 -gencode arch=compute_86,code=sm_86 -gencode arch=compute_86,code=compute_86 -o deviceQuery deviceQuery.o | |||
nvcc warning : The 'compute_35', 'compute_37', 'compute_50', 'sm_35', 'sm_37' and 'sm_50' architectures are deprecated, and may be removed in a future release (Use -Wno-deprecated-gpu-targets to suppress warning). | |||
mkdir -p ../../bin/x86_64/linux/release | |||
cp deviceQuery ../../bin/x86_64/linux/release | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
编译完成之后,执行生成的 <code>deviceQuery</code> 程序。 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
sudo /usr/local/cuda/samples/1_Utilities/deviceQuery/deviceQuery</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
/usr/local/cuda/samples/1_Utilities/deviceQuery/deviceQuery Starting... | |||
CUDA Device Query (Runtime API) version (CUDART static linking) | |||
Detected 1 CUDA Capable device(s) | |||
Device 0: "NVIDIA GeForce RTX 2060" | |||
CUDA Driver Version / Runtime Version 11.5 / 11.5 | |||
CUDA Capability Major/Minor version number: 7.5 | |||
Total amount of global memory: 5935 MBytes (6222839808 bytes) | |||
(030) Multiprocessors, (064) CUDA Cores/MP: 1920 CUDA Cores | |||
GPU Max Clock rate: 1560 MHz (1.56 GHz) | |||
Memory Clock rate: 5501 Mhz | |||
Memory Bus Width: 192-bit | |||
L2 Cache Size: 3145728 bytes | |||
Maximum Texture Dimension Size (x,y,z) 1D=(131072), 2D=(131072, 65536), 3D=(16384, 16384, 16384) | |||
Maximum Layered 1D Texture Size, (num) layers 1D=(32768), 2048 layers | |||
Maximum Layered 2D Texture Size, (num) layers 2D=(32768, 32768), 2048 layers | |||
Total amount of constant memory: 65536 bytes | |||
Total amount of shared memory per block: 49152 bytes | |||
Total shared memory per multiprocessor: 65536 bytes | |||
Total number of registers available per block: 65536 | |||
Warp size: 32 | |||
Maximum number of threads per multiprocessor: 1024 | |||
Maximum number of threads per block: 1024 | |||
Max dimension size of a thread block (x,y,z): (1024, 1024, 64) | |||
Max dimension size of a grid size (x,y,z): (2147483647, 65535, 65535) | |||
Maximum memory pitch: 2147483647 bytes | |||
Texture alignment: 512 bytes | |||
Concurrent copy and kernel execution: Yes with 3 copy engine(s) | |||
Run time limit on kernels: Yes | |||
Integrated GPU sharing Host Memory: No | |||
Support host page-locked memory mapping: Yes | |||
Alignment requirement for Surfaces: Yes | |||
Device has ECC support: Disabled | |||
Device supports Unified Addressing (UVA): Yes | |||
Device supports Managed Memory: Yes | |||
Device supports Compute Preemption: Yes | |||
Supports Cooperative Kernel Launch: Yes | |||
Supports MultiDevice Co-op Kernel Launch: Yes | |||
Device PCI Domain ID / Bus ID / location ID: 0 / 1 / 0 | |||
Compute Mode: | |||
< Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) > | |||
deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 11.5, CUDA Runtime Version = 11.5, NumDevs = 1 | |||
Result = PASS | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
=== run === | |||
下载并进行安装。 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
sudo sh cuda_*_linux.run</syntaxhighlight>|$|/home/kangkk}} | |||
安装完成之后,使用 <code>nvcc</code> 命令,提示 <code>sudo apt-get install nvidia-cuda-toolkit</code>,这个时候不确定是否安装成功,需要到 <code>/usr/local/cuda</code> 文件夹下面检查 cuda 目录下的 <code>bin</code> 文件夹中是否存在 <code>nvcc</code>。 | |||
确定存在之后,在 <code>.bashrc</code> 文件中加入 nvcc 的环境变量 | |||
{{Hc|~/.bashrc|<pre> | |||
export PATH=/usr/local/cuda/bin:$PATH | |||
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH | |||
</pre>}} | |||
<div class="pageBoxGreen">对应的环境变量。 | |||
# LIBRARY_PATH: 程序编译期间(开发阶段,如 gcc 编译)。 | |||
# LD_LIBRARY_PATH: 程序加载运行期间。</div> | |||
{{shc|<syntaxhighlight lang="bash"> | |||
source ~/.bashrc</syntaxhighlight>|$|/home/kangkk}} | |||
=== nvidia-cuda-toolkit === | |||
多种安装方式,最简单的如下,但是版本滞后,不建议。 | |||
{{shc|<syntaxhighlight lang="bash"> | {{shc|<syntaxhighlight lang="bash"> | ||
sudo apt install nvidia-cuda-toolkit -y</syntaxhighlight>|$|/home/kangkk}} | sudo apt install nvidia-cuda-toolkit -y</syntaxhighlight>|$|/home/kangkk}} | ||
=== nvcc === | |||
NVIDIA (R) Cuda compiler driver(CUDA-C 和 CUDA-C++ 编译器)。 | |||
验证版本信息。 | 验证版本信息。 | ||
{{Shc_out|<syntaxhighlight lang="bash"> | {{Shc_out|<syntaxhighlight lang="bash"> | ||
nvcc -V</syntaxhighlight>|<syntaxhighlight lang="bash">nvcc: NVIDIA (R) Cuda compiler driver | nvcc -V</syntaxhighlight>|<syntaxhighlight lang="bash">nvcc: NVIDIA (R) Cuda compiler driver | ||
Copyright (c) 2005- | nvcc: NVIDIA (R) Cuda compiler driver | ||
Built on | Copyright (c) 2005-2021 NVIDIA Corporation | ||
Cuda compilation tools, release | Built on Thu_Nov_18_09:45:30_PST_2021 | ||
Cuda compilation tools, release 11.5, V11.5.119 | |||
Build cuda_11.5.r11.5/compiler.30672275_0 | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
=== other === | |||
==== pycuda ==== | |||
{{shc|<syntaxhighlight lang="bash"> | |||
pip3 install pycuda</syntaxhighlight>|$|/home/kangkk}} | |||
== cuDNN == | |||
[https://developer.nvidia.com/rdp/cudnn-archive CUDNN] 是基于 CUDA 的深度学习 GPU 加速库(扩展计算库),是深度神经网络的必备库。 | |||
=== 插入式安装 === | |||
只需要解压后,将 CUDNN 文件复制到 CUDA 的对应文件夹。 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
sudo cp cuda/include/cudnn*.h /usr/local/cuda/include | |||
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64 | |||
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn* | |||
</syntaxhighlight>|$|/home/kangkk}} | </syntaxhighlight>|$|/home/kangkk}} | ||
验证安装是否成功。 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | {{Shc_out|<syntaxhighlight lang="bash"> | ||
cat /usr/local/cuda/include/cudnn_version.h | grep CUDNN_MAJOR -A 2 | |||
</syntaxhighlight>|<syntaxhighlight lang="bash"> | </syntaxhighlight>|<syntaxhighlight lang="bash"> | ||
#define CUDNN_MAJOR 8 | |||
#define CUDNN_MINOR 3 | |||
#define CUDNN_PATCHLEVEL 0 | |||
-- | |||
#define CUDNN_VERSION (CUDNN_MAJOR * 1000 + CUDNN_MINOR * 100 + CUDNN_PATCHLEVEL) | |||
#endif /* CUDNN_VERSION_H */ | |||
</syntaxhighlight>|$|/home/kangkk}} | </syntaxhighlight>|$|/home/kangkk}} | ||
== 推理引擎 == | |||
{| class="wikitable" | |||
|- | |||
| [[/TensorRT|TensorRT]] | |||
|} | |||
== 模型引擎 == | |||
[[/ONNX|ONNX]] | |||
== python == | |||
使用 python 检查安装的 cuda、cudnn 版本 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
python3</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
Python 3.8.8 (default, Apr 13 2021, 19:58:26) | |||
[GCC 7.3.0] :: Anaconda, Inc. on linux | |||
Type "help", "copyright", "credits" or "license" for more information. | |||
>>> import tensorflow | |||
>>> print(tensorflow.__version__) | |||
2.7.0 | |||
>>> import tensorflow.python.platform.build_info as build | |||
>>> print(build.build_info) | |||
OrderedDict([('cpu_compiler', '/dt7/usr/bin/gcc'), ('cuda_compute_capabilities', ['sm_35', 'sm_50', 'sm_60', 'sm_70', 'sm_75', 'compute_80']), ('cuda_version', '11.2'), ('cudnn_version', '8'), ('is_cuda_build', True), ('is_rocm_build', False), ('is_tensorrt_build', True)]) | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
== other == | |||
=== cuda 与 nvcc 版本不一致问题 === | |||
刚接触 cuda 的时候,我使用 <code>sudo apt install nvidia-driver-495</code> 安装 nvidia 驱动,同时又使用 <code>sudo apt install nvidia-cuda-toolkit -y</code> 安装 nvcc,导致版本 cuda 与 nvcc 版本不一致,cuda 版本是 <code>11.5</code>,而 nvcc 版本是 <code>9.5</code>。 | |||