[ML] WSL2 : Install Tensorflow (GPU)

2022. 7. 17. 08:33·Computer/CUDA
728x90
728x90

https://docs.nvidia.com/cuda/wsl-user-guide/index.html

 

wsl2부터는 gpu 사용가능함:
cuda 가능!

 

tensorflow 의 GPU 를 사용하기 위한 CUDA를 WSL2에서 본격적으로 지원.

DirectML을 통해서도 GPU지원이 가능하지만, 아직까지는 CUDA가 나은 편임.


Pre-requirements

1. Windows 11 (>=20150)

winver 을 수행하여, build 가 20150 이상이어야 WSL2에 cuda 설치가 가능


2. WSL2를 지원하는 NVIDIA Driver 설치.

그래픽 카드 종류 미리 확인할 것.

2022.03.10 - [Computer/ETC] - HWiNFO : PC의 HW 사양 정보 확인 SW

 

HWiNFO : PC의 HW 사양 정보 확인 SW

Windows OS에서 PC의 HW구성 상태를 확인해보기 가장 좋은 SW임. 여러 컴퓨터를 사용하는 경우, HW구성을 외우고 있기는 불가능한터라...참고로 Portable 형태도 있으므로 굳이 OS에 설치하지 않고 확인

dsaint31.tistory.com

다음 URL에서 그래픽카드 종류에 맞는 해당 driver 다운로드.

https://www.nvidia.com/Download/index.aspx?lang=en-us

 

Download the latest official NVIDIA drivers

Download the latest official NVIDIA drivers

www.nvidia.com

시간이 된다면, 아래 link를 한번 읽어보는 것도 좋음.

https://developer.nvidia.com/cuda/wsl

 

GPU in Windows Subsystem for Linux (WSL)

Delivers machine learning capabilities across industry segments.

developer.nvidia.com


3. WSL2 설치하고 Ubuntu distribution 설치.

2022.July.14 현재, kernel version of 5.10.43.3 or higher여야 함

설치하고자하는 WSL의 distribution 내에서 cat /proc/version 으로 확인할 것.

이보다 낮다면, wsl --update 수행.

가급적 Ubuntu를 설치할 것.

공식문서에서도 다음과 같이 애기함
Once you've installed the above driver,
ensure you enable WSL and install a glibc-based distribution (like Ubuntu or Debian).
For our testing, we used Ubuntu.
Ensure you have the latest kernel by selecting Check for updates in the Windows Update section of the Settings app.


4. Conda 설치.

Miniconda로 설치.

3-4번 수행의 자세한 건 conda install on WSL2 (miniconda) 참고.


Tensorflow-gpu설치 (conda) : 2022.12 이후

https://www.tensorflow.org/install/pip#windows-wsl2_1 를 참고한 내용임.
이전보다 많이 단순해짐. conda의 덕분.

  • 반드시 NVidia 사이트에서 WSL을 위한 Driver를 설치하고 난 이후에 wsl을 수행하여 다음 작업들을 해야함.

1. tf 라는 이름으로 새 conda환경 생성 및 해당환경 활성화

conda create --name tf python=3.9
  • tf를 원하는 이름으로 바꿔도 상관없음.
conda activate tf
  • 해당 가상환경 활성화

2. CUDA 및 cuDNN을 설치.

conda install -c conda-forge cudatoolkit=11.2 cudnn=8.1.0
  • 2022.12 당시의 version임.
  • 앞서 python의 버전과 함께 버전을 맞춰주어야 함.

3. 라이브러리 로딩 path설정.

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CONDA_PREFIX/lib/
  • conda가 설치한 CUDA 및 cuDNN에서 제공하는 library (.so파일들)를 읽어들이기 위한 PATH 추가.

4. CUDA 및 cuDNN을 설치.

pip install --upgrade pip
pip install tensorflow==2.10
  • pip를 업그레이드하고, tensorflow설치.
  • 2022.12 당시 tensorflow-2.11.0이 최신으로 pip install tensorflow라고 실행하면 2.11 버전이 설치되는 데 해당 버전에 문제가 있어서 training 수행에서 gpu사용시 에러가 발생 (libdevice를 못찾는다는 애기가 나오면서 수행 도중 죽음).
    Node: 'StatefulPartitionedCall_2'
    libdevice not found at ./libdevice.10.bc
         [[{{node StatefulPartitionedCall_2}}]] [Op:__inference_train_function_766]
  • 때문에 2022.12 기준으로 반드시 tensorflow-2.10 이 설치되도록 지정을 해야함.
  • 2022.12기준으로 Unable to register cuBLAS factory 에러 메시지가  tensorflow-2.10.x에서 발생.
  • 2022.12기준으로 tensorflow-2.9.3에서는 에러가 발생하지 않음.https://github.com/google-research/multinerf/issues/47#issuecomment-1262495402
 

