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

use 'cpu' instead of None for Iterator #745

Merged
merged 9 commits into from
May 3, 2020
Merged

use 'cpu' instead of None for Iterator #745

merged 9 commits into from
May 3, 2020

Conversation

avinashsai
Copy link
Contributor

Fixes #376
Points default device to torch cpu instead of None if no device is specified to be compatible with torch device types

@@ -64,7 +65,7 @@ def __init__(self, dataset, batch_size, sort_key=None, device=None,
logger.warning("The `device` argument should be set by using `torch.device`"
+ " or passing a string as an argument. This behavior will be"
+ " deprecated soon and currently defaults to cpu.")
device = None
device = torch.device('cpu')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense that the default device is cpu if device is None. But if device is provided in the init func, you still force to use cpu. Seem not correct. And double check the doc explains this clearly.

@avinashsai
Copy link
Contributor Author

Should I change accordingly or leave it as it is??

@zhangguanheng66
Copy link
Contributor

Should I change accordingly or leave it as it is??

If device is provided in the __init__ func, it should be assigned to device. Otherwise, the device is set to the default, which is cpu

@avinashsai
Copy link
Contributor Author

I will change accordingly

@avinashsai
Copy link
Contributor Author

Made changes as per your comments

@@ -65,7 +65,8 @@ def __init__(self, dataset, batch_size, sort_key=None, device=None,
logger.warning("The `device` argument should be set by using `torch.device`"
+ " or passing a string as an argument. This behavior will be"
+ " deprecated soon and currently defaults to cpu.")
device = torch.device('cpu')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not correct. The device is under if isinstance statement. For None device, it's no way to enter it.

@zhangguanheng66
Copy link
Contributor

You have to rebase to the master branch

@avinashsai
Copy link
Contributor Author

changed as per your comments

@zhangguanheng66
Copy link
Contributor

The CI tests fail.

@avinashsai
Copy link
Contributor Author

i fixed indentation issue

@zhangguanheng66
Copy link
Contributor

The circleci tests need to pass. Please check the log since the errors are relevant.

@avinashsai
Copy link
Contributor Author

Fixed

@zhangguanheng66
Copy link
Contributor

nit: could you remove the parentheses in Python conditional?

@avinashsai
Copy link
Contributor Author

Done!!

@@ -65,6 +66,10 @@ def __init__(self, dataset, batch_size, sort_key=None, device=None,
+ " or passing a string as an argument. This behavior will be"
+ " deprecated soon and currently defaults to cpu.")
device = None

if device is None:
device = torch.device('cpu')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the doc, device could also be string. Should we also cover that as:

if device is None:
    device = torch.device('cpu')
elif isinstance(device, str):
    device = torch.device(device)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also like to add one more thing. How about adding a warning if cuda is available and user still preferred 'cpu'

if torch.cuda.is_available():
    if device is None or device=='cpu':
        logger.warning("you have gpu available but preferred device as cpu")
if device is None:
    device = torch.device('cpu')
elif isinstance(device, str):
    device = torch.device(device)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't assume that GPU is preferred if it is available. This might be used in some unexpected context.

Alongside updating the default behavior we should also update the doc string to document this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes users forget to pass 'gpu' parameter even if gpu is available. So, if we send a warning, users can rectify it.

Copy link
Contributor

@cpuhrsch cpuhrsch May 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but it adds verbosity we commonly don't have. I suggest we do this in a separate PR where we address logging issues in general. If we were to do it for this operator we should also do it for the other ones and that'll require some strategy.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok then, for now i will ignore and add only required ones.

@avinashsai
Copy link
Contributor Author

@zhangguanheng66 please merge this

@zhangguanheng66 zhangguanheng66 merged commit 85be3b7 into pytorch:master May 3, 2020
@cpuhrsch
Copy link
Contributor

cpuhrsch commented May 5, 2020

@avinashsai - The docstring wasn't updated. Please send a follow-up PR.

@cpuhrsch
Copy link
Contributor

cpuhrsch commented May 5, 2020

 device (str or `torch.device`): A string or instance of `torch.device`
            specifying which device the Variables are going to be created on.	            specifying which device the Variables are going to be created on.
            If left as default, the tensors will be created on cpu. Default: None.	            If left as default, the tensors will be created on cpu. Default: None.

but it should be Default: cpu or such.

@avinashsai
Copy link
Contributor Author

@cpuhrsch where should I edit the docs? Iam not able to find the location.

@zhangguanheng66
Copy link
Contributor

The default is None and None will be converted to cpu device.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use "cpu" instead of None for representing device
3 participants