Skip to main content
 首页 » 编程设计

python之如何只使用一个 GPU 进行 tensorflow session

2024年09月03日26mq0036

我有两个 GPU。 我的程序使用 TensorRT 和 Tensorflow。

当我只运行 TensorRT 部分时,没问题。 当我与 Tensorflow 部分一起运行时,出现错误

[TensorRT] ERROR: engine.cpp (370) - Cuda Error in ~ExecutionContext: 77 (an illegal memory access was encountered) 
terminate called after throwing an instance of 'nvinfer1::CudaError' 
  what():  std::exception 

问题是 Tensorflow session 何时开始如下

self.graph = tf.get_default_graph() 
self.persistent_sess = tf.Session(graph=self.graph, config=tf_config) 

它加载两个 GPU 作为

2019-06-06 14:15:04.420265: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 6965 MB memory) -> physical GPU (device: 0, name: Quadro P4000, pci bus id: 0000:04:00.0, compute capability: 6.1) 
2019-06-06 14:15:04.420713: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:1 with 7159 MB memory) -> physical GPU (device: 1, name: Quadro P4000, pci bus id: 0000:05:00.0, compute capability: 6.1) 

我试图只加载一个 GPU 作为

(1)放在python代码之上

import os 
os.environ["CUDA_VISIBLE_DEVICES"]="0" 

(2)

with tf.device('/device:GPU:0'): 
    self.graph = tf.get_default_graph() 
    self.persistent_sess = tf.Session(graph=self.graph, config=tf_config) 

两者都不行。

如何解决问题?

请您参考如下方法:

我可以设法只加载一个 GPU 将以下行放在 python 代码的第一行。

import sys, os 
os.environ["CUDA_VISIBLE_DEVICES"]="0"