web-dev-qa-db-ja.com

GPUでTensorflowを使用する際のエラー

私はさまざまなTensorflowの例を試しましたが、これはCPUでは問題なく動作しますが、GPUで実行しようとすると同じエラーが発生します。 1つの小さな例は次のとおりです。

import tensorflow as tf

# Creates a graph.
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
c = tf.matmul(a, b)
# Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# Runs the op.
print sess.run(c)

エラーは常に同じです、CUDA_ERROR_OUT_OF_MEMORY:

I tensorflow/stream_executor/dso_loader.cc:101] successfully opened CUDA library libcublas.so.7.0 locally
I tensorflow/stream_executor/dso_loader.cc:101] successfully opened CUDA library libcudnn.so.6.5 locally
I tensorflow/stream_executor/dso_loader.cc:101] successfully opened CUDA library libcufft.so.7.0 locally
I tensorflow/stream_executor/dso_loader.cc:101] successfully opened CUDA library libcuda.so locally
I tensorflow/stream_executor/dso_loader.cc:101] successfully opened CUDA library libcurand.so.7.0 locally
I tensorflow/core/common_runtime/local_device.cc:40] Local device intra op parallelism threads: 24
I tensorflow/core/common_runtime/gpu/gpu_init.cc:103] Found device 0 with properties: 
name: Tesla K80
major: 3 minor: 7 memoryClockRate (GHz) 0.8235
pciBusID 0000:0a:00.0
Total memory: 11.25GiB
Free memory: 105.73MiB
I tensorflow/core/common_runtime/gpu/gpu_init.cc:103] Found device 1 with properties: 
name: Tesla K80
major: 3 minor: 7 memoryClockRate (GHz) 0.8235
pciBusID 0000:0b:00.0
Total memory: 11.25GiB
Free memory: 133.48MiB
I tensorflow/core/common_runtime/gpu/gpu_init.cc:127] DMA: 0 1 
I tensorflow/core/common_runtime/gpu/gpu_init.cc:137] 0:   Y Y 
I tensorflow/core/common_runtime/gpu/gpu_init.cc:137] 1:   Y Y 
I tensorflow/core/common_runtime/gpu/gpu_device.cc:702] Creating TensorFlow device (/gpu:0) -> (device: 0, name: Tesla K80, pci bus id: 0000:0a:00.0)
I tensorflow/core/common_runtime/gpu/gpu_device.cc:702] Creating TensorFlow device (/gpu:1) -> (device: 1, name: Tesla K80, pci bus id: 0000:0b:00.0)
I tensorflow/core/common_runtime/gpu/gpu_bfc_allocator.cc:42] Allocating 105.48MiB bytes.
E tensorflow/stream_executor/cuda/cuda_driver.cc:932] failed to allocate 105.48M (110608384 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY
F tensorflow/core/common_runtime/gpu/gpu_bfc_allocator.cc:47] Check failed: gpu_mem != nullptr  Could not allocate GPU device memory for device 0. Tried to allocate 105.48MiB
Aborted (core dumped)

問題は、この小さな例のメモリ使用量ではなく、私の設定に関係していると思います。誰かが何か考えを持っていますか?

編集:

この問題は、同じGPUでジョブを実行している他の人と同じくらい簡単である可能性があることがわかりました。その場合:時間をかけてごめんなさい...

15
user5654767

ここには2つの問題があるようです:

  1. デフォルトでは、TensorFlowは、tf.Sessionを作成すると、(各GPUデバイスで)利用可能なGPUメモリの大部分(95%)を割り当てます。 「システム」用に200 MBのGP​​Uメモリを予約する heuristic を使用しますが、空きメモリの量が それよりも小さい である場合、これを別に設定しません。

  2. どちらかのGPUデバイスに空きGPUメモリがほとんどないようです(105.73MiBおよび133.48MiB)。つまり、TensorFlowはおそらくシステム用に予約する必要があるメモリを割り当てようとするため、割り当ては失敗します。

このプログラムを実行しようとしているときに、別のTensorFlowプロセス(または他のGPUに飢えたコード)が実行されている可能性はありますか?たとえば、GPUを使用していない場合でも、Pythonオープンセッションのインタープリターは、GPUメモリのほぼ全体を割り当てようとします。

現在、TensorFlowが使用するGPUメモリの量を制限する唯一の方法は、次の構成オプションです( この質問 から):

# Assume that you have 12GB of GPU memory and want to allocate ~4GB:
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333)

sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
23
mrry

これは、TensorFlowセッションがGPUで十分な量のメモリを取得できないために発生する可能性があります。 TensorFlowなどの他のプロセス用の空きメモリが少ないか、システムで別のTensorFlowセッションが実行されている可能性があります。そのため、TensorFlowセッションが使用するメモリの量を構成する必要があります

TensorFlow 1.xを使用している場合

gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333)

sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))

tensorflow 2.xは1.xから大幅に変更されているため、TensorFlow 1.xバージョンのメソッド/関数を使用する場合、TensorFlow 2.xに互換性モジュールが保持されます。したがってTensorFlow 2.xユーザーはこのコードを使用できます

gpu_options = tf.compat.v1.GPUOptions(per_process_gpu_memory_fraction=0.333)

sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(gpu_options=gpu_options))
0
ujjal das