如何在tensorflow v2中启用CUDA统一内存

时间:2019-09-20 08:56:05

标签: python tensorflow tensorflow2.0

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;

3 个答案:

答案 0 :(得分:4)

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)

答案 1 :(得分:0)

如果某人希望在1.x中启用UVM,只需将#include <iostream> #include <cmath> using namespace std; void fillarr1 (double ar_x[], int n, double x_0, double h); void fillarr2 (double ar_y[], double a, double b, int n, int l); void Sin (double ar_y[], double sine_[], int l); int main () { int n, i; double a, b, h, x_0; cout << "Number of points \n"; cin >> n; int l=n+1; double *x = new double(n); //array_1 double *y = new double(l); //array_2 double *sine = new double(l); //array_3 cout << "Separation between points \n"; cin >> h; cout << "x(0) = "; cin >> x_0; fillarr1 (x, n, x_0, h); cout << "\na = "; cin >> a; cout << "\nb = "; cin >> b; fillarr2 (y, a, b, n, l); cout << "\nsin \n"; Sin(y, sine, l); cout << "\n\n\n"; for (i=0; i<n; i++) { cout << x[i] << "\t"; } cout << "\n\n\n"; for (i=0; i<l; i++) { cout << y[i] << "\t"; } cout << "\n\n\n"; for (i=0; i<l; i++) { cout << sine[i] << "\t"; } delete [] x; delete [] y; delete [] sine; return 0; } void fillarr1 (double ar_x[], int n, double x_0, double h) { int i; for (i=0; i<n; i++) { ar_x[i]=x_0+h*i; cout << ar_x[i] << "\t"; } } void fillarr2 (double ar_y[], double a, double b, int n, int l) { int i; double h_; h_=(b-a)/(n); for (i=0; i<l; i++) { ar_y[i]=a+h_*i; cout << ar_y[i] << "\t"; } } void Sin(double ar_y[], double sine_[], int l) { int i; for(i=0;i<l;i++) { sine_[i]=sin(ar_y[i]); cout << sine_[i] << "\t"; } } 设置为1以上(您想要的任何数字)即可。

per_process_gpu_memory_fraction不执行任何操作。

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

use_unified_memory

答案 2 :(得分:-1)

对于 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

希望这会有所帮助。