2

I can't get Tensorflow 2 to use my GPUs under WSL2. I am aware of this question, but GPU support is now (supposedly) no longer experimental.

Windows is on the required 21H2 version, which should support the WSL2 GPU connection.

Windows 10 Pro, 21H2, build 19044.1706

The PC has two GPUs:

GPU 0: NVIDIA GeForce GTX 1080 Ti (UUID: GPU-19c8549a-4b8d-5d70-456b-776ceece4b0f)
GPU 1: NVIDIA GeForce GTX 1080 Ti (UUID: GPU-2a946756-0472-fb90-f1a4-b40cce1bba4f)

I had installed Ubuntu under WSL2 some time ago:

PS C:\Users\jem-m> wsl --status
Default Distribution: Ubuntu-20.04
Default Version: 2
...
Kernel version: 5.10.16

In the Windows PowerShell, I can run nvidia-smi.exe, which gives me

PS C:\Users\jem-m> nvidia-smi.exe
Mon May 16 18:13:27 2022
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 512.77       Driver Version: 512.77       CUDA Version: 11.6     |
|-------------------------------+----------------------+----------------------+
| GPU  Name            TCC/WDDM | Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  NVIDIA GeForce ... WDDM  | 00000000:08:00.0  On |                  N/A |
| 23%   31C    P8    10W / 250W |    753MiB / 11264MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
|   1  NVIDIA GeForce ... WDDM  | 00000000:41:00.0 Off |                  N/A |
| 23%   31C    P8    12W / 250W |    753MiB / 11264MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+

while the nvidia-smi in the WSL2 Ubuntu shell gives

(testenv) jem-mosig:~/ $ nvidia-smi                                   [17:48:30]
Mon May 16 17:49:53 2022
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 510.68.02    Driver Version: 512.77       CUDA Version: 11.6     |
|-------------------------------+----------------------+----------------------+
| 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 GeForce ...  On   | 00000000:08:00.0  On |                  N/A |
| 23%   34C    P8    10W / 250W |    784MiB / 11264MiB |      8%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
|   1  NVIDIA GeForce ...  On   | 00000000:41:00.0 Off |                  N/A |
| 23%   34C    P8    13W / 250W |    784MiB / 11264MiB |     12%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+

Note the same driver and CUDA version, but different NVIDIA-SMI version.

This seems to indicate that CUDA works under WSL2 as it is supposed to. But when I run

import tensorflow as tf

print(tf.config.list_physical_devices('GPU'))

# 2022-05-17 12:13:05.016328: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set
# []

in python inside WSL2 I get [], so no GPU is recognized by Tensorflow. This is Python 3.8.0 and Tensorflow 2.4.1 freshly installed in a new Miniconda environment inside Ubuntu WSL2. I don't know what is going wrong. Any suggestions?

Addendum

I don't get any error messages when importing Tensorflow. But some warnings are produced when working with it. E.g., when I run

import tensorflow as tf

print(tf.__version__)
model = tf.keras.Sequential([tf.keras.layers.Dense(3)])
model.compile(loss="mse")
print(model.predict([[0.]]))

I get

2.4.1
2022-05-17 10:38:28.792209: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set
2022-05-17 10:38:28.792411: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  SSE4.1 SSE4.2 AVX AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2022-05-17 10:38:28.794356: I tensorflow/core/common_runtime/process_util.cc:146] Creating new thread pool with default inter op setting: 2. Tune using inter_op_parallelism_threads for best performance.
2022-05-17 10:38:28.853557: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:116] None of the MLIR optimization passes are enabled (registered 2)
2022-05-17 10:38:28.860126: I tensorflow/core/platform/profile_utils/cpu_utils.cc:112] CPU Frequency: 3792975000 Hz
[[0. 0. 0.]]

These don't seem to be GPU related, though.

JEM_Mosig
  • 246
  • 1
  • 13
  • 2
    When you import TensorFlow, a bunch of messages are printed in the terminal, those messages tell you what is missing to detect your GPUs, you should include them in your question. – Dr. Snoopy May 16 '22 at 18:45
  • I don't get any messages upon `import tensorflow as tf`. But some warnings during use. I'll add those to the question. – JEM_Mosig May 17 '22 at 08:33
  • 1
    Are you sure you installed a CUDA-enabled version of TensorFlow? If there are no CUDA messages, you probably installed a CPU only version of TensorFlow. – Dr. Snoopy May 17 '22 at 09:21
  • This is only for old TF 1 versions. In TF 2 GPU support should always come with `pip install tensorflow`: https://www.tensorflow.org/install/gpu#pip_package Or do I have to do something else? – JEM_Mosig May 17 '22 at 09:26
  • well you should know what you installed, maybe the messages are somewhere else, I don't know how WSL works to tell you about what you installed or not. – Dr. Snoopy May 17 '22 at 09:31
  • For example if you use jupyter notebook, sometimes the messages are printed in the jupyter terminal and not in the website you usually see. – Dr. Snoopy May 17 '22 at 09:34
  • I installed TF2, so it should have GPU support. I run everything directly in the terminal. No notebooks or anything. TF just doesn't complain about anything GPU related, as if there was no GPU, and also prints an empty list for `tf.config.list_physical_devices('GPU')`. – JEM_Mosig May 17 '22 at 10:10
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/244800/discussion-between-jem-mosig-and-dr-snoopy). – JEM_Mosig May 17 '22 at 10:18

1 Answers1

1

Dr. Snoopy got me onto the right track: Despite the fact that the TF website says that

The TensorFlow pip package includes GPU support for CUDA®-enabled cards

, I still needed to run conda install tensorflow-gpu and it worked! Now

import tensorflow as tf
from tensorflow.python.client import device_lib

print("devices: ", [d.name for d in device_lib.list_local_devices()])
print("GPUs:    ", tf.config.list_physical_devices('GPU'))
print("TF v.:   ", tf.__version__)

gives lots of debug messages and

devices:  ['/device:CPU:0', '/device:GPU:0', '/device:GPU:1']
GPUs:     [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU'), PhysicalDevice(name='/physical_device:GPU:1', device_type='GPU')]
TF v.:    2.4.1
JEM_Mosig
  • 246
  • 1
  • 13