chore: add zero demo
Browse files- app.py +19 -0
- pipeline.py +85 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import pipeline
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
|
| 6 |
+
pipe = pipeline(
|
| 7 |
+
task='onnx-base',
|
| 8 |
+
model='m3/onnx-base',
|
| 9 |
+
batch_size=10,
|
| 10 |
+
device='cpu',
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
def zero(name):
|
| 14 |
+
input = torch.tensor([[1, 2], [3, 4]], dtype=torch.float32).numpy()
|
| 15 |
+
result = pipe(input)
|
| 16 |
+
return str(result)
|
| 17 |
+
|
| 18 |
+
demo = gr.Interface(fn=zero, inputs="text", outputs="text")
|
| 19 |
+
demo.launch()
|
pipeline.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import PretrainedConfig, PreTrainedModel, AutoConfig, AutoModel
|
| 2 |
+
from transformers.pipelines import PIPELINE_REGISTRY
|
| 3 |
+
from huggingface_hub import hf_hub_download
|
| 4 |
+
|
| 5 |
+
import onnxruntime as ort
|
| 6 |
+
import torch
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
# 1. register AutoConfig
|
| 10 |
+
class ONNXBaseConfig(PretrainedConfig):
|
| 11 |
+
model_type = 'onnx-base'
|
| 12 |
+
|
| 13 |
+
AutoConfig.register('onnx-base', ONNXBaseConfig)
|
| 14 |
+
|
| 15 |
+
# 2. register AutoModel
|
| 16 |
+
class ONNXBaseModel(PreTrainedModel):
|
| 17 |
+
config_class = ONNXBaseConfig
|
| 18 |
+
def __init__(self, config, base_path=None):
|
| 19 |
+
super().__init__(config)
|
| 20 |
+
if base_path:
|
| 21 |
+
model_path = base_path + '/' + config.model_path
|
| 22 |
+
if os.path.exists(model_path):
|
| 23 |
+
self.session = ort.InferenceSession(model_path)
|
| 24 |
+
|
| 25 |
+
def forward(self, input=None, **kwargs):
|
| 26 |
+
outs = self.session.run(None, {'input': input})
|
| 27 |
+
return outs
|
| 28 |
+
|
| 29 |
+
def save_pretrained(self, save_directory: str, **kwargs):
|
| 30 |
+
super().save_pretrained(save_directory=save_directory, **kwargs)
|
| 31 |
+
onnx_file_path = save_directory + '/model.onnx'
|
| 32 |
+
dummy_input = torch.tensor([[1, 2], [3, 4]], dtype=torch.float32)
|
| 33 |
+
torch.onnx.export(self, dummy_input, onnx_file_path,
|
| 34 |
+
input_names=['input'], output_names=['output'],
|
| 35 |
+
dynamic_axes={'input': {0: 'batch_size'}, 'output': {0: 'batch_size'}})
|
| 36 |
+
|
| 37 |
+
@classmethod
|
| 38 |
+
def from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs):
|
| 39 |
+
config = AutoConfig.from_pretrained(pretrained_model_name_or_path, **kwargs)
|
| 40 |
+
if config.model_path is None:
|
| 41 |
+
config.model_path = 'model.onnx'
|
| 42 |
+
is_local = os.path.isdir(pretrained_model_name_or_path)
|
| 43 |
+
if is_local:
|
| 44 |
+
base_path = pretrained_model_name_or_path
|
| 45 |
+
else:
|
| 46 |
+
config_path = hf_hub_download(repo_id=pretrained_model_name_or_path, filename='config.json')
|
| 47 |
+
base_path = os.path.dirname(config_path)
|
| 48 |
+
hf_hub_download(repo_id=pretrained_model_name_or_path, filename=config.model_path)
|
| 49 |
+
return cls(config, base_path=base_path)
|
| 50 |
+
|
| 51 |
+
@property
|
| 52 |
+
def device(self):
|
| 53 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 54 |
+
return torch.device(device)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
AutoModel.register(ONNXBaseConfig, ONNXBaseModel)
|
| 58 |
+
|
| 59 |
+
# 2. register Pipeline
|
| 60 |
+
from transformers.pipelines import Pipeline
|
| 61 |
+
|
| 62 |
+
class ONNXBasePipeline(Pipeline):
|
| 63 |
+
def __init__(self, model, **kwargs):
|
| 64 |
+
self.device_id = kwargs['device']
|
| 65 |
+
super().__init__(model=model, **kwargs)
|
| 66 |
+
|
| 67 |
+
def _sanitize_parameters(self, **kwargs):
|
| 68 |
+
return {}, {}, {}
|
| 69 |
+
|
| 70 |
+
def preprocess(self, input):
|
| 71 |
+
return {'input': input}
|
| 72 |
+
|
| 73 |
+
def _forward(self, model_input):
|
| 74 |
+
with torch.no_grad():
|
| 75 |
+
outputs = self.model(**model_input)
|
| 76 |
+
return outputs
|
| 77 |
+
|
| 78 |
+
def postprocess(self, model_outputs):
|
| 79 |
+
return model_outputs
|
| 80 |
+
|
| 81 |
+
PIPELINE_REGISTRY.register_pipeline(
|
| 82 |
+
task='onnx-base',
|
| 83 |
+
pipeline_class=ONNXBasePipeline,
|
| 84 |
+
pt_model=ONNXBaseModel
|
| 85 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
onnx
|
| 3 |
+
onnxruntime
|