码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • TensorFlow的GPU使用相关设置整理


    前言

    TensorFlow是一个在机器学习和深度学习领域被广泛使用的开源软件库,用于各种感知和语言理解任务的机器学习。

    在这里插入图片描述
    默认情况下,TensorFlow 会映射进程可见的所有 GPU(取决于 CUDA_VISIBLE_DEVICES)的几乎全部内存。这是为了减少内存碎片,更有效地利用设备上相对宝贵的 GPU 内存资源。但有时我们不需要这种特性,所以需要想办法避免这种情况。

    本文简要介绍TensorFlow的GPU使用相关设置。

    import tensorflow as tf
    
    • 1

    显卡使用设置方式

    1. 设置进程在指定的显卡上运行
    
    device_id = 5
    gpus = tf.config.experimental.list_physical_devices('GPU')
    gpu = gpus[device_id]
    tf.config.set_visible_devices(gpu, 'GPU')
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    1. 设置单卡分布式模式运行
    strategy = tf.distribute.OneDeviceStrategy(device=f"/gpu:{device_id}")
    with self.strategy.scope():
    	outputs = model(inputs)
    
    • 1
    • 2
    • 3

    在这里插入图片描述
    3. 设备指定显卡放置变量和运行计算

    
    device_id = 2
    with tf.device(f"/gpu:{device_id}"):
    	outputs = model(inputs)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    显存占用设置方式

    1. 设置每张卡上所运行进程占用的显存比
    
    tf_config = tf.compat.v1.ConfigProto()
    tf_config.gpu_options.per_process_gpu_memory_fraction = 0.15 # 设置太小可能会导致所加载的网络某些层初始化失败
    session = tf.compat.v1.Session(config=tf_config)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    该方法为兼容v1版本的TensorFlow的方法,相应的还有set_memory_growth方法。见参考资料[3]。

    1. 设置每张卡上所运行进程的显存占用上限
    
     gpus = tf.config.experimental.list_physical_devices('GPU')
     for gpu_i in gpus:
         tf.config.experimental.set_virtual_device_configuration(gpu_i, [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=3096)])
            
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    其他说明

    实测发现以下组合能够较好控制显卡和显存的使用:

    
    device_id = 5
    
    gpus = tf.config.experimental.list_physical_devices('GPU')
    
    gpu = gpus[device_id]
    
    tf.config.experimental.set_virtual_device_configuration(gpu, [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=3096)])
    
    tf.config.set_visible_devices(gpu, 'GPU')
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    版权说明

    本文为原创文章,独家发布在blog.csdn.net/TracelessLe。未经个人允许不得转载。如需帮助请email至tracelessle@163.com或扫描个人介绍栏二维码咨询。
    在这里插入图片描述

    参考资料

    [1] 使用 GPU | TensorFlow Core
    [2] 使用 TensorFlow 进行分布式训练 | TensorFlow Core
    [3] tf.config.experimental.set_memory_growth | TensorFlow v2.10.0
    [4] machine learning - Why Tensorflow’s MirroredStrategy and OneDevicestrategy does not work on colab? - Stack Overflow
    [5] tf.distribute.OneDeviceStrategy | TensorFlow v2.10.0
    [6] Tensorflow v2 Limit GPU Memory usage · Issue #25138 · tensorflow/tensorflow

  • 相关阅读:
    简单算法——回溯、贪心、动态规划
    【PAT甲级 - C++题解】1104 Sum of Number Segments
    vue模板语法: 插值语法和指令语法以及v-bind指令使用
    golang中的网络轮询器netpoll源码解析
    Unity Golang项目教程-创建项目
    微信开发工具构建npm and git切换分支
    音视频从入门到精通——超简单的基于FFMPEG+SDL的视频播放器(二)
    连表查询数据,将数据分组串联到一起
    Linux ❀ ps进程操作与僵尸进程解决方法
    Redis 清空数据库
  • 原文地址:https://blog.csdn.net/TracelessLe/article/details/127758774
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号