Skip to navigation

Running TensorFlow

View as Markdown

This chapter goes through some example runs with TensorFlow. Some output messages have been removed or simplified in the runs for readability.

The sample runs assume that the GPU drivers are in place (section 9.1 of the Installation Manual). The runs also assume that TensorFlow package and the TensorFlow extras package have both been installed from the NVIDIA Base Command Manager repository with a package manager.

TensorFlow can be installed, for example with

yum install cm-tensorflow2-py39-cuda11.2-gcc9 cm-tensorflow2-extra-py39-cuda11.2-gcc9

In addition to requiring the extras package, TensorFlow requires an OpenMPI implementation to work. Chapter 3 of the User Manual describes the different OpenMPI packages that the BCM repositories provide. The different OpenMPI packages allow the user to choose which one to use. For example, depending on which interconnect is being used, or depending on if CUDA support is required.

In this chapter, the cm-openmpi4-cuda11.2-ofed51-gcc9 package is used. It can be installed with, for example:

yum install cm-openmpi4-cuda11.2-ofed51-gcc9

More information on the examples can be found at https://github.com/tensorflow/examples.

2.1 Hello World

A “Hello World” interactive example, that just shows that the software is in place for TensorFlow, can be run as follows:

Example

[root@basecm10 ~]# module load shared
[root@basecm10 ~]# module load tensorflow2-py39-cuda11.2-gcc9
Loading tensorflow2-py39-cuda11.2-gcc9/2.7.0
Loading requirement: openblas/dynamic/0.3.18 hdf5_18/1.8.21 gcc9/9.5.0 python39 \
cuda11.2/toolkit/11.2.2 cudnn8.1-cuda11.2/8.1.1.33 ml-pythondeps-py39-cuda11.2-gcc9/4.8.1 \
protobuf3-gcc9/3.9.2 nccl2-cuda11.2-gcc9/2.14.3
[root@basecm10 ~]# module load openmpi4-cuda11.2-ofed51-gcc9/
Loading openmpi4-cuda11.2-ofed51-gcc9/4.1.4
Loading requirement: hpcx/mlnx-ofed51/2.7.4 ucx/1.10.1 cm-pmix3/3.1.4 hwloc/1.11.11
[root@basecm10 ~]# python3.9
Python 3.9.16 (main, Mar 16 2023, 14:03:52)
[GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
...
>>> hello = tf.constant('TensorFlow 2 Hello World')
...
2023-03-22 17:20:10.514379: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created \
device /job:localhost/replica:0/task:0/device:GPU:0 with 31002 MB memory: -> device: 0, name: \
Tesla V100-SXM3-32GB, pci bus id: 0000:00:06.0, compute capability: 7.0
>>>
>>> hello = tf.constant('TensorFlow 2 Hello World')
>>> tf.print(hello)
TensorFlow 2 Hello World
>>> a = tf.constant(10)
>>> b = tf.constant(32)
>>> tf.print(a+b)
42
>>>

2.2 Deep Convolutional Generative Adversarial Network

The following trains a deep convolutional generative adversarial network (DCGAN).

The example code is included in the TensorFlow extras package. Once its module has been loaded, the example directory is defined with the CM_TENSORFLOW2_EXTRA environment variable.

The example picks up training images and labels from the MNIST site, and places them in a directory tensorflow_datasets/ if it needs to. The images are then used to train the model. End users would be expected to train the neural network within their home directories.

Example

[root@basecm10 ~]# module load tensorflow2-extra-py39-cuda11.2-gcc9
Loading tensorflow2-extra-py39-cuda11.2-gcc9/2.7.0
Loading requirement: openblas/dynamic/0.3.18 hdf5_18/1.8.21 gcc9/9.5.0 python39 \
cuda11.2/toolkit/11.2.2 cudnn8.1-cuda11.2/8.1.1.33 ml-pythondeps-py39-cuda11.2-gcc9/4.8.1\
protobuf3-gcc9/3.9.2 nccl2-cuda11.2-gcc9/2.14.3
tensorflow2-py39-cuda11.2-gcc9/2.7.0 opencv4-py39-cuda11.2-gcc9/4.5.4
[root@basecm10 ~]# module load openmpi4-cuda11.2-ofed51-gcc9/
Loading requirement: hpcx/mlnx-ofed51/2.7.4 ucx/1.10.1 cm-pmix3/3.1.4 hwloc/1.11.11
[root@basecm10 ~]# cd ${CM_TENSORFLOW2_EXTRA}/tensorflow_examples/models/dcgan/
[root@basecm10 dcgan]# python3.9 dcgan.py --epochs 5
...
I0322 18:24:37.097312 23456247932736 dataset_builder.py:400] Generating dataset mnist \
(/root/tensorflow_datasets/mnist/3.0.1)
Downloading and preparing dataset Unknown size (download: Unknown size, generated: Unknown \
size, total: Unknown size) to /root/tensorflow_datasets/mnist/3.0.1...
Dl Completed...: 0 url [00:00, ? url/s] I0322 18:24:37.484689 23456247932736 \
download_manager.py:354] Downloading https://storage.googleapis.com/cvdf-datasets/mnist/...
...
Generating splits...: ...
...
2023-03-22 18:24:39.178613: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created \
device /job:localhost/replica:0/task:0/device:GPU:0 with 31002 MB memory: -> device: 0, name: \
Tesla V100-SXM3-32GB, pci bus id: 0000:00:06.0, compute capability: 7.0
... Done writing mnist-train.tfrecord. Number of examples: 60000 (shards: [60000])
Generating splits...: ...
Dataset mnist downloaded and prepared to /root/tensorflow_datasets/mnist/3.0.1. Subsequent calls will \
reuse this data.
I0322 18:25:39.912966 23456247932736 logging_logger.py:35] Constructing tf.data.Dataset mnist for split \
train, from /root/tensorflow_datasets/mnist/3.0.1
Training ...
2023-03-22 18:25:42.881613: I tensorflow/stream_executor/cuda/cuda_dnn.cc:366] Loaded cuDNN version 8101
Epoch 0, Generator loss 1.0063459873199463, Discriminator Loss 1.0740859508514404
Epoch 1, Generator loss 0.9040709733963013, Discriminator Loss 1.0049669742584229
Epoch 2, Generator loss 0.88487708568573, Discriminator Loss 1.3763940334320068
Epoch 3, Generator loss 0.8470083475112915, Discriminator Loss 1.4865632057189941
Epoch 4, Generator loss 1.2192224264144897, Discriminator Loss 1.2187243700027466
[root@basecm10 dcgan]#

