如何判断tensorflow是否在python shell内部使用gpu加速?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38009682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:13:08  来源:igfitidea点击:

How to tell if tensorflow is using gpu acceleration from inside python shell?

pythontensorflowubuntugpu

提问by Tamim Addari

I have installed tensorflow in my ubuntu 16.04 using the second answer herewith ubuntu's builtin apt cuda installation.

我已经使用这里的第二个答案在我的 ubuntu 16.04 中安装了 tensorflow和 ubuntu 的内置 apt cuda 安装。

Now my question is how can I test if tensorflow is really using gpu? I have a gtx 960m gpu. When I import tensorflowthis is the output

现在我的问题是如何测试 tensorflow 是否真的在使用 GPU?我有一个 gtx 960m gpu。当我import tensorflow这是输出

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 ?

此输出是否足以检查 tensorflow 是否正在使用 gpu?

采纳答案by Yao Zhang

No, I don't think "open CUDA library" is enough to tell, because different nodes of the graph may be on different devices.

不,我认为“开放 CUDA 库”不足以说明问题,因为图的不同节点可能位于不同的设备上。

To find out which device is used, you can enable log device placement like this:

要找出使用的是哪个设备,您可以像这样启用日志设备放置:

sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))

Check your console for this type of output.

检查您的控制台是否有这种类型的输出。

回答by Salvador Dali

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.

除了使用sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))which 在其他答案以及官方 TensorFlow文档中概述之外,您还可以尝试将计算分配给 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))

Here

这里

  • "/cpu:0": The CPU of your machine.
  • "/gpu:0": The GPU of your machine, if you have one.
  • “/cpu:0”:您机器的 CPU。
  • "/gpu:0": 你机器的 GPU,如果你有的话。

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:

如果你有一个gpu并且可以使用它,你就会看到结果。否则你会看到一个长堆栈跟踪的错误。最后你会得到这样的东西:

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

无法将设备分配给节点“MatMul”:无法满足显式设备规范“/device:GPU:0”,因为在此过程中没有注册匹配该规范的设备



Recently a few helpful functions appeared in TF:

最近在TF中出现了一些有用的功能:

You can also check for available devices in the session:

您还可以检查会话中的可用设备:

with tf.Session() as sess:
  devices = sess.list_devices()

deviceswill return you something like

devices会返回给你类似的东西

[_DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:CPU:0, CPU, -1, 4670268618893924978),
 _DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:XLA_CPU:0, XLA_CPU, 17179869184, 6127825144471676437),
 _DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:XLA_GPU:0, XLA_GPU, 17179869184, 16148453971365832732),
 _DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:TPU:0, TPU, 17179869184, 10003582050679337480),
 _DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:TPU:1, TPU, 17179869184, 5678397037036584928)

回答by Sheraz

Following piece of code should give you all devices available to tensorflow.

以下代码应该为您提供可用于 tensorflow 的所有设备。

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

Sample Output

[name: "/cpu:0" device_type: "CPU" memory_limit: 268435456 locality { } incarnation: 4402277519343584096,

name: "/gpu:0" device_type: "GPU" memory_limit: 6772842168 locality { bus_id: 1 } incarnation: 7471795903849088328 physical_device_desc: "device: 0, name: GeForce GTX 1070, pci bus id: 0000:05:00.0" ]

样本输出

