SingularityとDebian12 bookworm

Linux

はじめに

Tensorflowを使ったGPUの計算を行うのにSingularity便利そうなので設定しました。
Singularityが便利なのはユーザ権限で動作するところです。
Debianにはsingularity-containerパッケージがsidにしか存在しないのでGoとSingularityをコンパイルしてインストールします。
Ubuntuにはパッケージが存在しますが、今回はDebianを使います。

参考

singularity 4 にはGo 1.20以上が必要です。

Installing SingularityCE — SingularityCE Admin Guide 4.0 documentation

環境

nvidia-container-toolkitは不要です。

- Debian 12 bookworm
- GO 1.21.3
- Singularity 4.0.1
- nvidia-driver 525.125.06-1~deb12u1
- nvidia-container-toolkit 1.14.3-1

NVIDIA driverのインストール

Debian 12 bookworm(ホスト)にNVIDIA driverをインストールします。
再起動が必要です。

apt -y install nvidia-driver firmware-misc-nonfree dkms

NVIDIA Container Toolkitのインストール

Debian 12 bookworm(ホスト)にNVIDIA Container Toolkitをインストールします。

curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \
  && curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list | \
    sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
    sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list \
  && \
    sudo apt-get update


sudo apt install nvidia-container-toolkit

Debian 12 bookworm(ホスト)での環境

Debian 12 bookworm(ホスト)でのnvidia-smiの結果です。

+-----------------------------------------------------------------------------+
| NVIDIA-SMI 525.125.06   Driver Version: 525.125.06   CUDA Version: 12.0     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  NVIDIA A30          On   | 00000000:18:00.0 Off |                    0 |
| N/A   43C    P0    37W / 165W |  22795MiB / 24576MiB |     14%      Default |
|                               |                      |             Disabled |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|    0   N/A  N/A      1032      G   /usr/lib/xorg/Xorg                  4MiB |
|    0   N/A  N/A     10052      C   python                          22788MiB |
+-----------------------------------------------------------------------------+

必要なライブラリをインストール

公式のホームページの通りに必要なライブラリをインストールします。

sudo apt-get install -y \
   autoconf \
   automake \
   cryptsetup \
   git \
   libfuse-dev \
   libglib2.0-dev \
   libseccomp-dev \
   libtool \
   pkg-config \
   runc \
   squashfs-tools \
   squashfs-tools-ng \
   uidmap \
   wget \
   zlib1g-dev

GOのインストール

公式のホームページの通りにGOをインストールします。
「VERSION=1.21.3」の部分は、「https://github.com/sylabs/singularity/releases/」でインストールするSingularityのバージョンを探して、そのバージョンをコンパイルしているGO言語のバージョンを指定します。
今回は「SingularityCE 4.0.1」をインストールするので、「These packages were built with Go 1.21.3」の記述通りに「Go 1.21.3」を指定します。

export VERSION=1.21.3 OS=linux ARCH=amd64 && \
    wget https://dl.google.com/go/go$VERSION.$OS-$ARCH.tar.gz && \
    sudo tar -C /usr/local -xzvf go$VERSION.$OS-$ARCH.tar.gz && \
    rm go$VERSION.$OS-$ARCH.tar.gz

Go言語の環境変数を指定します。
Singularityのインストール後は「~/.bashrc」から削除しても良いです。

echo 'export GOPATH=${HOME}/go' >> ~/.bashrc && \
    echo 'export PATH=/usr/local/go/bin:${PATH}:${GOPATH}/bin' >> ~/.bashrc && \
    source ~/.bashrc

Goのバージョンを確認します。

go version

    go version go1.21.3 linux/amd64

Singularityのインストール

公式のホームページの通りにSingularityをインストールします。
今回は「SingularityCE 4.0.1」をインストールするので「VERSION=4.0.1」を指定します。

export VERSION=4.0.1 && # adjust this as necessary \
    wget https://github.com/sylabs/singularity/releases/download/v${VERSION}/singularity-ce-${VERSION}.tar.gz && \
    tar -xzf singularity-ce-${VERSION}.tar.gz && \
    cd singularity-ce-${VERSION}
./mconfig && \
    make -C ./builddir && \
    sudo make -C ./builddir install

Singularityのバージョンを確認します。

singularity version

    4.0.1

nvidia-container-cliコマンドのパスを確認します。

sudo vi /usr/local/etc/singularity/singularity.conf
    nvidia-container-cli path = /usr/bin/nvidia-container-cli

Singularityの削除

Singularityを削除する場合は、以下のファイルを削除します。

sudo rm -rf \
    /usr/local/libexec/singularity \
    /usr/local/var/singularity \
    /usr/local/etc/singularity \
    /usr/local/bin/singularity \
    /usr/local/bin/run-singularity \
    /usr/local/etc/bash_completion.d/singularity

Goの削除

Goを削除する場合は、以下のファイルを削除します。

sudo rm -rf /usr/local/go

Singularityのテスト

NGCからTensorflowを実行可能なコンテナをダウンロードして、コンテナからGPUを使えるかテストします。
JupyterLabやJupyter Notebookも含まれています。

下記のURLにあるコンテナを使います。
このURLの「Framework Containers Support Matrix」をクリックすると、コンテナに含まれるCUDAやシステムの必要条件が読めます。

TensorFlow | NVIDIA NGC
TensorFlow is an open source platform for machine learning. It provides comprehensive tools and libr...
singularity pull docker://nvcr.io/nvidia/tensorflow:23.10-tf2-py3

singularity run --nv ~/singularity/tensorflow_23.10-tf2-py3.sif

pythonのコンソールに入って「import tensorflow as tf」、「tf.config.list_physical_devices(‘GPU’)」で確認します。
以下がその結果で、ホストのGPUを認識しています。

13:4: not a valid test operator: (
13:4: not a valid test operator: 525.125.06

================
== TensorFlow ==
================

NVIDIA Release 23.10-tf2 (build 70926626)
TensorFlow Version 2.13.0

Container image Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
Copyright 2017-2023 The TensorFlow Authors.  All rights reserved.

Various files include modifications (c) NVIDIA CORPORATION & AFFILIATES.  All rights reserved.

This container image and its contents are governed by the NVIDIA Deep Learning Container License.
By pulling and using the container, you accept the terms and conditions of this license:

https://developer.nvidia.com/ngc/nvidia-deep-learning-container-license
NOTE: CUDA Forward Compatibility mode ENABLED. Using CUDA 12.2 driver version 535.104.05 with kernel driver version 525.125.06. See https://docs.nvidia.com/deploy/cuda-compatibility/ for details.

SingularityのコンソールからGPUが認識できているか確認します。

Singularity> python
Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.


>>> import tensorflow as tf
2023-10-15 14:59:28.701126: I tensorflow/core/util/port.cc:110] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2023-10-15 14:59:29.251742: I tensorflow/core/platform/cpu_feature_guard.cc:183] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: SSE3 SSE4.1 SSE4.2 AVX, in other operations, rebuild TensorFlow with the appropriate compiler flags.
tf.config.list_physical_devices('GPU')


>>> tf.config.list_physical_devices('GPU')
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]

また、SingularityのコンソールからCUDAのバージョンを確認します。

Singularity> nvcc -V


nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2023 NVIDIA Corporation
Built on Tue_Aug_15_22:02:13_PDT_2023
Cuda compilation tools, release 12.2, V12.2.140
Build cuda_12.2.r12.2/compiler.33191640_0

Singularityからのnvidia-smiの実行結果です。
ホストで実行した結果とは「CUDA Version: 12.2」が違います。
ホストでは「CUDA Version: 12.0」でした。

Singularity> nvidia-smi

+-----------------------------------------------------------------------------+
| NVIDIA-SMI 525.125.06   Driver Version: 525.125.06   CUDA Version: 12.2     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  NVIDIA A30          On   | 00000000:18:00.0 Off |                    0 |
| N/A   41C    P0    31W / 165W |      4MiB / 24576MiB |      0%      Default |
|                               |                      |             Disabled |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|    0   N/A  N/A      1032      G   /usr/lib/xorg/Xorg                  4MiB |
+-----------------------------------------------------------------------------+

Jupyterの起動

Jupyter Notebookを起動する場合は、下記となります。

Singularity> jupyter-notebook

JupyterLabを起動する場合は、下記となります。

Singularity> jupyter-lab

DebianのGUIのインストール

私の場合、Debian 12 bookwormのインストール時に、最初からGUIをインストールすると、コンソールの液晶モニタででGUIが表示されなかったので、CUIでインストールして、後でGUIをインストールしました。

apt -y install task-gnome-desktop
apt -y install xrdp tigervnc-standalone-server
systemctl enable xrdp

デスクトップ環境をインストールするとスリープ設定が有効(20分)になるので無効化します。

Debian12とサスペンド無効

Singularityのキャッシュデータ削除

ダウンロードしたコンテナのキャッシュデータを削除します。

singularity cache clean

Comments