Example Recipe Files
Deck |
---|
startHidden | false |
---|
id | Example Recipe Files |
---|
|
Card |
---|
title-old | Ubuntu with Tensorflow |
---|
id | ubuntu-tensorflow |
---|
label | Ubuntu with Tensorflow 2.0 |
---|
title | Ubuntu with Tensorflow 2.0 |
---|
| CentOS with Tensorflow Code Block |
---|
theme | Midnight |
---|
title | tensorflow-2.0.recipe |
---|
collapse | true |
---|
| Bootstrap: docker
FROM: nvidia/cuda:10.0-cudnn7-devel-ubuntu18.04
%post
. /environment
SHELL=/bin/bash
CPATH="/usr/local/cuda/include:$CPATH"
PATH="/usr/local/cuda/bin:$PATH"
LD_LIBRARY_PATH="/usr/local/cuda/lib64:$LD_LIBRARY_PATH"
CUDA_HOME="/usr/local/cuda"
apt-get update
apt-get install -y wget git vim build-essential cmake libgtk2.0-0 python3.6 python3.6-dev python3.6-venv python3-distutils python3-apt libgtk-3-dev xauth curl
wget https://bootstrap.pypa.io/pip/3.6/get-pip.py
python3.6 get-pip.py
ln -s /usr/bin/python3.6 /usr/local/bin/python3
pip install tensorflow-gpu==2.0.0
pip install astropy
%environment
# use bash as default shell
SHELL=/bin/bash
# add CUDA paths
CPATH="/usr/local/cuda/include:$CPATH"
PATH="/usr/local/cuda/bin:$PATH"
LD_LIBRARY_PATH="/usr/local/cuda/lib64:$LD_LIBRARY_PATH"
CUDA_HOME="/usr/local/cuda"
export PATH LD_LIBRARY_PATH CPATH CUDA_HOME |
To build and test a container from the recipe from an interactive session on a GPU node: Code Block |
---|
language | bash |
---|
theme | Midnight |
---|
title | Building from recipe |
---|
collapse | true |
---|
| [netid@cpu37 ~]$ vi tensorflow-2.0.recipe
[netid@cpu37 ~]$ apptainer build tensorflow-2.0.sif tensorflow-2.0.recipe
INFO: User not listed in /etc/subuid, trying root-mapped namespace
INFO: The %post section will be run under fakeroot
INFO: Starting build...
. . .
INFO: Adding environment to container
INFO: Creating SIF file...
INFO: Build complete: tensorflow-2.0.sif |
As a TensorFlow example, you could use the following script: Code Block |
---|
language | py |
---|
theme | Midnight |
---|
title | TFlow_example.py |
---|
collapse | true |
---|
| #Linear Regression Example with TensorFlow v2 library
from __future__ import absolute_import, division, print_function
#
import tensorflow as tf
import numpy as np
rng = np.random
#
# Parameters.
learning_rate = 0.01
training_steps = 1000
display_step = 50
#
# Training Data.
X = np.array([3.3,4.4,5.5,6.71,6.93,4.168,9.779,6.182,7.59,2.167,
7.042,10.791,5.313,7.997,5.654,9.27,3.1])
Y = np.array([1.7,2.76,2.09,3.19,1.694,1.573,3.366,2.596,2.53,1.221,
2.827,3.465,1.65,2.904,2.42,2.94,1.3])
n_samples = X.shape[0]
#
# Weight and Bias, initialized randomly.
W = tf.Variable(rng.randn(), name="weight")
b = tf.Variable(rng.randn(), name="bias")
# Linear regression (Wx + b).
def linear_regression(x):
return W * x + b
# Mean square error.
def mean_square(y_pred, y_true):
return tf.reduce_sum(tf.pow(y_pred-y_true, 2)) / (2 * n_samples)
# Stochastic Gradient Descent Optimizer.
optimizer = tf.optimizers.SGD(learning_rate)
#
# Optimization process.
def run_optimization():
# Wrap computation inside a GradientTape for automatic differentiation.
with tf.GradientTape() as g:
pred = linear_regression(X)
loss = mean_square(pred, Y)
# Compute gradients.
gradients = g.gradient(loss, [W, b])
# Update W and b following gradients.
optimizer.apply_gradients(zip(gradients, [W, b]))
#
# Run training for the given number of steps.
for step in range(1, training_steps + 1):
# Run the optimization to update W and b values.
run_optimization()
if step % display_step == 0:
pred = linear_regression(X)
loss = mean_square(pred, Y)
print("step: %i, loss: %f, W: %f, b: %f" % (step, loss, W.numpy(), b.numpy())) |
The output might resemble the following: Code Block |
---|
language | bash |
---|
theme | Midnight |
---|
title | Executing Tensorflow Example |
---|
collapse | true |
---|
| [netid@i16n2 ~]$ apptainer exec --nv tensorflow-2.0.sif python3 TFlow_example.py
INFO: underlay of /etc/localtime required more than 50 (104) bind mounts
INFO: underlay of /usr/bin/nvidia-smi required more than 50 (540) bind mounts
2022-10-27 13:14:25.049501: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcuda.so.1
2022-10-27 13:14:25.069796: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties:
name: Tesla P100-PCIE-16GB major: 6 minor: 0 memoryClockRate(GHz): 1.3285
pciBusID: 0000:0b:00.0
2022-10-27 13:14:25.076227: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.0
2022-10-27 13:14:25.130541: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcublas.so.10.0
2022-10-27 13:14:25.152105: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcufft.so.10.0
2022-10-27 13:14:25.170536: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcurand.so.10.0
2022-10-27 13:14:25.221289: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusolver.so.10.0
2022-10-27 13:14:25.254668: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusparse.so.10.0
2022-10-27 13:14:25.335204: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudnn.so.7
2022-10-27 13:14:25.335836: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0
2022-10-27 13:14:25.337681: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2022-10-27 13:14:25.371777: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 2399925000 Hz
2022-10-27 13:14:25.373812: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x3e6c350 executing computations on platform Host. Devices:
2022-10-27 13:14:25.373928: I tensorflow/compiler/xla/service/service.cc:175] StreamExecutor device (0): Host, Default Version
2022-10-27 13:14:25.511775: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x3b4aaf0 executing computations on platform CUDA. Devices:
2022-10-27 13:14:25.511949: I tensorflow/compiler/xla/service/service.cc:175] StreamExecutor device (0): Tesla P100-PCIE-16GB, Compute Capability 6.0
2022-10-27 13:14:25.512236: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties:
name: Tesla P100-PCIE-16GB major: 6 minor: 0 memoryClockRate(GHz): 1.3285
pciBusID: 0000:0b:00.0
2022-10-27 13:14:25.512614: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.0
2022-10-27 13:14:25.512730: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcublas.so.10.0
2022-10-27 13:14:25.512756: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcufft.so.10.0
2022-10-27 13:14:25.512772: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcurand.so.10.0
2022-10-27 13:14:25.512884: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusolver.so.10.0
2022-10-27 13:14:25.512909: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusparse.so.10.0
2022-10-27 13:14:25.512926: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudnn.so.7
2022-10-27 13:14:25.513202: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0
2022-10-27 13:14:25.514805: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.0
2022-10-27 13:14:25.515918: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1159] Device interconnect StreamExecutor with strength 1 edge matrix:
2022-10-27 13:14:25.516108: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1165] 0
2022-10-27 13:14:25.516290: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1178] 0: N
2022-10-27 13:14:25.517711: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1304] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 15223 MB memory) -> physical GPU (device: 0, name: Tesla P100-PCIE-16GB, pci bus id: 0000:0b:00.0, compute capability: 6.0)
step: 50, loss: 0.364507, W: 0.555666, b: -1.356643
step: 100, loss: 0.331617, W: 0.537752, b: -1.229641
step: 150, loss: 0.302488, W: 0.520894, b: -1.110123
step: 200, loss: 0.276691, W: 0.505029, b: -0.997647
step: 250, loss: 0.253844, W: 0.490099, b: -0.891799
step: 300, loss: 0.233610, W: 0.476048, b: -0.792186
step: 350, loss: 0.215690, W: 0.462825, b: -0.698444
step: 400, loss: 0.199820, W: 0.450382, b: -0.610224
step: 450, loss: 0.185765, W: 0.438671, b: -0.527203
step: 500, loss: 0.173317, W: 0.427651, b: -0.449073
step: 550, loss: 0.162293, W: 0.417280, b: -0.375547
step: 600, loss: 0.152530, W: 0.407520, b: -0.306353
step: 650, loss: 0.143884, W: 0.398335, b: -0.241236
step: 700, loss: 0.136226, W: 0.389691, b: -0.179956
step: 750, loss: 0.129444, W: 0.381557, b: -0.122287
step: 800, loss: 0.123438, W: 0.373902, b: -0.068015
step: 850, loss: 0.118119, W: 0.366698, b: -0.016941
step: 900, loss: 0.113408, W: 0.359918, b: 0.031123
step: 950, loss: 0.109236, W: 0.353538, b: 0.076356
step: 1000, loss: 0.105541, W: 0.347534, b: 0.118923
[netid@i16n2 ~]$ |
|
Card |
---|
title-old | MPI |
---|
id | mpi-recipe |
---|
label | MPI |
---|
title | MPI |
---|
| MPIApptainer supports MPI pretty well since, by default, the network is the same inside and outside the container. The more complicated bit is making sure that the container has the right set of MPI libraries. MPI is an open specification, but there are several different implementations (OpenMPI, MVAPICH2, and Intel MPI to name three) with some non-overlapping feature sets. If the host and container are running different MPI implementations, or even different versions of the same implementation, hilarity may ensue. The general rule is that you want the version MPI inside the container to be the same version or newer than the host. You may be thinking that this is not good for the portability of your container and you are right. Containerizing MPI applications is not terribly difficult with Singularity, but it comes at the cost of additional requirements for the host system. In this example, the infiniband pieces are installed and then the MVAPICH version of MPI. When the job is run, the script will need to load the correct module with the matching version of MVAPICH. Code Block |
---|
theme | Midnight |
---|
title | MPI Recipe File |
---|
collapse | true |
---|
| BootStrap: debootstrap
OSVersion: xenial
MirrorURL: http://us.archive.ubuntu.com/ubuntu/
%runscript
echo "This is what happens when you run the container..."
%post
echo "Hello from inside the container"
sed -i 's/$/ universe/' /etc/apt/sources.list
apt update
apt -y --allow-unauthenticated install vim build-essential wget gfortran bison libibverbs-dev libibmad-dev libibumad-dev librdmacm-dev libmlx5-dev libmlx4-dev
wget http://mvapich.cse.ohio-state.edu/download/mvapich/mv2/mvapich2-2.1.tar.gz
tar xvf mvapich2-2.1.tar.gz
cd mvapich2-2.1
./configure --prefix=/usr/local
make -j4
make install
/usr/local/bin/mpicc examples/hellow.c -o /usr/bin/hellow |
|
|
|