7

Question

Please confirm that to use both CPU and GPU with TensorFlow after 1.15, install tensorflow package is enough and tensorflow-gpu is no more required.

Background

Still see articles stating to install tensorflow-gpu e.g. pip install tensorflow-gpu==2.2.0 and the PyPi repository for tensorflow-gpu package is active with the latest tensorflow-gpu 2.4.1.

The Annaconda document also refers to tensorflow-gpu package still.

TensorFlow is a general machine learning library, but most popular for deep learning applications. There are three supported variants of the tensorflow package in Anaconda, one of which is the NVIDIA GPU version. This is selected by installing the meta-package tensorflow-gpu:

However, according to the TensorFlow v2.4.1 (as of Apr 2021) Core document GPU support - Older versions of TensorFlow

For releases 1.15 and older, CPU and GPU packages are separate:

pip install tensorflow==1.15      # CPU
pip install tensorflow-gpu==1.15  # GPU

According to the TensorFlow Core Guide Use a GPU.

TensorFlow code, and tf.keras models will transparently run on a single GPU with no code changes required.

According to Difference between installation libraries of TensorFlow GPU vs CPU.

Just a quick (unnecessary?) note... from TensorFlow 2.0 onwards these are not separated, and you simply install tensorflow (as this includes GPU support if you have an appropriate card/CUDA installed).

Hence would like to have a definite confirmation that the tensorflow-gpu package would be for convenience (legacy script which has specified tensorflow-gpu, etc) only and no more required. There is no difference between tensorflow and tensorflow-gpu packages now.

mon
  • 18,789
  • 22
  • 112
  • 205

1 Answers1

9

It's reasonable to get confused here about the package naming. However, here is my understanding. For tf 1.15 or older, the CPU and GPU packages are separate:

pip install tensorflow==1.15      # CPU
pip install tensorflow-gpu==1.15  # GPU

So, if I want to work entirely on the CPU version of tf, I would go with the first command and otherwise, if I want to work entirely on the GPU version of tf, I would go with the second command.


Now, in tf 2.0 or above, we only need one command that will conveniently work on both hardware. So, in the CPU and GPU based system, we need the same command to install tf, and that is:

pip install tensorflow

Now, we can test it on a CPU based system ( no GPU)

import tensorflow as tf 
print(tf.__version__)

print('1: ', tf.config.list_physical_devices('GPU'))
print('2: ', tf.test.is_built_with_cuda)
print('3: ', tf.test.gpu_device_name())
print('4: ', tf.config.get_visible_devices())

2.4.1
1:  []
2:  <function is_built_with_cuda at 0x7f2ce91415f0>
3:  
4:  [PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU')]

or also test it on a CPU based system ( with GPU)

2.4.1
1:  [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
2:  <function is_built_with_cuda at 0x7fb6affd0560>
3:  /device:GPU:0
4:  [PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU'),
     PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]

So, as you can see this is just a single command for both CPU and GPU cases. Hope it's clear now more. But until now (in tf > = 2) we can also use -gpu / -cpu postfix while installing tf that delicately use for GPU / CPU respectively.

!pip install tensorflow-gpu 
....
Installing collected packages: tensorflow-gpu
Successfully installed tensorflow-gpu-2.4.1

# -------------------------------------------------------------

!pip install tensorflow-cpu
....
Installing collected packages: tensorflow-cpu
Successfully installed tensorflow-cpu-2.4.1

Check: Similar response from tf-team.

Innat
  • 16,113
  • 6
  • 53
  • 101
  • Thanks for the answer and sorry for the late reply. I am afraid I am not still clear if the answer is yes to "There is no difference between tensorflow and tensorflow-gpu packages now (after 1.15)". Is there no difference anymore and there is no need for "tensorflow-gpu" for anyone? – mon Apr 19 '21 at 21:55
  • The exact answer to your last question including `tensorflow-cpu`, I don't have it. To the best of my knowledge, there is no difference and we don't need `tensorflow-gpu/cpu` (after `1.15`). To be honest, I never bother with this postfix term of `cpu/gpu`, I simply go for `tensorflow`. – Innat Apr 20 '21 at 02:56
  • 4
    I raised an issue regarding this to confirm, the [response](https://github.com/tensorflow/tensorflow/issues/48640#issuecomment-825064981) I think is convincing. – Innat Apr 22 '21 at 23:48