TensorFlow 2.10 causes trouble! · Issue #47 · google-research/multinerf

Hi - many thanks for releasing this code! Tensorflow 2.10 is now (since about September 1st 2022) the default version for a pip install but running in a CUDA 11.2.2 CUDNN 8.1 environment you'll...

github.com

5. Test

python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

더보기

Tensorflow-gpu설치 (conda) : old version

아래 내용들은 위의 내용으로 대체됨.

1. CUDA설치

현재 GPG key 삭제

sudo apt-key del 7fa2af80

2. CUDA Toolkit 설치.

CUDA on WSL :: CUDA Toolkit Documentation

[CUDA on WSL :: CUDA Toolkit Documentation

Whether to efficiently use hardware resources or to improve productivity, virtualization is a more widely used solution in both consumer and enterprise space. There are different types of virtualizations, and it is beyond the scope of this document to delv

docs.nvidia.com](https://docs.nvidia.com/cuda/wsl-user-guide/index.html#getting-started-with-cuda-on-wsl)

WSL2는 윈도우에서 설치한 Nvida 드라이버를 통해 /usr/lib/wsl/lib 에 필요한 binary 파일들을 가지게 됨. 문제는 일반적인 linux를 고려한 cuda toolkit들을 설치할 경우, 드라이버의 파일들을 여기서 찾지 않기 때문에 wsl2에서 정상동작하지 않는다. 반드시 위의 문서대로 wsl2를 고려한 CUDA toolkit을 설치해야함.(즉, conda install 로 설치하면 절대 안됨)

Installation of Linux x86 CUDA Toolkit using WSL-Ubuntu Package – Recommended (←위의 첨부URL의 option1에 해당)

 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.0/local_installers/cuda-repo-wsl-ubuntu-11-7-local_11.7.0-1_amd64.deb 
 sudo dpkg -i cuda-repo-wsl-ubuntu-11-7-local_11.7.0-1_amd64.deb 
 sudo apt-get update 
 sudo apt-get -y install cuda
  • 한 라인씩 실행시킬 것.
  • 특히, sudo apt-get update 실행 전에 GPG키를 새로 등록하길 권함.
  • 그전 명령어인 sudo dpkg -i cuda-repo-wsl-ubuntu-11-7-local_11.7.0-1_amd64.deb 를 수행하면 GPG키 등록하는 방법이 나옴. 이 명령어를 수행하고 update하길.

더보기

내 경우엔, 다음을 실행시키고 나서 apt-get update 수행함.

sudo cp /var/cuda-repo-wsl-ubuntu-11-7-local/cuda-B81839D3-keyring.gpg /usr/share/keyrings/

3. CUDA Toolkit Test

  • https://github.com/nvidia/cuda-samples 를 clone하고, cuda-samples/Samples/5_Domain_Specific/BlackScholes 에서 다음을 실행하여 동작을 확인.
make 
./BlackScholes`

4. cudnn설치

1) 다음의 site에서 cudnn을 다운로드.

CUDA Deep Neural Network

[CUDA Deep Neural Network

cuDNN provides researchers and developers with high-performance GPU acceleration.

developer.nvidia.com](https://developer.nvidia.com/cudnn)

2) 받은 파일을 압축해제

tar xvf cudnn-linux-x86_64-8.4.1.50_cuda11.6-archive.tar.xz

CUDA Toolkit 설치된 /usr/local/CUDA 에 필요한 파일들을 복사.

sudo cp cudnn-*-archive/include/cudnn*.h /usr/local/cuda/include 
sudo cp -P cudnn-*-archive/lib/libcudnn* /usr/local/cuda/lib64 
sudo chmod a+r /usr/local/cuda/include/cudnn*.h /usr/local/cuda/lib64/libcudnn*`

이 부분의 Nvidia의 공식문서는 다음 url을 확인.

https://docs.nvidia.com/deeplearning/cudnn/install-guide/index.html#installlinux-tar

[Installation Guide :: NVIDIA Deep Learning cuDNN Documentation

About this task export QNX_HOST={flash_dir}/toolchains/qnx_toolchain/host/linux/x86_64 export QNX_TARGET={flash_dir}/toolchains/qnx_toolchain/target/qnx7 export HOST_COMPILER=$QNX_HOST/usr/bin/aarch64-unknown-nto-qnx7.1.0-g++ export CUDNN_LIB_PATH=/usr/lib

docs.nvidia.com](https://docs.nvidia.com/deeplearning/cudnn/install-guide/index.html#installlinux-tar)

3) conda 가상환경 만들고 해당 가상환경으로 이동.

가상환경 생성.

conda create -n tf`or`mamba create -n tf

해당 가상환경 활성화

conda activate tf
  • 이 과정에서, mamba create -n tf opencv 같이 필요한 다른 library를 설치한다. (opencv도 같이 설치하는게 편함.)

5. tensorflow-gpu를 pip 로 설치

pip install tensorflow-gpu

Test

sample 1

from tensorflow.python.client import device_lib
import os

# 0: print all messages
# 1: filter out INFO messages
# 2: filter out INFO & WARNING messages
# 3: filter out all messages
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

print('------------------------------')
print(device_lib.list_local_devices())

더보기

[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 946308889788800499
xla_global_id: -1
, name: "/device:GPU:0"
device_type: "GPU"
memory_limit: 22676504576
locality {
  bus_id: 1
  links {
  }
}
incarnation: 11341199347828679360
physical_device_desc: "device: 0, name: NVIDIA GeForce RTX 3090, pci bus id: 0000:01:00.0, compute capability: 8.6"
xla_global_id: 416903419
]
  • GPU가 보여야 함.

sample 2

import tensorflow as tf

mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(x_train, y_train, epochs=5)

model.evaluate(x_test,  y_test, verbose=2)
  • gpu로 데이터를 이전시키는 로드로 인해 오히려 늦어짐…
    참고할 것.

같이 보면 좋은 자료들

https://www.tensorflow.org/install/pip#windows-wsl2

 

pip로 TensorFlow 설치

이 페이지는 Cloud Translation API를 통해 번역되었습니다. pip로 TensorFlow 설치 컬렉션을 사용해 정리하기 내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요. 이 가이드는 TensorFlow의 최신 안정

www.tensorflow.org

 

'Computer > CUDA' 카테고리의 다른 글

CUDA 3D, 2D data processing.  (0) 2009.12.18
CUDA Compile Environment with VS 2008  (0) 2009.12.18
'Computer/CUDA' 카테고리의 다른 글
  • CUDA 3D, 2D data processing.
  • CUDA Compile Environment with VS 2008
dsaint31x
dsaint31x
    반응형
    250x250
  • dsaint31x
    Dsaint31's blog
    dsaint31x
  • 전체
    오늘
    어제
    • 분류 전체보기 (752)
      • Private Life (13)
      • Programming (56)
        • DIP (112)
        • ML (26)
      • Computer (119)
        • CE (53)
        • ETC (33)
        • CUDA (3)
        • Blog, Markdown, Latex (4)
        • Linux (9)
      • ... (355)
        • Signals and Systems (107)
        • Math (172)
        • Linear Algebra (33)
        • Physics (42)
        • 인성세미나 (1)
      • 정리필요. (54)
        • 의료기기의 이해 (6)
        • PET, MRI and so on. (1)
        • PET Study 2009 (1)
        • 방사선 장해방호 (4)
        • 방사선 생물학 (3)
        • 방사선 계측 (9)
        • 기타 방사능관련 (3)
        • 고시 (9)
        • 정리 (18)
      • RI (0)
      • 원자력,방사능 관련법 (2)
  • 블로그 메뉴

    • Math
    • Programming
    • SS
    • DIP
  • 링크

    • Convex Optimization For All
  • 공지사항

    • Test
    • PET Study 2009
    • 기타 방사능관련.
  • 인기 글

  • 태그

    Convolution
    fourier transform
    Vector
    linear algebra
    numpy
    cv2
    Optimization
    signals_and_systems
    math
    opencv
    Programming
    SIGNAL
    function
    Term
    인허가제도
    Python
    SS
    DIP
    signal_and_system
    Probability
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
dsaint31x
[ML] WSL2 : Install Tensorflow (GPU)
상단으로

티스토리툴바