【问题标题】:Pytorch - Caught StopIteration in replica 1 on device 1 error while Training on GPUPytorch - 在 GPU 上训练时在设备 1 上的副本 1 中捕获 StopIteration 错误
【发布时间】:2021-04-21 08:21:34
【问题描述】:

我正在尝试在 git 链接中使用的 train2012 数据上训练 BertPunc 模型:https://github.com/nkrnrnk/BertPunc。 在服务器上运行时,启用了 4 个 GPU,以下是我得到的错误:

StopIteration: Caught StopIteration in replica 1 on device 1.
Original Traceback (most recent call last):
  File "/home/stenoaimladmin/.local/lib/python3.8/site-packages/torch/nn/parallel/parallel_apply.py", line 61, in _worker
    output = module(*input, **kwargs)
  File "/home/stenoaimladmin/.local/lib/python3.8/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/stenoaimladmin/notebooks/model_BertPunc.py", line 16, in forward
    x = self.bert(x)
  File "/home/stenoaimladmin/.local/lib/python3.8/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/stenoaimladmin/anaconda3/lib/python3.8/site-packages/pytorch_pretrained_bert/modeling.py", line 861, in forward
    sequence_output, _ = self.bert(input_ids, token_type_ids, attention_mask,
  File "/home/stenoaimladmin/.local/lib/python3.8/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/stenoaimladmin/anaconda3/lib/python3.8/site-packages/pytorch_pretrained_bert/modeling.py", line 727, in forward
    extended_attention_mask = extended_attention_mask.to(dtype=next(self.parameters()).dtype) # fp16 compatibility
StopIteration

来自链接:https://github.com/huggingface/transformers/issues/8145,这似乎是在数据在多个 GPU 之间来回移动时发生的。

根据 git 链接:https://github.com/interpretml/interpret-text/issues/117,我们需要将 PyTorch 版本从我目前使用的 1.7 降级到 1.4。对我来说,降级版本不是一个选项,因为我有其他使用 Torch 1.7 版本的脚本。我应该怎么做才能克服这个错误?

由于行太多,我无法将整个代码放在这里,但这是给我错误的 sn-p:

bert_punc, optimizer, best_val_loss = train(bert_punc, optimizer, criterion, epochs_top, 
        data_loader_train, data_loader_valid, save_path, punctuation_enc, iterations_top, best_val_loss=1e9)

这是我的 DataParallel 代码:

   bert_punc = nn.DataParallel(BertPunc(segment_size, output_size, dropout)).cuda()

我尝试更改 Dataparallel 线以将训练转移到只有 1 个 GPU ,共 4 个。但这给了我一个空间问题,因此不得不将代码恢复为默认值。

这是我正在使用的所有脚本的链接:https://github.com/nkrnrnk/BertPunc 请指教。

【问题讨论】:

    标签: python-3.x pytorch bert-language-model


    【解决方案1】:

    改变

    extended_attention_mask = extended_attention_mask.to(dtype=next(self.parameters()).dtype) # fp16 compatibility

    到

    extended_attention_mask = extended_attention_mask.to(dtype=torch.float32) # fp16 compatibility

    更多详情请见https://github.com/vid-koci/bert-commonsense/issues/6

    【讨论】:

      猜你喜欢
      • 2021-03-08
      • 2021-04-19
      • 2020-11-29
      • 1970-01-01
      • 2020-11-28
      • 1970-01-01
      • 2020-12-06
      • 2021-12-26
      • 2018-03-28
      相关资源
      最近更新 更多