前回はcuda10, cudnn7.3, python3.7の環境でTensorFlowのbuildに失敗したので、今度はcuda10, cudnn7.3, python3.6の組み合わせでtensorflowをbuildしてみた。
スポンサーリンク
tensorflowをbuild¶
!pip3 install -U six wheel mock
!pip3 install -U keras_applications==1.0.5 --no-deps
!pip install -U keras_preprocessing==1.0.3 --no-deps
cd /root/tensorflow
!git pull
!git checkout v1.11.0
!./configure
!bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package
cd /root/tensorflow
!./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
スポンサーリンク
tensorflowをinstall¶
ls /tmp/tensorflow_pkg/
!pip3 install /tmp/tensorflow_pkg/tensorflow-1.11.0-cp36-cp36m-linux_x86_64.whl
cd
スポンサーリンク
tensorflowをimport¶
%load_ext version_information
import tensorflow
%version_information tensorflow, pycuda, scikit-cuda
!nvcc --version
スポンサーリンク
tensorflowをテストする¶
import numpy as np
import tensorflow as tf
import time
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.667)
sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
dim = 10000
rnd = np.random.RandomState(0)
a = rnd.rand(dim, dim).astype(np.float32)
b = rnd.rand(dim, dim).astype(np.float32)
start = time.time()
rescpu = np.dot(a, b)
c = time.time() - start
print ('CPU:', c)
X = tf.placeholder(tf.float32, shape=(dim, dim), name=None)
Y = tf.placeholder(tf.float32, shape=(dim, dim), name=None)
Z = tf.matmul(X, Y)
sess = tf.Session()
start = time.time()
resgpu = sess.run(Z, feed_dict={X: a, Y: b})
d = time.time() - start
print ('GPU:', d)
print('Speed difference: {:0.1f}x'.format(c / d))
print (np.allclose(rescpu, resgpu))
print (np.allclose(resgpu, rescpu))
numpyが高速化されている一方でtensorflowは劣化している。
import pycuda.gpuarray as gpuarray
import pycuda.autoinit
import numpy as np, time
import skcuda.linalg as culinalg
import skcuda
culinalg.init()
import tensorflow as tf
import time
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.667)
sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
dim = 10000
rnd = np.random.RandomState(0)
a = rnd.rand(dim, dim).astype(np.float32)
b = rnd.rand(dim, dim).astype(np.float32)
a_gpu = gpuarray.to_gpu(a)
b_gpu = gpuarray.to_gpu(b)
start = time.time()
ressk = culinalg.dot(a_gpu, b_gpu)
c = time.time() - start
print ('sk:', c)
ressk = ressk.get()
X = tf.placeholder(tf.float32, shape=(dim, dim), name=None)
Y = tf.placeholder(tf.float32, shape=(dim, dim), name=None)
Z = tf.matmul(X, Y)
sess = tf.Session()
start = time.time()
restf = sess.run(Z, feed_dict={X: a, Y: b})
d = time.time() - start
print ('tf:', d)
print ('Speed difference: {:0.1f}x'.format(d / c))
print (np.allclose(ressk, restf))
print (np.allclose(restf, ressk))
skcudaもtensorflowも速度が劣化している。cuda10とcudnn7.3のメリットがないというか、GPUカードがしょぼいのが原因なのか、何れにしても、2年以上前の古臭いGPUカードではあまり効果がないということか。最新のRTX 2070あたりを買う必要があるのかもしれない。
スポンサーリンク
スポンサーリンク