Skip to content

Commit

Permalink
Make from_tensorflow.py more GPU memory friendly. (#8763)
Browse files Browse the repository at this point in the history
* 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.

* Also deploy_sparse.py. Should probably also be done to tensorflow.rst.
  • Loading branch information
mbs-octoml authored Aug 17, 2021
1 parent 2008d62 commit cfa498c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
14 changes: 14 additions & 0 deletions tutorials/frontend/deploy_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
# ------------------
Expand Down
15 changes: 15 additions & 0 deletions tutorials/frontend/from_tensorflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@
# Tensorflow imports
import tensorflow as tf


# 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))


try:
tf_compat_v1 = tf.compat.v1
except ImportError:
Expand Down

0 comments on commit cfa498c

Please sign in to comment.