0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

关于TensorFlow在GPU中的使用规则

Tensorflowers 来源:cg 2018-12-04 09:27 次阅读

支持的设备

在一套标准系统中通常有多台计算设备。TensorFlow 支持CPUGPU这两种设备。它们均用strings表示。

例如:

"/cpu:0":机器的 CPU

"/device:GPU:0":机器的 GPU(如果有一个)

"/device:GPU:1":机器的第二个 GPU(以此类推)

如果 TensorFlow 指令中兼有 CPU 和 GPU 实现,当该指令分配到设备时,GPU 设备有优先权。例如,如果matmul同时存在 CPU 和 GPU 核函数,在同时有cpu:0和gpu:0设备的系统中,gpu:0会被选来运行matmul。

记录设备分配方式

要找出您的指令和张量被分配到哪个设备,请创建会话并将log_device_placement配置选项设为True。

# Creates a graph.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)# Creates a session with log_device_placement set to True.sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))# Runs the op.print(sess.run(c))

您应该会看到以下输出内容:

Device mapping:/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: Tesla K40c, pci busid: 0000:05:00.0b: /job:localhost/replica:0/task:0/device:GPU:0a: /job:localhost/replica:0/task:0/device:GPU:0MatMul: /job:localhost/replica:0/task:0/device:GPU:0[[ 22. 28.][ 49. 64.]]

手动分配设备

如果您希望特定指令在您选择的设备(而非系统自动为您选择的设备)上运行,您可以使用with tf.device创建设备上下文,这个上下文中的所有指令都将被分配在同一个设备上运行。

# Creates a graph.with tf.device('/cpu: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)# Creates a session with log_device_placement set to True.sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))# Runs the op.print(sess.run(c))

您会看到现在a和b被分配到cpu:0。由于未明确指定运行MatMul指令的设备,因此 TensorFlow 运行时将根据指令和可用设备(此示例中的gpu:0)选择一个设备,并会根据要求自动复制设备间的张量。

Device mapping:/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: Tesla K40c, pci busid: 0000:05:00.0b: /job:localhost/replica:0/task:0/cpu:0a: /job:localhost/replica:0/task:0/cpu:0MatMul: /job:localhost/replica:0/task:0/device:GPU:0[[ 22. 28.][ 49. 64.]]

允许增加 GPU 内存

默认情况下,TensorFlow 会映射进程可见的所有 GPU 的几乎所有 GPU 内存(取决于CUDA_VISIBLE_DEVICES)。通过减少内存碎片,可以更有效地使用设备上相对宝贵的 GPU 内存资源。

在某些情况下,最理想的是进程只分配可用内存的一个子集,或者仅根据进程需要增加内存使用量。TensorFlow 在 Session 上提供两个 Config 选项来进行控制。

第一个是allow_growth选项,它试图根据运行时的需要来分配 GPU 内存:它刚开始分配很少的内存,随着 Session 开始运行并需要更多 GPU 内存,我们会扩展 TensorFlow 进程所需的 GPU 内存区域。请注意,我们不会释放内存,因为这可能导致出现更严重的内存碎片情况。要开启此选项,请通过以下方式在 ConfigProto 中设置选项:

config = tf.ConfigProto()config.gpu_options.allow_growth = Truesession = tf.Session(config=config, ...)

如要真正限制 TensorFlow 进程可使用的 GPU 内存量,这非常实用。

在多 GPU 系统中使用单一 GPU

如果您的系统中有多个 GPU,则默认情况下将选择 ID 最小的 GPU。如果您希望在其他 GPU 上运行,则需要显式指定偏好设置:

# Creates a graph.with tf.device('/device:GPU:2'): 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)# Creates a session with log_device_placement set to True.sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))# Runs the op.print(sess.run(c))

如果您指定的设备不存在,您会看到InvalidArgumentError:

InvalidArgumentError: Invalid argument: Cannot assign a device to node 'b':Could not satisfy explicit device specification '/device:GPU:2' [[Node: b = Const[dtype=DT_FLOAT, value=Tensor, _device="/device:GPU:2"]()]]

当指定设备不存在时,如果您希望 TensorFlow 自动选择现有的受支持设备来运行指令,则可以在创建会话时将配置选项中的allow_soft_placement设为True。

# Creates a graph.with tf.device('/device:GPU:2'): 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)# Creates a session with allow_soft_placement and log_device_placement set# to True.sess = tf.Session(config=tf.ConfigProto( allow_soft_placement=True, log_device_placement=True))# Runs the op.print(sess.run(c))

使用多个 GPU

如果您想要在多个 GPU 上运行 TensorFlow,则可以采用多塔式方式构建模型,其中每个塔都会分配给不同 GPU。例如:

# Creates a graph.c = []for d in ['/device:GPU:2', '/device:GPU:3']: with tf.device(d): a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3]) b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2]) c.append(tf.matmul(a, b))with tf.device('/cpu:0'): sum = tf.add_n(c)# Creates a session with log_device_placement set to True.sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))# Runs the op.print(sess.run(sum))

您会看到以下输出内容:

