[2] TensorFlow – GPU

12 marca 2022 Wyłączono przez Adam [zicherka] Nogły

Zainstaluj TensorFlow, czyli bibliotekę uczenia maszynowego.

W tym przykładzie skonfigurujemy TensorFlow, aby używać procesora graficznego NVIDIA na swoim komputerze.

Ten przykład jest oparty na konfiguracjach kompilacji, jak poniżej.

TensorFlow 2.8, CUDA 11.6, cuDNN 8.3.2, Python 3.8

[root@vlsr05 ~]$ python -c 'import tensorflow as tf; print(tf.__version__)'

[root@vlsr05 ~]$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2022 NVIDIA Corporation
Built on Thu_Feb_10_18:23:41_PST_2022
Cuda compilation tools, release 11.6, V11.6.112
Build cuda_11.6.r11.6/compiler.30978841_0

[root@vlsr05 ~]$ python -V
Python 3.8.12

Możesz zobaczyć testowane konfiguracje kompilacji w poniższej witrynie.

https://www.tensorflow.org/install/source?hl=en#gpu_support_2

[1] Zainstaluj CUDA, patrz tutaj.

[2] Zainstaluj Pythona 3.8, patrz tutaj.

[3] Pobierz cuDNN (bibliotekę CUDA Deep Neural Network) z oficjalnej strony NVIDIA. (trzeba zarejestrować konto programisty):

https://developer.nvidia.com/rdp/cudnn-download.

[4] Prześlij cuDNN na swój serwer i zlokalizuj pliki jak poniżej.

[root@vlsr05 ~]# tar -xvf cudnn-linux-x86_64-8.3.2.44_cuda11.5-archive.tar.xz
[root@vlsr05 ~]# cp ./cudnn-linux-x86_64-8.3.2.44_cuda11.5-archive/include/cudnn.h /usr/local/cuda-11.6/include/
[root@vlsr05 ~]# cp -a ./cudnn-linux-x86_64-8.3.2.44_cuda11.5-archive/lib/libcudnn* /usr/local/cuda-11.6/lib64/
[root@vlsr05 ~]# ldconfig

[5] Zainstaluj inne wymagane pakiety.

W przypadku Cudart (natywne biblioteki wykonawcze CUDA Runtime) wymaga również CUDA, więc zainstaluj go również.

[root@vlsr05 ~]# dnf install python38-devel gcc gcc-c++ make cuda-cudart-11-6
[root@vlsr05 ~]# dnf install libcudnn8 libcudnn8-devel
[root@vlsr05 ~]# dnf install libnvinfer8 libnvinfer-plugin8 libnvinfer-plugin-devel libnvinfer-devel

[6] Zaloguj się jako zwykły użytkownik i przygotuj wirtualne środowisko Python do instalacji TensorFlow.

Jeśli chcesz zainstalować go w całym systemie, pomiń tę sekcję i wykonaj następny [7] z kontem użytkownika root.

[user01@vlsr05 ~]$ pip3 install --upgrade virtualenv –-user
[user01@vlsr05 ~]$ virtualenv --system-site-packages -p python3 ./venv
created virtual environment CPython3.8.12.final.0-64 in 242ms
  creator CPython3Posix(dest=/home/user01/venv, clear=False, no_vcs_ignore=False, global=True)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/user01/.local/share/virtualenv)
    added seed packages: Keras_Preprocessing==1.1.2, Markdown==3.3.6, Werkzeug==2.0.3, absl_py==1.0.0, astunparse==1.6.3, cachetools==5.0.0, certifi==2021.10.8, charset_normalizer==2.0.12, flatbuffers==2.0, gast==0.5.3, google_auth==2.6.0, google_auth_oauthlib==0.4.6, google_pasta==0.2.0, idna==3.3, importlib_metadata==4.11.2, keras==2.8.0, libclang==13.0.0, oauthlib==3.2.0, opt_einsum==3.3.0, pip==22.0.4, pyasn1==0.4.8, pyasn1_modules==0.2.8, requests==2.27.1, requests_oauthlib==1.3.1, rsa==4.8, setuptools==60.9.3, tensorboard==2.8.0, tensorboard_data_server==0.6.1, tensorboard_plugin_wit==1.8.1, termcolor==1.1.0, tf_estimator_nightly==2.8.0.dev2021122109, typing_extensions==4.1.1, urllib3==1.26.8, wheel==0.37.1, zipp==3.7.0
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
[user01@vlsr05 ~]$ source ./venv/bin/activate
(venv) [user01@vlsr05 ~]$

[7] Zainstaluj TensorFlow 2.8

(venv) [user01@vlsr05 ~]$ pip3 install --upgrade tensorflow

# sprawdź poprawność instalacji TensorFlow
(venv) [user01@vlsr05 ~]$ python3 -c "from tensorflow.python.client import device_lib; device_lib.list_local_devices()"
2022-03-11 12:04:36.070280: I tensorflow/core/platform/cpu_feature_guard.cc:151] 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.
2022-03-11 12:04:36.325311: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-03-11 12:04:36.605792: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-03-11 12:04:36.605982: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-03-11 12:04:37.019055: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-03-11 12:04:37.019230: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-03-11 12:04:37.019365: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-03-11 12:04:37.019485: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created device /device:GPU:0 with 1609 MB memory:  -> device: 0, name: NVIDIA GeForce GTX 1050, pci bus id: 0000:01:00.0, compute capability: 6.1

(venv) [user01@vlsr05 ~]$ python3 -c "import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"
2022-03-11 12:06:12.373512: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-03-11 12:06:12.401019: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-03-11 12:06:12.401215: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-03-11 12:06:12.401551: I tensorflow/core/platform/cpu_feature_guard.cc:151] 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.
2022-03-11 12:06:12.401923: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-03-11 12:06:12.402103: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-03-11 12:06:12.402229: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-03-11 12:06:12.778572: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-03-11 12:06:12.778762: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-03-11 12:06:12.778901: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:936] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-03-11 12:06:12.779038: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 1609 MB memory:  -> device: 0, name: NVIDIA GeForce GTX 1050, pci bus id: 0000:01:00.0, compute capability: 6.1
tf.Tensor(763.72534, shape=(), dtype=float32)