【问题标题】:How to enable cuda unified memory in tensorflow v2如何在 tensorflow v2 中启用 cuda 统一内存
【发布时间】:2020-01-21 07:37:40
【问题描述】:

tensorflow 1.x 中,有一个类似use_unified_memoryper_process_gpu_memory_fraction 的选项可能会触发使用的CUDA UVM。但是在tensorflow 2.0 中如何做到这一点呢?

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/protobuf/config.proto

// If true, uses CUDA unified memory for memory allocations. If
// per_process_gpu_memory_fraction option is greater than 1.0, then unified
// memory is used regardless of the value for this field. See comments for
// per_process_gpu_memory_fraction field for more details and requirements
// of the unified memory. This option is useful to oversubscribe memory if
// multiple processes are sharing a single GPU while individually using less
// than 1.0 per process memory fraction.
bool use_unified_memory = 2;

【问题讨论】:

    标签: python tensorflow tensorflow2.0


    【解决方案1】:
    from tensorflow.compat.v1 import ConfigProto
    from tensorflow.compat.v1 import InteractiveSession
    
    config = ConfigProto()
    config.gpu_options.per_process_gpu_memory_fraction = 2
    config.gpu_options.allow_growth = True
    session = InteractiveSession(config=config)
    

    【讨论】:

    • 为什么在 gpu_memory_fraction 中使用2?据我所知,应该小于1。
    • 所以除了 gpu 内存之外,您还可以使用系统内存。如果您的 gpu 有 2 GB 内存,它可以开始分配 4 GB(2 倍)或 6 GB(3 倍)等。但是这会使训练过程变慢。
    • 使用大于1的值会强制使用统一内存。
    • 我收到以下错误:tensorflow.python.framework.errors_impl.InternalError: Unified memory on GPUs with compute capability lower than 6.0 (pre-Pascal class GPUs) does not support oversubscription. 这是什么意思?
    • compute capability lower than 6.0 表示您使用的 GPU 早于 Pascal 微架构。请参阅此列表:developer.nvidia.com/cuda-gpus#compute
    【解决方案2】:

    如果有人希望在 1.x 中启用 UVM,只需将 per_process_gpu_memory_fraction 设置为 1(设置为您想要的任何数字)。

    use_unified_memory 什么都不做。

    TensorFlow 的另一个潜在错误:您可能希望在建立会话之后将模型定义移动到。喜欢

    with tf.Session(GPUOptions...) as s:
        model = xxx
        s.run(model)
    

    【讨论】:

      【解决方案3】:

      对于 TensorFlow 2.0,您可以使用 tf.config.experimental api。
      与 TF 1.X 类似,有两种方法可以限制 gpu 的使用,如下所示:

      (1) 允许 GPU 内存增长
      第一种选择是通过调用tf.config.experimental.set_memory_growth打开内存增长
      比如;

      gpus = tf.config.experimental.list_physical_devices('GPU')
      tf.config.experimental.set_memory_growth(gpus[0], True)
      

      https://www.tensorflow.org/versions/r2.0/api_docs/python/tf/config/experimental/set_memory_growth

      (2) 设置 GPU 内存增长的硬性限制
      第二种方法是使用tf.config.experimental.set_virtual_device_configuration 配置一个虚拟GPU 设备,并对要在GPU 上分配的总内存设置硬限制。
      比如;

      gpus = tf.config.experimental.list_physical_devices('GPU')
      tf.config.experimental.set_virtual_device_configuration(
                gpus[0],
                  [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=1024)])
      

      查看以下链接了解更多https://www.tensorflow.org/versions/r2.0/api_docs/python/tf/config/experimental/set_virtual_device_configuration

      希望这会有所帮助。

      【讨论】:

      • 感谢reqply,但这似乎没有回答我的问题,我在问如何启用nvidia统一内存以防gpu内存不足,您在这里提供的是关于如何限制内存使用情况。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-01
      • 2016-12-18
      • 1970-01-01
      • 2017-04-30
      • 2014-04-19
      • 2018-11-13
      相关资源
      最近更新 更多