如何在 google colab tpu 上使用 torchaudio 和 torch xla

how to use torchaudio with torch xla on google colab tpu

我正在尝试 运行 一个在 google TPU 上使用 torchaudio 的 pytorch 脚本。为此,我使用 pytorch xla following 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

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

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

我尝试使用 torch audio 的夜间版本,但出现如下错误:

!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 加载稳定版或 1.4.0 版的 pytorch,或者对于这种情况还有其他解决方法吗?

非常感谢您的帮助!

我使用下面的笔记本进行了测试; 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

所有单元格 运行 成功,下面的导入语句没有抛出任何错误;

    # imports pytorch
      import torch

    # imports the torch_xla package
      import torch_xla
      import torch_xla.core.xla_model as xm
!pip uninstall torch -y
!pip install torch==1.6 

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

享受...!!