アプリケーション開発ポータルサイト
ServerNote.NET
カテゴリー【PythonWindowsGoogle
【Windows10】NVIDIAグラフィックカードのGPUをTensorFlowに認識させて動かす
POSTED BY
2024-01-13

【Windows10】Python統合開発環境Anaconda3メモ
【Anaconda3】指定した仮想環境でJupyter Notebookを動かす

続きです。当方の個人パソコンにはNVIDIAのグラフィックカードを差しているので、TensorFlowにGPUを認識させられるはずですので、やってみました。

TensorFlowのGPU認識はそれぞれのソフトウェアが決められたバージョン同士でないとかたくなに認識してくれませんでした(全部最新を入れてみたが認識せず)

当方が成功した組み合わせは以下のとおりでした。

【ハードウェア】

・Windows 10 Pro 64-bit
・Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz 2.81GHz
・32.0GB RAM
NVIDIA GeForce GTX 1050 Ti 1341MHz Memory 4096MB GDDR5

【ソフトウェア】

・Anaconda3-2020.11-Windows-x86_64

・vs_community__824172766.1610265689
※↑Visual Studioが必須かどうか不明ですが一応入れました
Visual Studio 2019 コミュニティエディション
https://visualstudio.microsoft.com/ja/downloads/

NVIDIA cuda_10.1.105_418.96_win10
NVIDIA CUDAをダウンロード・インストールします。バージョンは10.1です。

https://developer.nvidia.com/cuda-10.1-download-archive-base?target_os=Windows&target_arch=x86_64&target_version=10&target_type=exelocal

インストールが完了したら、下記

NVIDIA cudnn-10.1-windows10-x64-v7.6.5.32
NVIDIA cuDNNのZIPファイルをダウンロードします。バージョンはCUDA10.1用のcuDNN7.6です。
ダウンロードにはユーザー登録が必要です(無料)。

ここから7.6を探してダウンロード、ZIPを展開します。
https://developer.nvidia.com/rdp/cudnn-archive

展開したら、中身を手動でCUDAのプログラムフォルダに放り込みます。

  1. cuda/binの中身を
  2. C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.1/binへコピー。
  3.  
  4. cuda/includeの中身を
  5. C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.1/includeへコピー。
  6.  
  7. cuda/lib/x64の中身を
  8. C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.1/lib/x64へコピー。

TensorFlow GPU用の仮想環境を作成しセットアップ・確認

ここからはAnaconda Promptでの作業です。

CUDA Driverの確認

  1. nvcc -V
  2. nvcc: NVIDIA (R) Cuda compiler driver
  3. Copyright (c) 2005-2019 NVIDIA Corporation
  4. Built on Fri_Feb__8_19:08:26_Pacific_Standard_Time_2019
  5. Cuda compilation tools, release 10.1, V10.1.105

cuDNNライブラリパスが通っているかの確認

  1. where cudnn64_7.dll
  2. C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin\cudnn64_7.dll

Pythonバージョンは3.7で仮想環境を作成

  1. conda create -n tf-gpu python=3.7
  2. conda activate tf-gpu

TensorFlow-GPUは2.2をインストール

  1. (tf-gpu) C:\Users\hogeuser>pip install tensorflow-gpu==2.2

いよいよGPUの認識確認

Pythonを立ち上げて、以下のように打ちます。

  1. (tf-gpu) C:\Users\hogeuser>python
  2. from tensorflow.python.client import device_lib
  3. device_lib.list_local_devices()

