【问题标题】:Tensorflow C API Selecting GPUTensorFlow C API 选择 GPU
【发布时间】:2020-06-15 17:02:47
【问题描述】:

我正在使用 Tensorflow C API 来运行在 python 中保存/冻结的模型。我们曾经在 CPU 上运行这些模型,但最近切换到 GPU 以提高性能。为了与 C API 交互,我们使用了一个名为 CPPFlow (https://github.com/serizba/cppflow) 的包装库。我最近更新了这个库,以便我们可以传入 GPU 配置选项,以便我们可以控制 GPU 内存分配。但是,我们现在也有具有多个 GPU 的系统,这会导致一些问题。似乎我无法让 Tensorflow 使用与我们的软件相同的 GPU。

我使用与我们的软件具有相同 GPU ID 的 visible_device_list 参数。如果我将我们的软件设置为在设备 1 上运行,将 Tensorflow 设置为在设备 1 上运行,Tensorflow 将选择设备 2。如果我将我们的软件设置为使用设备 1,而 Tensorflow 使用设备 2,则这两个软件都使用相同的 GPU。

Tensorflow 如何订购 GPU 设备,我是否需要使用其他方法手动选择设备?我看到的每个地方都表明可以使用 GPU Config 选项来完成。

【问题讨论】:

    标签: c++ c tensorflow tensorflow-c++


    【解决方案1】:

    设置设备的一种方法是在 python 中获取十六进制字符串,然后在 C API 中使用该字符串:例如, 示例 1:

    gpu_options = tf.GPUOptions(allow_growth=True,visible_device_list='1')
    config = tf.ConfigProto(gpu_options=gpu_options)
    serialized = config.SerializeToString()
    print(list(map(hex, serialized)))
    

    示例 2:

    import tensorflow as tf
    config = tf.compat.v1.ConfigProto(device_count={"CPU":1}, inter_op_parallelism_threads=1,intra_op_parallelism_threads=1)
    ser = config.SerializeToString()
    list(map(hex,ser))
    Out[]: 
    ['0xa',
    '0x7',
    '0xa',
    '0x3',
    '0x43',
    '0x50',
    '0x55',
    '0x10',
    '0x1',
    '0x10',
    '0x1',
    '0x28',
    '0x1']
    

    在 C API 中将此字符串用作

    uint8_t config[13] = {0xa, 0x7, 0xa, ... , 0x28, 0x1};
    TF_SetConfig(opts, (void*)config, 13, status);
    

    更多详情:

    https://github.com/tensorflow/tensorflow/issues/29217
    https://github.com/cyberfire/tensorflow-mtcnn/issues/1
    https://github.com/tensorflow/tensorflow/issues/27114
    

    【讨论】:

      【解决方案2】:

      您可以在执行期间通过设置环境变量 CUDA_VISIBLE_DEVICES 来设置 Tensorflow GPU 顺序。更多详情可以查看here

      //Set TF to use GPU:1 and GPU:0 (in this order)     
      setenv( "CUDA_VISIBLE_DEVICES", "1,0", 1 );
      
      //Set TF to use only GPU:0 (in this order)     
      setenv( "CUDA_VISIBLE_DEVICES", "0", 1 );
      
      //Set TF to do not use GPUs     
      setenv( "CUDA_VISIBLE_DEVICES", "-1", 1 );
      

      【讨论】:

        猜你喜欢
        • 2016-12-24
        • 1970-01-01
        • 1970-01-01
        • 2019-09-24
        • 1970-01-01
        • 2020-01-14
        • 2017-07-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多