TensorflowがGPUで動作しない

TensorflowがGPUで動作しない


 数時間前までGPUで動作していたプログラムがGPUで動作しなくなりました。 バージョンアップもしていない、新しいライブラリを入れたわけではないし、なぜそうなったか結局原因はわからなかったです。 数年前にtensorflow-gpuを導入した時もかなり時間とられてた気がします…

最終的にどうにかなりました。以下やってみた順番どおりに記載します。 (私はwindowsでanaconda3で環境を構築してます)

tensorflowをGPUで動作させるためのtensorflow-gpuに問題があると思い、 一度、tensorflowとtensorflow-gpuをアンインストールして、tensorflow-gpuを再度インストール。

アンインストール

pip uninstall tensorflow
pip uninstall tensorflow-gpu

インストール

pip install tensorflow-gpu

以下コマンドを打って、”/device:GPU:0”と表示されてGPUが認識されていればよいです。

from tensorflow.python.client import device_lib
device_lib.list_local_devices()

GPUは認識されませんでした。

tensorflow-gpu, CUDA, cuDNN のバージョンが合っていないといけないですが、 これまで動作していたので、問題はなかったはずです。しかし、tensorflow-gpuを再インストールしたので、 今一度確認しました。

利用していたのは、cuda: 8.0, cudann: 6.0でした。 以下公式サイトでtensorflow-gpuとCUDA, cuDNNの対応するバージョンが確認できます。
https://www.tensorflow.org/install/source#common_installation_problems

CUDAのバージョンはnvcc -Vコマンドで確認できますが、やってみたところ、

> nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2017 NVIDIA Corporation
Built on Fri_Nov__3_21:08:12_Central_Daylight_Time_2017
Cuda compilation tools, release 9.1, V9.1.85

9.1が入っていました。次のパス C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA には8.0 と9.1の両方がありました。パスが通っていたのは9.1だった?ただし、cuDNNは6.0しかなく、CUDA 9.1に対応するのはcuDNN 7 のはずです。混乱してきました。

とりあえず、CUDAのサイトより、cuDNNの7.1.3をダウンロードしてライブラリをCUDA9.1に導入しました。

実行してみると、次は以下のエラーがでました。

ImportError: DLL load failed: The specified module could not be found

tensorflow-gpuは1.12.0を入れていて、CUDAもcuDNNもバージョンはあっているはずですが… 調べてみると、tensorflow-gpuはanaconda環境でpipではなく、condaで入れるとエラーが消えたという例があるみたいです。

conda uninstall tensorflow-gpu
conda install tensorflow-gpu

install中、cuda関係のパッケージダウンロードもしているみたい?なんとなくよさそうです。 実行すると、またもエラーが出ました。

FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
from ._conv import register_converters as _register_converters
Warning! ***HDF5 library version mismatched error***
The HDF5 header files used to compile this application do not match
the version used by the HDF5 library to which this application is linked.
Data corruption or segmentation faults may occur if the application continues.
This can happen when an application was compiled by one version of HDF5 but
linked with a different version of static or shared HDF5 library.
You should recompile the application or check your shared library related
settings such as 'LD_LIBRARY_PATH'.
You can, at your own risk, disable this warning by setting the environment
variable 'HDF5_DISABLE_VERSION_CHECK' to a value of '1'.
Setting it to 2 or higher will suppress the warning messages totally.
Headers are 1.10.1, library is 1.10.2
SUMMARY OF THE HDF5 CONFIGURATION

kerasのバージョンが古い可能性がありそうなので、アップグレード。

pip install keras --upgrade

変化なしです。次に、h5pyをアップグレード。

pip install --upgrade h5py

上のエラーは消えました。しかし、またもエラーです!

E tensorflow/stream_executor/cuda/cuda_dnn.cc:373] Could not create cudnn handle: CUDNN_STATUS_ALLOC_FAILED
E tensorflow/stream_executor/cuda/cuda_dnn.cc:373] Could not create cudnn handle: CUDNN_STATUS_ALLOC_FAILED
F tensorflow/core/kernels/conv_grad_input_ops.cc:981] Check failed: stream->parent()->GetConvolveBackwardDataAlgorithms( conv_parameters.ShouldIncludeWinogradNonfusedAlgo<T>(stream->parent()), &algorithms)

ALLOC_FAILEDってあるので、メモリ関係なのだろうと思い、.pyソースの以下gpuのメモリ使用率 を0.9から0.8に下げてみました。

import tensorflow as tf
from keras.backend.tensorflow_backend import set_session
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.9 # → 0.8に変更
set_session(tf.Session(config=config))

やっと動きました~。