# How can I use the ONNX model?

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-can-i-use-the-onnx-model%2F70923%3E%5C
**Category:** 🤗Transformers\
**Created:** [January 28, 2024, 1:52pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-can-i-use-the-onnx-model%2F70923 "2024-01-28T13:52:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DeleMike](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/delemike/32/26732_2.png) [@DeleMike](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FDeleMike)%5C
**Post date:** [January 28, 2024, 1:52pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-can-i-use-the-onnx-model%2F70923%2F1 "2024-01-28T13:52:38Z")

</div>

I performed some tasks and my model was saved at `./oyot5_small_unyo_dcyo_mix`. Later, I converted it to an onnx model using this script

```auto
optimum-cli export onnx --model oyot5_small_unyo_dcyo_mix/ --task text2text-generation oyto_t5_small_onnx/

```

which was saved as `./oyto_t5_small_onnx`. See the picture below for the output of the conversion. How can I use these onnx models? I understand why I have `encoder_model.onnx` and `decoder_model.onnx`. I’m using a pre-trained t5 model which has an encoder\_decoder architecture. My pain point here is I cannot use these converted onnx models.

What resources can help me?

![Screenshot 2024-01-28 at 14.39.13](https://us1.discourse-cdn.com/hellohellohello/original/3X/9/c/9c107edddef219710df781472620ac77e8ab6083.png)

---

<div class="post-metadata">

**Author:** ![nielsr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nielsr/32/39617_2.png) [@nielsr](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fnielsr)%5C
**Post date:** [January 28, 2024, 8:49pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-can-i-use-the-onnx-model%2F70923%2F2 "2024-01-28T20:49:34Z")

</div>

Hi,

You can run inference with the `ORTModelForSeq2SeqLM` class (ORT is short for ONNX runtime). Just pass your folder to the `from_pretrained` method. Docs: [Models](https://huggingface.co/docs/optimum/main/en/onnxruntime/package_reference/modeling_ort#optimum.onnxruntime.ORTModelForSeq2SeqLM.forward.example)

See for instance the T5 ONNX model here: [optimum/t5-small · Hugging Face](https://huggingface.co/optimum/t5-small).

---

<div class="post-metadata">

**Author:** ![DeleMike](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/delemike/32/26732_2.png) [@DeleMike](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FDeleMike)%5C
**Post date:** [January 29, 2024, 12:25pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-can-i-use-the-onnx-model%2F70923%2F3 "2024-01-29T12:25:18Z")

</div>

Thank you @nielsr , I extensively read the documentation you shared and came up with these codes.

```auto
from transformers import AutoTokenizer, pipeline, PretrainedConfig
from optimum.onnxruntime import ORTModelForSeq2SeqLM
import onnxruntime

# Load encoder model
encoder_session = onnxruntime.InferenceSession('oyto_t5_small_onnx/encoder_model.onnx')

# Load decoder model
decoder_session = onnxruntime.InferenceSession('oyto_t5_small_onnx/decoder_model.onnx')

model_id = "oyto_t5_small_onnx/"
tokenizer = AutoTokenizer.from_pretrained(model_id)

config = PretrainedConfig.from_json_file('oyto_t5_small_onnx/config.json')

model = ORTModelForSeq2SeqLM(
    config=config,
    onnx_paths=['oyto_t5_small_onnx/decoder_model.onnx','oyto_t5_small_onnx/encoder_model.onnx'],
    encoder_session=encoder_session, 
    decoder_session=decoder_session, 
    model_save_dir='oyto_t5_small_onnx',
    use_cache=False, 
)

onnx_translation = pipeline("translation_src_to_target", model=model, tokenizer=tokenizer)

text = 'the text to perform your translation task'
result = onnx_translation(text, max_length = 10000)
print(result)

```
