Calling cuda() with async results in SyntaxError

霸气de小男生 提交于 2021-02-04 17:42:46

问题


I'm trying to run this PyTorch code:

for i, (input, target) in enumerate(train_loader):

    input = input.float().cuda(async=True)
    target = target.cuda(async=True)
    input_var = torch.autograd.Variable(input)
    target_var = torch.autograd.Variable(target)

    output = model(input_var)

But when I try I am getting this error message:

input = input.float().cuda(async=True)
                               ^
SyntaxError: invalid syntax
Process finished with exit code 1

What am I doing wrong? I already installed cuda.


回答1:


Your code does not work because:

  • async is a reserved keyword in python which cannot be used in that way, that is why you get the SyntaxError

  • cuda() no longer has an argument async. The constructor looks like this:

cuda(device=None, non_blocking=False) → Tensor

  • Previously there was an argument async but this replaced by non_blocking as async became a reserved keyword in Python 3.7.
    https://github.com/pluskid/fitting-random-labels/pull/5

Use instead non_blocking:

The argument non_blocking has the same effect as async previously had:

  • non_blocking (bool):
    If True and the source is in pinned memory, the copy will be asynchronous with respect to the host. Otherwise, the argument has no effect. Default: False.

    https://pytorch.org/docs/stable/tensors.html#torch.Tensor.cuda


As an add-on: If you are interested in what async is actually used for you can take a look here: https://www.python.org/dev/peps/pep-0492/#new-syntax




回答2:


There was an async parameter but it was deprecated now, because async became a reserved word in Python 3.7. Details are included in this issue rename .cuda(async=..) parameters. You can use non_blocking as an alternative.



来源:https://stackoverflow.com/questions/53201534/calling-cuda-with-async-results-in-syntaxerror

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!