Device mapping:/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: Tesla K20m, pci busid: 0000:02:00.0/job:localhost/replica:0/task:0/device:GPU:1 -> device: 1, name: Tesla K20m, pci busid: 0000:03:00.0/job:localhost/replica:0/task:0/device:GPU:2 -> device: 2, name: Tesla K20m, pci busid: 0000:83:00.0/job:localhost/replica:0/task:0/device:GPU:3 -> device: 3, name: Tesla K20m, pci busid: 0000:84:00.0Const_3: /job:localhost/replica:0/task:0/device:GPU:3Const_2: /job:localhost/replica:0/task:0/device:GPU:3MatMul_1: /job:localhost/replica:0/task:0/device:GPU:3Const_1: /job:localhost/replica:0/task:0/device:GPU:2Const: /job:localhost/replica:0/task:0/device:GPU:2MatMul: /job:localhost/replica:0/task:0/device:GPU:2AddN: /job:localhost/replica:0/task:0/cpu:0[[ 44. 56.][ 98. 128.]]

cifar10 教程就是个很好的例子(https://tensorflow.google.cn/tutorials/images/deep_cnn?hl=zh-CN),演示了如何使用多个 GPU 进行训练。

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • gpu
    gpu
    +关注

    关注

    27

    文章

    4417

    浏览量

    126689
  • tensorflow
    +关注

    关注

    13

    文章

    313

    浏览量

    60242

原文标题:TensorFlow 指南:GPU 的使用

文章出处:【微信号:tensorflowers,微信公众号:Tensorflowers】欢迎添加关注!文章转载请注明出处。

收藏 人收藏

    评论

    相关推荐

    关于 TensorFlow

    节点间相互联系的多维数据数组,即张量(tensor)。它灵活的架构让你可以多种平台上展开计算,例如台式计算机的一个或多个CPU(或GPU),服务器,移动设备等等。
    发表于 03-30 19:57

    使用 TensorFlow, 你必须明白 TensorFlow

    :1": 机器的第二个 GPU, 以此类推.阅读使用GPU章节, 了解 TensorFlow GPU 使用的更多信息.交互式使用文档的 P
    发表于 03-30 20:03

    阿里云Kubernetes容器服务上打造TensorFlow实验室

    GPU的使用,同时支持最新的TensorFLow版本, 对于数据科学家来说既是复杂的,同时也是浪费精力的。阿里云的Kubernetes集群上,您可以通过简单的按钮提交创建一套完整的Tenso
    发表于 05-10 10:24

    可以vGPU配置文件上运行TensorFlow吗?

    大家好,我有一些问题,你们可能知道答案。我阅读了文档,但我还不确定。开发团队中有一些人正在为TensorFlow(AI项目)寻找GPU。我们对工作站和Dockers上运行的Quadro GP
    发表于 09-18 16:35

    干货!教你怎么搭建TensorFlow深度学习开发环境!

    的Installation一栏,Windows的Tensorflow有CPU和GPU两个版本,安装了CUDA8.0的朋友们可以选择下载GPU版本。下载好以后,“开始”菜单的“运行”里
    发表于 09-27 13:56

    深度学习框架TensorFlow&TensorFlow-GPU详解

    TensorFlow&TensorFlow-GPU:深度学习框架TensorFlow&TensorFlow-GPU的简介、安装、使用方法详细攻略
    发表于 12-25 17:21

    tensorflow-gpu安装报错的修改

    tensorflow-gpu安装遇到的一些问题解决
    发表于 05-20 10:25

    TensorFlow是什么

    更长。TensorFlow 使这一切变得更加简单快捷,从而缩短了想法到部署之间的实现时间。本教程,你将学习如何利用 TensorFlow 的功能来实现深度神经网络。
    发表于 07-22 10:14

    TensorFlow安装和下载(超详细)

    TensorFlow安装具体做法命令行中使用以下命令创建 conda 环境(如果使用 Windows,最好在命令行以管理员身份执行):conda create -n tensorflow
    发表于 07-22 10:25

    TensorFlow教程|常见问题

    (name): context 创建操作(operation),这样可以指定的设备上运行操作(operation)。 关于 TensorFlow 怎样将操作(operations)
    发表于 07-27 18:33

    TensorFlow 系统当前实际的应用

    如果您在做研究、教育、或在某些产品中正在使用 TensorFlow, 我们非常乐意在这里添加一些有关您的使用情况。 请随时给我们发电子邮件简要说明您是如何使用TensorFlow的, 或者给我们发
    发表于 07-27 18:33

    TensorFlow XLA加速线性代数编译器

    编译:会话级别打开JIT编译: 这是手动打开 JIT 编译: 还可以通过将操作指定在特定的 XLA 设备(XLA_CPU 或 XLA_GPU)上,通过 XLA 来运行计算: AoT编译:独立使用 tfcompile 将
    发表于 07-28 14:31

    TensorFlow指定CPU和GPU设备操作详解

    TensorFlow 支持 CPU 和 GPU。它也支持分布式计算。可以一个或多个计算机系统的多个设备上使用 TensorFlowTensorF
    发表于 07-28 14:33

    Mali GPU支持tensorflow或者caffe等深度学习模型吗

    Mali GPU 支持tensorflow或者caffe等深度学习模型吗? 好像caffe2go和tensorflow lit可以部署到ARM,但不知道是否支持
    发表于 09-16 14:13

    如何在AMD的GPU上运行TensorFlow

    ROCm 即 Radeon 开放生态系统 (Radeon Open Ecosystem),是我们在 Linux 上进行 GPU 计算的开源软件基础。而 TensorFlow 实现则使用了 MIOpen,这是一个适用于深度学习的高度优化
    的头像 发表于 10-04 08:59 2.4w次阅读