Multi-GPU performance regression with #44
Hello,
First, congrats on the release of 0.8.0 and especially for the distributed code! However, I noticed a performance regression when training inception v3 on a multi-GPU system (NVIDIA DevBox: 4x Titan X, driver 361.48). I was able to pinpoint the performance regression at 84b58a600638d89ce71aecbf1d9234b46ce2de14:
2016-04-14 20:46:34.577596: step 0, loss = 13.08 (2.0 examples/sec; 127.239 sec/batch)
2016-04-14 20:47:44.064244: step 10, loss = 13.16 (68.1 examples/sec; 3.762 sec/batch)
2016-04-14 20:48:21.515250: step 20, loss = 13.25 (68.9 examples/sec; 3.718 sec/batch)
2016-04-14 20:48:59.089292: step 30, loss = 13.27 (68.7 examples/sec; 3.728 sec/batch)
2016-04-14 20:49:36.558736: step 40, loss = 13.38 (68.2 examples/sec; 3.753 sec/batch)
2016-04-14 20:50:13.988347: step 50, loss = 13.29 (68.9 examples/sec; 3.718 sec/batch)
2016-04-14 20:50:51.547393: step 60, loss = 13.35 (67.5 examples/sec; 3.795 sec/batch)
2016-04-14 20:51:29.056161: step 70, loss = 13.30 (68.1 examples/sec; 3.761 sec/batch)
2016-04-14 20:52:06.558016: step 80, loss = 13.32 (68.4 examples/sec; 3.744 sec/batch)
2016-04-14 20:52:44.036974: step 90, loss = 12.97 (68.4 examples/sec; 3.745 sec/batch)
2016-04-14 20:53:21.528334: step 100, loss = 13.19 (68.3 examples/sec; 3.746 sec/batch)
With the commit just before, 9a1dfdf263b358a1560b8f7ef6c76595b71ca201:
2016-04-14 21:19:02.731016: step 0, loss = 13.12 (2.1 examples/sec; 122.740 sec/batch)
2016-04-14 21:19:54.731369: step 10, loss = 13.73 (116.1 examples/sec; 2.204 sec/batch)
2016-04-14 21:20:16.844538: step 20, loss = 13.59 (116.4 examples/sec; 2.200 sec/batch)
2016-04-14 21:20:38.962047: step 30, loss = 13.98 (116.0 examples/sec; 2.207 sec/batch)
2016-04-14 21:21:01.137735: step 40, loss = 14.15 (114.9 examples/sec; 2.228 sec/batch)
2016-04-14 21:21:23.400314: step 50, loss = 13.68 (114.6 examples/sec; 2.235 sec/batch)
2016-04-14 21:21:45.651567: step 60, loss = 13.66 (115.9 examples/sec; 2.209 sec/batch)
2016-04-14 21:22:07.986970: step 70, loss = 13.35 (115.0 examples/sec; 2.226 sec/batch)
2016-04-14 21:22:30.290880: step 80, loss = 13.14 (115.1 examples/sec; 2.225 sec/batch)
2016-04-14 21:22:52.503986: step 90, loss = 13.15 (116.1 examples/sec; 2.205 sec/batch)
2016-04-14 21:23:14.733461: step 100, loss = 13.11 (114.9 examples/sec; 2.227 sec/batch)
I also tried 5d7612c63c6b8585a4cbd87093294e747f3f0b08 with --num_readers=16, with no improvement.
I'm running the inception training inside a Docker container using nvidia-docker (I'm one of the maintainer).
FROM gcr.io/tensorflow/tensorflow:0.8.0rc0-devel-gpu
WORKDIR /models
RUN git clone https://github.com/tensorflow/models.git . && \
git checkout 9a1dfdf263b358a1560b8f7ef6c76595b71ca201
WORKDIR /models/inception
RUN bazel build -c opt --config=cuda inception/imagenet_train
CMD bazel-bin/inception/imagenet_train --num_gpus=4 --batch_size=256 \
--max_steps=110 --log_device_placement=true --num_epochs_per_decay=2.0 --learning_rate_decay_factor=0.94 \
--train_dir=/data/imagenet --data_dir=/raid/tensorflow_imagenet
With --log_device_placement=true the full log is huge, but I can share it if you want.
ping @jmchen-g, author of PR #44
Let me know how I can help!
Could you send me the log please? Thanks.
Also just to confirm, can you revert the change of the two files: inception_model and inception_train in PR #44 and see if the regression is gone?
I just sent you the log. Regarding reverting the two files, I tried with commit 9a1dfdf, before #44. Shouldn't that be enough? I can still try.
How many GPU's are you using?
In the mean time, would you try increasing the number of preprocessing threads via --num_preprocess_threads? As a starting point, I would try doubling this number and see if your speed gets better. If the speed does improves, try increasing this FLAG in proportion to the number of GPU's.
@shlens: 4 Titan X
I used --num_preprocess_threads=8 (default seems to be 4), same problem:
2016-04-18 17:35:11.673950: step 0, loss = 13.10 (1.8 examples/sec; 139.853 sec/batch)
2016-04-18 17:36:22.821083: step 10, loss = 13.42 (64.1 examples/sec; 3.993 sec/batch)
2016-04-18 17:37:02.507205: step 20, loss = 13.46 (64.5 examples/sec; 3.968 sec/batch)
2016-04-18 17:37:42.534497: step 30, loss = 13.67 (63.6 examples/sec; 4.026 sec/batch)
2016-04-18 17:38:22.399331: step 40, loss = 13.19 (64.3 examples/sec; 3.984 sec/batch)
2016-04-18 17:39:02.577790: step 50, loss = 13.10 (64.7 examples/sec; 3.954 sec/batch)
2016-04-18 17:39:42.775319: step 60, loss = 13.04 (62.8 examples/sec; 4.079 sec/batch)
2016-04-18 17:40:22.614517: step 70, loss = 13.07 (64.3 examples/sec; 3.981 sec/batch)
2016-04-18 17:41:02.639924: step 80, loss = 13.07 (64.7 examples/sec; 3.954 sec/batch)
2016-04-18 17:41:42.810802: step 90, loss = 13.03 (64.2 examples/sec; 3.988 sec/batch)
2016-04-18 17:42:22.819867: step 100, loss = 13.04 (64.0 examples/sec; 4.001 sec/batch)
Did #44 modify this parameter?
@flx42 Thanks for the quick test. I would like to dig into this more. Would you repeat this experiment but perform the following modifications?
- Change the number of visible GPU's to 2 via CUDA_VISIBLE_DEVICES and setting --num_gpus=2 in your binary for TensorFlow.
- Run two experiments with 4 preprocess threads and 8 preprocessing threads.
- Set --input_queue_memory_factor=0. This removes the prefetching queue so you can run your experiments quickly.
- Let the system run for at least >600 steps. There is a significant burn-in time required before you can get a reliable measurement.
- Keep an eye on "top" or "ps" to check that you are now overloading your CPU cores in the preprocessing.
Note the reason for the latter comments and requests is that you are seeing increases in the step time after increasing the number of processing threads (e.g. 3.7 sec for 4 threads to ~3.9 sec for 8 threads). This is the opposite behavior as I would expect that the step time would be in anything faster.
It looks like --num_preprocess_threads=8 means 8 threads per GPU:
https://github.com/tensorflow/models/blob/84b58a600638d89ce71aecbf1d9234b46ce2de14/inception/inception/inception_train.py#L213
If that's the case, do you still want to test with 4 and 8 per GPU then?
There are two changes involved here. Could you try revert inception_model.py and then also change:
-
with slim.arg_scope([slim.variables.variable], device='/cpu:0'):-
# Calculate the loss for one tower of the ImageNet model. This -
# function constructs the entire ImageNet model but shares the -
# variables across all towers. -
loss = _tower_loss(images_batch, labels_batch, num_classes, scope)
-
back to:
-
loss = _tower_loss(images, labels, num_classes, scope)
This reverts one of the two so it should isolate the root cause. Thanks.
@jmchen-g I will try, thanks.
Just to confirm, should it be:
loss = _tower_loss(images, labels, num_classes, scope)
Or:
loss = _tower_loss(images_batch, labels_batch, num_classes, scope)
?
Yap, The latter: loss = _tower_loss(images_batch, labels_batch, num_classes, scope)
If the performance regression is fixed with this change, then we need to check the placement. Thanks.
@jmchen-g: I tried the changes you suggested, it didn't work. However, I was able to fix the performance regression by reverting the batch slicing to the previous code:
@@ -213,6 +213,7 @@
num_preprocess_threads = FLAGS.num_preprocess_threads * FLAGS.num_gpus
images, labels = image_processing.distorted_inputs(
dataset,
+ batch_size=split_batch_size,
num_preprocess_threads=num_preprocess_threads)
input_summaries = copy.copy(tf.get_collection(tf.GraphKeys.SUMMARIES))
@@ -226,22 +227,13 @@
for i in xrange(FLAGS.num_gpus):
with tf.device('/gpu:%d' % i):
with tf.name_scope('%s_%d' % (inception.TOWER_NAME, i)) as scope:
- # Split the batch of images and labels.
- batch_start = split_batch_size * i
- images_batch = tf.slice(images,
- begin=[batch_start, 0, 0, 0],
- size=[split_batch_size, -1, -1, -1])
- labels_batch = tf.slice(labels,
- begin=[batch_start],
- size=[split_batch_size])
-
# Force all Variables to reside on the CPU.
with slim.arg_scope([slim.variables.variable], device='/cpu:0'):
# Calculate the loss for one tower of the ImageNet model. This
# function constructs the entire ImageNet model but shares the
# variables across all towers.
- loss = _tower_loss(images_batch, labels_batch, num_classes, scope)
+ loss = _tower_loss(images, labels, num_classes, scope)
# Reuse variables for the next tower.
tf.get_variable_scope().reuse_variables()
I'm not sure why, or if it makes any sense at all.
Good. This is what I wanted. My suggestion above is to check which change resulted in the regression.
In this case, it means the CPU/IO cannot catch up with the speed of 4 GPUs. Unfortunately there is no easy fix for this since your CPU or IO is running at its peak already... One thing to try is to get another host and distribute 2 GPUs there.
My suggestion above is to check which change resulted in the regression.
I'm not sure what you mean here, reverting inception_model.py or the device placement, as suggested in https://github.com/tensorflow/models/issues/47#issuecomment-211529348 did not fix the problem. Only the patch above did.
As mentioned above, #44 has 2 changes related to regression. The suggestion I gave is to revert one of them so we know which change is the root cause. Since my suggestion didn't fix it, the other change should be the root cause as you tried with your patch.
However, the "root cause" change is necessary because it was a fix to a bug which resulted in the same input data passed into all four GPUs so we cannot really revert that. Since the regression is due to the fact that now we have to generate 4x input data to 4 GPUs, it seems to suggest that the system is now bottlenecked by CPU or data transfer somewhere.
We are also looking into this issue and will reply back if we have updates. Please let me know if it is still not clear. Thanks.
@flx42 Thanks for all your help, could you try this patch #51 and see if that fixes the problem, otherwise we will need to analyze the performance regression with more detail. We are planning in creating some benchmark utils to help with this.
@jmchen-g it's perfectly clear now, thank you. I didn't realize there was a bug with the previous code. I admit I was a bit puzzled when looking at the diff, since there used to be no offset when slicing the batch, but I assumed it worked anyway somehow.
@sguada I will test right away, thanks a lot.
I've been running TF in the background a lot recently :)
First, I tried with --num_preprocess_threads=4 (which is the default value apparently), at multiple commits.
9a1dfdf263b358a1560b8f7ef6c76595b71ca201 (bogus batch split):
step 0, loss = 13.05 (2.1 examples/sec; 122.488 sec/batch)
step 10, loss = 14.93 (114.1 examples/sec; 2.244 sec/batch)
step 20, loss = 13.24 (113.1 examples/sec; 2.264 sec/batch)
step 30, loss = 13.25 (113.7 examples/sec; 2.252 sec/batch)
step 40, loss = 13.22 (114.6 examples/sec; 2.234 sec/batch)
step 50, loss = 13.21 (113.2 examples/sec; 2.262 sec/batch)
step 60, loss = 13.19 (113.4 examples/sec; 2.257 sec/batch)
step 70, loss = 13.20 (112.4 examples/sec; 2.277 sec/batch)
step 80, loss = 13.22 (113.0 examples/sec; 2.265 sec/batch)
step 90, loss = 13.18 (113.0 examples/sec; 2.266 sec/batch)
step 100, loss = 13.15 (113.8 examples/sec; 2.250 sec/batch)
84b58a600638d89ce71aecbf1d9234b46ce2de14:
step 0, loss = 13.12 (2.0 examples/sec; 128.300 sec/batch)
step 10, loss = 13.54 (67.7 examples/sec; 3.783 sec/batch)
step 20, loss = 14.47 (68.2 examples/sec; 3.753 sec/batch)
step 30, loss = 13.86 (68.1 examples/sec; 3.760 sec/batch)
step 40, loss = 13.32 (67.3 examples/sec; 3.804 sec/batch)
step 50, loss = 13.05 (66.8 examples/sec; 3.833 sec/batch)
step 60, loss = 13.09 (67.9 examples/sec; 3.769 sec/batch)
step 70, loss = 13.08 (68.3 examples/sec; 3.746 sec/batch)
step 80, loss = 13.07 (67.5 examples/sec; 3.795 sec/batch)
step 90, loss = 13.08 (67.3 examples/sec; 3.806 sec/batch)
step 100, loss = 13.07 (68.1 examples/sec; 3.759 sec/batch)
5d7612c63c6b8585a4cbd87093294e747f3f0b08 and branch sguada-patch-1 are pretty much the same.
step 0, loss = 13.10 (12.2 examples/sec; 20.970 sec/batch)
step 10, loss = 13.68 (67.3 examples/sec; 3.803 sec/batch)
step 20, loss = 14.09 (67.4 examples/sec; 3.796 sec/batch)
step 30, loss = 13.79 (67.8 examples/sec; 3.774 sec/batch)
step 40, loss = 13.18 (67.2 examples/sec; 3.812 sec/batch)
step 50, loss = 13.08 (68.1 examples/sec; 3.758 sec/batch)
step 60, loss = 13.19 (66.2 examples/sec; 3.866 sec/batch)
step 70, loss = 13.07 (67.0 examples/sec; 3.821 sec/batch)
step 80, loss = 13.06 (67.1 examples/sec; 3.816 sec/batch)
step 90, loss = 13.07 (66.9 examples/sec; 3.828 sec/batch)
step 100, loss = 13.04 (67.0 examples/sec; 3.820 sec/batch)
But after seeing the huge amount of threads launched by TF, I switched to --num_preprocess_threads=2 instead of the default
--num_preprocess_threads=4, since I noticed above that this parameter is scaled by the number of GPUs.
9a1dfdf263b358a1560b8f7ef6c76595b71ca201 (bogus batch split):
step 0, loss = 13.08 (2.0 examples/sec; 125.903 sec/batch)
step 10, loss = 15.69 (132.6 examples/sec; 1.930 sec/batch)
step 20, loss = 13.95 (132.2 examples/sec; 1.937 sec/batch)
step 30, loss = 13.28 (134.0 examples/sec; 1.910 sec/batch)
step 40, loss = 13.21 (131.9 examples/sec; 1.941 sec/batch)
step 50, loss = 13.25 (132.4 examples/sec; 1.933 sec/batch)
step 60, loss = 13.21 (133.7 examples/sec; 1.915 sec/batch)
step 70, loss = 13.17 (130.4 examples/sec; 1.963 sec/batch)
step 80, loss = 13.18 (133.9 examples/sec; 1.911 sec/batch)
step 90, loss = 13.18 (133.1 examples/sec; 1.924 sec/batch)
step 100, loss = 13.16 (132.9 examples/sec; 1.926 sec/batch)
84b58a600638d89ce71aecbf1d9234b46ce2de14:
step 0, loss = 13.05 (2.0 examples/sec; 130.557 sec/batch)
step 10, loss = 13.69 (85.7 examples/sec; 2.986 sec/batch)
step 20, loss = 13.77 (84.8 examples/sec; 3.018 sec/batch)
step 30, loss = 13.39 (84.6 examples/sec; 3.028 sec/batch)
step 40, loss = 13.24 (88.6 examples/sec; 2.890 sec/batch)
step 50, loss = 13.09 (86.4 examples/sec; 2.964 sec/batch)
step 60, loss = 13.07 (88.1 examples/sec; 2.907 sec/batch)
step 70, loss = 13.07 (88.5 examples/sec; 2.893 sec/batch)
step 80, loss = 13.06 (88.7 examples/sec; 2.885 sec/batch)
step 90, loss = 13.04 (87.7 examples/sec; 2.921 sec/batch)
step 100, loss = 13.03 (88.8 examples/sec; 2.883 sec/batch)
5d7612c63c6b8585a4cbd87093294e747f3f0b08 and branch sguada-patch-1 are still pretty much the same, but the first few iterations are faster than 84b58a600638d89ce71aecbf1d9234b46ce2de14:
step 0, loss = 13.11 (20.2 examples/sec; 12.667 sec/batch)
step 10, loss = 13.59 (128.6 examples/sec; 1.990 sec/batch)
step 20, loss = 13.81 (128.2 examples/sec; 1.998 sec/batch)
step 30, loss = 13.57 (126.5 examples/sec; 2.024 sec/batch)
step 40, loss = 13.14 (88.1 examples/sec; 2.905 sec/batch)
step 50, loss = 13.07 (88.2 examples/sec; 2.903 sec/batch)
step 60, loss = 13.14 (86.8 examples/sec; 2.949 sec/batch)
step 70, loss = 13.05 (86.2 examples/sec; 2.972 sec/batch)
step 80, loss = 13.03 (88.7 examples/sec; 2.885 sec/batch)
step 90, loss = 13.03 (89.4 examples/sec; 2.863 sec/batch)
step 100, loss = 13.03 (87.8 examples/sec; 2.916 sec/batch)
Earlier in this thread I mentioned than I saw no difference between 84b58a600638d89ce71aecbf1d9234b46ce2de14 and 5d7612c63c6b8585a4cbd87093294e747f3f0b08. But apparently this is not true if you use less threads for preprocessing, and only for the first few iterations. This is because of the improvement from 5d7612c63c6b8585a4cbd87093294e747f3f0b08 Any idea why it's only for the first iterations?
@sguada your patch seems to make sense but I didn't notice an improvement on my machine. The problem is that I don't know what the expected performance should be if the previous version of the code was not slicing the batch correctly.
Maybe we should try on a machine with a faster CPU to have a better understanding of the impact of each commit.
At 9a1dfdf it was only reading and processing a batch of 32 images per step and then each GPU process them in parallel (but they are just processing the same images) so that gives us an upper bound of how fast the GPUs could process examples.
At 84b58a6 the bug was fixed, so now a batch of 4*32=128 images per step need to be processed (more CPU work) before they were sliced so each GPU only process 32 in parallel. It seems that your machine cannot handle 16 threads working in parallel to pre-processing the images.
At 5d7612c plus sguada-patch-1 is trying to reduce the synchronization issues between GPU and CPU, so it is faster but at some point the CPUs cannot process images fast enough ~90 images per second and then GPUs need to wait. Have you tried --num_preprocess_threads=1 it may be better, so the threads are not competing for the cores. A bigger queue capacity=10 * batch_size here https://github.com/tensorflow/models/blob/master/inception/inception/image_processing.py#L497 could help a bit.
It seems that your machine is CPU bounded and cannot read and process examples fast enough to keep the 4 GPUs busy. We will try to provide a benchmark utility to measure the different modules involved, data reading and shuffling, data pre-processing, and GPU processing.
I will also try to make a different queue for each GPU and see if that can help.
@sguada: thanks for the detailed explanation. My batch size is actually 256, so 64 images per GPU.
Using --num_preprocess_threads=1 yields worse performance consistently, but I still have 65 threads:
$ cat /proc/27423/status | grep Threads
Threads: 65
Modifying the size of the queue didn't help.
FYI, I have a i7-5930K, one of the fastest i7 processor from the Haswell E family. HT is enabled (which should help in a mixed IO/compute workload such as preprocessing). The disk is SSD.
Yes, I have the same problem. I tried both inception_train.py and inception_distributed_train.py to train, both --batch-size=32. the incption_train.py can reach ~0.6 sec/batch, very satisfying, while inception_distributed_train.py only ~1.2 sec/batch not good enough. Can anybody give some instruction ?
According to the discussion above, I made this commit to inception_distributed_train.py trying to make the split of batch and other operations out of GPU, but the error occurs:
INFO:tensorflow:2016-05-13 15:17:16.147372 Supervisor
INFO:tensorflow:Waiting for model to be ready: Variables not initialized: global_step, local_steps