반응형

WSL2에 CUDA 사용하도록 Tensorflow 설치하는 방법을 다룹니다.

 

2022. 11. 26  최초작성

2023. 2. 12 tensorflow-gpu 대신에 tensorflow 설치

2023. 4. 3  gpu 사용하기 위한 확인사항 추가

2024. 1. 9

2024. 2. 4 텐서플로우 버전 선택 정리

2024. 3. 11   tensorflow[and-cuda] 설치시 발생한 문제 해결방법 추가

2024. 3. 13

2024. 3. 14

2024. 3. 16 NVIDIA 그래픽 카드 드라이버 설치 방법 분리




Tensorflow에서 GPU를 사용하려면 다음 버전 이상으로 윈도우를 업데이트 해야 합니다. 윈도우 11은 아래 언급한 버전보다 상위 버전인것으로 보입니다. 

 

Windows 10 19044 이상(64비트). 이는 Windows 10 버전 21H2, 2021년 11월 업데이트에 해당합니다.

( https://www.tensorflow.org/install/pip?hl=ko#windows-wsl2 )




WSL 설치

 

WSL2를 설치하는 방법은 다음 링크를 참고하세요.

 

WSL2를 설치하여 Ubuntu 22.04 사용하는 방법

https://webnautes.tistory.com/1847





Miniconda 개발 환경 설치

다음 포스트를 참고하여 Miniconda 개발환경을 설치하세요. Ubuntu 설명을 따라하면 됩니다.  파이썬 프로젝트 별로 패키지를 따로 관리할 수 있습니다.  주의할 점은 Miniconda를 설치 후, WSL 창을 새로 열어야 conda 명령이 가능해집니다.

 

Visual Studio Code와 Miniconda를 사용한 Python 개발 환경 만들기( Windows, Ubuntu, WSL2)

https://webnautes.tistory.com/1842 



NVIDIA 그래픽 카드 드라이버 설치

 

다음 포스트를 참고하여 최신 버전 NVIDIA 그래픽 카드 드라이버를 설치하는 것을 권장합니다. 최신 버전 Tensorflow에서 최신 버전 CUDA를 요구하는 경우에 최신 버전의 NVIDIA 그래픽 카드 드라이버 설치가 필요하기 때문입니다. 

 

특별한 경우를 제외하고는 CUDA를 따로 설치할 필요는 없습니다. 

 

Windows에 NVIDIA 그래픽 카드 드라이버 설치하기

https://webnautes.tistory.com/2292 




Tensorflow 설치

 

tensorflow 2.14 이상을 사용할거라면 방법 1을 사용하고 tensorflow 2.13 이하를 사용할 거라면 방법 2를 진행하세요.

 

방법 1에는 시행착오한 진행사항도 추가되어있기 때문에 무작정 따라하지 말고 진행하기 전에  한번 읽어보고 하는게 좋을듯합니다. 

 

NVIDIA 사이트에서 다운로드 하는 속도가 느린 것으로 예상되어(보통 느립니다.)  방법1에만 miniconda를 사용하여 진행하는 방법을 추가했습니다. 방법2 진행시에는 방법1을 참고하세요 .



conda를 사용하는 방법은 다음 포스트도 참고하세요.

 

miniconda/anaconda에서 conda 사용하는 방법

https://webnautes.tistory.com/2276 



방법 1. CUDA 설치 필요없는 경우

 

먼저 간단히 설명하고 뒤에서 miniconda로 진행하는 방법을 설명합니다.

 

tensorflow[and-cuda] 패키지를 설치하면 CUDA를 따로 설치할 필요가 없습니다. Tensorflow[and-cuda] 2.14의 경우엔 CUDA 11 파이썬 패키지가 같이 설치되며 Tensorflow[and-cuda] 2.15의 경우엔 CUDA 12 파이썬 패키지가 같이 설치되었습니다.

 

Tensorflow[and-cuda] 2.14 이하에서는 CUDA 11, Tensorflow[and-cuda] 2.15 이상에서는 CUDA 12가 사용되는 것으로 보입니다. 

 

각각 다음 명령으로 설치 가능합니다. 

 

$ pip install tensorflow[and-cuda]==2.15

$ pip install tensorflow[and-cuda]==2.14



Tensorflow 2.13 이하 버전에서는 Tensorflow[and-cuda]를 지원하지 않기 때문에 설치하는 도중 다음과 같은 메시지가 보입니다.

WARNING: tensorflow 2.13.0 does not provide the extra 'and-cuda'



다음 명령으로 확인시 CUDA 같이 설치되는 tensorflow[and-cuda] 패키지 버전 리스트와 다르니 유의하세요.

 

$ pip install tensorflow[and-cuda]==

ERROR: Could not find a version that satisfies the requirement tensorflow== (from versions: 2.12.0rc0, 2.12.0rc1, 2.12.0, 2.12.1, 2.13.0rc0, 2.13.0rc1, 2.13.0rc2, 2.13.0, 2.13.1, 2.14.0rc0, 2.14.0rc1, 2.14.0, 2.14.1, 2.15.0rc0, 2.15.0rc1, 2.15.0, 2.15.0.post1)

ERROR: No matching distribution found for tensorflow==



다음 명령으로 Tensorflow에서 GPU(CUDA)를 사용할 수 있는지 확인합니다.

 

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



명령 실행결과 맨 마지막에 다음처럼 출력되어야 GPU(CUDA)를 사용할 수 있는 상태입니다.

 

[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]



GPU(CUDA)를 사용할 수 없다면 다음처럼 출력됩니다.

 

[ ]




이제 miniconda를 사용하여 진행해봅니다. 



Python 3.10을 사용하는 tensorflow 이름의 가상환경을 생성합니다. Python 버전에 따라 사용가능한  tensorflow[and-cuda] 패키지의 버전에 차이가 있었습니다. Python 3.12의 경우엔 2.16 버전대만 설치가능합니다. Ubuntu에서 테스트시 Python 3.9를 사용해야 정상적으로 Tensorflow 2.15설치하는 것이 진행가능했기 때문에 WSL2 문서도 수정합니다.  나중에는 글작성 시점과 상황이 다를 수 있습니다.

 

(base) webnautes@webnautes-laptop:~$ conda create -n tensorflow python=3.9



2024. 3. 13  기준으로 Python 버전을 3.10으로 한 경우 tensorflow 설치하는 과정에서 다음과 같은 에러가 발생했습니다.

ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
tensorflow-gpu 2.11.0 requires keras<2.12,>=2.11.0, but you have keras 2.15.0 which is incompatible.
tensorflow-gpu 2.11.0 requires protobuf<3.20,>=3.9.2, but you have protobuf 4.25.3 which is incompatible.
tensorflow-gpu 2.11.0 requires tensorboard<2.12,>=2.11, but you have tensorboard 2.15.2 which is incompatible.
tensorflow-gpu 2.11.0 requires tensorflow-estimator<2.12,>=2.11.0, but you have tensorflow-estimator 2.15.0 which is incompatible.





가상환경을 활성화합니다.

 

(base) webnautes@webnautes-laptop:~$ conda activate tensorflow

(tensorflow) webnautes@webnautes-laptop:~$



CUDA가 포함된 Tensorflow 패키지를 설치합니다. 글 작성 시점(2024. 3. 11)에는 CUDA 12.3, cudnn 8.9.7, Tensorflow 2.16.1이 설치됩니다.  

 

(tensorflow) webnautes@webnautes-laptop:~$ pip install tensorflow[and-cuda]



Tensorflow에서 GPU(CUDA)를 사용할 수 있는지 확인합니다. 

 

(tensorflow) webnautes@webnautes-laptop:~$ python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

2024-03-11 21:01:47.784217: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.

To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.

2024-03-11 21:01:49.031185: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT

2024-03-11 21:01:50.024652: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:984] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node

