I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcublas.so locally
I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcudnn.so locally
I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcufft.so locally
I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcuda.so.1 locally
I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcurand.so locally
I have installed tensorflow in my ubuntu 16.04 using the second answer here with ubuntu’s builtin apt cuda installation.
Now my question is how can I test if tensorflow is really using gpu? I have a gtx 960m gpu. When I import tensorflow this is the output
I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcublas.so locally
I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcudnn.so locally
I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcufft.so locally
I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcuda.so.1 locally
I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcurand.so locally
Is this output enough to check if tensorflow is using gpu ?
import tensorflow as tf
with tf.device('/gpu:0'):
a = tf.constant([1.0,2.0,3.0,4.0,5.0,6.0], shape=[2,3], name='a')
b = tf.constant([1.0,2.0,3.0,4.0,5.0,6.0], shape=[3,2], name='b')
c = tf.matmul(a, b)with tf.Session()as sess:print(sess.run(c))
Apart from using sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)) which is outlined in other answers as well as in the official TensorFlow documentation, you can try to assign a computation to the gpu and see whether you have an error.
import tensorflow as tf
with tf.device('/gpu:0'):
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
c = tf.matmul(a, b)
with tf.Session() as sess:
print (sess.run(c))
Here
“/cpu:0”: The CPU of your machine.
“/gpu:0”: The GPU of your machine, if you have one.
If you have a gpu and can use it, you will see the result. Otherwise you will see an error with a long stacktrace. In the end you will have something like this:
Cannot assign a device to node ‘MatMul’: Could not satisfy explicit
device specification ‘/device:GPU:0’ because no devices matching that
specification are registered in this process
import tensorflow as tf
if tf.test.gpu_device_name():print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))else:print("Please install GPU version of TF")
import tensorflow as tf
if tf.test.gpu_device_name():
print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))
else:
print("Please install GPU version of TF")
It usually prints like
Default GPU Device: /device:GPU:0
This seems easier to me rather than those verbose logs.
Ok, first launch an ipython shell from the terminal and import TensorFlow:
$ ipython --pylab
Python 3.6.5 |Anaconda custom (64-bit)| (default, Apr 29 2018, 16:14:56)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.4.0 -- An enhanced Interactive Python. Type '?' for help.
Using matplotlib backend: Qt5Agg
In [1]: import tensorflow as tf
Now, we can watch the GPU memory usage in a console using the following command:
# realtime update for every 2s
$ watch -n 2 nvidia-smi
Since we’ve only imported TensorFlow but have not used any GPU yet, the usage stats will be:
Notice how the GPU memory usage is very less (~ 700MB); Sometimes the GPU memory usage might even be as low as 0 MB.
Now, let’s load the GPU in our code. As indicated in tf documentation, do:
In [2]: sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
Now, the watch stats should show an updated GPU usage memory as below:
Observe now how our Python process from the ipython shell is using ~ 7 GB of the GPU memory.
P.S. You can continue watching these stats as the code is running, to see how intense the GPU usage is over time.
I prefer to use nvidia-smi to monitor GPU usage. if it goes up significantly when you start you program, it’s a strong sign that your tensorflow is using GPU.
You can check if you are currently using the GPU by running the following code:
import tensorflow as tf
tf.test.gpu_device_name()
If the output is '', it means you are using CPU only;
If the output is something like that /device:GPU:0, it means GPU works.
And use the following code to check which GPU you are using:
from tensorflow.python.client import device_lib
device_lib.list_local_devices()
回答 14
将其放在jupyter笔记本顶部附近。注释掉您不需要的内容。
# confirm TensorFlow sees the GPUfrom tensorflow.python.client import device_lib
assert'GPU'in str(device_lib.list_local_devices())# confirm Keras sees the GPU (for TensorFlow 1.X + Keras)from keras import backend
assert len(backend.tensorflow_backend._get_available_gpus())>0# confirm PyTorch sees the GPUfrom torch import cuda
assert cuda.is_available()assert cuda.device_count()>0print(cuda.get_device_name(cuda.current_device()))
Put this near the top of your jupyter notebook. Comment out what you don’t need.
# confirm TensorFlow sees the GPU
from tensorflow.python.client import device_lib
assert 'GPU' in str(device_lib.list_local_devices())
# confirm Keras sees the GPU (for TensorFlow 1.X + Keras)
from keras import backend
assert len(backend.tensorflow_backend._get_available_gpus()) > 0
# confirm PyTorch sees the GPU
from torch import cuda
assert cuda.is_available()
assert cuda.device_count() > 0
print(cuda.get_device_name(cuda.current_device()))
NOTE: With the release of TensorFlow 2.0, Keras is now included as part of the TF API.
The recommended way in which to check if TensorFlow is using GPU is the following:
tf.config.list_physical_devices('GPU')
As of TensorFlow 2.1, tf.test.gpu_device_name() has been deprecated in favour of the aforementioned.
Then, in the terminal you can use nvidia-smi to check how much GPU memory has been alloted; at the same time, using watch -n K nvidia-smi would tell you for example every K seconds how much memory you are using (you may want to use K = 1 for real-time)
回答 17
这是我用来列出可tf.session直接从bash 访问的设备的行:
python -c "import os; os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'; import tensorflow as tf; sess = tf.Session(); [print(x) for x in sess.list_devices()]; print(tf.__version__);"
This is the line I am using to list devices available to tf.session directly from bash:
python -c "import os; os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'; import tensorflow as tf; sess = tf.Session(); [print(x) for x in sess.list_devices()]; print(tf.__version__);"
It will print available devices and tensorflow version, for example:
import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()with tf.device('/gpu:0'):
a = tf.constant([1.0,2.0,3.0,4.0,5.0,6.0], shape=[2,3], name='a')
b = tf.constant([1.0,2.0,3.0,4.0,5.0,6.0], shape=[3,2], name='b')
c = tf.matmul(a, b)with tf.Session()as sess:print(sess.run(c))
Tensorflow 1测试
import tensorflow as tf
with tf.device('/gpu:0'):
a = tf.constant([1.0,2.0,3.0,4.0,5.0,6.0], shape=[2,3], name='a')
b = tf.constant([1.0,2.0,3.0,4.0,5.0,6.0], shape=[3,2], name='b')
c = tf.matmul(a, b)with tf.Session()as sess:print(sess.run(c))
I found below snippet is very handy to test the gpu ..
Tensorflow 2.0 Test
import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
with tf.device('/gpu:0'):
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
c = tf.matmul(a, b)
with tf.Session() as sess:
print (sess.run(c))
Tensorflow 1 Test
import tensorflow as tf
with tf.device('/gpu:0'):
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
c = tf.matmul(a, b)
with tf.Session() as sess:
print (sess.run(c))
You have some options to test whether GPU acceleration is being used by your TensorFlow installation.
You can type in the following commands in three different platforms.
import tensorflow as tf
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
Jupyter Notebook – Check the console which is running the Jupyter Notebook. You will be able to see the GPU being used.
Python Shell – You will be able to directly see the output. (Note- do not assign the output of the second command to the variable ‘sess’; if that helps).
Spyder – Type in the following command in the console.
import tensorflow as tf
tf.test.is_gpu_available()
回答 22
Tensorflow 2.1
可以使用nvidia-smi进行验证的简单计算,以了解GPU上的内存使用情况。
import tensorflow as tf
c1 =[]
n =10def matpow(M, n):if n <1:#Abstract cases where n < 1return M
else:return tf.matmul(M, matpow(M, n-1))with tf.device('/gpu:0'):
a = tf.Variable(tf.random.uniform(shape=(10000,10000)), name="a")
b = tf.Variable(tf.random.uniform(shape=(10000,10000)), name="b")
c1.append(matpow(a, n))
c1.append(matpow(b, n))
A simple calculation that can be verified with nvidia-smi for memory usage on the GPU.
import tensorflow as tf
c1 = []
n = 10
def matpow(M, n):
if n < 1: #Abstract cases where n < 1
return M
else:
return tf.matmul(M, matpow(M, n-1))
with tf.device('/gpu:0'):
a = tf.Variable(tf.random.uniform(shape=(10000, 10000)), name="a")
b = tf.Variable(tf.random.uniform(shape=(10000, 10000)), name="b")
c1.append(matpow(a, n))
c1.append(matpow(b, n))
回答 23
>>>import tensorflow as tf
>>> tf.config.list_physical_devices('GPU')2020-05-1014:58:16.243814: I tensorflow/stream_executor/platform/default/dso_loader.cc:44]Successfully opened dynamic library libcuda.so.12020-05-1014:58:16.262675: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read fromSysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-05-1014:58:16.263119: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1555]Found device 0with properties:
pciBusID:0000:01:00.0 name:GeForce GTX 10606GB computeCapability:6.1
coreClock:1.7715GHz coreCount:10 deviceMemorySize:5.93GiB deviceMemoryBandwidth:178.99GiB/s
2020-05-1014:58:16.263143: I tensorflow/stream_executor/platform/default/dso_loader.cc:44]Successfully opened dynamic library libcudart.so.10.12020-05-1014:58:16.263188: I tensorflow/stream_executor/platform/default/dso_loader.cc:44]Successfully opened dynamic library libcublas.so.102020-05-1014:58:16.264289: I tensorflow/stream_executor/platform/default/dso_loader.cc:44]Successfully opened dynamic library libcufft.so.102020-05-1014:58:16.264495: I tensorflow/stream_executor/platform/default/dso_loader.cc:44]Successfully opened dynamic library libcurand.so.102020-05-1014:58:16.265644: I tensorflow/stream_executor/platform/default/dso_loader.cc:44]Successfully opened dynamic library libcusolver.so.102020-05-1014:58:16.266329: I tensorflow/stream_executor/platform/default/dso_loader.cc:44]Successfully opened dynamic library libcusparse.so.102020-05-1014:58:16.266357: I tensorflow/stream_executor/platform/default/dso_loader.cc:44]Successfully opened dynamic library libcudnn.so.72020-05-1014:58:16.266478: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read fromSysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-05-1014:58:16.266823: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read fromSysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-05-1014:58:16.267107: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1697]Adding visible gpu devices:0[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
>>> import tensorflow as tf
>>> tf.config.list_physical_devices('GPU')
2020-05-10 14:58:16.243814: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcuda.so.1
2020-05-10 14:58:16.262675: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-05-10 14:58:16.263119: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1555] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: GeForce GTX 1060 6GB computeCapability: 6.1
coreClock: 1.7715GHz coreCount: 10 deviceMemorySize: 5.93GiB deviceMemoryBandwidth: 178.99GiB/s
2020-05-10 14:58:16.263143: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.1
2020-05-10 14:58:16.263188: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcublas.so.10
2020-05-10 14:58:16.264289: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcufft.so.10
2020-05-10 14:58:16.264495: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcurand.so.10
2020-05-10 14:58:16.265644: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusolver.so.10
2020-05-10 14:58:16.266329: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusparse.so.10
2020-05-10 14:58:16.266357: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudnn.so.7
2020-05-10 14:58:16.266478: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-05-10 14:58:16.266823: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2020-05-10 14:58:16.267107: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1697] Adding visible gpu devices: 0
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
As suggested by @AmitaiIrron:
This section indicates that a gpu was found
2020-05-10 14:58:16.263119: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1555] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: GeForce GTX 1060 6GB computeCapability: 6.1
coreClock: 1.7715GHz coreCount: 10 deviceMemorySize: 5.93GiB deviceMemoryBandwidth: 178.99GiB/s
And here that it got added as an available physical device