QBoard » Artificial Intelligence & ML » AI and ML - Conceptual » How to prevent tensorflow from allocating the totality of a GPU memory?

How to prevent tensorflow from allocating the totality of a GPU memory?

  • I work in an environment in which computational resources are shared, i.e., we have a few server machines equipped with a few Nvidia Titan X GPUs each.

    For small to moderate size models, the 12 GB of the Titan X is usually enough for 2–3 people to run training concurrently on the same GPU. If the models are small enough that a single model does not take full advantage of all the computational units of the GPU, this can actually result in a speedup compared with running one training process after the other. Even in cases where the concurrent access to the GPU does slow down the individual training time, it is still nice to have the flexibility of having multiple users simultaneously train on the GPU.

    The problem with TensorFlow is that, by default, it allocates the full amount of available GPU memory when it is launched. Even for a small two-layer neural network, I see that all 12 GB of the GPU memory is used up.

    Is there a way to make TensorFlow only allocate, say, 4 GB of GPU memory, if one knows that this is enough for a given model?

      August 3, 2020 5:11 PM IST
    0
  • For Tensorflow version 2.0 and 2.1 use the following snippet:
     import tensorflow as tf
     gpu_devices = tf.config.experimental.list_physical_devices('GPU')
     tf.config.experimental.set_memory_growth(gpu_devices[0], True)​

    For prior versions , following snippet used to work for me:

    import tensorflow as tf
    tf_config=tf.ConfigProto()
    tf_config.gpu_options.allow_growth=True
    sess = tf.Session(config=tf_config)
      August 3, 2020 5:15 PM IST
    0
  • You can set the fraction of GPU memory to be allocated when you construct a tf.Session by passing a tf.GPUOptions as part of the optional config argument:

    # Assume that you have 12GB of GPU memory and want to allocate ~4GB:
    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333)
    
    sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))

    The per_process_gpu_memory_fraction acts as a hard upper bound on the amount of GPU memory that will be used by the process on each GPU on the same machine. Currently, this fraction is applied uniformly to all of the GPUs on the same machine; there is no way to set this on a per-GPU basis.

     

     
      September 16, 2020 3:10 PM IST
    0
  • Here is an excerpt from the Book Deep Learning with TensorFlow

    In some cases it is desirable for the process to only allocate a subset of the available memory, or to only grow the memory usage as it is needed by the process. TensorFlow provides two configuration options on the session to control this. The first is the allow_growth option, which attempts to allocate only as much GPU memory based on runtime allocations, it starts out allocating very little memory, and as sessions get run and more GPU memory is needed, we extend the GPU memory region needed by the TensorFlow process.

    1) Allow growth: (more flexible)

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    session = tf.Session(config=config, ...)

    The second method is per_process_gpu_memory_fraction option, which determines the fraction of the overall amount of memory that each visible GPU should be allocated. Note: No release of memory needed, it can even worsen memory fragmentation when done.

    2) Allocate fixed memory:

    To only allocate 40%  of the total memory of each GPU by:

    config = tf.ConfigProto()
    config.gpu_options.per_process_gpu_memory_fraction = 0.4
    session = tf.Session(config=config, ...)

    Note: That's only useful though if you truly want to bind the amount of GPU memory available on the TensorFlow process

    This post was edited by Jainew Nanda at September 16, 2020 3:15 PM IST
      September 16, 2020 3:14 PM IST
    0
  • Updated for TensorFlow 2.0 Alpha and beyond

    From the 2.0 Alpha docs, the answer is now just one line before you do anything with TensorFlow:

    import tensorflow as tf
    tf.config.gpu.set_per_process_memory_growth(True)
      September 16, 2020 3:17 PM IST
    0
  • config = tf.ConfigProto()
    config.gpu_options.allow_growth=True
    sess = tf.Session(config=config)

    https://github.com/tensorflow/tensorflow/issues/1578

      September 16, 2020 3:28 PM IST
    0
    • Rakesh Racharla
      Rakesh Racharla This one is exactly what I want because in a multi-user environment, it is very inconvenient to specify the exact amount of GPU memory to reserve in the code itself.
      September 16, 2020
    • Pranav B
      Pranav B Also, if you're using Keras with a TF backend, you can use this and run from keras import backend as K and K.set_session(sess) to avoid memory limitations
      September 16, 2020