---
lang: ja-jp
breaks: true
---
# Python TensorFlow で GPU(NVIDIA CUDA)を使用するための設定 Windows10 2021-07-21
## TensorFlow GPUサポート
https://www.tensorflow.org/install/gpu
## 環境
```shell=
python -V
Python 3.8.10
```
:::warning
Python 3.8.10では、kerasが動作しない問題があった為、Python 3.9.5にその後変更しました。
※この問題が発生した時のtensorflowのバージョン:2.5.0。
:::
### GPU
* NVIDIA GeForce RTX 3090
* NVIDIA Studio ドライバー 471.11 06/23/2021
#### CUDA ライブラリをインストールする前のコンソールログ
```python=
import os
import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib
import matplotlib.pyplot as plt
import tensorflow
import keras
from tensorflow.python.framework.ops import NullContextmanager
# GPUを無効化
#os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
# GPUを有効化
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
os.environ['TF_XLA_FLAGS']= '--tf_xla_enable_xla_devices'
print('getcwd: ', os.getcwd())
print('__file__: ', __file__)
print('script path: ', os.path.dirname(os.path.abspath(__file__)))
print("numpy :", np.__version__)
print("pandas :", pd.__version__)
print("tensorflow :", tf.__version__)
print("keras :", keras.__version__)
print("matplotlib :", matplotlib.__version__)
#--------------------------------------
# GPUの使用メモリを制限する。
#--------------------------------------
physical_devices = tensorflow.config.list_physical_devices('GPU')
if len(physical_devices) > 0:
for device in physical_devices:
# GPUの占有メモリーを一気に確保せず、処理に必要な量を随時に確保する
tensorflow.config.experimental.set_memory_growth(device, True)
# コードを実行するプロセッサを指定したにもかかわらず、それが存在しない場合は自動で指定を変える
tensorflow.config.set_soft_device_placement(True)
# tensorflow.debugging.set_log_device_placement(True)
growth = tensorflow.config.experimental.get_memory_growth(device)
print('{} memory growth: {}'.format(device, growth))
else:
print("Not enough GPU hardware devices available.")
```
```=
2021-07-21 17:54:23.612416: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
2021-07-21 17:54:23.621809: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
getcwd: D:\Samples\Samples_Anaconda_venv\TensorFlow
__file__: d:\Samples\Samples_Anaconda_venv\TensorFlow\GPU_Check_tensorflow.py
numpy : 1.19.5
pandas : 1.3.0
tensorflow : 2.5.0
matplotlib : 3.4.2
2021-07-21 17:54:40.506160: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library nvcuda.dll
2021-07-21 17:54:40.530419: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1733] Found device 0 with properties:
pciBusID: 0000:65:00.0 name: NVIDIA GeForce RTX 3090 computeCapability: 8.6
coreClock: 1.725GHz coreCount: 82 deviceMemorySize: 24.00GiB deviceMemoryBandwidth: 871.81GiB/s
2021-07-21 17:54:40.543745: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
2021-07-21 17:54:40.552439: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cublas64_11.dll';
dlerror: cublas64_11.dll not found
2021-07-21 17:54:40.560250: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cublasLt64_11.dll'; dlerror: cublasLt64_11.dll not found
2021-07-21 17:54:40.568953: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cufft64_10.dll'; dlerror: cufft64_10.dll not found
2021-07-21 17:54:40.576691: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'curand64_10.dll';
dlerror: curand64_10.dll not found
2021-07-21 17:54:40.584491: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cusolver64_11.dll'; dlerror: cusolver64_11.dll not found
2021-07-21 17:54:40.592547: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cusparse64_11.dll'; dlerror: cusparse64_11.dll not found
2021-07-21 17:54:40.600570: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudnn64_8.dll'; dlerror: cudnn64_8.dll not found
2021-07-21 17:54:40.608134: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1766] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...
Not enough GPU hardware devices available
```
特にCUDA関連のライブラリはインストールしていないが、`nvcuda.dll`は正常に読み込まれている。
`cudart64_110.dll`、`cufft64_10.dll`、`cudnn64_8.dll`等のライブラリが読み込めないとのエラーが出力されている。
## CUDA Toolkit のインストール
nVidiaの`CUDA Toolkit Archive`より、`CUDA Toolkit 11.0 Update 1`をインストールする。
> CUDA Toolkit Archive
> https://developer.nvidia.com/cuda-toolkit-archive
https://developer.nvidia.com/cuda-11.0-update1-download-archive?target_os=Windows&target_arch=x86_64&target_version=10&target_type=exelocal
* cuda_11.0.3_451.82_win10.exe
## NVIDIA cuDNN のインストール
> cuDNN Archive
> https://developer.nvidia.com/rdp/cudnn-archive
#### 以下のモジュールは使えませんでした。
> `Download cuDNN v8.0.5 (November 9th, 2020), for CUDA 11.0`
* cudnn-11.0-windows-x64-v8.0.5.39.zip
#### TensorFlow 2.5 では、以下で動作確認済み。
* cudnn-11.2-windows-x64-v8.1.0.77.zip
zipファイルを解凍し、`C:\cuda\bin`となるように移動する。
## 環境変数を設定
C:\cuda\bin
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\bin
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\libnvvp
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\extras\CUPTI\lib64
## `cupti64_110.dll not found` エラーとなる場合の対応
以下のようなエラーが出力された場合は、「cupti64_2020.1.0.dll」をコピーして、「cupti64_110.dll」にリネームする。
```=
2021-01-10 20:54:30.383277: I tensorflow/core/profiler/lib/profiler_session.cc:136] Profiler session initializing.
2021-01-10 20:54:30.386894: I tensorflow/core/profiler/lib/profiler_session.cc:155] Profiler session started.
2021-01-10 20:54:30.425702: I tensorflow/core/profiler/internal/gpu/cupti_tracer.cc:1365] Profiler found 1 GPUs
2021-01-10 20:54:30.430023: W tensorflow/stream_executor/platform/default/dso_loader.cc:60] Could not load dynamic library 'cupti64_110.dll'; dlerror: cupti64_110.dll not found
2021-01-10 20:54:30.435958: W tensorflow/stream_executor/platform/default/dso_loader.cc:60] Could not load dynamic library 'cupti.dll'; dlerror: cupti.dll not found
2021-01-10 20:54:30.463590: E tensorflow/core/profiler/internal/gpu/cupti_tracer.cc:1415] function cupti_interface_->Subscribe( &subscriber_, (CUpti_CallbackFunc)ApiCallback, this)failed with error CUPTI could not be loaded or symbol could not be found.
2021-01-10 20:54:30.479144: E tensorflow/core/profiler/internal/gpu/cupti_tracer.cc:1496] function cupti_interface_->Finalize()failed with error CUPTI could not be loaded or symbol could not be found.
2021-01-10 20:54:30.501209: I tensorflow/core/profiler/internal/gpu/cupti_collector.cc:219] GpuTracer has collected 0 callback api events and 0 activity events.
```
管理者として以下を実行。
```shell=
copy "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\extras\CUPTI\lib64\cupti64_2020.1.1.dll" "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\extras\CUPTI\lib64\cupti64_110.dll"
```
:::info
【参考資料】
> Incorrect Cupti dll name on W10
> https://www.gitmemory.com/issue/tensorflow/tensorflow/44697/750923526
:::
## `cusolver64_11.dll not found` エラーとなる場合の対応
以下のエラーが出力された場合は、「cusolver64_10.dll」をコピーして、「cusolver64_11.dll」にリネームする。
```=
2021-07-21 18:28:27.203680: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cusolver64_11.dll'; dlerror: cusolver64_11.dll not found
2021-07-21 18:28:27.392422: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library cusparse64_11.dll
2021-07-21 18:28:27.418842: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library cudnn64_8.dll
2021-07-21 18:28:27.423960: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1766] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...
Not enough GPU hardware devices available.
```
管理者として以下を実行。
```shell=
copy "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\bin\cusolver64_10.dll" "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\bin\cusolver64_11.dll"
```
:::info
【参考資料】
※下記では、`cusolver64_11.dll` → `cusolver64_10.dll` となっているが、今回は逆の現象が発生した。
> tensorflow-nightly-gpu looking for cusolver64_10.dll on a cuDNN 11.1 installation #44291
> https://github.com/tensorflow/tensorflow/issues/44291
:::
## 実行確認
```shell=
2021-07-21 22:36:45.818048: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library cudart64_110.dll
getcwd: D:\Samples\Samples_Anaconda_venv\TensorFlow
__file__: d:\Samples\Samples_Anaconda_venv\TensorFlow\GPU_Check_tensorflow.py
numpy : 1.19.5
pandas : 1.3.0
tensorflow : 2.5.0
matplotlib : 3.4.2
2021-07-21 22:36:49.368977: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library nvcuda.dll
2021-07-21 22:36:49.398998: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1733] Found device 0 with properties:
pciBusID: 0000:65:00.0 name: NVIDIA GeForce RTX 3090 computeCapability: 8.6
coreClock: 1.725GHz coreCount: 82 deviceMemorySize: 24.00GiB deviceMemoryBandwidth: 871.81GiB/s
2021-07-21 22:36:49.408871: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library cudart64_110.dll
2021-07-21 22:36:49.421486: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library cublas64_11.dll
2021-07-21 22:36:49.426404: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library cublasLt64_11.dll
2021-07-21 22:36:49.434965: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library cufft64_10.dll
2021-07-21 22:36:49.441392: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library curand64_10.dll
2021-07-21 22:36:49.456449: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library cusolver64_11.dll
2021-07-21 22:36:49.464337: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library cusparse64_11.dll
2021-07-21 22:36:49.471211: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library cudnn64_8.dll
2021-07-21 22:36:49.476442: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1871] Adding visible gpu devices: 0
PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU') memory growth: True
```
###### tags: `Python` `TensorFlow` `GPU` `CUDA` `Windows 10`