【问题标题】:Why is TensorFlow using my GPU when the device is set to the CPU为什么 TensorFlow 在设备设置为 CPU 时使用我的 GPU
【发布时间】:2018-07-19 23:46:36
【问题描述】:

TensorFlow 正在分配我所有的 GPU 内存并忽略我的命令来使用 CPU,我该如何解决这个问题?

这是我的testprog的代码摘录

Session *session;
SessionOptions opts = SessionOptions();

//force to allocate 0 memory on gpu
opts.config.mutable_gpu_options()->set_per_process_gpu_memory_fraction(0);
opts.config.mutable_gpu_options()->set_allow_growth(false);

//create session with these settings
TF_CHECK_OK(NewSession(opts, &session));
TF_CHECK_OK(session->Create(graph_def));

//set device to cpu
graph::SetDefaultDevice("/cpu:0", &graph_def);

//run arbitrary model
Status status = session->Run(classifierInput, {output_layer},{},&outputs);

TF_CHECK_OK(session->Close());

打电话给nvidi-smi 告诉我:

+-----------------------------------------------------------------------------+
| NVIDIA-SMI 375.66                 Driver Version: 375.66                    |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  Quadro P4000        Off  | 0000:01:00.0     Off |                  N/A |
| N/A   50C    P0    28W /  N/A |   7756MiB /  8114MiB |     42%      Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID  Type  Process name                               Usage      |
|=============================================================================|
|    0      1784    G   /usr/bin/X                                     139MiB |
|    0      3828    G   qtcreator                                       28MiB |
|    0      7721    C   ...testprog/build/testprog                    7585MiB |
+-----------------------------------------------------------------------------+

为什么会这样?

【问题讨论】:

    标签: c++ tensorflow memory-management gpu cpu


    【解决方案1】:

    因为这个问题是用 C++ 标记的。解决办法是

    tensorflow::Session *sess;
    tensorflow::SessionOptions options;
    
    tensorflow::ConfigProto* config = &options.config;
    // disabled GPU entirely
    (*config->mutable_device_count())["GPU"] = 0;
    // place nodes somewhere
    config->set_allow_soft_placement(true);
    

    请参阅example here。 还有我的另一个帖子,how TensorFlow places the nodes

    编辑:有GitHub issue。你可以试试:

    #include <stdlib.h>
    setenv("CUDA_VISIBLE_DEVICES", "", 1);
    

    auto gpu_options = config->gpu_options();
    gpu_options.set_visible_device_list("");
    

    但这可能会给你failed call to cuInit: CUDA_ERROR_NO_DEVICE

    【讨论】:

    • 现在它使用 ~ 130 MB GPU 内存
    • 是的,Tensorflow 存在一个未解决的问题。查看我的编辑。
    【解决方案2】:

    当您将参数设置为 cpu:1 时,它不会阻止 tensorflow 初始化 GPU 设备。

    session_conf = tf.ConfigProto(
        device_count={'CPU' : 1, 'GPU' : 0},
        allow_soft_placement=True,
        log_device_placement=False
    )
    

    还有……最后的手段:

    alias nogpu='export CUDA_VISIBLE_DEVICES=-1;'

    nogpu python disable_GPU_tensorflow.py

    setenv("CUDA_VISIBLE_DEVICES", "", 1);

    【讨论】:

    • 能否请您重写 C++ API 的答案(见标签)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多