Calling cuda() with async results in SyntaxError(使用异步调用 cuda() 会导致 SyntaxError)
问题描述
我正在尝试运行此 PyTorch 代码:
I'm trying to run this PyTorch code:
但是当我尝试时,我收到此错误消息:
But when I try I am getting this error message:
我做错了什么?我已经安装了 cuda.
What am I doing wrong? I already installed cuda.
推荐答案
您的代码不起作用,因为:
async
是 python 中的保留关键字,不能以这种方式使用,这就是为什么你得到SyntaxError
async
is a reserved keyword in python which cannot be used in that way, that is why you get theSyntaxError
cuda()
不再 有一个参数 async
.构造函数看起来像这样:
cuda()
no longer has an argument async
. The constructor looks like this:
cuda(device=None, non_blocking=False) → 张量
- 以前有一个参数
async
但这被non_blocking
代替,因为async
成为 Python 3.7 中的保留关键字.
https://github.com/pluskid/fit-random-labels/拉/5 - Previously there was an argument
async
but this replaced bynon_blocking
asasync
became a reserved keyword in Python 3.7.
https://github.com/pluskid/fitting-random-labels/pull/5 non_blocking
(bool):
如果True
并且源在固定内存中,则复制将与主机异步.否则,论证没有效果.默认值:False
.
https://pytorch.org/docs/stable/tensors.html#torch.Tensor.cudanon_blocking
(bool):
IfTrue
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
参数 non_blocking
与之前的 async
具有相同的效果:
The argument non_blocking
has the same effect as async
previously had:
<小时>
作为附加组件:如果您对 async
的实际用途感兴趣,可以查看这里:https://www.python.org/dev/peps/pep-0492/#新语法
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
这篇关于使用异步调用 cuda() 会导致 SyntaxError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!