【问题标题】:pytorch summary fails with huggingface modelpytorch 摘要因拥抱脸模型而失败
【发布时间】:2021-10-05 04:28:38
【问题描述】:

我想要一个从 huggingface 下载的 PyTorch 模型的摘要。

我在这里做错了吗?

from torchinfo import summary
from transformers import AutoModelForSequenceClassification

model = AutoModelForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=2)
summary(model, input_size=(16, 512))

给出错误:

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

RuntimeError                              Traceback (most recent call last)

/usr/local/lib/python3.7/dist-packages/torchinfo/torchinfo.py in forward_pass(model, x, batch_dim, cache_forward_pass, device, **kwargs)
    257             if isinstance(x, (list, tuple)):
--> 258                 _ = model.to(device)(*x, **kwargs)
    259             elif isinstance(x, dict):

11 frames

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1050                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1051             return forward_call(*input, **kwargs)
   1052         # Do not call functions when jit is used

/usr/local/lib/python3.7/dist-packages/transformers/models/bert/modeling_bert.py in forward(self, input_ids, attention_mask, token_type_ids, position_ids, head_mask, inputs_embeds, labels, output_attentions, output_hidden_states, return_dict)
   1530             output_hidden_states=output_hidden_states,
-> 1531             return_dict=return_dict,
   1532         )

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1070 
-> 1071         result = forward_call(*input, **kwargs)
   1072         if _global_forward_hooks or self._forward_hooks:

/usr/local/lib/python3.7/dist-packages/transformers/models/bert/modeling_bert.py in forward(self, input_ids, attention_mask, token_type_ids, position_ids, head_mask, inputs_embeds, encoder_hidden_states, encoder_attention_mask, past_key_values, use_cache, output_attentions, output_hidden_states, return_dict)
    988             inputs_embeds=inputs_embeds,
--> 989             past_key_values_length=past_key_values_length,
    990         )

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1070 
-> 1071         result = forward_call(*input, **kwargs)
   1072         if _global_forward_hooks or self._forward_hooks:

/usr/local/lib/python3.7/dist-packages/transformers/models/bert/modeling_bert.py in forward(self, input_ids, token_type_ids, position_ids, inputs_embeds, past_key_values_length)
    214         if inputs_embeds is None:
--> 215             inputs_embeds = self.word_embeddings(input_ids)
    216         token_type_embeddings = self.token_type_embeddings(token_type_ids)

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1070 
-> 1071         result = forward_call(*input, **kwargs)
   1072         if _global_forward_hooks or self._forward_hooks:

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/sparse.py in forward(self, input)
    159             input, self.weight, self.padding_idx, self.max_norm,
--> 160             self.norm_type, self.scale_grad_by_freq, self.sparse)
    161 

/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in embedding(input, weight, padding_idx, max_norm, norm_type, scale_grad_by_freq, sparse)
   2042         _no_grad_embedding_renorm_(weight, input, max_norm, norm_type)
-> 2043     return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
   2044 

RuntimeError: Expected tensor for argument #1 'indices' to have one of the following scalar types: Long, Int; but got torch.cuda.FloatTensor instead (while checking arguments for embedding)


The above exception was the direct cause of the following exception:

RuntimeError                              Traceback (most recent call last)

<ipython-input-8-4f70d4e6fa82> in <module>()
      5 else:
      6     # Can't get this working
----> 7     summary(model, input_size=(16, 512)) #, device='cpu')
      8     #print(model)

/usr/local/lib/python3.7/dist-packages/torchinfo/torchinfo.py in summary(model, input_size, input_data, batch_dim, cache_forward_pass, col_names, col_width, depth, device, dtypes, row_settings, verbose, **kwargs)
    190     )
    191     summary_list = forward_pass(
--> 192         model, x, batch_dim, cache_forward_pass, device, **kwargs
    193     )
    194     formatting = FormattingOptions(depth, verbose, col_names, col_width, row_settings)

/usr/local/lib/python3.7/dist-packages/torchinfo/torchinfo.py in forward_pass(model, x, batch_dim, cache_forward_pass, device, **kwargs)
    268             "Failed to run torchinfo. See above stack traces for more details. "
    269             f"Executed layers up to: {executed_layers}"
--> 270         ) from e
    271     finally:
    272         if hooks is not None:

RuntimeError: Failed to run torchinfo. See above stack traces for more details. Executed layers up to: []

【问题讨论】:

    标签: python pytorch huggingface-transformers


    【解决方案1】:

    最后一行显示的torchinfo 库[torchinfo.py] 中有一个错误[also reported]。当dtypesNone 时,默认创建torch.float 张量,而bert 模型的forward 方法使用torch.nn.embedding,它只需要int/long 张量。

    def process_input(
        input_data: Optional[INPUT_DATA_TYPE],
        input_size: Optional[INPUT_SIZE_TYPE],
        batch_dim: Optional[int],
        device: Union[torch.device, str],
        dtypes: Optional[List[torch.dtype]] = None,
    ) -> Tuple[CORRECTED_INPUT_DATA_TYPE, Any]:
        """Reads sample input data to get the input size."""
    
        if input_size is not None:
            if dtypes is None:
                dtypes = [torch.float] * len(input_size)
    

    如果您尝试将行修改为以下内容,则可以正常工作。

    dtypes = [torch.int] * len(input_size)
    

    编辑(无需更改内部代码的直接解决方案):

    from torchinfo import summary
    from transformers import AutoModelForSequenceClassification, AutoTokenizer
    
    model = AutoModelForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=2)
    summary(model, input_size=(2, 512), dtypes=['torch.IntTensor'])
    

    替代:

    为了简单概括,您可以使用print(model) 代替summary 函数。

    【讨论】:

    • 首先,print(model)模式不包含summary中的信息。您建议的解决方案是修改pytorch 库代码?
    • 编辑了答案
    • 您可以选择这是一个错误,是吗?如果我理解正确,它还有待 pytorch 开发人员审查。
    • 是的,这是torchinfo 中的一个错误。 dtypes 可能一直在为其他具有浮点类型的模型工作,但看起来他们没有为像 bert 这样有不同期望的拥抱脸模型测试它。他们可能必须添加一些其他检查来解决此错误。仅供参考,在他们的回购中提出了这个错误。
    • 是的,我们可以跟踪它
    猜你喜欢
    • 2022-01-17
    • 1970-01-01
    • 2021-08-08
    • 2020-12-18
    • 2021-12-30
    • 2020-11-06
    • 2020-09-10
    • 2015-03-29
    • 2021-12-21
    相关资源
    最近更新 更多