[名称:"/cpu:0" device_type: "CPU" memory_limit: 268435456 locality { } 化身:4402277519343584096,

name: "/gpu:0" device_type: "GPU" memory_limit: 6772842168 locality { bus_id: 1 } incarnation: 7471795903849088328 physical_device_desc: "device: 0, name: GeForce GTX 1070, pci bus id:0.05"

回答by Ishan Bhatt

I think there is an easier way to achieve this.

我认为有一种更简单的方法可以实现这一目标。

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.

这对我来说似乎比那些冗长的日志更容易。

回答by ma3oun

Tensorflow 2.0

TensorFlow 2.0

Sessions are no longer used in 2.0. Instead, one can use tf.test.is_gpu_available:

会话在 2.0 中不再使用。相反,可以使用tf.test.is_gpu_available

import tensorflow as tf

assert tf.test.is_gpu_available()
assert tf.test.is_built_with_cuda()

If you get an error, you need to check your installation.

如果出现错误,则需要检查您的安装。

回答by himanshurobo

This will confirm that tensorflow using GPU while training also ?

这将确认 tensorflow 在训练时也使用 GPU 吗?

Code

代码

sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))

Output

输出

I tensorflow/core/common_runtime/gpu/gpu_device.cc:885] Found device 0 with properties: 
name: GeForce GT 730
major: 3 minor: 5 memoryClockRate (GHz) 0.9015
pciBusID 0000:01:00.0
Total memory: 1.98GiB
Free memory: 1.72GiB
I tensorflow/core/common_runtime/gpu/gpu_device.cc:906] DMA: 0 
I tensorflow/core/common_runtime/gpu/gpu_device.cc:916] 0:   Y 
I tensorflow/core/common_runtime/gpu/gpu_device.cc:975] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GT 730, pci bus id: 0000:01:00.0)
Device mapping:
/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: GeForce GT 730, pci bus id: 0000:01:00.0
I tensorflow/core/common_runtime/direct_session.cc:255] Device mapping:
/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: GeForce GT 730, pci bus id: 0000:01:00.0

回答by karaspd

In addition to other answers, the following should help you to make sure that your version of tensorflow includes GPU support.

除了其他答案之外,以下内容还可以帮助您确保您的 tensorflow 版本包括 GPU 支持。

import tensorflow as tf
print(tf.test.is_built_with_cuda())

回答by kmario23

Ok, first launch an ipython shellfrom the terminal and importTensorFlow:

好的,首先ipython shell从终端和importTensorFlow启动一个:

$ 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 watchthe GPU memory usage in a console using the following command:

现在,我们可以使用以下命令在控制台中查看GPU 内存使用情况:

# 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:

由于我们只import使用了 TensorFlow 但还没有使用任何 GPU,因此使用统计数据将是:

tf non-gpu usage

tf 非 GPU 使用

Notice how the GPU memory usage is very less (~ 700MB); Sometimes the GPU memory usage might even be as low as 0 MB.

请注意 GPU 内存使用量是如何减少的(~ 700MB);有时 GPU 内存使用量甚至可能低至 0 MB。



Now, let's load the GPU in our code. As indicated in tf documentation, do:

现在,让我们在代码中加载 GPU。如 中所示tf documentation,执行:

In [2]: sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))

Now, the watchstats should show an updated GPU usage memory as below:

现在,手表统计信息应显示更新后的 GPU 使用内存,如下所示:

tf gpu-watch

tf gpu-watch

Observe now how our Python process from the ipython shell is using ~ 7 GB of the GPU memory.

现在观察 ipython shell 中的 Python 进程如何使用 ~ 7 GB 的 GPU 内存。



P.S. You can continue watchingthese stats as the code is running, to see how intense the GPU usage is over time.

PS 您可以在代码运行时继续观察这些统计数据,以了解 GPU 使用率随时间推移的强度。

回答by f0nzie

This should give the list of devices available for Tensorflow (under Py-3.6):

这应该提供可用于 Tensorflow 的设备列表(在 Py-3.6 下):

tf = tf.Session(config=tf.ConfigProto(log_device_placement=True))
tf.list_devices()
# _DeviceAttributes(/job:localhost/replica:0/task:0/device:CPU:0, CPU, 268435456)

回答by scott huang

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.

我更喜欢使用 nvidia-smi 来监控 GPU 使用情况。如果它在您开始编程时显着上升,则表明您的 tensorflow 正在使用 GPU。