Datasets generated by the preceding run can be purged with:

Example

[root@basecm10 dcgan]# rm -rf /root/tensorflow_datasets/

2.3 Image-to-Image Translation with Conditional Adversarial Nets

The following trains a conditional adversarial networks as a general-purpose solution to image-to-image translation problems. The trained model is capable of completing different tasks, such as colorizing black and white photos.

The example code is included in the TensorFlow extras package. Once its module has been loaded, the example directory is defined with the CM_TENSORFLOW2_EXTRA environment variable.

The example uses a preprocessed copy of the CMP Facade Database, helpfully provided by the Center for Machine Perception at the Czech Technical University in Prague. The original dataset includes 606 rectified images of facades from various sources, which have been manually annotated. The facades are from different cities around the world and diverse architectural styles.

The example conveniently downloads the dataset in a temporary directory and then trains the model. End users would be expected to train the neural network within their home directories.

Example

[root@basecm10 ~]# module load tensorflow2-extra-py39-cuda11.2-gcc9
[root@basecm10 ~]# module load openmpi4-cuda11.2-ofed51-gcc9/
[root@basecm10 ~]# cd ${CM_TENSORFLOW2_EXTRA}/tensorflow_examples/models/pix2pix
[root@basecm10 pix2pix]# python3.9 data_download.py --download_path /tmp
...
Downloading data from http://efrosgans.eecs.berkeley.edu/pix2pix/datasets/facades.tar.gz
...
[root@basecm10 pix2pix]# python3.9 pix2pix.py --path /tmp/facades --epochs 5
...
2023-03-22 20:11:12.173915: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created \
device /job:localhost/replica:0/task:0/device:GPU:0 with 31002 MB memory: -> device: 0,\
name: Tesla V100-SXM3-32GB, pci bus id: 0000:00:06.0, compute capability: 7.0
Training ...
2023-03-22 20:11:16.660300: I tensorflow/stream_executor/cuda/cuda_dnn.cc:366] Loaded cuDNN version 8101
Epoch 0, Generator loss 39.03266906738281, Discriminator Loss 0.23912914097309113
Epoch 1, Generator loss 41.69317626953125, Discriminator Loss 0.07581206411123276
Epoch 2, Generator loss 33.53696823120117, Discriminator Loss 0.13100799918174744
Epoch 3, Generator loss 30.571992874145508, Discriminator Loss 1.0946425199508667
Epoch 4, Generator loss 29.1583309173584, Discriminator Loss 1.0569590330123901
[root@basecm10 pix2pix]#

2.4 Neural Machine Translation With Attention

The following trains a sequence to sequence model for neural machine translation with attention using gated recurrent units (GRUs).

The example code is included in the TensorFlow extra package. Once its module has been loaded, the example directory is defined with the CM_TENSORFLOW2_EXTRA environment variable.

The example conveniently downloads a dataset for Spanish to English translation in a temporary directory and then trains the model. End users would be expected to train the neural network within their home directories.

Example

[root@basecm10 ~]# module load tensorflow2-extra-py39-cuda11.2-gcc9
[root@basecm10 ~]# module load openmpi4-cuda11.2-ofed51-gcc9
[root@basecm10 ~]# cd ${CM_TENSORFLOW2_EXTRA}/tensorflow_examples/models/nmt_with_attention/
[root@basecm10 nmt_with_attention]# python3.9 train.py --epochs 5 --download_path /tmp
...
Downloading data from http://storage.googleapis.com/download.tensorflow.org/data/spa-eng.zip
...
2023-03-22 21:18:11.717144: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created \
device /job:localhost/replica:0/task:0/device:GPU:0 with 31002 MB memory: -> device: 0,\
name: Tesla V100-SXM3-32GB, pci bus id: 0000:00:06.0, compute capability: 7.0
Training ...
2023-03-22 21:18:27.574448: I tensorflow/stream_executor/cuda/cuda_dnn.cc:366] Loaded cuDNN version 8101
Epoch: 0, Train Loss: 1.814919114112854, Test Loss: 2.885890483856201
Epoch: 1, Train Loss: 1.085754156112671, Test Loss: 2.093322515487671
Epoch: 2, Train Loss: 0.6579596996307373, Test Loss: 1.7698687314987183
Epoch: 3, Train Loss: 0.43058785796165466, Test Loss: 1.7104995250701904
Epoch: 4, Train Loss: 0.29615747928619385, Test Loss: 1.7149256467819214
[root@basecm10 nmt_with_attention]#