I am reading this performance guide on the best practices for optimizing TensorFlow code for GPU. One suggestion they have is to place the preprocessing operations on the CPU so that the GPU is dedicated for training. To try to understand how one would actually implement this within an experiment (ie. learn_runner.run()). To further the discussion, I'd like to consider the best way to apply this strategy to the Custom Estimator Census Sample provided here.
The article suggests placing with tf.device('/cpu:0')
around the preprocessing operations. However, when I look at the custom estimator the 'preprocessing' appears to be done in multiple steps:
- Line 152/153
inputs = tf.feature_column.input_layer(features, transformed_columns) & label_values = tf.constant(LABELS)
-- if I wrappedwith tf.device('/cpu:0')
around these two lines would that be sufficient to cover the 'preprocessing' in this example? - Line 282/294 - There is also a
generate_input_fn
andparse_csv
function that are used to set up input data queues. Would it be necessary to placewith tf.device('/cpu:0')
within these functions as well or would that basically be forced by having the inputs & label_values already wrapped?
Main Question: Which of the above implementation suggestions is sufficient to properly place all preprocessing on the CPU?
Some additional questions that aren't addressed in the post:
- What if the machine has multiple cores? Would 'cpu:0' be limiting?
- The post implies to me that by wrapping the preprocessing on the cpu, the GPU would be automatically used for the rest. Is that actually the case?
Distributed ML Engine Experiment As a follow up, I would like to understand how this can be further adapted in a distributed ML engine experiment - would any of the recommendations above need to change if there were say 2 worker GPUs, 1 master CPU and a parameter server? My understanding is that the distributed training would be data-parallel asynchronous training so that each worker will be independently iterating through the data (and passing gradients asynchronously back to the PS) which suggests to me that no further modifications from the single GPU above would be needed if you train in this way. However, this seems a bit to easy to be true.