Your kernel may have been built without NUMA support.

2024-03-11 21:01:50.025062: W tensorflow/core/common_runtime/gpu/gpu_device.cc:2251] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.

Skipping registering GPU devices...

[]



위 출력결과 마지막에 []로 표시된 것은 GPU를 Tensorflow에서 사용할 수 없다는 의미입니다.  예상되는 원인은 테스트에 사용한 노트북의 그래픽카드에서 최대 지원하는 CUDA 버전이 12.1인데(아래 스크린샷 오른쪽위)  Tensorflow와 같이 설치된 CUDA 버전이 12.3이라서 문제가 발생한 것으로 짐작됩니다. 메시지 중간에도 Cannot dlopen some GPU libraries. 라고 GPU 라이브러리를 로드할 수 없다는 에러가 보입니다. 

 



 tensorflow[and-cuda]의 버전을 2.15로 낮추어서 설치해봅니다.  기존에 설치된 tensorflow를 삭제하지 않고 바로 진행해도 됩니다. 

 

(tensorflow) webnautes@webnautes-laptop:~$ pip install tensorflow[and-cuda]==2.15



진행 중 다음과 같은 에러가 발생했습니다. 

 

ERROR: Could not find a version that satisfies the requirement tensorrt-libs==8.6.1; extra == "and-cuda" (from tensorflow[and-cuda]) (from versions: 9.0.0.post11.dev1, 9.0.0.post12.dev1, 9.0.1.post11.dev4, 9.0.1.post12.dev4, 9.1.0.post11.dev4, 9.1.0.post12.dev4, 9.2.0.post11.dev5, 9.2.0.post12.dev5, 9.3.0.post11.dev1, 9.3.0.post12.dev1)

