首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用T5将fastT5模型导出到onnx时,获取“RuntimeError: shape [5,8,1,2]的输出与广播形状[5,8,2,2]不匹配

使用T5将fastT5模型导出到onnx时,获取“RuntimeError: shape [5,8,1,2]的输出与广播形状[5,8,2,2]不匹配
EN

Stack Overflow用户
提问于 2021-03-18 15:04:05
回答 1查看 830关注 0票数 4

我试图使用T5库将fastT5模型转换为onnx,但在运行以下代码时出错

代码语言:javascript
运行
复制
from fastT5 import export_and_get_onnx_model
from transformers import AutoTokenizer

model_name = 't5-small'
model = export_and_get_onnx_model(model_name)

tokenizer = AutoTokenizer.from_pretrained(model_name)
t_input = "translate English to French: The universe is a dark forest."
token = tokenizer(t_input, return_tensors='pt')

tokens = model.generate(input_ids=token['input_ids'],
               attention_mask=token['attention_mask'],
               num_beams=2)

output = tokenizer.decode(tokens.squeeze(), skip_special_tokens=True)
print(output)

错误:

代码语言:javascript
运行
复制
/usr/local/lib/python3.7/dist-packages/transformers/modeling_utils.py:244: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  if causal_mask.shape[1] < attention_mask.shape[1]:
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-16-80094b7c4f6f> in <module>()
      7                     input_names=decoder_input_names,
      8                     output_names=decoder_output_names,
----> 9                     dynamic_axes=dyn_axis_params,
     10                     )

24 frames
/usr/local/lib/python3.7/dist-packages/transformers/models/t5/modeling_t5.py in forward(self, hidden_states, mask, key_value_states, position_bias, past_key_value, layer_head_mask, query_length, use_cache, output_attentions)
    497                 position_bias = position_bias + mask  # (batch_size, n_heads, seq_length, key_length)
    498 
--> 499         scores += position_bias
    500         attn_weights = F.softmax(scores.float(), dim=-1).type_as(
    501             scores

RuntimeError: output with shape [5, 8, 1, 2] doesn't match the broadcast shape [5, 8, 2, 2]

有人能帮我解决这个问题吗?

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-19 04:19:43

我已经检查了存储库,它看起来是一个已知的问题,如这里所报告的:https://github.com/Ki6an/fastT5/issues/1

库的开发人员已经发布了一个解决方案,并在这里创建了一个笔记本文件:https://colab.research.google.com/drive/1HuH1Ui3pCBS22hW4djIOyUBP5UW93705?usp=sharing

解决方案是修改modeling_t5.py文件,在第494行:

代码语言:javascript
运行
复制
# Define this at line 426:
int_seq_length = int(seq_length)

# Change this at line 494:
position_bias = position_bias[:, :, -seq_length:, :]
position_bias = position_bias[:, :, -int_seq_length:, :]  # Updated version

如果您不想自己修改文件,则需要等到此拉请求被合并到Transformers库中。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66693724

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档