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.

Exporting Fine tuned T5ForConditionalGeneration model to TF-Serving using ONNX

See original GitHub issue

Environment info

  • transformers version: 4.9.1
  • Platform: Linux-5.4.0-1049-gcp-x86_64-with-Ubuntu-18.04-bionic
  • Python version: 3.6.9
  • PyTorch version (GPU?): 1.9.0 (False)
  • Tensorflow version (GPU?): 2.5.0 (False)
  • Flax version (CPU?/GPU?/TPU?): not installed (NA)
  • Jax version: not installed
  • JaxLib version: not installed
  • Using GPU in script?: No
  • Using distributed or parallel set-up in script?: No

Who can help

@patrickvonplaten, @patil-suraj

Information

Model I am using (Bert, XLNet …): T5

The problem arises when using:

When I serve this model and do inference, it seems the model being loaded isn’t the fine tuned one, as it gives output of the following nature : In In In In In auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf auf

The tasks I am working on is:

  • an official GLUE/SQUaD task: Summarization

To reproduce

Steps to reproduce the behavior:

def load_ckp(checkpoint_fpath, model, optimizer):
    checkpoint = torch.load(checkpoint_fpath, map_location=torch.device('cpu'))
    model.load_state_dict(checkpoint['state_dict'])
    optimizer.load_state_dict(checkpoint['optimizer'])
    return model, optimizer, checkpoint['epoch']

import os

tokenizer = T5Tokenizer.from_pretrained("t5-base")
model = T5ForConditionalGeneration.from_pretrained("t5-base")
from torch import cuda
device = 'cuda' if cuda.is_available() else 'cpu'
model = model.to(device)
optimizer = torch.optim.Adam(params =  model.parameters(), lr=1e-4)

ckp_path = '/checkpoint_dir/checkpoint.pt'
model, optimizer, start_epoch = load_ckp(ckp_path, model, optimizer)

model.save_pretrained('fine-tuned') ##saves it to pytorch_model.bin format and config.json, which is needed for onnx
tokenizer.save_pretrained('fine-tuned')

tokenizer = T5Tokenizer.from_pretrained("fine-tuned")
model = T5ForConditionalGeneration.from_pretrained("fine-tuned")

==== COMMAND LINE====

python -m transformers.onnx --model=fine-tuned onnx/t5-tf-serving/

At this point, I get the following warning : Some weights of the model checkpoint at fine-tuned were not used when initializing T5Model: ['lm_head.weight'], but the process completes with the following message : All good, model saved at: onnx/t5-tf-serving/model.onnx.

Post this I use onnx-tf convert -i onnx/t5-tf-serving/model.onnx -o output.pb to get the corresponding Tensorflow SavedModel, and use standard docker based procedure for deploying it with TF-Serving.

Expected behavior

I’m able to serve the model using the proper request formats, but the outputs are way off, as shown above. I’m guessing it has to do with the warning message that was displayed when converting the pytorch model to onnx. Fwiw, I tested out normal inference on the .bin formatted pytorch model that was obtained using the model.save_pretrained('fine-tuned') function, and it was generating expected outputs.

Can you please suggest workarounds? @patrickvonplaten, @patil-suraj

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
sekharvthcommented, Sep 2, 2021

Hey @patrickvonplaten, just checking in to see if @mfuntowicz is back 😃

1reaction
sekharvthcommented, Oct 1, 2021

Hey @michaelbenayoun .

Aaah, there’s a param to be set then. I’ll try this out and let you know 😃

Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Export to ONNX - Transformers - Hugging Face
In this guide, we'll show you how to export Transformers models to ONNX (Open Neural Network eXchange). Once exported, a model can be...
Read more >
ONNX Export for Fine-Tuned DistilBertForTokenClassification
I'd like to export a fine-tuned DistilBertForTokenClassification model to ONNX. Right now the conversion script convert_graph_to_onnx.py ...
Read more >
Export Fine-tuned Bert model to ONNX and inference using ...
In this post, a fine-tuned XLM-Roberta Bert model will be exported as onnx format and the exported onnx model will be inferred on...
Read more >
How could I convert onnx model to tensorflow saved model?
I am trying to use tf-serving to deploy my torch model. I have exported my torch model to onnx. How could I generate...
Read more >
Running Inference With BERT Using TensorFlow Serving
Learn how to implement BERT as a language model in a production ... the weights in the added layer are initialized first (fine-tuning...
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