【WSL2】【OpenBLAS】MXNet 1.9 + CUDA 11.7(Building From Source)

公開日:2022年8月31日
最終更新日:2022年9月6日

Introduction

2022年8月現在、pipでインストールできる MXNet 1.x は「mxnet-cu112==1.9.1」が最新です。

CUDA 11.7で使用したかったのでソースからBuildすることにしました。

手順を記録として残しておきます。

ちなみにCUDA 11.7の環境で「mxnet-cu112==1.9.1」を使っても問題なさそうな印象です。

Environment

Ubuntu 20.04 on WSL2 (Windows 11)
VERSION="20.04.4 LTS (Focal Fossa)"



WSL2を介さずWindowsでGPU版MXNetが使いたいかたはこちらの記事を参照して下さい。
touch-sp.hatenablog.com
touch-sp.hatenablog.com

Preparation

Install CUDA

公式通りに行いました。

wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-wsl-ubuntu.pin
sudo mv cuda-wsl-ubuntu.pin /etc/apt/preferences.d/cuda-repository-pin-600
wget https://developer.download.nvidia.com/compute/cuda/11.7.1/local_installers/cuda-repo-wsl-ubuntu-11-7-local_11.7.1-1_amd64.deb
sudo dpkg -i cuda-repo-wsl-ubuntu-11-7-local_11.7.1-1_amd64.deb
sudo cp /var/cuda-repo-wsl-ubuntu-11-7-local/cuda-96193861-keyring.gpg /usr/share/keyrings/
sudo apt update
sudo apt install cuda



最後にこの1行を実行

sudo ln -s /usr/lib/wsl/lib/libcuda.so.1 /usr/local/cuda/lib64/libcuda.so

Install cuDNN

NVIDIA公式から「cudnn-local-repo-ubuntu2004-8.5.0.96_1.0-1_amd64.deb」をダウンロードしてからインストールしました。(ダウンロードにはアカウントが必要です)

sudo dpkg -i cudnn-local-repo-ubuntu2004-8.5.0.96_1.0-1_amd64.deb
sudo cp /var/cudnn-local-repo-ubuntu2004-8.5.0.96/cudnn-local-0579404E-keyring.gpg /usr/share/keyrings/
sudo apt update
sudo apt install libcudnn8 libcudnn8-dev

Install NCCL

NVIDIA公式通りにインストールしました。

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-keyring_1.0-1_all.deb
sudo dpkg -i cuda-keyring_1.0-1_all.deb
sudo apt update
sudo apt install libnccl2=2.14.3-1+cuda11.7 libnccl-dev=2.14.3-1+cuda11.7

Install others

sudo apt install build-essential ninja-build ccache libopenblas-dev libopencv-dev cmake
build-essential==12.8
ninja-build==1.10.0
ccache==3.7.7
libopenblas-dev==0.3.8
libopencv-dev==4.2.0
cmake==3.16.3

Download MXNet sources

git clone --recursive https://github.com/apache/incubator-mxnet.git -b v1.9.x --single-branch --depth 1 mxnet
cd mxnet

Build

modify config.cmake

cp config/distribution/linux_cu117.cmake config.cmake



「config.cmake」ファイルの内容を下記のように書き換える。

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

set(CMAKE_BUILD_TYPE "Distribution" CACHE STRING "Build type")
set(CFLAGS "-mno-avx" CACHE STRING "CFLAGS")
set(CXXFLAGS "-mno-avx" CACHE STRING "CXXFLAGS")
set(USE_CUDA ON CACHE BOOL "Build with CUDA support")
set(USE_CUDNN ON CACHE BOOL "Build with CUDA support")
set(USE_NCCL ON CACHE BOOL "Build with NCCL support")
set(USE_OPENCV ON CACHE BOOL "Build with OpenCV support")
set(USE_OPENMP ON CACHE BOOL "Build with Openmp support")
set(USE_MKL_IF_AVAILABLE OFF CACHE BOOL "Use Intel MKL if found")
set(USE_MKLDNN ON CACHE BOOL "Build with MKL-DNN support")
set(USE_LAPACK ON CACHE BOOL "Build with lapack support")
set(USE_TVM_OP OFF CACHE BOOL "Enable use of TVM operator build system.")
set(USE_SSE ON CACHE BOOL "Build with x86 SSE instruction support")
set(USE_F16C OFF CACHE BOOL "Build with x86 F16C instruction support")
set(USE_LIBJPEG_TURBO ON CACHE BOOL "Build with libjpeg-turbo")
set(USE_DIST_KVSTORE OFF CACHE BOOL "Build with DIST_KVSTORE support")
set(MXNET_CUDA_ARCH "Auto" CACHE STRING "Cuda architectures")
set(CMAKE_CUDA_COMPILER "/usr/local/cuda/bin/nvcc" CACHE STRING "Cuda compiler")

Build

mkdir build
cd build
cmake ..
cmake --build .

Install MNXet

リポジトリ内で以下を実行。

pip install -e ./python

Check installation

curl --retry 10 -s https://raw.githubusercontent.com/apache/incubator-mxnet/master/tools/diagnose.py | python3

Check operation

以下のスクリプトで動作確認できています。
touch-sp.hatenablog.com
touch-sp.hatenablog.com
touch-sp.hatenablog.com