Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TF 2.0] How to globally force CPU? #31135

Closed
jbgh2 opened this issue Jul 29, 2019 · 9 comments
Closed

[TF 2.0] How to globally force CPU? #31135

jbgh2 opened this issue Jul 29, 2019 · 9 comments
Assignees
Labels
comp:gpu GPU related issues stat:awaiting response Status - Awaiting response from author TF 2.0 Issues relating to TensorFlow 2.0 type:docs-bug Document issues

Comments

@jbgh2
Copy link

jbgh2 commented Jul 29, 2019

In TF 1.x it was possible to force CPU only by using:

config = tf.ConfigProto(device_count = {'GPU': 0})

However, ConfigProto doesn't exist in TF 2.0 and changing a OS environment variable seems very clunky.

What's the TF 2.0 way of doing this?

@jbgh2 jbgh2 added the type:docs-bug Document issues label Jul 29, 2019
@gadagashwini-zz gadagashwini-zz self-assigned this Jul 30, 2019
@gadagashwini-zz gadagashwini-zz added the TF 2.0 Issues relating to TensorFlow 2.0 label Jul 30, 2019
@pandrey-fr
Copy link
Contributor

There might be a better way, but you can use tf.config.experimental.set_visible_devices([], 'GPU') to hide any GPU (they can still be listed using tf.config.experimental.list_physical_devices('GPU') and restored using the first function with different arguments, but will no longer appear when running tf.config.experimental.list_logical_devices('GPU') and should therefore not be used to place operations).

@ymodak
Copy link
Contributor

ymodak commented Jul 30, 2019

This can help you set all operations on CPU;

import tensorflow as tf
print(tf.__version__)

# Set CPU as available physical device
my_devices = tf.config.experimental.list_physical_devices(device_type='CPU')
tf.config.experimental.set_visible_devices(devices= my_devices, device_type='CPU')

# To find out which devices your operations and tensors are assigned to
tf.debugging.set_log_device_placement(True)

# Create some tensors and perform an operation
a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
c = tf.matmul(a, b)

print(c)

Output

2.0.0-beta1
Executing op MatMul in device /job:localhost/replica:0/task:0/device:CPU:0
tf.Tensor(
[[22. 28.]
 [49. 64.]], shape=(2, 2), dtype=float32)

@ymodak ymodak added comp:gpu GPU related issues stat:awaiting response Status - Awaiting response from author labels Jul 30, 2019
@jbgh2
Copy link
Author

jbgh2 commented Aug 1, 2019

That works, thank you.

@jbgh2 jbgh2 closed this as completed Aug 1, 2019
@ymodak
Copy link
Contributor

ymodak commented Aug 1, 2019

Are you satisfied with the resolution of your issue?
Yes
No

@henrysky
Copy link

henrysky commented Jan 8, 2020

for anyone who is using tf 2.1, the above comment does not seems to work.

I use tf.config.set_visible_devices([], 'GPU') seems to do the job correctly

@pandrey-fr
Copy link
Contributor

@henrysky this is indeed part of the breaking changes introduced in 2.1 (as documented in the release notes), which promoted some former experimental functions to the public scope; thank you for pointing the new correct syntax out :)

@JackXueIndiana
Copy link

tf.config.set_visible_devices([], 'GPU')
Works for tf 2.2

@gregoruar
Copy link

I managed to limit cpus by using psutils lib.
I provided this the beginning of the function

pid = psutil.Process(os.getpid())
pid.cpu_affinity([0, 1])

The later call of model.fit utilized exactly 2 cpus

@marcusobrien
Copy link

I also wanted to use TF 2.10 with CPU only as it defaults to using my GPU. However the above can not be used in Jupyter Labs due to this error message

"RuntimeError: Visible devices cannot be modified after being initialized"

So is there any other way to force tensorflow to use CPU to perform all calculations ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:gpu GPU related issues stat:awaiting response Status - Awaiting response from author TF 2.0 Issues relating to TensorFlow 2.0 type:docs-bug Document issues
Projects
None yet
Development

No branches or pull requests

8 participants