如何在Google Colab TPU上将TorchAudio与Torch Xla结合使用

时间:2020-03-17 08:39:06

标签: google-cloud-platform pytorch tpu

我正在尝试运行一个在Google TPU上使用torchaudio的pytorch脚本。为此,我在pytorch xla之后使用this notebook,更具体地说,我正在使用此代码单元来加载xla:

!pip install torchaudio
import os
assert os.environ['COLAB_TPU_ADDR'], 'Make sure to select TPU from Edit > Notebook settings > Hardware accelerator'

VERSION = "20200220"  #@param ["20200220","nightly", "xrt==1.15.0"]
!curl https://raw.githubusercontent.com/pytorch/xla/master/contrib/scripts/env-setup.py -o pytorch-xla-env-setup.py
!python pytorch-xla-env-setup.py --version $VERSION

import torch

import torchaudio

import torch_xla

但这与我需要的ERROR: torchaudio 0.4.0 has requirement torch==1.4.0, but you'll have torch 1.5.0a0+e95282a which is incompatible.

的torchaudio版本不兼容

我在任何地方都找不到如何使用pytorch xla加载torch 1.4.0的方法。

我尝试使用夜间版本的手电筒音频,但出现以下错误:

!pip install torchaudio_nightly -f https://download.pytorch.org/whl/nightly/torch_nightly.html

import os
assert os.environ['COLAB_TPU_ADDR'], 'Make sure to select TPU from Edit > Notebook settings > Hardware accelerator'

VERSION = "20200220"  #@param ["20200220","nightly", "xrt==1.15.0"]
!curl https://raw.githubusercontent.com/pytorch/xla/master/contrib/scripts/env-setup.py -o pytorch-xla-env-setup.py
!python pytorch-xla-env-setup.py --version $VERSION

import torch
import torchaudio

import torch_xla
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-2-968e9d93c06f> in <module>()
      9 
     10 import torch
---> 11 import torchaudio
     12 
     13 import torch_xla

/usr/local/lib/python3.6/dist-packages/torchaudio/__init__.py in <module>()
      3 
      4 import torch
----> 5 import _torch_sox
      6 
      7 from .version import __version__, git_version

ImportError: /usr/local/lib/python3.6/dist-packages/_torch_sox.cpython-36m-x86_64-linux-gnu.so: undefined symbol: _ZN6caffe26detail37_typeMetaDataInstance_preallocated_29E

---------------------------------------------------------------------------

那么我该如何使用xla加载pytorch的稳定版本或1.4.0版本,或者有其他解决方法?

非常感谢您的帮助!

2 个答案:

答案 0 :(得分:3)

我使用以下笔记本电脑进行了测试; Getting Started with PyTorch on Cloud TPUs

更改包含的单元格后

从:

    VERSION = "20200325"  #@param ["1.5" , "20200325", "nightly"]
    !curl https://raw.githubusercontent.com/pytorch/xla/master/contrib/scripts/env-setup.py -o pytorch-xla-env-setup.py
    !python pytorch-xla-env-setup.py --version $VERSION

收件人:

    VERSION = "20200325"  #@param ["1.5" , "20200325", "nightly"]
    !curl https://raw.githubusercontent.com/pytorch/xla/master/contrib/scripts/env-setup.py -o pytorch-xla-env-setup.py
    !pip install torchvision
    !pip install torch==1.4.0
    !pip install torchaudio==0.4.0
    %matplotlib inline
    !python pytorch-xla-env-setup.py --version $VERSION

所有单元都成功运行,并且下面的import语句未发生任何错误;

    # imports pytorch
      import torch

    # imports the torch_xla package
      import torch_xla
      import torch_xla.core.xla_model as xm

答案 1 :(得分:0)

!pip uninstall torch -y
!pip install torch==1.6 

如果它看起来像..“警告:root:TPU 已成功启动,版本为 pytorch-1.6”

享受……!!