question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

onnx run error at translation model

See original GitHub issue

System Info

  • transformers version: 4.17.0
  • Platform: Linux-5.4.0-122-generic-x86_64-with-debian-bullseye-sid
  • Python version: 3.7.11
  • PyTorch version (GPU?): 1.8.1 (True)
  • Tensorflow version (GPU?): not installed (NA)
  • Flax version (CPU?/GPU?/TPU?): not installed (NA)
  • Jax version: not installed
  • JaxLib version: not installed
  • Using GPU in script?: <fill in>
  • Using distributed or parallel set-up in script?: <fill in>

Who can help?

@patrickvonplaten @patil-suraj

Information

  • The official example scripts
  • My own modified scripts

Tasks

  • An officially supported task in the examples folder (such as GLUE/SQuAD, …)
  • My own task or dataset (give details below)

Reproduction

1、convert model to onnx: python3 -m transformers.onnx --model opus-mt-en-zh --atol=2e-04 --feature=seq2seq-lm opus-mt-en-zh-onnx-301 tips: Validating ONNX model… -[✓] ONNX model output names match reference model ({‘logits’}) - Validating ONNX Model output “logits”: -[✓] (2, 8, 65001) matches (2, 8, 65001) -[✓] all values close (atol: 0.0002) All good, model saved at: opus-mt-en-zh-onnx-301/model.onnx

2、translation:

from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
from onnxruntime import InferenceSession

tokenizer=AutoTokenizer.from_pretrained("opus-mt-en-zh")
session = InferenceSession("opus-mt-en-zh-onnx-301/model.onnx")
inputs = tokenizer("Using DistilBERT with ONNX Runtime!", return_tensors="pt")
outputs = session.run(output_names=["last_hidden_state"], input_feed=dict(inputs))

tips:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/xieyouxi/anaconda3/envs/HuggingFace-torch-gpu/lib/python3.7/site-packages/onnxruntime/capi/onnxruntime_inference_collection.py", line 196, in run
    raise ValueError("Model requires {} inputs. Input Feed contains {}".format(num_required_inputs, num_inputs))
ValueError: Model requires 4 inputs. Input Feed contains 2

Expected behavior

Unable to translate from en to zh, Am I using the wrong interface?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
regissscommented, Aug 9, 2022

@xyx361100238 The error message says that the model requires 4 inputs but you are providing only 2 of them. Either you need to provide the missing inputs, or you need to modify the OnnxConfig associated to your model to specify only 2 inputs.

The architecture of opus-mt-en-zh seems to be MarianMTModel. According to what I see in the OnnxConfig here, the 4 expected inputs are:

  • input_ids,
  • attention_mask,
  • decoder_input_ids,
  • decoder_attention_mask.

So I think you are only providing input_ids and attention_mask here. To generate the missing inputs, you can take a look at how dummy inputs used for exporting the model are generated.

1reaction
michaelbenayouncommented, Aug 10, 2022

So you also need to provide the inputs for the decoder side, something along the lines:

from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
from onnxruntime import InferenceSession

tokenizer=AutoTokenizer.from_pretrained("opus-mt-en-zh")
session = InferenceSession("opus-mt-en-zh-onnx-301/model.onnx")
inputs = tokenizer("Using DistilBERT with ONNX Runtime!", return_tensors="pt")
inputs["decoder_input_ids"] = torch.tensor([tokenizer.bos_token_id], dtype=torch.long)
inputs["decoder_attention_mask"] = torch.tensor([1], dtype=torch.long)
outputs = session.run(output_names=["last_hidden_state"], input_feed=inputs)

What is true is that it would be easier if the decoder_attention_mask was automatically generated, but we you currently need to provide it manually.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Translation Model in ONNX: Choosable Output Formats #9784
Feature request I am requesting to provide an option to specify the output format for the translation_xx_to_yy export to ONNX models.
Read more >
Export M2M100 model to ONNX - Hugging Face Forums
I've port facebook/m2m100_418M to ONNX for translation task using this but when visualize by netron, it required 4 inputs: input_ids, attention ...
Read more >
Operator translate error occurs when I try to convert onnx file ...
The problem is that the Caffe2 ONNX backend does not yet support the export of the Resize operator. Please raise an issue on...
Read more >
Conversion of Trained Model from PyTorch to ONNX Format ...
This error means that Model Optimizer cannot infer shaped or values for the specified node. It can happen because of a bug in...
Read more >
Common errors with onnxruntime
The model fails to return an output if the name is misspelled. try: ; The output name is optional, it can be replaced...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found