zoukankan      html  css  js  c++  java
  • Tensorflow、Pytorch、Keras的多GPU使用

    Tensorflow、Pytorch、Keras的多GPU的并行操作

    方法一 :使用深度学习工具提供的 API指定

    1.1 Tesorflow

     tensroflow指定GPU的多卡并行的时候,也是可以先将声明的变量放入GPU中(PS:这点我还是不太明白,为什么其他的框架没有这样做)

    with tf.device("/gpu:%d"%i):
    
    with tf.device("cpu:0")

    在创建Session的时候,通过指定session的参数,便可以指定GPU的数量和使用率

    ConfigProto()类提供有对GPU使用率的函数方法:

    config = tf.ConfigProto() 
    config.gpu_options.per_process_gpu_memory_fraction = 0.9 # 设置使用率,占用GPU90%的显存 
    session = tf.Session(config=config) 

    还可以指定GPU的数量是否增长

    # os.environ["CUDA_VISIBLE_DEVICES"] = "0,1"(其中0.1是选择所调用的gpu id)
    gpu_options = tf.GPUOptions(allow_growth=True
    config = tf.ConfigProto(gpu_options=gpu_options) config.gpu_options.allow_growth = True  session = tf.Session(config=config)

    个人认为最好的方式:

    import os # 建议使用这种方式
    import tensorflow as tf
    os.environ["CUDA_VISIBLE_DEVICES"] = "2" # python 的方式指定GPU id
    from keras.backend.tensorflow_backend import set_session # 如果不是使用Kears的话,可以不用写这句话
    config = tf.ConfigProto() 
    config.gpu_options.per_process_gpu_memory_fraction = 0.3 # 指定GPU 利用率
    set_session(tf.Session(config=config))

    1.2 Pytorch

    Pytorch 是目前比较方便的一种深度学习框架,在指定GPU的方法上,也是比较简洁。PyTorch提供有torch.cuda.set_device() 方法

    import torch
    torch.cuda.set_device(id)

    这种方式只能制定一个GPU,不太建议使用

    1.3 Keras

    由于Kears是作为Tesorflow或者Theano的前端出现的,从某种方式上也是可以用后端深度学习框架进行多GPU的指定的,比如 1.1小节中就是调用的kears的多GPU并行的方式,但这种方式写的代码不美观

    Kears本身提供有 keras.utils.multi_gpu_model(model=xxx,gpus=nums) 函数来指定使用多少GPU进行编译

    1 from keras.utils import multi_gpu_model
    2 model=SegNet() # 构建model
    4 parralle_model = multi_gpu_model(model,gpus=3) # 指定gpu的数量
    6 parralle_model.compile(loss='mse', optimizer=optimizer, loss_weights=[1,1]) # 模型编译

    方法二: 在终端指定

    2.1 显式的在终端指定

    这种方法与深度学习工具无关了,无论是Keras tensorflow 还是Pytorch都是可以使用的

    CUDA_VISIBLE_DEVICES=0,2,3  python your_file.py

    这里的0,1,3是通过nvidia-smi 命令查看的GPU编号.

    2.2 在Python代码中指定

    这种方法也是与深度学习工具无关的方法。仔细看看这种方式其实和方法二类似,关键字都是一样的

    import os
    
    os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
    os.environ["CUDA_VISIBLE_DEVICES"] = "0" # 多个GPU下使用的方法是“0,1,2,3”

    总结

    目前,很多深度学习框架已经把多GPU的并行操作封装的很好了,基本上可以满足开发者的应用需求。

    需要注意的是,第二种方法在有些时候是无法起到作用的;比如我今天就遇到到这种情况: 用Kears的时候指定了多个GPU,但还是出现OOM异常,最后请教了一位厉害的程序媛小姐姐才知道Kears需要使用 keras.utils.multi_gpu_model(model=xxx,gpus=nums) 函数来指定使用多少GPU; 非常感谢

    15:09:15

    Tips:

    1. 如何实时的查看GPU 的变化?实时的查看GPU可以使用watch, 相关的命令解释和使用方式可以使用whatis watch 和 man watch 查看

    whatis watch
    watch (1)            - execute a program periodically, showing output fullscreen
    
    man watch 
    WATCH(1)                                   User Commands                                   WATCH(1)
    
    NAME
           watch - execute a program periodically, showing output fullscreen
    
    SYNOPSIS
           watch [options] command
    
    DESCRIPTION
           watch  runs  command  repeatedly,  displaying  its output and errors (the first screenfull).
           This allows you to watch the program output change over time.  By default,  the  program  is
           run every 2 seconds.  By default, watch will run until interrupted.
    
    OPTIONS
    ....
    
           -n, --interval seconds
                  Specify  update  interval.  The command will not allow quicker than 0.1 second inter‐
                  val, in which the smaller values are converted.
    
    ....

    通过man 可以知道watch -n 可以指定时间 ,因此可以使用 

    watch -n 3 nvidia-smi

    同时也可以使用nvidia-smi -l

    也能达到相同的效果

  • 相关阅读:
    【网络】【操作系统】select、poll、epoll
    【JMM】java内存模型及volatile关键字底层
    【数据库】连接查询(from 内连接 外连接)
    【数据库】SQL牛客练习关键点复习
    【SpringMVC】文件/图片 的下载与上传
    【SpringMVC】拦截器实现与网页跳转步骤
    什么是hashMap,初始长度,高并发死锁,java8 hashMap做的性能提升
    自己写一个HashMap
    String去除重复字符两个方法
    Solr与Elasticsearch比较
  • 原文地址:https://www.cnblogs.com/greentomlee/p/9379139.html
Copyright © 2011-2022 走看看