Export M2M100 model to ONNX
I’ve portfacebook/m2m100_418Mto ONNX for translation task usingthisbut when visualize by netron, it required 4 inputs:input_ids,attention_mask,decoder_input_ids,decoder_attention_maskand I don’t know how to inference with ONNX-runtime.image1906×964 46.6 KBHow can I solve this problem ?Thanks in advance for your help.
2022-05-16T16:25:23Z
Did you find a solution?
2022-06-30T16:50:26Z
I have the same issue. Have you found a solution yet?
2022-07-07T12:31:16Z
I tried to convert this model with onnx by adding this type of the taskpython3.8 -m transformers.onnx  --model=facebook/m2m100_418M onnx/  --feature=seq2seq-lm-with-past, but in this case it says that it needs 54 inputs, otherwise I have the same problem. I know that the model needs the input and output language but I can’t really understand how to use the model with onnx. An example would be welcomeI also looked for indications in the commit of the model:M2M100 support for ONNX export by michaelbenayoun · Pull Request #15193 · huggingface/transformers · GitHub. I think it can be useful.
2022-07-08T09:29:33Z
cc@lewtun
2022-08-22T01:48:25Z
Also having the same question, Could I have an example for this m2m-100 onnx model? It will be very helpful.
2022-08-22T14:52:05Z
Hi folks, the best way to run inference with ONNX models is via theoptimumlibrary. This library allows you to inject ONNX models directly in thepipeline()function fromtransformersand thus skip all the annoying pre- and post-processing stepsHere’s a demo for M2M100 based on thedocs:from transformers import AutoTokenizer, pipeline
from optimum.onnxruntime import ORTModelForSeq2SeqLM

tokenizer = AutoTokenizer.from_pretrained("facebook/m2m100_418M")
# `from_transformers` will export the model to ONNX on-the-fly 🤯
model = ORTModelForSeq2SeqLM.from_pretrained("facebook/m2m100_418M", from_transformers=True)
onnx_translation = pipeline("translation_en_to_de", model=model, tokenizer=tokenizer)

text = "My name is Lewis."
# returns [{'translation_text': 'Mein Name ist Lewis.'}]
pred = onnx_translation(text)Hope that helps!
2022-11-30T13:37:15Z
Running into following error when I run code as is from@lewtunAttributeError: type object 'FeaturesManager' has no attribute 'determine_framework'Using following version:torch → ‘1.10.0’transformers → ‘4.20.1’
2022-11-30T13:52:08Z
cc@fxmartywho might be able to take a look
2022-11-30T13:57:12Z
Thanks, also, not sure where is target language ‘de’ mentioned above in tokenizer/model. Greatly appreciate your help.
2022-12-01T16:30:49Z
Hi@awaiskaleem,transformers==4.20.1is 5 months old. Could you try to update (pip install --upgrade transformers)? Current supported stable version is4.20.0. The code snippet from@lewtunworks well for me withtransformers==4.20.0andoptimum==1.5.1.Additionally@NNDam,@double@omoekan,@Jour,@echoRG,@awaiskaleem, I wanted to let you know that the ONNX export throughtransformers.onnxwill likely soon rely on a soft dependency tooptimum.exporterswhere all things export will be maintained. You can checkthe documentation here.Now, specifically for M2M100, keep in mind that it is a seq2seq (translation) model! Hence, it uses both an encoder and decoder, as detailed intransformers doc. In transformers, the standard use is tomodel.generate(**inputs). However, by default the ONNX export can not handle the loop that there is in the decoder:transformers/utils.py at d51e7c7e8265d69db506828dce77eb4ef9b72157 · huggingface/transformers · GitHub. Hence, when exporting to ONNX in a single file, unless you do some manual surgery on the ONNX graph, the model will be hardly usable.The solution that is currently explored & in use inOptimum’s ORTModelForSeq2SeqLMleveraging ONNX Runtime is to use two ONNX files: one for the encoder, and one for the decoder.Using Optimum main (not yet in the stable release, but you can expect it next week),python -m optimum.exporters.onnx --model valhalla/m2m100_tiny_random --for-ort m2m100_tiny_onnx_ort, we obtain two models:an encoder expecting theinput_ids,attention_maska decoder expectingencoder_attention_mask,input_idsandencoder_hidden_states. This follows closely transformersdecoderandgenerate.image1560×613 56 KBimage1183×497 59.2 KBSo if you would like to use these exported ONNX models outside of Optimum, I simply recommend to use the above command to export and handle yourself the models then. ButORTModelForSeq2SeqLMis meant to save you the hassle.If you want to try it right away, feel free to try the dev version:pip install -U git+https://github.com/huggingface/optimum.git@mainEdit 2022-12-27: Feel free to have a look at the latest release notes which includes the feature:Release v1.6.0: Optimum CLI, Stable Diffusion ONNX export, BetterTransformer & ONNX support for more architectures · huggingface/optimum · GitHub
2023-05-29T07:56:12Z
This partially worked for me. I mean I was able to load the infer the model successfully but the text wasn’t translated into “de” in your example. The result I got was as following:pred: [{'translation_text': 'de: My name is Lewis.'}]The model I’m using is: “facebook/nllb-200-distilled-600M”
2023-06-14T08:52:39Z
Hi there! Thanks for the insights, I have a couple of questions about the architecture of the ORT seq2seq model that I hope you could clarify.Firstly, I’m curious why Optimum requires the encoder and decoder to be loaded from two separate ONNX files, instead of a single ONNX file? I’m guessing (from a quick glance at the source code) that it’s because it utilizes two ORT inference sessions for the encoder and decoder instead of using a single session for the entire model – is there a rationale for this design?Secondly, you mentioned that the ONNX export of the model faces difficulties with the generate loop in the decoder, while the transformers model seems to handle it fine. I’m wondering what specifically in the generate loop makes it challenging for the ORT model to handle?
2023-06-15T05:52:15Z
@ahmedbrCoud you fill a bug report with a reproduction script onIssues · huggingface/optimum · GitHubso that I can have a look at it?@luckytThe main reason is because you normally want to run the encoder only once, while you’d like to loop over the decoder when generating. You could say, ok why not wrap everything into a single ONNX, with say anIfnode to decide whether or not to run the encoder? Something like this with subgraphs:image836×327 12.6 KBThis could be doable actually. The issue with that is that usability is a bit harder, as the encoder and decoder do not have the same inputs/outputs. So you would need to create fake input/outputs, which theoretically works, but may lead into errors and be a bit unintuitive.About generation, what is slightly challenging is that inputs/outputs are fixed with ONNX, and more importantly when exporting we usetorch.jit.tracethat can not handle controlflows, that are typically use to handle the without/with past (use KV cache or not) case. In the first step of the generation, you don’t use the KV cache, while in later steps you do. Seetransformers/src/transformers/models/t5/modeling_t5.py at v4.30.2 · huggingface/transformers · GitHub&How does the ONNX exporter work for GenerationModel with `past_key_value`?