【问题标题】:ImportError after installing torchtext 0.11.0 with conda使用 conda 安装 torchtext 0.11.0 后出现 ImportError
【发布时间】:2022-01-03 00:14:39
【问题描述】:

我已经使用 conda 安装了 pytorch 版本 1.10.0 以及 torchtexttorchvisiontorchaudio。我的 PyTorch 是纯 cpu 的,我已经尝试使用 conda install pytorch-mutex -c pytorchconda install pytorch cpuonly -c pytorch 来安装 cpuonly 版本,两者都会产生相同的错误,我将在以下几行中描述。

我还在 conda 中安装了pytorch-lightning,在环境中通过 pip 安装了jsonargparse[summaries

我已经编写了这段代码来查看LightningCLI 是否有效。

# script.py
import torch 
import pytorch_lightning as pl

class BoringModel(LightningModule):
    def __init__(self):
        super().__init__()
        self.layer = torch.nn.Linear(32, 2)

    def forward(self, x):
        return self.layer(x)

    def training_step(self, batch, batch_idx):
        loss = self(batch).sum()
        self.log("train_loss", loss)
        return {"loss": loss}

    def validation_step(self, batch, batch_idx):
        loss = self(batch).sum()
        self.log("valid_loss", loss)

    def test_step(self, batch, batch_idx):
        loss = self(batch).sum()
        self.log("test_loss", loss)

    def configure_optimizers(self):
        return torch.optim.SGD(self.layer.parameters(), lr=0.1)

cli = LightningCLI(BoringModel)

但是当我使用python -m script fit --print_config 运行它时,我收到以下错误:

ImportError: /home/farhood/miniconda3/envs/pytorch_dummy_environment/lib/python3.9/site-packages/torchtext/_torchtext.so: undefined symbol: _ZNK5torch3jit6MethodclESt6vectorIN3c106IValueESaIS4_EERKSt13unordered_mapISsS4_St4hashISsESt8equal_toISsESaISt4pairIKSsS4_EEE

这表明我的 Conda 安装出现问题,并且可能与 torchtext 有关。

这是安装的torch相关包的版本:

pytorch                   1.10.0          cpu_py39hc5866cc_0    conda-forge
pytorch-lightning         1.5.2              pyhd8ed1ab_0    conda-forge
pytorch-mutex             1.0                        cuda    pytorch
torchaudio                0.10.0                 py39_cpu    pytorch
torchmetrics              0.6.0              pyhd8ed1ab_0    conda-forge
torchtext                 0.11.0                     py39    pytorch
torchvision               0.11.1                 py39_cpu    pytorch

【问题讨论】:

  • 您可以使用 c++filt 与您的错误 ID:$c++filt _ZNK5torch3jit6MethodclESt6vectorIN3c106IValueESaIS4_EERKSt13unordered_mapISsS4_St4hashISsESt8equal_toISsESaISt4pairIKSsS4_EEE
  • 看来torch::jit::Method::operator()有问题。 torchtext 版本是否与 Pytorch 版本兼容?
  • 尝试从 pytorch 通道而不是 Conda Forge 安装 PyTorch,即conda install pytorch::pytorch。通常情况下,不同通道使用的不同构建堆栈不会导致动态库中的符号相同。
  • @merv 是的 environment.yaml 文件正在从 conda-forge 而不是 pytorch 通道安装 pytorch。用pytorch::pytorch 强制它现在可以工作了!

标签: pytorch conda torchtext


【解决方案1】:

所以为了解决这个问题,我不得不更改我的environment.yaml 以强制pytorchpytorch 频道安装。

现在这是我的environment.yaml

channels:
  - defaults
  - pytorch
  - conda-forge
dependencies:
  # ML section
  - pytorch::pytorch
  - pytorch::torchtext
  - pytorch::torchvision
  - pytorch::torchaudio
  - pytorch::cpuonly
  - mlflow=1.21.0
  - pytorch-lightning>=1.5.2
  - pip:
    - jsonargparse[signatures]

使用它我不再收到错误消息。现在安装的pytorch相关的东西是:

cpuonly                   2.0                           0    pytorch
pytorch                   1.10.0              py3.9_cpu_0    pytorch
pytorch-lightning         1.5.2              pyhd8ed1ab_0    conda-forge
pytorch-mutex             1.0                         cpu    pytorch
torchaudio                0.10.0                 py39_cpu  [cpuonly]  pytorch
torchtext                 0.11.0                     py39    pytorch
torchvision               0.11.1                 py39_cpu  [cpuonly]  pytorch

【讨论】:

    猜你喜欢
    • 2020-08-27
    • 2021-06-07
    • 2019-12-08
    • 1970-01-01
    • 2023-02-24
    • 1970-01-01
    • 2017-10-09
    • 2017-07-31
    • 2015-03-18
    相关资源
    最近更新 更多