Commit ce36fff0 authored by Carsten Eie Frigaard's avatar Carsten Eie Frigaard
Browse files

update

parent 4f4a240c
No preview for this file type
#!/usr/bin/env python3 #!/usr/bin/env python3
def UsesGPU(): def RestartKernel() :
TF_gpu = False
K_gpu = False
try: try:
# confirm TensorFlow sees the GPU from IPython.display import display_html
from tensorflow.python.client import device_lib display_html("<script>Jupyter.notebook.kernel.restart()</script>",raw=True)
if 'GPU' in str(device_lib.list_local_devices()):
TF_gpu = True
except: except:
print("WARNING: could not import from tensorflow"); print("ERROR in RestartKernel()!")
# NOTE: will not work under some keras/TF versions.. def ResetSeeds(seed=42):
#try: import numpy as np
# # confirm Keras sees the GPU import tensorflow as tf
# from keras import backend import random as python_random
# if len(backend.tensorflow_backend._get_available_gpus()) > 0:
# K_gpu = True from os import environ
#except: environ['PYTHONHASHSEED']=str(seed)
# print("WARNING: could not import from keras");
return TF_gpu, K_gpu np.random.seed(seed)
python_random.seed(seed)
tf.random.set_seed(seed)
def DisableGPU(): def DisableGPUs():
import os import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" # see issue #152 os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
os.environ["CUDA_VISIBLE_DEVICES"] = ""
TF_gpu, K_gpu = UsesGPU()
#if K_gpu:
# raise ImportException("no keras import allowed before calling DisableGPU(), reset your kernel and call DisableGPU() in the beginning of your code.")
def EnableGPU(gpu_mem_fraction=0.05, gpus=None, cpus=None):
#try: #try:
# import os # # Disable all GPUS
# os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" # see issue #152 # tf.config.set_visible_devices([], 'GPU')
# os.environ["CUDA_VISIBLE_DEVICES"] = "1" # visible_devices = tf.config.get_visible_devices()
# for device in visible_devices:
# assert device.device_type != 'GPU'
#except: #except:
# print("ERROR: something failed in EnableGPU(), environment part") # # Invalid device or cannot modify virtual devices once initialized.
# WARN("exception in DisableGPUs() ignored")
try:
import tensorflow as tf def ReEnableGPUs():
from keras.backend.tensorflow_backend import set_session # if DisableGPUs() was called, you can re-enable GPUS via this function
config = tf.ConfigProto() import os
if gpus!=None or cpus!=None: os.environ["CUDA_VISIBLE_DEVICES"] = ""
if gpus==None:
gpus=1 # default def StartupSequence_GPU(verbose=False):
if cpus==None: n = -1
cpus=4 # default if verbose:
config = tf.ConfigProto( device_count = { 'GPU': gpus, 'CPU': cpus} ) print("StartupSequence_GPU()..")
if gpu_mem_fraction<0:
raise ValueExeption("gpu_mem_fraction is below zero")
if gpu_mem_fraction>0.5:
raise ValueExeption("sorry Dave, can't do gpu_mem_fraction>50%")
config.gpu_options.per_process_gpu_memory_fraction = gpu_mem_fraction
config.gpu_options.allow_growth=True
set_session(tf.Session(config=config))
except:
print("ERROR: something failed in EnableGPU(), Tensorflow part")
def RestartKernel() :
try: try:
from IPython.display import display_html import tensorflow
display_html("<script>Jupyter.notebook.kernel.restart()</script>",raw=True)
except: physical_devices = tensorflow.config.list_physical_devices('GPU')
print("ERROR in RestartKernel()!") n = 0
for i in range(len(physical_devices)):
if verbose:
print(f" setting physical_devices[{i}] set_memory_growth=True..")
tensorflow.config.experimental.set_memory_growth(physical_devices[i], True)
n += 1
except Exception as e:
if (verbose):
print(f"ERROR: something failed in StartupSequence_EnableGPU(), re-raising exception='{e}'\n")
raise e
assert n > -1, f"something went wrong in startup-sequence, n={n} should be > -1"
return n
########################################
def TestAll(): def TestAll():
print("(not tests yet)") print("(not tests yet)")
...@@ -72,3 +68,8 @@ def TestAll(): ...@@ -72,3 +68,8 @@ def TestAll():
if __name__ == '__main__': if __name__ == '__main__':
TestAll() TestAll()
#Versions()
#ResetSeeds()
#DisableGPUs()
#StartupSequence_GPU(verbose=False)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment