Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/add-dino' into add-hiera-onnx
Browse files Browse the repository at this point in the history
  • Loading branch information
xenova committed Aug 29, 2024
2 parents b96bb61 + cb8d362 commit 95336c0
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/source/exporters/onnx/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Supported architectures from [🤗 Transformers](https://huggingface.co/docs/tra
- Deberta-v2
- Deit
- Detr
- DINOv2
- DistilBert
- Donut-Swin
- Electra
Expand Down
39 changes: 39 additions & 0 deletions optimum/exporters/onnx/model_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,45 @@ class HieraOnnxConfig(ViTOnnxConfig):
DEFAULT_ONNX_OPSET = 11


class Dinov2DummyInputGenerator(DummyVisionInputGenerator):
def __init__(
self,
task: str,
normalized_config: NormalizedVisionConfig,
batch_size: int = DEFAULT_DUMMY_SHAPES["batch_size"],
num_channels: int = DEFAULT_DUMMY_SHAPES["num_channels"],
width: int = DEFAULT_DUMMY_SHAPES["width"],
height: int = DEFAULT_DUMMY_SHAPES["height"],
**kwargs,
):
super().__init__(
task=task,
normalized_config=normalized_config,
batch_size=batch_size,
num_channels=num_channels,
width=width,
height=height,
**kwargs,
)

from transformers.onnx.utils import get_preprocessor

preprocessor = get_preprocessor(normalized_config._name_or_path)
if preprocessor is not None and hasattr(preprocessor, "crop_size"):
self.height = preprocessor.crop_size.get("height", self.height)
self.width = preprocessor.crop_size.get("width", self.width)

def generate(self, input_name: str, framework: str = "pt", int_dtype: str = "int64", float_dtype: str = "fp32"):
input_ = super().generate(
input_name=input_name, framework=framework, int_dtype=int_dtype, float_dtype=float_dtype
)
return input_


class Dinov2OnnxConfig(ViTOnnxConfig):
DUMMY_INPUT_GENERATOR_CLASSES = (Dinov2DummyInputGenerator,)


class MobileViTOnnxConfig(ViTOnnxConfig):
ATOL_FOR_VALIDATION = 1e-4
DEFAULT_ONNX_OPSET = 11
Expand Down
5 changes: 5 additions & 0 deletions optimum/exporters/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,11 @@ class TasksManager:
"image-segmentation",
onnx="DetrOnnxConfig",
),
"dinov2": supported_tasks_mapping(
"feature-extraction",
"image-classification",
onnx="Dinov2OnnxConfig",
),
"distilbert": supported_tasks_mapping(
"feature-extraction",
"fill-mask",
Expand Down
2 changes: 1 addition & 1 deletion optimum/onnxruntime/modeling_ort.py
Original file line number Diff line number Diff line change
Expand Up @@ -1682,7 +1682,7 @@ def forward(
@add_end_docstrings(ONNX_MODEL_END_DOCSTRING)
class ORTModelForImageClassification(ORTModel):
"""
ONNX Model for image-classification tasks. This class officially supports beit, convnext, convnextv2, data2vec_vision, deit, levit, mobilenet_v1, mobilenet_v2, mobilevit, poolformer, resnet, segformer, swin, swinv2, vit.
ONNX Model for image-classification tasks. This class officially supports beit, convnext, convnextv2, data2vec_vision, deit, dinov2, levit, mobilenet_v1, mobilenet_v2, mobilevit, poolformer, resnet, segformer, swin, swinv2, vit.
"""

auto_model_class = AutoModelForImageClassification
Expand Down
1 change: 1 addition & 0 deletions optimum/utils/normalized_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ class NormalizedConfigManager:
'data2vec-text',
'data2vec-vision',
'detr',
'dinov2',
'flaubert',
'groupvit',
'hiera',
Expand Down
1 change: 1 addition & 0 deletions tests/exporters/exporters_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"deberta": "hf-internal-testing/tiny-random-DebertaModel",
"deberta-v2": "hf-internal-testing/tiny-random-DebertaV2Model",
"deit": "hf-internal-testing/tiny-random-DeiTModel",
"dinov2": "hf-internal-testing/tiny-random-Dinov2Model",
"donut": "fxmarty/tiny-doc-qa-vision-encoder-decoder",
"donut-swin": "hf-internal-testing/tiny-random-DonutSwinModel",
"detr": "hf-internal-testing/tiny-random-DetrModel", # hf-internal-testing/tiny-random-detr is larger
Expand Down
1 change: 1 addition & 0 deletions tests/onnxruntime/test_modeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -2727,6 +2727,7 @@ class ORTModelForImageClassificationIntegrationTest(ORTModelTestMixin):
"convnextv2",
"data2vec_vision",
"deit",
"dinov2",
"levit",
"mobilenet_v1",
"mobilenet_v2",
Expand Down
3 changes: 2 additions & 1 deletion tests/onnxruntime/utils_onnxruntime_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@
"deit": "hf-internal-testing/tiny-random-DeiTModel",
"donut": "fxmarty/tiny-doc-qa-vision-encoder-decoder",
"detr": "hf-internal-testing/tiny-random-detr",
"dpt": "hf-internal-testing/tiny-random-DPTModel",
"dinov2": "hf-internal-testing/tiny-random-Dinov2Model",
"distilbert": "hf-internal-testing/tiny-random-DistilBertModel",
"dpt": "hf-internal-testing/tiny-random-DPTModel",
"electra": "hf-internal-testing/tiny-random-ElectraModel",
"encoder-decoder": {
"hf-internal-testing/tiny-random-EncoderDecoderModel-bert-bert": [
Expand Down

0 comments on commit 95336c0

Please sign in to comment.