chore: add empty sample
Browse files- src/empty.py +98 -0
src/empty.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import PretrainedConfig, PreTrainedModel, AutoConfig, AutoModel
|
| 2 |
+
from transformers.pipelines import PIPELINE_REGISTRY
|
| 3 |
+
from huggingface_hub import hf_hub_download
|
| 4 |
+
import onnxruntime as ort
|
| 5 |
+
|
| 6 |
+
import torch
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# 1. register AutoConfig
|
| 11 |
+
class ONNXBaseConfig(PretrainedConfig):
|
| 12 |
+
model_type = 'onnx-base'
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
AutoConfig.register('onnx-base', ONNXBaseConfig)
|
| 16 |
+
|
| 17 |
+
# 2. register AutoModel
|
| 18 |
+
class ONNXBaseModel(PreTrainedModel):
|
| 19 |
+
config_class = ONNXBaseConfig
|
| 20 |
+
|
| 21 |
+
def __init__(self, config):
|
| 22 |
+
super().__init__(config)
|
| 23 |
+
|
| 24 |
+
def forward(self, input=None, **kwargs):
|
| 25 |
+
return {}
|
| 26 |
+
|
| 27 |
+
@classmethod
|
| 28 |
+
def from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs):
|
| 29 |
+
config = AutoConfig.from_pretrained(pretrained_model_name_or_path, **kwargs)
|
| 30 |
+
return cls(config)
|
| 31 |
+
|
| 32 |
+
@property
|
| 33 |
+
def device(self):
|
| 34 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 35 |
+
return torch.device(device)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
AutoModel.register(ONNXBaseConfig, ONNXBaseModel)
|
| 39 |
+
|
| 40 |
+
# 2. register Pipeline
|
| 41 |
+
from transformers.pipelines import Pipeline
|
| 42 |
+
|
| 43 |
+
class ONNXBasePipeline(Pipeline):
|
| 44 |
+
def __init__(self, model, **kwargs):
|
| 45 |
+
super().__init__(model=model, **kwargs)
|
| 46 |
+
self.device_id = kwargs['device']
|
| 47 |
+
model_path = hf_hub_download(repo_id='m3/onnx-base', filename='model.onnx', local_files_only=True)
|
| 48 |
+
self.session = ort.InferenceSession(model_path)
|
| 49 |
+
|
| 50 |
+
def __call__(
|
| 51 |
+
self,
|
| 52 |
+
inputs: str,
|
| 53 |
+
**kwargs,
|
| 54 |
+
):
|
| 55 |
+
inputs = {"inputs": inputs}
|
| 56 |
+
return super().__call__(inputs, **kwargs)
|
| 57 |
+
|
| 58 |
+
def _sanitize_parameters(self, **kwargs):
|
| 59 |
+
return {}, {}, {}
|
| 60 |
+
|
| 61 |
+
def preprocess(self, input):
|
| 62 |
+
return {'input': input}
|
| 63 |
+
|
| 64 |
+
def _forward(self, model_input):
|
| 65 |
+
input = model_input['input']['inputs']
|
| 66 |
+
outs = self.session.run(None, {'input': input})
|
| 67 |
+
return input
|
| 68 |
+
|
| 69 |
+
def postprocess(self, model_outputs):
|
| 70 |
+
return model_outputs
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
PIPELINE_REGISTRY.register_pipeline(
|
| 74 |
+
task='onnx-base',
|
| 75 |
+
pipeline_class=ONNXBasePipeline,
|
| 76 |
+
pt_model=ONNXBaseModel,
|
| 77 |
+
default={"pt": ("m3/onnx-base", "a5e4e8f")},
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
# 4. show how to use
|
| 81 |
+
from transformers import pipeline
|
| 82 |
+
|
| 83 |
+
cfg = ONNXBaseConfig(model_path='model.onnx',
|
| 84 |
+
id2label={0: 'label_0', 1: 'label_1'},
|
| 85 |
+
label2id={0: 'label_1', 1: 'label_0'})
|
| 86 |
+
|
| 87 |
+
pipe = pipeline(
|
| 88 |
+
task='onnx-base',
|
| 89 |
+
batch_size=10,
|
| 90 |
+
device='cuda',
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
dummy_input = torch.tensor([[[[1, 2, 3], [4, 5, 6], [7, 8, 9]]]], dtype=torch.float32)
|
| 94 |
+
input_data = dummy_input.numpy()
|
| 95 |
+
result = pipe(
|
| 96 |
+
inputs=input_data, device='cuda',
|
| 97 |
+
)
|
| 98 |
+
print(result)
|