From 8d6afb2eea0112c5748cf5a32751464da0dbc9ed Mon Sep 17 00:00:00 2001 From: Mark Shields Date: Mon, 16 Aug 2021 15:19:20 -0700 Subject: [PATCH 1/2] Make from_tensorflow.py more GPU memory friendly. Sphinx-gallery runs everything in a single process. There doesn't appear to be any easy way to force Tensorflow to return memory other than terminating the process. This at least gives us a little more wiggle room. --- tutorials/frontend/from_tensorflow.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tutorials/frontend/from_tensorflow.py b/tutorials/frontend/from_tensorflow.py index fc87c07fb569..eb8f21d4d049 100644 --- a/tutorials/frontend/from_tensorflow.py +++ b/tutorials/frontend/from_tensorflow.py @@ -36,6 +36,21 @@ # Tensorflow imports import tensorflow as tf + +# Ask tensorflow to limit it's GPU memory to what's actually needed +# instead of gobbling everything that's available. +# https://www.tensorflow.org/guide/gpu#limiting_gpu_memory_growth +# This way this tutorial is a little more friendly to sphinx-gallery. +gpus = tf.config.list_physical_devices("GPU") +if gpus: + try: + for gpu in gpus: + tf.config.experimental.set_memory_growth(gpu, True) + print("tensorflow will use experimental.set_memory_growth(True)") + except RuntimeError as e: + print("experimental.set_memory_growth option is not available: {}".format(e)) + + try: tf_compat_v1 = tf.compat.v1 except ImportError: From 97b7a10c2d228ce211f4397ec5cf358e318a1e23 Mon Sep 17 00:00:00 2001 From: Mark Shields Date: Mon, 16 Aug 2021 15:33:09 -0700 Subject: [PATCH 2/2] Also deploy_sparse.py. Should probably also be done to tensorflow.rst. --- tutorials/frontend/deploy_sparse.py | 14 ++++++++++++++ tutorials/frontend/from_tensorflow.py | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/tutorials/frontend/deploy_sparse.py b/tutorials/frontend/deploy_sparse.py index d3375c40fe72..f0af12b709e2 100644 --- a/tutorials/frontend/deploy_sparse.py +++ b/tutorials/frontend/deploy_sparse.py @@ -90,6 +90,20 @@ import scipy.sparse as sp +# Ask tensorflow to limit its GPU memory to what's actually needed +# instead of gobbling everything that's available. +# https://www.tensorflow.org/guide/gpu#limiting_gpu_memory_growth +# This way this tutorial is a little more friendly to sphinx-gallery. +gpus = tf.config.list_physical_devices("GPU") +if gpus: + try: + for gpu in gpus: + tf.config.experimental.set_memory_growth(gpu, True) + print("tensorflow will use experimental.set_memory_growth(True)") + except RuntimeError as e: + print("experimental.set_memory_growth option is not available: {}".format(e)) + + ############################################################################### # Configure Settings # ------------------ diff --git a/tutorials/frontend/from_tensorflow.py b/tutorials/frontend/from_tensorflow.py index eb8f21d4d049..4563e245c0cf 100644 --- a/tutorials/frontend/from_tensorflow.py +++ b/tutorials/frontend/from_tensorflow.py @@ -37,7 +37,7 @@ import tensorflow as tf -# Ask tensorflow to limit it's GPU memory to what's actually needed +# Ask tensorflow to limit its GPU memory to what's actually needed # instead of gobbling everything that's available. # https://www.tensorflow.org/guide/gpu#limiting_gpu_memory_growth # This way this tutorial is a little more friendly to sphinx-gallery.