From 5fa9406fbd8c41a7bce2fbbd05519a2c74353d89 Mon Sep 17 00:00:00 2001 From: "Gerber, Mike" Date: Sat, 5 Sep 2020 12:59:40 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Test=20TensorFlow=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 11 ---------- README.md | 7 +++++++ assets/Dockerfile | 19 ++++++++++++++++++ .../requirements-tf1.txt | 0 assets/requirements-tf2.txt | 1 + run | 18 ++++++++++------- run-docker | 15 ++++++++++++-- test-nvidia | 20 +++++++++++++------ 8 files changed, 65 insertions(+), 26 deletions(-) delete mode 100644 Dockerfile create mode 100644 README.md create mode 100644 assets/Dockerfile rename requirements.txt => assets/requirements-tf1.txt (100%) create mode 100644 assets/requirements-tf2.txt diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 414686d..0000000 --- a/Dockerfile +++ /dev/null @@ -1,11 +0,0 @@ -FROM nvidia/cuda:10.0-cudnn7-runtime-ubuntu18.04 -RUN apt-get update &&\ - apt-get install -y python3 python3-pip &&\ - apt-get clean && rm -rf /var/lib/apt/lists/* -COPY requirements.txt /tmp -RUN pip3 install --no-cache-dir --upgrade pip && \ - pip3 install --no-cache-dir -r /tmp/requirements.txt - -COPY test-nvidia /usr/bin - -CMD ["/usr/bin/test-nvidia"] diff --git a/README.md b/README.md new file mode 100644 index 0000000..b04a088 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +Test Nvidia environment in relation to TensorFlow +================================================= + +* `./run` tests the native system. One of tf1 or tf2 is expected to have no + GPU available due to CUDA library incompatibility +* `./run-docker` tests Docker support. Both TensorFlow versions should work + as we're using a base image compatible to the respective version. diff --git a/assets/Dockerfile b/assets/Dockerfile new file mode 100644 index 0000000..8f0fefd --- /dev/null +++ b/assets/Dockerfile @@ -0,0 +1,19 @@ +ARG BASE_IMAGE + +FROM $BASE_IMAGE + +ARG tf + +RUN apt-get update && \ + apt-get install -y python3 python3-distutils curl &&\ + apt-get clean && rm -rf /var/lib/apt/lists/* && \ + \ + curl -sSL https://bootstrap.pypa.io/get-pip.py -o get-pip.py && \ + python3 get-pip.py && \ + rm -f get-pip.py +COPY assets/requirements-$tf.txt /tmp +RUN pip install --no-cache-dir -r /tmp/requirements-$tf.txt + +COPY test-nvidia /usr/bin + +CMD ["/usr/bin/test-nvidia"] diff --git a/requirements.txt b/assets/requirements-tf1.txt similarity index 100% rename from requirements.txt rename to assets/requirements-tf1.txt diff --git a/assets/requirements-tf2.txt b/assets/requirements-tf2.txt new file mode 100644 index 0000000..ba56ae2 --- /dev/null +++ b/assets/requirements-tf2.txt @@ -0,0 +1 @@ +tensorflow == 2.* diff --git a/run b/run index f32c9a4..3510f39 100755 --- a/run +++ b/run @@ -1,12 +1,16 @@ #!/bin/sh -vdir=`mktemp -d /tmp/test-nvidia.XXXXXX` +for tf in tf1 tf2; do + echo "== $tf" + vdir=`mktemp -d /tmp/test-nvidia.XXXXXX` -virtualenv -p /usr/bin/python3 $vdir -. $vdir/bin/activate + # Need Python 3.7 here as TF1 does not support 3.8 + virtualenv -q -p /usr/bin/python3.7 $vdir >/dev/null + . $vdir/bin/activate -pip3 install -r requirements.txt -python3 test-nvidia + pip install -q -r assets/requirements-$tf.txt + python3 test-nvidia -deactivate -rm --preserve-root -rf $vdir + deactivate + rm --preserve-root -rf $vdir +done diff --git a/run-docker b/run-docker index 1002148..c95d212 100755 --- a/run-docker +++ b/run-docker @@ -1,3 +1,14 @@ #!/bin/sh -docker build -t test-nvidia `dirname $0` -docker run --gpus all -it --rm test-nvidia +set -e + +for tf in tf1 tf2; do + echo "== $tf" + case "$tf" in + tf1) BASE_IMAGE=nvidia/cuda:10.0-cudnn7-runtime-ubuntu18.04;; + tf2) BASE_IMAGE=nvidia/cuda:10.1-cudnn7-runtime-ubuntu18.04;; + esac + work_dir=`dirname $0` + image_id=`docker build -q --build-arg tf=$tf --build-arg BASE_IMAGE=$BASE_IMAGE -f assets/Dockerfile $work_dir` + docker run --gpus all -it --rm $image_id + docker rmi $image_id >/dev/null || true +done diff --git a/test-nvidia b/test-nvidia index b3a2751..44c357c 100755 --- a/test-nvidia +++ b/test-nvidia @@ -1,13 +1,21 @@ #!/usr/bin/python3 import os -import tensorflow as tf -os.system('nvidia-smi') +os.system('nvidia-smi -L') -hello = tf.constant('Hello, TensorFlow!') -sess = tf.compat.v1.Session() -print(sess.run(hello)) -print('GPU available:', tf.test.is_gpu_available(cuda_only=True)) +os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1' # '1' means >= WARN +import tensorflow as tf +print('TensorFlow', tf.__version__) +with tf.compat.v1.Session() as sess: + hello = tf.constant('Hello, TensorFlow!') + result = sess.run(hello) + #print(result) +if hasattr(tf.config, 'list_physical_devices'): + # TensorFlow 2 + is_gpu_available = len(tf.config.list_physical_devices('GPU')) > 0 + print('GPU available:', is_gpu_available) +else: + print('GPU available:', tf.test.is_gpu_available(cuda_only=True))