torch.autograd.Variable(target.cuda(async=True)) ---用async调用cuda()出现 SyntaxError: invalid syntax

 又比如:

./projects/personachat/persona_seq2seq.py:2220:43: E999 SyntaxError: invalid syntax
                    self.zs.copy_(zs, async=True)
                                          ^
./parlai/agents/vsepp_caption/vsepp_caption.py:160:38: E999 SyntaxError: invalid syntax
            images = images.cuda(async=True)
                                     ^
./parlai/agents/drqa/model.py:89:43: E999 SyntaxError: invalid syntax
            inputs = [Variable(e.cuda(async=True)) for e in ex[:5]]
                                          ^
3     E999 SyntaxError: invalid syntax

原因:

  • python3.7已经移除了async关键字,而用non_blocking代替。(导致apache-airflow也出了问题)
  • cuda() 本身也没有async. 构造函数如下:

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

参考:

https://github.com/facebookresearch/ParlAI/issues/1141

https://stackoverflow.com/questions/56085657/pytorch-nccl-error-unhandled-system-error-during-backprop

**kwargs: For compatibility, may contain the key ``async`` in place of the ``non_blocking`` argument. The ``async`` arg is deprecated.

解决办法:

  • 直接用cuda()
  • async_ 代替
  • 用non_blocking代替。ps:两个关键字(device and non_blocking)可以用来调用cuda( ) ,device是选取显卡序列号的。
    • 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.
    • 如果为True且源位于锁定内存中,则副本将与主机异步。 否则,参数无效。 默认值:False。Always good to take a look at the Documentation:
  • 更新 Pipenv。 pip install pipenv  https://github.com/quark0/darts/pull/25 

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

  • 18
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值