ERROR: No matching distribution found for tensorrt-libs==8.6.1; extra == "and-cuda"



다음 명령으로 설치를 문제없이  진행할 수 있습니다.  

(참고 : https://github.com/tensorflow/tensorflow/issues/61986#issuecomment-1811284728  )



(tensorflow) webnautes@webnautes-laptop:~$ python -m pip install "tensorflow[and-cuda]==2.15" --extra-index-url https://pypi.nvidia.com



Tensorflow에서 GPU(CUDA)를 사용할 수 있는지 확인합니다. 

 

(tensorflow) webnautes@webnautes-laptop:~$ python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

2024-03-11 21:23:28.563888: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:9261] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered

2024-03-11 21:23:28.564008: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:607] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered

2024-03-11 21:23:28.566230: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1515] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered

2024-03-11 21:23:28.579733: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.

To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.

2024-03-11 21:23:30.997365: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT

2024-03-11 21:23:32.851668: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node

Your kernel may have been built without NUMA support.

2024-03-11 21:23:32.938596: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node

Your kernel may have been built without NUMA support.

2024-03-11 21:23:32.938942: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node

Your kernel may have been built without NUMA support.

[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]



실행 결과 마지막에 [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')] 처럼 보이면 Tensorflow에서 GPU를 사용할 수 있습니다. 



방법 2. CUDA 설치 필요한 경우




이 경우 기존 Tensorflow 패키지 이름으로 설치를 진행하며 따로 설치되어있는 CUDA를 사용합니다. 



https://www.tensorflow.org/install/source#gpu 에 따르면 GPU를 사용하는 Tensorflow 2.12, Tensorflow 2.13을 설치하려면 CUDA는 11.8, cuDNN은 8.6이어야 합니다.  

 



다음 포스트를 참고하여 WSL2에 CUDA를 설치하세요.

 

WSL2에 CUDA 설치하는 방법

https://webnautes.tistory.com/1848




CUDA 11.8과 cuDNN 8.6을 설치후 Tensorflow 버전별로 GPU(CUDA)를 사용할 수 있는지 체크해봤습니다. 

 

$ pip install tensorflow==2.12 설치후  GPU 사용가능

$ pip install tensorflow==2.13 설치후 GPU 사용가능

$ pip install tensorflow==2.14 설치후 GPU 사용가능

$ pip install tensorflow==2.15  설치후 GPU 사용불가능, CUDA 12.2를 요구하기 때문으로 보입니다.  



Tensorflow에서 GPU(CUDA)를 사용할 수 있는지 확인합니다.

 

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



명령 실행결과 맨 마지막에 다음처럼 출력되어야 GPU(CUDA)를 사용할 수 있는 상태입니다.

 

[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]



GPU(CUDA)를 사용할 수 없다면 다음처럼 출력됩니다.

 

[ ]





반응형

문제 발생시 지나치지 마시고 댓글 남겨주시면 가능한 빨리 답장드립니다.

도움이 되셨다면 토스아이디로 후원해주세요.
https://toss.me/momo2024


제가 쓴 책도 한번 검토해보세요 ^^

+ Recent posts