optimum icon indicating copy to clipboard operation
optimum copied to clipboard

ORTOptimizer for the model type table-transformer

Open zachmayer opened this issue 1 year ago • 5 comments
trafficstars

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 avatar Mar 20 '24 14:03 zachmayer

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 🙂

mht-sharma avatar Mar 26 '24 09:03 mht-sharma

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

https://github.com/huggingface/optimum/pull/1820

zachmayer avatar Apr 19 '24 13:04 zachmayer

segformers PR merged!

zachmayer avatar Jun 06 '24 01:06 zachmayer

@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 avatar Jun 06 '24 01:06 zachmayer

I took a shot at this. What do you think? https://github.com/huggingface/optimum/pull/1900

zachmayer avatar Jun 07 '24 22:06 zachmayer