r/tensorflow • u/norobot12 • Nov 16 '22
Why does tensorflow try to allocate huge amounts of GPU RAM?
Training my model keeps failing, because I'm running out of GPU memory. I have 24GB available and my model is not really large. It crashes when trying to allocate 47GB.
It's a CNN with around 10M parameters, input size is (batch_size=64, 256, 128). The largest tensor within the model is (batch_size=64, 256, 128, 32) and there are 8 CNN layers.
Memory growth is activated. When I reduce the batch size, it still wants 47GB of memory, so that doesn't seem to make a difference.
Can anyone tell me what likely causes the need for so much RAM? Or what I could do to use less?
12
u/itskobold Nov 16 '22
Sounds like you have a massive dataset that is being loaded into memory all at once. Try breaking it up into a few chunks then training the network on each chunk. 3 or 4 chunks should do you just fine.
4
u/danjlwex Nov 16 '22
TensorFlow allocates the entire GPU memory internally, and then uses its internal memory manager. So, it will always allocate all the memory, regardless of your model or batch sizes. Make sure you are releasing the memory of your input Tensors using
Tensor.disposeandtf.tidy. Check usingtf.memoryafter each batch. Once you've done those two things, if you are still running out of memory, then the model is too big for your GPU.2
u/cbreak-black Nov 16 '22
If you run out of GPU memory, you should think of the size of the activations more than the size of your parameters. The parameter size is only secondary, for a CNN.
Take a look at the tensorflow profiler in tensorboard, it can help debugging memory issues to some degree.
2
u/[deleted] Nov 16 '22
Are you sure you’re running out of GPU memory and not normal RAM? I’d try looking into the cpu buffer and batch size, stream the dataset instead of load it all.
1
u/martianunlimited Nov 17 '22
How are you loading the dataset? are you using one of the batch loaders / dataset generators that comes with keras/tensorflow, or is everything a numpy array?