📅  最后修改于: 2023-12-03 15:40:35.270000             🧑  作者: Mango
如果你已经安装了 TensorFlow 并且想要使用 GPU 来加速模型训练或预测,那么你需要确认是否安装了 TensorFlow GPU 版本。
首先,你需要确认你的 TensorFlow 版本是否支持 GPU。你可以通过以下方式来获取你的 TensorFlow 版本号:
import tensorflow as tf
print(tf.__version__)
如果你看到了一个类似于 2.4.1
的版本号,那么你已经成功地安装了 TensorFlow。
在安装 TensorFlow GPU 版本之前,你需要确认已经安装并配置了适当版本的 NVIDIA GPU 驱动。你可以通过以下命令来查看你的 NVIDIA 驱动版本:
!nvidia-smi
如果你看到了类似于以下输出,那么你已经成功配置了 NVIDIA GPU 驱动:
Tue Aug 24 11:53:48 2021
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 450.119.04 Driver Version: 450.119.04 CUDA Version: 11.0 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|===============================+======================+======================|
| 0 Tesla K80 Off | 00000000:00:04.0 Off | 0 |
| N/A 33C P8 27W / 149W | 0MiB / 11441MiB | 0% Default |
| | | ERR! |
+-------------------------------+----------------------+----------------------+
TensorFlow GPU 版本需要 CUDA Toolkit 的支持。你需要确认已经安装了兼容的 CUDA Toolkit 版本。你可以通过以下命令来查看你的 CUDA Toolkit 版本:
!nvcc --version
如果你看到了类似于以下输出,那么你已经成功安装了 CUDA Toolkit:
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2020 NVIDIA Corporation
Built on Fri_Jul_10_16:01:54_Pacific_Daylight_Time_2020
Cuda compilation tools, release 11.0, V11.0.221
Build cuda_11.0_bu.relgpu_drv_prod.101053333_0
TensorFlow GPU 版本还需要 cuDNN 的支持。你需要确认已经安装了兼容的 cuDNN 版本。你可以通过以下命令来查看你已安装的 cuDNN 版本:
import tensorflow as tf
print(tf.config.list_physical_devices('GPU'))
如果你看到了类似于以下输出,并且 name
列表示的是你期望使用的 GPU 设备,那么你已经成功安装了 cuDNN:
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
如果你确认了上述步骤并且已经准备好开始使用 TensorFlow GPU 版本,你可以使用以下命令来安装 TensorFlow GPU 版本:
!pip install tensorflow-gpu
之后你可以通过以下代码来测试 TensorFlow GPU 版本是否可用:
import tensorflow as tf
tf.test.is_gpu_available()
如果你看到了类似于以下输出,并且 True
表示你的 TensorFlow 可以在 GPU 上运行,那么你已经成功安装了 TensorFlow GPU 版本!
2021-08-24 12:17:12.912115: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2021-08-24 12:17:12.914248: I tensorflow/core/platform/profile_utils/cpu_utils.cc:104] CPU Frequency: 2299995000 Hz
2021-08-24 12:17:12.914595: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7fa41c000b20 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2021-08-24 12:17:12.914629: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
2021-08-24 12:17:13.019086: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcuda.so.1
2021-08-24 12:17:13.164177: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:1005] ARM64 does not support NUMA - returning NUMA node zero
2021-08-24 12:17:13.164948: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x5594d4e2fac0 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
2021-08-24 12:17:13.164980: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Tesla T4, Compute Capability 7.5
2021-08-24 12:17:13.165408: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1733] Found device 0 with properties:
pciBusID: 0000:00:04.0 name: Tesla T4 computeCapability: 7.5
coreClock: 1.59GHz coreCount: 40 deviceMemorySize: 14.75GiB deviceMemoryBandwidth: 298.08GiB/s
2021-08-24 12:17:13.165441: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcudart.so.11.0
2021-08-24 12:17:13.334409: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcublas.so.11
2021-08-24 12:17:13.334552: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcublasLt.so.11
2021-08-24 12:17:13.457303: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcufft.so.10
2021-08-24 12:17:13.471242: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcurand.so.10
2021-08-24 12:17:13.656579: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcusolver.so.10
2021-08-24 12:17:13.715914: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcusparse.so.11
2021-08-24 12:17:13.720610: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcudnn.so.8
2021-08-24 12:17:13.720783: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:1005] ARM64 does not support NUMA - returning NUMA node zero
2021-08-24 12:17:13.721689: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:1005] ARM64 does not support NUMA - returning NUMA node zero
2021-08-24 12:17:13.722358: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1871] Adding visible gpu devices: 0
2021-08-24 12:17:13.722946: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1258] Device interconnect StreamExecutor with strength 1 edge matrix:
2021-08-24 12:17:13.722982: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1264] 0
2021-08-24 12:17:13.722993: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1277] 0: N
2021-08-24 12:17:13.723252: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:1005] ARM64 does not support NUMA - returning NUMA node zero
2021-08-24 12:17:13.723464: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:1005] ARM64 does not support NUMA - returning NUMA node zero
2021-08-24 12:17:13.723635: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1402] Created TensorFlow device (/device:GPU:0 with 13602 MB memory) -> physical GPU (device: 0, name: Tesla T4, pci bus id: 0000:00:04.0, compute capability: 7.5)
True