device_type: "GPU"が表示されれば成功です。当方環境は以下のように認識されました。

  1. (tf-gpu) C:\Users\hogeuser>python
  2. Python 3.7.9 (default, Aug 31 2020, 17:10:11) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
  3. Type "help", "copyright", "credits" or "license" for more information.
  4. >>> from tensorflow.python.client import device_lib
  5. 2021-01-13 23:40:28.746568: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
  6. >>> device_lib.list_local_devices()
  7. 2021-01-13 23:40:40.077791: I tensorflow/core/platform/cpu_feature_guard.cc:143] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
  8. 2021-01-13 23:40:40.085936: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x2a602c459e0 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
  9. 2021-01-13 23:40:40.086023: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
  10. 2021-01-13 23:40:40.087743: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
  11. 2021-01-13 23:40:40.110202: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1561] Found device 0 with properties:
  12. pciBusID: 0000:01:00.0 name: GeForce GTX 1050 Ti computeCapability: 6.1
  13. coreClock: 1.455GHz coreCount: 6 deviceMemorySize: 4.00GiB deviceMemoryBandwidth: 104.43GiB/s
  14. 2021-01-13 23:40:40.110372: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
  15. 2021-01-13 23:40:40.503797: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll
  16. 2021-01-13 23:40:40.731304: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_10.dll
  17. 2021-01-13 23:40:40.751724: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_10.dll
  18. 2021-01-13 23:40:40.970555: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_10.dll
  19. 2021-01-13 23:40:41.178489: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_10.dll
  20. 2021-01-13 23:40:41.392853: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
  21. 2021-01-13 23:40:41.393067: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1703] Adding visible gpu devices: 0
  22. 2021-01-13 23:40:41.973926: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102] Device interconnect StreamExecutor with strength 1 edge matrix:
  23. 2021-01-13 23:40:41.974051: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1108] 0
  24. 2021-01-13 23:40:41.974834: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1121] 0: N
  25. 2021-01-13 23:40:41.975317: W tensorflow/core/common_runtime/gpu/gpu_bfc_allocator.cc:39] Overriding allow_growth setting because the TF_FORCE_GPU_ALLOW_GROWTH environment variable is set. Original config value was 0.
  26. 2021-01-13 23:40:41.975628: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1247] Created TensorFlow device (/device:GPU:0 with 2562 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1050 Ti, pci bus id: 0000:01:00.0, compute capability: 6.1)
  27. 2021-01-13 23:40:41.978480: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x2a630e56380 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
  28. 2021-01-13 23:40:41.978576: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): GeForce GTX 1050 Ti, Compute Capability 6.1
  29. [name: "/device:CPU:0"
  30. device_type: "CPU"
  31. memory_limit: 268435456
  32. locality {
  33. }
  34. incarnation: 5241415701586966866
  35. , name: "/device:XLA_CPU:0"
  36. device_type: "XLA_CPU"
  37. memory_limit: 17179869184
  38. locality {
  39. }
  40. incarnation: 15841878375801700475
  41. physical_device_desc: "device: XLA_CPU device"
  42. , name: "/device:GPU:0"
  43. device_type: "GPU"
  44. memory_limit: 2686628660
  45. locality {
  46. bus_id: 1
  47. links {
  48. }
  49. }
  50. incarnation: 13497597365978317240
  51. physical_device_desc: "device: 0, name: GeForce GTX 1050 Ti, pci bus id: 0000:01:00.0, compute capability: 6.1"
  52. , name: "/device:XLA_GPU:0"
  53. device_type: "XLA_GPU"
  54. memory_limit: 17179869184
  55. locality {
  56. }
  57. incarnation: 4541417229844871161
  58. physical_device_desc: "device: XLA_GPU device"
  59. ]
  60. >>>

cuDNNメモリエラー回避環境変数の設定

Could not create cudnn handle: CUDNN_STATUS_ALLOC_FAILEDエラーの対処法のとおり環境変数TF_FORCE_GPU_ALLOW_GROWTHをtrueにセットしておくこと。

次回はTensorFlow公式のチュートリアルを進める(3)にて、上記無事認識されたGPUに明示的に計算させてみて、CPUで行った結果と比較してみます。

※本記事は当サイト管理人の個人的な備忘録です。本記事の参照又は付随ソースコード利用後にいかなる損害が発生しても当サイト及び管理人は一切責任を負いません。
※本記事内容の無断転載を禁じます。
【WEBMASTER/管理人】
自営業プログラマーです。お仕事ください!
ご連絡は以下アドレスまでお願いします★

【キーワード検索】