Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ORTOptimizer for the model type table-transformer #1761

Open
zachmayer opened this issue Mar 20, 2024 · 5 comments
Open

ORTOptimizer for the model type table-transformer #1761

zachmayer opened this issue Mar 20, 2024 · 5 comments

Comments

@zachmayer
Copy link
Contributor

zachmayer commented Mar 20, 2024

Feature request

I'd like to add support for the new microsoft table transformers to ORTOptimizer

Motivation

These table transformers are object detection models. They have a backbone that's a CNN, so at the very least I think we could optimize the CNN part of these models?

Your contribution

Here is my sample script:

import os
from pathlib import Path

import torch
from optimum.onnxruntime import ORTModel, ORTOptimizer
from optimum.onnxruntime.configuration import OptimizationConfig
from transformers import AutoImageProcessor, TableTransformerForObjectDetection

# Define the input model and the output directory
model_name = "microsoft/table-transformer-detection"
onnx_path = Path("~/Downloads/onnx_model/").expanduser()
onnx_model_name = "table_transformer.onnx"

# Load the Processor and save its config
processor = AutoImageProcessor.from_pretrained(model_name)
processor.save_pretrained(onnx_path)

# Load the model and save its config
model = TableTransformerForObjectDetection.from_pretrained(
    "microsoft/table-transformer-detection"
)
model.eval()
model.config.save_pretrained(onnx_path)

# Convert the model to onnx
torch.onnx.export(
    model,
    torch.rand((1, 3, 800, 600)),
    os.path.join(onnx_path, onnx_model_name),
    export_params=True,
    do_constant_folding=True,
    opset_version=11,
    input_names=["pixel_values"],
    output_names=["max_probability"],
    dynamic_axes={
        "pixel_values": {0: "batch_size", 2: "height", 3: "width"},
        "max_probability": {0: "batch_size"},
    },
)

# Optimize the onnx model
onnx_model = ORTModel.from_pretrained(onnx_path, file_name=onnx_model_name)
optimizer = ORTOptimizer.from_pretrained(onnx_model)
optimizer.optimize(
    save_dir=onnx_path,
    optimization_config=OptimizationConfig(optimization_level=99),
)

Which raises NotImplementedError: Tried to use ORTOptimizer for the model type table-transformer, but it is not available yet. Please open an issue or submit a PR at https://github.com/huggingface/optimum.

I'm more than happy to make PR, if someone could share some example PRs I could use as a template or give me some guidance as to how to start.

@zachmayer zachmayer changed the title Tried to use ORTOptimizer for the model type table-transformer ORTOptimizer for the model type table-transformer Mar 20, 2024
@mht-sharma
Copy link
Contributor

Hi @zachmayer thanks for interest in adding the table-transformer model in ORTOptimizer.

Here are the steps to follow:

Check more detail in the very good first issue here #351.

Let me know if you require additional details 🙂

@zachmayer
Copy link
Contributor Author

I made a PR here for segformers. once that's merged I'll add table transformers!

#1820

@zachmayer
Copy link
Contributor Author

segformers PR merged!

@zachmayer
Copy link
Contributor Author

@mht-sharma — I don't see a modeling ORT for table-transformers (like there was with segformers). Does this mean I'll need to make one?

https://github.com/huggingface/optimum/blob/main/optimum/onnxruntime/modeling_ort.py

@zachmayer zachmayer mentioned this issue Jun 7, 2024
3 tasks
@zachmayer
Copy link
Contributor Author

I took a shot at this. What do you think? #1900

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants