【问题标题】:ValueError: You have to specify either decoder_input_ids or decoder_inputs_embedsValueError:您必须指定decoder_input_ids 或decoder_inputs_embeds
【发布时间】:2021-03-16 07:40:48
【问题描述】:

尝试将question-generation t5 模型转换为torchscript model,同时执行此操作时遇到此错误

ValueError:您必须指定decoder_input_ids 或decoder_inputs_embeds

这是我在 colab 上运行的代码。

!pip install -U transformers==3.0.0
!python -m nltk.downloader punkt

from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import torch

model = AutoModelForSeq2SeqLM.from_pretrained('valhalla/t5-base-qg-hl')

t_input =  'Python is a programming language. It is developed by <hl> Guido Van Rossum <hl>. </s>'

tokenizer = AutoTokenizer.from_pretrained('valhalla/t5-base-qg-hl', return_tensors = 'pt')

def _tokenize(
    inputs,
    padding=True,
    truncation=True,
    add_special_tokens=True,
    max_length=64
):
    inputs = tokenizer.batch_encode_plus(
        inputs, 
        max_length=max_length,
        add_special_tokens=add_special_tokens,
        truncation=truncation,
        padding="max_length" if padding else False,
        pad_to_max_length=padding,
        return_tensors="pt"
    )
    return inputs

token = _tokenize(t_input, padding=True, truncation=True)


traced_model = torch.jit.trace(model, [token['input_ids'], token['attention_mask']] )
torch.jit.save(traced_model, "traced_t5.pt")

收到此错误

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-1-f9b449524ef1> in <module>()
     32 
     33 
---> 34 traced_model = torch.jit.trace(model, [token['input_ids'], token['attention_mask']] )
     35 torch.jit.save(traced_model, "traced_t5.pt")

7 frames
/usr/local/lib/python3.6/dist-packages/transformers/modeling_t5.py in forward(self, input_ids, attention_mask, encoder_hidden_states, encoder_attention_mask, inputs_embeds, head_mask, past_key_value_states, use_cache, output_attentions, output_hidden_states)
    682         else:
    683             if self.is_decoder:
--> 684                 raise ValueError("You have to specify either decoder_input_ids or decoder_inputs_embeds")
    685             else:
    686                 raise ValueError("You have to specify either input_ids or inputs_embeds")

ValueError: You have to specify either decoder_input_ids or decoder_inputs_embeds

如何解决这个问题?或者有没有更好的方法将 t5 模型转换为torchscript。

谢谢。

【问题讨论】:

  • 我假设torch.jit.trace调用了模型的forward方法(如果我错了请纠正我)。正如错误所说,forward 方法需要decoder_input_ids 或decoder_inputs_embeds。 Link 文档链接。
  • 是的,我需要传递三个参数(input_ids、attention_mask、decoder_input_ids)。但我不确定在上述情况下应该为decoder_input_ids 传递什么。

标签: python deep-learning huggingface-transformers torchscript


【解决方案1】:

更新:参考this的答案,如果你将t5导出到onnx,可以使用fastT5库轻松完成。

我找出了导致问题的原因。由于上面的模型是sequential,它既有编码器又有解码器。我们需要将特征传递给编码器,并将标签(目标)传递给解码器。

traced_model = torch.jit.trace(model, 
                               (input_ids, attention_mask, decoder_input_ids, decoder_attention_mask)
                               )
torch.jit.save(traced_model, "qg_model.pt")

decoder_input_ids 是问题的标记化 id(这里问题是一个标签)。

即使创建了torchscript 模型,它也没有huggingface' t5 那样的generate() 方法。

【讨论】:

    猜你喜欢
    • 2019-12-23
    • 1970-01-01
    • 2021-09-23
    • 2019-06-23
    • 2021-08-21
    • 2021-02-13
    • 2015-06-22
    • 1970-01-01
    • 2017-05-13
    相关资源
    最近更新 更多