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
def UsesGPU():
TF_gpu = False
K_gpu = False
def RestartKernel() :
try:
# confirm TensorFlow sees the GPU
from tensorflow.python.client import device_lib
if 'GPU' in str(device_lib.list_local_devices()):
TF_gpu = True
from IPython.display import display_html
display_html("<script>Jupyter.notebook.kernel.restart()</script>",raw=True)
except:
print("WARNING: could not import from tensorflow");
print("ERROR in RestartKernel()!")
# NOTE: will not work under some keras/TF versions..
#try:
# # confirm Keras sees the GPU
# from keras import backend
# if len(backend.tensorflow_backend._get_available_gpus()) > 0:
# K_gpu = True
#except:
# print("WARNING: could not import from keras");
def ResetSeeds(seed=42):
import numpy as np
import tensorflow as tf
import random as python_random
return TF_gpu, K_gpu
from os import environ
environ['PYTHONHASHSEED']=str(seed)
def DisableGPU():
import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" # see issue #152
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.")
np.random.seed(seed)
python_random.seed(seed)
tf.random.set_seed(seed)
def EnableGPU(gpu_mem_fraction=0.05, gpus=None, cpus=None):
def DisableGPUs():
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
#try:
# import os
# os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" # see issue #152
# os.environ["CUDA_VISIBLE_DEVICES"] = "1"
# # Disable all GPUS
# tf.config.set_visible_devices([], 'GPU')
# visible_devices = tf.config.get_visible_devices()
# for device in visible_devices:
# assert device.device_type != 'GPU'
#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
from keras.backend.tensorflow_backend import set_session
config = tf.ConfigProto()
if gpus!=None or cpus!=None:
if gpus==None:
gpus=1 # default
if cpus==None:
cpus=4 # default
config = tf.ConfigProto( device_count = { 'GPU': gpus, 'CPU': cpus} )
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 ReEnableGPUs():
# if DisableGPUs() was called, you can re-enable GPUS via this function
import os
os.environ["CUDA_VISIBLE_DEVICES"] = ""
def RestartKernel() :
def StartupSequence_GPU(verbose=False):
n = -1
if verbose:
print("StartupSequence_GPU()..")
try:
from IPython.display import display_html
display_html("<script>Jupyter.notebook.kernel.restart()</script>",raw=True)
except:
print("ERROR in RestartKernel()!")
import tensorflow
physical_devices = tensorflow.config.list_physical_devices('GPU')
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():
print("(not tests yet)")
......@@ -72,3 +68,8 @@ def TestAll():
if __name__ == '__main__':
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