Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tensorflow store training data on GPU memory

I am pretty new to tensorflow. I used to use theano for deep learning development. I notice a difference between these two, that is where input data can be stored.

In Theano, it supports shared variable to store input data on GPU memory to reduce the data transfer between CPU and GPU.

In tensorflow, we need to feed data into placeholder, and the data can come from CPU memory or files.

My question is: is it possible to store input data on GPU memory for tensorflow? or does it already do it in some magic way?

Thanks.

like image 783
xyd Avatar asked Jun 02 '16 15:06

xyd


People also ask

How does TensorFlow allocate GPU memory?

By default, TensorFlow maps nearly all of the GPU memory of all GPUs (subject to CUDA_VISIBLE_DEVICES ) visible to the process. This is done to more efficiently use the relatively precious GPU memory resources on the devices by reducing memory fragmentation. To limit TensorFlow to a specific set of GPUs, use the tf.

Does TensorFlow automatically use GPU?

If a TensorFlow operation has both CPU and GPU implementations, TensorFlow will automatically place the operation to run on a GPU device first. If you have more than one GPU, the GPU with the lowest ID will be selected by default. However, TensorFlow does not place operations into multiple GPUs automatically.

Does TensorFlow use both GPUs?

Strategy is a TensorFlow API to distribute training across multiple GPUs, multiple machines, or TPUs. Using this API, you can distribute your existing models and training code with minimal code changes. tf.

Why does TensorFlow use so much memory?

(The REST service is still running but inference load is complete). TensorFlow processes by default will acquire the full memory of the GPU, even if it does not need it. If you build a small neural network, it will acquire the complete GPU memory. This might lead to inefficient GPU utilization if your work load is not heavy.

Can I run TensorFlow on a single GPU?

TensorFlow code, and models will transparently run on a single GPU with no code changes required. Use Note… By default, TensorFlow maps nearly all of the GPU memory of all GPUs (subject to CUDA_VISIBLE_DEVICES) visible to the process.

How much video RAM do I need for TensorFlow?

TensorFlow is interesting that it can store not only weights, but also training data in video RAM. Feeding training data and weights to GPU for matrix mul is faster than from regular RAM. However, I believe that video RAM required should be at least 1.5 times the above value just to be sure things would be working.

What is the training step time in TensorFlow?

The training step time is thus the sum of opening, reading and training times. The next sections build on this input pipeline, illustrating best practices for designing performant TensorFlow input pipelines. Prefetching overlaps the preprocessing and model execution of a training step.


1 Answers

If your data fits on the GPU, you can load it into a constant on GPU from e.g. a numpy array:

with tf.device('/gpu:0'):
  tensorflow_dataset = tf.constant(numpy_dataset)

One way to extract minibatches would be to slice that array at each step instead of feeding it using tf.slice:

  batch = tf.slice(tensorflow_dataset, [index, 0], [batch_size, -1])

There are many possible variations around that theme, including using queues to prefetch the data to GPU dynamically.

like image 111
Vincent Vanhoucke Avatar answered Oct 04 '22 15:10

Vincent Vanhoucke