caffe icon indicating copy to clipboard operation
caffe copied to clipboard

Out of memory even when batchsize is 1

Open liuyudut opened this issue 11 years ago • 6 comments

Hi, I am trying to finetune the VGG16 net on VOC dataset. But even I set the batchsize=1, it stopped due to out of memory. I use the Titan 6GB card, and I think it should be enough for this task. I run the nvidia-smi, the GPU memory is really used up quickly. Do anyone know how to solve this problem? Thanks in advance!

liuyudut avatar Feb 27 '15 16:02 liuyudut

I am facing the memory usage problem too (but it's not a bug), so I will share my experience.

I calculated the memory usage of Caffe with VGG16-net (num of output of my task is 3312).

Needed memory for parameters (weights) in MB, (...) indicating the shape of a parameter:

conv1_1 (64, 3, 3, 3) (1, 1, 1, 64) 0.0068359375
conv1_2 (64, 64, 3, 3) (1, 1, 1, 64) 0.140869140625
conv2_1 (128, 64, 3, 3) (1, 1, 1, 128) 0.28173828125
conv2_2 (128, 128, 3, 3) (1, 1, 1, 128) 0.56298828125
conv3_1 (256, 128, 3, 3) (1, 1, 1, 256) 1.1259765625
conv3_2 (256, 256, 3, 3) (1, 1, 1, 256) 2.2509765625
conv3_3 (256, 256, 3, 3) (1, 1, 1, 256) 2.2509765625
conv4_1 (512, 256, 3, 3) (1, 1, 1, 512) 4.501953125
conv4_2 (512, 512, 3, 3) (1, 1, 1, 512) 9.001953125
conv4_3 (512, 512, 3, 3) (1, 1, 1, 512) 9.001953125
conv5_1 (512, 512, 3, 3) (1, 1, 1, 512) 9.001953125
conv5_2 (512, 512, 3, 3) (1, 1, 1, 512) 9.001953125
conv5_3 (512, 512, 3, 3) (1, 1, 1, 512) 9.001953125
fc6 (1, 1, 4096, 25088) (1, 1, 1, 4096) 392.015625
fc7 (1, 1, 4096, 4096) (1, 1, 1, 4096) 64.015625
fc-anp (1, 1, 3312, 4096) (1, 1, 1, 3312) 51.7626342773
total 563.925964355

Needed memory for blobs (intermediate results) in MB:


data (1, 3, 224, 224) 0.57421875
label (1, 1, 1, 1) 3.81469726562e-06
conv1_1 (1, 64, 224, 224) 12.25
conv1_2 (1, 64, 224, 224) 12.25
conv1-pool (1, 64, 112, 112) 3.0625
conv2_1 (1, 128, 112, 112) 6.125
conv2_2 (1, 128, 112, 112) 6.125
conv2-pool (1, 128, 56, 56) 1.53125
conv3_1 (1, 256, 56, 56) 3.0625
conv3_2 (1, 256, 56, 56) 3.0625
conv3_3 (1, 256, 56, 56) 3.0625
conv3-pool (1, 256, 28, 28) 0.765625
conv4_1 (1, 512, 28, 28) 1.53125
conv4_2 (1, 512, 28, 28) 1.53125
conv4_3 (1, 512, 28, 28) 1.53125
conv4-pool (1, 512, 14, 14) 0.3828125
conv5_1 (1, 512, 14, 14) 0.3828125
conv5_2 (1, 512, 14, 14) 0.3828125
conv5_3 (1, 512, 14, 14) 0.3828125
conv5-pool (1, 512, 7, 7) 0.095703125
fc6 (1, 4096, 1, 1) 0.015625
fc7 (1, 4096, 1, 1) 0.015625
fc-anp (1, 3312, 1, 1) 0.0126342773438
(automatic) (1, 1, 1, 1) 3.81469726562e-06
total 58.1356887817

In training stage, we also need additional memory for diff of Backprop for both parameters and blobs. Further, we need history buffer for Stochastic Gradient Descent with momentum. Consequently, We will need (param_memory + blob_memory) * 2 + param_memory. In addition, if you set the test_iter in your SolverParameter, it will instantiate another net sharing parameter's memory with the train net but not sharing blobs. Finally, we will use (param_memory + blob_memory) * 2 + param_memory + blob_memory = (563.925964355 + 58.1356887817) * 3 = 1866.18495941. Actually, some layers have their buffer region but they shouldn't be so large. I think it would be a couple of hundred MB. I remember when I saw nvidia-smi in this setting, It was around 2500MB. I believe Titan 6GB is large enough to run this solver. Are you working with current master branch? Is input size 224? Are you running from /build/tools/caffe?

According to the policy, I think you should post this kind of usage issue to caffe-users (https://groups.google.com/forum/#!forum/caffe-users).

tnarihi avatar Feb 27 '15 17:02 tnarihi

@shelhamer @longjon @jeffdonahue Is my calculation right?

tnarihi avatar Feb 27 '15 17:02 tnarihi

I am assuming Dtype=float above.

tnarihi avatar Feb 27 '15 18:02 tnarihi

thanks @tnarihi

I will try what you say!

liuyudut avatar Mar 02 '15 08:03 liuyudut

Hi @Liuy8, did you trained the VGG16 net with your 6GB card?. I am training the same model with a 4 GB card, but it works just for batch sizes <= 10 :( !

jmendozais avatar Apr 09 '15 02:04 jmendozais

@Liuy8 可以聊一下吗,我现在算这个也没有算对

YIFanH avatar Aug 29 '17 12:08 YIFanH