chore: add dummpy model
Browse files- .gitignore +1 -0
- model.onnx +0 -3
- src/{empty.py → dummy.py} +30 -39
.gitignore
CHANGED
|
@@ -1 +1,2 @@
|
|
| 1 |
./custom_model
|
|
|
|
|
|
| 1 |
./custom_model
|
| 2 |
+
.idea
|
model.onnx
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:b3a7a3939f0d8c3ba7d20dd08fc5caf88da2477a64a4d083bbff7bac346935b1
|
| 3 |
-
size 251
|
|
|
|
|
|
|
|
|
|
|
|
src/{empty.py → dummy.py}
RENAMED
|
@@ -1,51 +1,45 @@
|
|
| 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 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
| 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
|
| 44 |
def __init__(self, model, **kwargs):
|
| 45 |
super().__init__(model=model, **kwargs)
|
| 46 |
self.device_id = kwargs['device']
|
| 47 |
-
model_path =
|
| 48 |
-
self.
|
|
|
|
| 49 |
|
| 50 |
def __call__(
|
| 51 |
self,
|
|
@@ -62,30 +56,27 @@ class ONNXBasePipeline(Pipeline):
|
|
| 62 |
return {'input': input}
|
| 63 |
|
| 64 |
def _forward(self, model_input):
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
| 68 |
|
| 69 |
def postprocess(self, model_outputs):
|
| 70 |
return model_outputs
|
| 71 |
|
| 72 |
-
|
| 73 |
PIPELINE_REGISTRY.register_pipeline(
|
| 74 |
-
task='
|
| 75 |
-
pipeline_class=
|
| 76 |
-
pt_model=
|
| 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 |
-
|
|
|
|
| 89 |
batch_size=10,
|
| 90 |
device='cuda',
|
| 91 |
)
|
|
|
|
| 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 torch
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
# 1. create auto config
|
| 8 |
+
class DummyConfig(PretrainedConfig):
|
| 9 |
+
model_type = 'dummy'
|
| 10 |
+
|
| 11 |
+
# 2. create model
|
| 12 |
+
class DummyModel(PreTrainedModel):
|
| 13 |
+
config_class = DummyConfig
|
| 14 |
+
def __init__(self, model: str, model_path: str):
|
| 15 |
+
is_local = os.path.isdir(model)
|
| 16 |
+
if is_local:
|
| 17 |
+
base_path = model
|
| 18 |
+
model_path = os.path.join(base_path, model_path)
|
| 19 |
+
else:
|
| 20 |
+
model_path = hf_hub_download(repo_id=model, filename=model_path)
|
| 21 |
+
base_path = os.path.dirname(model_path)
|
| 22 |
+
config = DummyConfig(base_path=base_path, model_path=model_path)
|
| 23 |
super().__init__(config)
|
| 24 |
|
| 25 |
def forward(self, input=None, **kwargs):
|
| 26 |
return {}
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
@property
|
| 29 |
def device(self):
|
| 30 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 31 |
return torch.device(device)
|
| 32 |
|
|
|
|
|
|
|
|
|
|
| 33 |
# 2. register Pipeline
|
| 34 |
from transformers.pipelines import Pipeline
|
| 35 |
|
| 36 |
+
class DummyPipeline(Pipeline):
|
| 37 |
def __init__(self, model, **kwargs):
|
| 38 |
super().__init__(model=model, **kwargs)
|
| 39 |
self.device_id = kwargs['device']
|
| 40 |
+
self.model_path = self.model.config.model_path
|
| 41 |
+
self.base_path = self.model.config.base_path
|
| 42 |
+
|
| 43 |
|
| 44 |
def __call__(
|
| 45 |
self,
|
|
|
|
| 56 |
return {'input': input}
|
| 57 |
|
| 58 |
def _forward(self, model_input):
|
| 59 |
+
return {'data': 'dummy',
|
| 60 |
+
'device_id': self.device_id,
|
| 61 |
+
'base_path': self.base_path,
|
| 62 |
+
'model_path': self.model_path
|
| 63 |
+
}
|
| 64 |
|
| 65 |
def postprocess(self, model_outputs):
|
| 66 |
return model_outputs
|
| 67 |
|
|
|
|
| 68 |
PIPELINE_REGISTRY.register_pipeline(
|
| 69 |
+
task='dummy-task',
|
| 70 |
+
pipeline_class=DummyPipeline,
|
| 71 |
+
pt_model=DummyModel,
|
|
|
|
| 72 |
)
|
| 73 |
|
| 74 |
# 4. show how to use
|
| 75 |
from transformers import pipeline
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
pipe = pipeline(
|
| 78 |
+
model=DummyModel("Ultralytics/YOLOv8", 'yolov8m.pt'),
|
| 79 |
+
task='dummy-task',
|
| 80 |
batch_size=10,
|
| 81 |
device='cuda',
|
| 82 |
)
|